WIP: add a more comfortable way of adding grammars
This uses the grammar compiler of the parsing module but adds a new mechanism of specifying grammars via a metaclass (rather than using a module as a container for a grammar), using a simple DSL for specifying grammar rules, and a simple mechanism for specifying a (bare-bones but usable) scanner that interacts with the grammar.
Using this, you can specify a grammar more succinctly as in
from parsing import Grammar, Precedence, Lr
from parsing.automaton import Spec
from parsing.ast import mktoken, print_ast
class SimpleGrammar(Grammar):
pAddOp = Precedence.left()
pMulOp = Precedence.left(before=pAddOp)
add = mktoken('add', pAddOp, tokens='+ -')
mul = mktoken('mul', pMulOp, tokens='* /')
whitespace = '\s+|\n'
IntValue = mktoken('IntValue', re='-?(:0|[1-9]\d*)', convert=int)
Nonterm = SimpleGrammar.nonterm_base()
class Expr(Nonterm):
"""
%start
%reduce Expr op=add Expr
%reduce Expr op=mul Expr
%reduce IntValue
%reduce '(' Expr ')'
"""
and instead of repeatedly callling parser.token(...) you'd call SimpleGrammar.feed(text, parser) to invoke the scanner derived from the token declarations.
This is essentially a forward port of the yv/parsing fork to the mainline parsing repo. I haven't ported the tests yet, and one potentially controversial item that's yet to be ported is an addition to reduce that adds type and range attributes to the nonterminals where the reduction method returns None. This makes it easier to create sensible ASTs from simple grammar descriptions.
I've put "WIP" in the title to signify that there are things missing from a merge-able state, but that I'm already interested in opinions/comments on this.