Пример #1
0
//ParseGrammar parses a relapse grammar and returns an abstract syntax tree.
func (this *Parser) ParseGrammar(s string) (*ast.Grammar, error) {
	scanner := lexer.NewLexer([]byte(s))
	g, err := this.Parse(scanner)
	if err != nil {
		return nil, err
	}
	gram, ok := g.(*ast.Grammar)
	if !ok {
		return nil, &errWrongType{"*relapse.Grammar", g}
	}
	return gram, nil
}
Пример #2
0
//ParseExpr returns a parsed expression or error, given a string.
func (this *Parser) ParseExpr(s string) (res *ast.Expr, err error) {
	scanner := lexer.NewLexer([]byte(s))
	g, err := this.Parse(scanner)
	if err != nil {
		return nil, err
	}
	gram, ok := g.(*ast.Grammar)
	if !ok {
		return nil, &errWrongType{"*relapse.Grammar", g}
	}
	if len(gram.GetPatternDecls()) != 0 {
		return nil, &errWrongType{"found pattern declarations where none were expected", g}
	}
	if gram.GetTopPattern().LeafNode == nil {
		return nil, &errWrongType{"LeafNode == nil", g}
	}
	return gram.GetTopPattern().GetLeafNode().GetExpr(), nil
}