Beispiel #1
0
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)
	}
}
Beispiel #2
0
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
}