2021-12-26 23:23:41 +00:00
|
|
|
package gg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
2021-12-29 19:32:53 +00:00
|
|
|
|
|
|
|
"github.com/mediocregopher/ginger/graph"
|
2021-12-26 23:23:41 +00:00
|
|
|
)
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
// Type aliases for convenience
|
|
|
|
type (
|
|
|
|
Graph = graph.Graph[Value, Value]
|
|
|
|
OpenEdge = graph.OpenEdge[Value, Value]
|
|
|
|
)
|
|
|
|
|
2021-12-26 23:23:41 +00:00
|
|
|
// Punctuations which are used in the gg file format.
|
|
|
|
const (
|
|
|
|
punctTerm = ";"
|
|
|
|
punctOp = "<"
|
|
|
|
punctAssign = "="
|
|
|
|
punctOpenGraph = "{"
|
|
|
|
punctCloseGraph = "}"
|
|
|
|
punctOpenTuple = "("
|
|
|
|
punctCloseTuple = ")"
|
|
|
|
)
|
|
|
|
|
|
|
|
func decoderErr(tok LexerToken, err error) error {
|
2021-12-28 16:49:02 +00:00
|
|
|
return fmt.Errorf("%s: %w", tok.errPrefix(), err)
|
2021-12-26 23:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func decoderErrf(tok LexerToken, str string, args ...interface{}) error {
|
|
|
|
return decoderErr(tok, fmt.Errorf(str, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func isPunct(tok LexerToken, val string) bool {
|
|
|
|
return tok.Kind == LexerTokenKindPunctuation && tok.Value == val
|
|
|
|
}
|
|
|
|
|
|
|
|
func isTerm(tok LexerToken) bool {
|
|
|
|
return isPunct(tok, punctTerm)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decoder is currently only really used to namespace functions related to
|
|
|
|
// decoding Graphs. It may later have actual fields added to it, such as for
|
|
|
|
// options passed by the caller.
|
|
|
|
type decoder struct{}
|
|
|
|
|
|
|
|
// returned boolean value indicates if the token following the single token is a
|
|
|
|
// term. If a term followed the first token then it is not included in the
|
|
|
|
// returned leftover tokens.
|
|
|
|
//
|
|
|
|
// if termed is false then leftover tokens cannot be empty.
|
|
|
|
func (d *decoder) parseSingleValue(
|
|
|
|
toks []LexerToken,
|
|
|
|
) (
|
|
|
|
Value, []LexerToken, bool, error,
|
|
|
|
) {
|
|
|
|
|
|
|
|
tok, rest := toks[0], toks[1:]
|
|
|
|
|
|
|
|
if len(rest) == 0 {
|
2021-12-28 16:49:02 +00:00
|
|
|
return ZeroValue, nil, false, decoderErrf(tok, "cannot be final token, possibly missing %q", punctTerm)
|
2021-12-26 23:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
termed := isTerm(rest[0])
|
|
|
|
|
|
|
|
if termed {
|
|
|
|
rest = rest[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tok.Kind {
|
|
|
|
|
|
|
|
case LexerTokenKindName:
|
2021-12-28 16:49:02 +00:00
|
|
|
return Value{Name: &tok.Value, LexerToken: &tok}, rest, termed, nil
|
2021-12-26 23:23:41 +00:00
|
|
|
|
|
|
|
case LexerTokenKindNumber:
|
|
|
|
|
|
|
|
i, err := strconv.ParseInt(tok.Value, 10, 64)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-12-28 16:49:02 +00:00
|
|
|
return ZeroValue, nil, false, decoderErrf(tok, "parsing %q as integer: %w", tok.Value, err)
|
2021-12-26 23:23:41 +00:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:49:02 +00:00
|
|
|
return Value{Number: &i, LexerToken: &tok}, rest, termed, nil
|
2021-12-26 23:23:41 +00:00
|
|
|
|
|
|
|
case LexerTokenKindPunctuation:
|
2021-12-28 16:49:02 +00:00
|
|
|
return ZeroValue, nil, false, decoderErrf(tok, "expected value, found punctuation %q", tok.Value)
|
2021-12-26 23:23:41 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected token kind %q", tok.Kind))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoder) parseOpenEdge(
|
|
|
|
toks []LexerToken,
|
|
|
|
) (
|
2021-12-30 16:56:20 +00:00
|
|
|
*OpenEdge, []LexerToken, error,
|
2021-12-26 23:23:41 +00:00
|
|
|
) {
|
|
|
|
|
|
|
|
if isPunct(toks[0], punctOpenTuple) {
|
|
|
|
return d.parseTuple(toks)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
val Value
|
|
|
|
termed bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
|
|
|
case isPunct(toks[0], punctOpenGraph):
|
|
|
|
val, toks, termed, err = d.parseGraphValue(toks, true)
|
|
|
|
|
|
|
|
default:
|
|
|
|
val, toks, termed, err = d.parseSingleValue(toks)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2021-12-29 20:57:14 +00:00
|
|
|
return nil, nil, err
|
2021-12-26 23:23:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if termed {
|
2021-12-30 18:38:36 +00:00
|
|
|
return graph.ValueOut[Value](ZeroValue, val), toks, nil
|
2021-12-26 23:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opTok, toks := toks[0], toks[1:]
|
|
|
|
|
|
|
|
if !isPunct(opTok, punctOp) {
|
2021-12-29 20:57:14 +00:00
|
|
|
return nil, nil, decoderErrf(opTok, "must be %q or %q", punctOp, punctTerm)
|
2021-12-26 23:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(toks) == 0 {
|
2021-12-29 20:57:14 +00:00
|
|
|
return nil, nil, decoderErrf(opTok, "%q cannot terminate an edge declaration", punctOp)
|
2021-12-26 23:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
oe, toks, err := d.parseOpenEdge(toks)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-12-29 20:57:14 +00:00
|
|
|
return nil, nil, err
|
2021-12-26 23:23:41 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 18:38:36 +00:00
|
|
|
oe = graph.TupleOut[Value](val, oe)
|
2021-12-26 23:23:41 +00:00
|
|
|
|
|
|
|
return oe, toks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoder) parseTuple(
|
|
|
|
toks []LexerToken,
|
|
|
|
) (
|
2021-12-30 16:56:20 +00:00
|
|
|
*OpenEdge, []LexerToken, error,
|
2021-12-26 23:23:41 +00:00
|
|
|
) {
|
|
|
|
|
|
|
|
openTok, toks := toks[0], toks[1:]
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
var edges []*OpenEdge
|
2021-12-26 23:23:41 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
if len(toks) == 0 {
|
2021-12-29 20:57:14 +00:00
|
|
|
return nil, nil, decoderErrf(openTok, "no matching %q", punctCloseTuple)
|
2021-12-26 23:23:41 +00:00
|
|
|
|
|
|
|
} else if isPunct(toks[0], punctCloseTuple) {
|
|
|
|
toks = toks[1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2021-12-30 16:56:20 +00:00
|
|
|
oe *OpenEdge
|
2021-12-26 23:23:41 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
oe, toks, err = d.parseOpenEdge(toks)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-12-29 20:57:14 +00:00
|
|
|
return nil, nil, err
|
2021-12-26 23:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
edges = append(edges, oe)
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is a quirk of the syntax, _technically_ a tuple doesn't need a
|
|
|
|
// term after it, since it can't be used as an edge value, and so
|
|
|
|
// nothing can come after it in the chain.
|
|
|
|
if len(toks) > 0 && isTerm(toks[0]) {
|
|
|
|
toks = toks[1:]
|
|
|
|
}
|
|
|
|
|
2021-12-30 18:38:36 +00:00
|
|
|
return graph.TupleOut[Value](ZeroValue, edges...), toks, nil
|
2021-12-26 23:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// returned boolean value indicates if the token following the graph is a term.
|
|
|
|
// If a term followed the first token then it is not included in the returned
|
|
|
|
// leftover tokens.
|
|
|
|
//
|
|
|
|
// if termed is false then leftover tokens cannot be empty.
|
|
|
|
func (d *decoder) parseGraphValue(
|
|
|
|
toks []LexerToken, expectWrappers bool,
|
|
|
|
) (
|
|
|
|
Value, []LexerToken, bool, error,
|
|
|
|
) {
|
|
|
|
|
|
|
|
var openTok LexerToken
|
|
|
|
|
|
|
|
if expectWrappers {
|
|
|
|
openTok, toks = toks[0], toks[1:]
|
|
|
|
}
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
g := new(Graph)
|
2021-12-26 23:23:41 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
if len(toks) == 0 {
|
|
|
|
|
|
|
|
if !expectWrappers {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:49:02 +00:00
|
|
|
return ZeroValue, nil, false, decoderErrf(openTok, "no matching %q", punctCloseGraph)
|
2021-12-26 23:23:41 +00:00
|
|
|
|
|
|
|
} else if closingTok := toks[0]; isPunct(closingTok, punctCloseGraph) {
|
|
|
|
|
|
|
|
if !expectWrappers {
|
2021-12-28 16:49:02 +00:00
|
|
|
return ZeroValue, nil, false, decoderErrf(closingTok, "unexpected %q", punctCloseGraph)
|
2021-12-26 23:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toks = toks[1:]
|
|
|
|
|
|
|
|
if len(toks) == 0 {
|
2021-12-28 16:49:02 +00:00
|
|
|
return ZeroValue, nil, false, decoderErrf(closingTok, "cannot be final token, possibly missing %q", punctTerm)
|
2021-12-26 23:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if g, toks, err = d.parseValIn(g, toks); err != nil {
|
2021-12-28 16:49:02 +00:00
|
|
|
return ZeroValue, nil, false, err
|
2021-12-26 23:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val := Value{Graph: g}
|
|
|
|
|
|
|
|
if !expectWrappers {
|
|
|
|
return val, toks, true, nil
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:49:02 +00:00
|
|
|
val.LexerToken = &openTok
|
|
|
|
|
2021-12-26 23:23:41 +00:00
|
|
|
termed := isTerm(toks[0])
|
|
|
|
|
|
|
|
if termed {
|
|
|
|
toks = toks[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return val, toks, termed, nil
|
|
|
|
}
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
func (d *decoder) parseValIn(into *Graph, toks []LexerToken) (*Graph, []LexerToken, error) {
|
2021-12-26 23:23:41 +00:00
|
|
|
|
|
|
|
if len(toks) == 0 {
|
|
|
|
return into, nil, nil
|
|
|
|
|
|
|
|
} else if len(toks) < 3 {
|
|
|
|
return nil, nil, decoderErrf(toks[0], `must be of the form "<name> = ..."`)
|
|
|
|
}
|
|
|
|
|
|
|
|
dst := toks[0]
|
|
|
|
eq := toks[1]
|
|
|
|
toks = toks[2:]
|
|
|
|
|
|
|
|
if dst.Kind != LexerTokenKindName {
|
|
|
|
return nil, nil, decoderErrf(dst, "must be a name")
|
|
|
|
|
|
|
|
} else if !isPunct(eq, punctAssign) {
|
|
|
|
return nil, nil, decoderErrf(eq, "must be %q", punctAssign)
|
|
|
|
}
|
|
|
|
|
|
|
|
oe, toks, err := d.parseOpenEdge(toks)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:49:02 +00:00
|
|
|
dstVal := Value{Name: &dst.Value, LexerToken: &dst}
|
2021-12-26 23:23:41 +00:00
|
|
|
|
2021-12-30 18:38:36 +00:00
|
|
|
return into.AddValueIn(dstVal, oe), toks, nil
|
2021-12-26 23:23:41 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
func (d *decoder) decode(lexer Lexer) (*Graph, error) {
|
2021-12-26 23:23:41 +00:00
|
|
|
|
|
|
|
var toks []LexerToken
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
tok, err := lexer.Next()
|
|
|
|
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
break
|
|
|
|
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading next token: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
toks = append(toks, tok)
|
|
|
|
}
|
|
|
|
|
|
|
|
val, _, _, err := d.parseGraphValue(toks, false)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return val.Graph, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeLexer reads lexigraphical tokens from the given Lexer and uses them to
|
|
|
|
// construct a Graph according to the rules of the gg file format. DecodeLexer
|
|
|
|
// will only return an error if there is a non-EOF file returned from the Lexer,
|
|
|
|
// or the tokens read cannot be used to construct a valid Graph.
|
2021-12-30 16:56:20 +00:00
|
|
|
func DecodeLexer(lexer Lexer) (*Graph, error) {
|
2021-12-26 23:23:41 +00:00
|
|
|
decoder := &decoder{}
|
|
|
|
return decoder.decode(lexer)
|
|
|
|
}
|