def _expressions_from_rules(self, rule_syntax): """Return the rules for parsing the grammar definition syntax. Return a 2-tuple: a dict of rule names pointing to their expressions, and then the top-level expression for the first rule. """ # Hard-code enough of the rules to parse the grammar that describes the # grammar description language, to bootstrap: ws = Regex(r'\s+', name='ws') _ = Regex(r'[ \t]+', name='_') label = Regex(r'[a-zA-Z_][a-zA-Z_0-9]*', name='label') quantifier = Regex(r'[*+?]', name='quantifier') # This pattern supports empty literals. TODO: A problem? literal = Regex(r'u?r?"[^"\\]*(?:\\.[^"\\]*)*"', ignore_case=True, dot_all=True, name='literal') regex = Sequence(Literal('~'), literal, Regex('[ilmsux]*', ignore_case=True), name='regex') atom = OneOf(label, literal, regex, name='atom') quantified = Sequence(atom, quantifier, name='quantified') term = OneOf(quantified, atom, name='term') another_term = Sequence(_, term, name='another_term') sequence = Sequence(term, OneOrMore(another_term), name='sequence') or_term = Sequence(_, Literal('/'), another_term, name='or_term') ored = Sequence(term, OneOrMore(or_term), name='ored') and_term = Sequence(_, Literal('&'), another_term, name='and_term') anded = Sequence(term, OneOrMore(and_term), name='anded') poly_term = OneOf(anded, ored, sequence, name='poly_term') rhs = OneOf(poly_term, term, name='rhs') eol = Regex(r'[\r\n$]', name='eol') # TODO: Support $. rule = Sequence(Optional(ws), label, Optional(_), Literal('='), Optional(_), rhs, Optional(_), eol, name='rule') rules = Sequence(OneOrMore(rule), Optional(ws), name='rules') # Use those hard-coded rules to parse the (possibly more extensive) # rule syntax. (For example, unless I start using parentheses in the # rule language definition itself, I should never have to hard-code # expressions for those above.) rule_tree = rules.parse(rule_syntax) # Turn the parse tree into a map of expressions: return RuleVisitor().visit(rule_tree)
def test_optional(self): """``Optional`` should return its own node wrapping the succeeded child.""" expr = Optional(Literal('a', name='lit'), name='opt') text = 'a' eq_(expr.match(text), Node('opt', text, 0, 1, children=[Node('lit', text, 0, 1)])) # Test failure of the Literal inside the Optional; the # LengthTests.test_optional is ambiguous for that. text = '' eq_(expr.match(text), Node('opt', text, 0, 0))
def test_optional(self): len_eq(Sequence(Optional(Literal('a')), Literal('b')).match('b'), 1) # contained expr fails len_eq(Sequence(Optional(Literal('a')), Literal('b')).match('ab'), 2) # contained expr succeeds