func testParser(t *testing.T, name, rules, input string, debug bool) { s := syms.NewSymbolSet() l := lexer.NewLexer(&s) l.Ignore(syms.WS) // l.Ignore(syms.EOL) functions := make(map[string]grammar.UserFn) functions[""] = grammar.DefaultUserFn if debug { functions[""] = grammar.RuleUserFn } g, err := grammar.NewGrammarBlock(l, rules, functions) if !tlog.Ok(err) { t.Fatal(err) } p, err := NewParserExt(g, debug, false) if !tlog.Ok(err) { t.Fatal(err) } p.debug = debug l.SetReader(strings.NewReader(input)) if parse, err := p.Parse(l); tlog.Ok(err) { if debug { t.Errorf("Successful parse: %v\n", parse) } else { t.Logf("Successful parse: %v\n", parse) } } else { t.Logf("Partial parse: %v\n", parse) t.Fatal(err) } }
func TestGrammarHelper(t *testing.T) { s := syms.NewSymbolSet() l := lexer.NewLexer(&s) functions := make(map[string]UserFn) functions["buildFunc"] = DefaultUserFn grammar := ` Func → "func" IDENT '(' ')' '{' Stat '}' @buildFunc Stat → NUM | HEXNUM @raiseFirst ` if g, err := NewGrammarBlock(l, grammar, functions); !tlog.Ok(err) { tlog.Println("Grammar: ", g) t.Fatal("Cannot create grammar: ", err) } else if g.Rules.Size() == 0 { t.Fatal("Empty grammar") } }
func Parse(r io.Reader) nex.Nex { if l == nil { s = syms.NewSymbolSet() l = lexer.NewLexer(&s) l.Ignore(syms.WS) functions = make(map[string]grammar.UserFn) functions[""] = nexBuilder functions["synth"] = nexBuilder functions["nil"] = nilHandler var err error g, err = grammar.NewGrammarBlock(l, readGrammar(), functions) if !tlog.Ok(err) { return nex.NewErrorNex(err) } p, err = parser.NewParserExt(g, false, false) if !tlog.Ok(err) { return nex.NewErrorNex(err) } } l.SetReader(r) pt, err := p.Parse(l) if err != nil { _, isParseError := err.(parser.UnexpectedTerminalParseError) if !isParseError { return nex.NewErrorNex(err) } } n, isNex := pt.(nex.Nex) if !isNex { n = makeNexFromArray(g, g.Rules.Get(0), pt) } return n }
func TestGrammarBasic(t *testing.T) { s := syms.NewSymbolSet() l := lexer.NewLexer(&s) NUM := s.MustGetByName("NUM") STAR := l.Operator("*") PLUS := l.Operator("+") E := s.MustAdd("E", false) B := s.MustAdd("B", false) grammar := []Rule{ Rule{E, []syms.SymbolID{E, STAR, B}, DefaultUserFn}, Rule{E, []syms.SymbolID{E, PLUS, B}, DefaultUserFn}, Rule{E, []syms.SymbolID{B}, DefaultUserFn}, Rule{B, []syms.SymbolID{NUM}, DefaultUserFn}, } if g, err := NewGrammar(l.GetSymbolSet(), grammar); !tlog.Ok(err) { tlog.Println("Grammar: ", g) t.Fatal("Cannot create grammar: ", err) } else if g.Rules.Size() == 0 { t.Fatal("Empty grammar") } }
func TestParseBasic(t *testing.T) { s := syms.NewSymbolSet() l := lexer.NewLexer(&s) l.Ignore(syms.WS) l.Ignore(syms.EOL) NUM := s.MustGetByName("NUM") STAR := l.Operator("*") PLUS := l.Operator("+") E := l.GetSymbolSet().MustAdd("E", false) B := l.GetSymbolSet().MustAdd("B", false) rules := []grammar.Rule{ grammar.Rule{LHS: E, RHS: []syms.SymbolID{E, STAR, B}, UserFn: grammar.StringUserFn}, grammar.Rule{LHS: E, RHS: []syms.SymbolID{E, PLUS, B}, UserFn: grammar.StringUserFn}, grammar.Rule{LHS: E, RHS: []syms.SymbolID{B}, UserFn: grammar.StringUserFn}, grammar.Rule{LHS: B, RHS: []syms.SymbolID{NUM}, UserFn: grammar.StringUserFn}, } g, err := grammar.NewGrammar(&s, rules) if !tlog.Ok(err) { t.Fatal("Cannot create grammar: ", err) } p, err := NewParser(g) if !tlog.Ok(err) { t.Fatal("Cannot create parser from grammar:", err) } input := `1 + 1` l.SetReader(strings.NewReader(input)) if parse, err := p.Parse(l); tlog.Ok(err) { // t.Errorf("Successful parse: %v\n", parse) t.Logf("Successful parse: %v\n", parse) } else { t.Logf("Partial parse: %v\n", parse) t.Fatal(err) } }