Exemplo n.º 1
0
    def test_optional(self):
        tree = rule_grammar.parse('boy = "howdy"?\n')
        rules, default_rule = RuleVisitor().visit(tree)

        howdy = 'howdy'

        # It should turn into a Node from the Optional and another from the
        # Literal within.
        self.assertEqual(default_rule.parse(howdy), Node(default_rule, howdy, 0, 5, children=[
                                           Node(Literal("howdy"), howdy, 0, 5)]))
Exemplo n.º 2
0
    def test_optional(self):
        tree = rule_grammar.parse('boy = "howdy"?\n')
        _, default_rule = RuleVisitor().visit(tree)

        howdy = 'howdy'

        # It should turn into a Node from the Optional and another from the
        # Literal within.
        eq_(
            default_rule.parse(howdy),
            Node('boy',
                 howdy,
                 0,
                 5,
                 children=[Node('__Literal__', howdy, 0, 5)]))
Exemplo n.º 3
0
    def test_round_trip(self):
        """Test a simple round trip.

        Parse a simple grammar, turn the parse tree into a map of expressions,
        and use that to parse another piece of text.

        Not everything was implemented yet, but it was a big milestone and a
        proof of concept.

        """
        tree = rule_grammar.parse('''number = ~"[0-9]+"\n''')
        _, default_rule = RuleVisitor().visit(tree)

        text = '98'
        eq_(default_rule.parse(text), Node('number', text, 0, 2))
Exemplo n.º 4
0
 def test_undefined_rule(self):
     """Make sure we throw the right exception on undefined rules."""
     tree = rule_grammar.parse('boy = howdy\n')
     assert_raises(UndefinedLabel, RuleVisitor().visit, tree)