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.
        eq_(default_rule.parse(howdy), Node("boy", howdy, 0, 5, children=[Node("", howdy, 0, 5)]))
Exemplo n.º 2
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.º 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''')
        rules, default_rule = RuleVisitor().visit(tree)

        text = '98'
        eq_(default_rule.parse(text), Node('number', text, 0, 2))
Exemplo n.º 4
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.º 5
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.º 6
0
    def test_successes(self):
        """Make sure the PEG recognition grammar succeeds on various inputs."""
        ok_(rule_grammar["label"].parse("_"))
        ok_(rule_grammar["label"].parse("jeff"))
        ok_(rule_grammar["label"].parse("_THIS_THING"))

        ok_(rule_grammar["atom"].parse("some_label"))
        ok_(rule_grammar["atom"].parse('"some literal"'))
        ok_(rule_grammar["atom"].parse('~"some regex"i'))

        ok_(rule_grammar["quantified"].parse('~"some regex"i*'))
        ok_(rule_grammar["quantified"].parse("thing+"))
        ok_(rule_grammar["quantified"].parse('"hi"?'))

        ok_(rule_grammar["term"].parse("this"))
        ok_(rule_grammar["term"].parse("that+"))

        ok_(rule_grammar["sequence"].parse("this that? other"))

        ok_(rule_grammar["ored"].parse('this / that+ / "other"'))

        # + is higher precedence than &, so 'anded' should match the whole
        # thing:
        ok_(rule_grammar["lookahead_term"].parse("&this+"))

        ok_(rule_grammar["expression"].parse("this"))
        ok_(rule_grammar["expression"].parse("this? that other*"))
        ok_(rule_grammar["expression"].parse('&this / that+ / "other"'))
        ok_(rule_grammar["expression"].parse('this / that? / "other"+'))
        ok_(rule_grammar["expression"].parse("this? that other*"))

        ok_(rule_grammar["rule"].parse("this = that\r"))
        ok_(rule_grammar["rule"].parse("this = the? that other* \t\r"))
        ok_(rule_grammar["rule"].parse('the=~"hi*"\n'))

        ok_(
            rule_grammar.parse(
                """
            this = the? that other*
            that = "thing"
            the=~"hi*"
            other = "ahoy hoy"
            """
            )
        )
Exemplo n.º 7
0
    def test_successes(self):
        """Make sure the PEG recognition grammar succeeds on various inputs."""
        ok_(rule_grammar['label'].parse('_'))
        ok_(rule_grammar['label'].parse('jeff'))
        ok_(rule_grammar['label'].parse('_THIS_THING'))

        ok_(rule_grammar['atom'].parse('some_label'))
        ok_(rule_grammar['atom'].parse('"some literal"'))
        ok_(rule_grammar['atom'].parse('~"some regex"i'))

        ok_(rule_grammar['quantified'].parse('~"some regex"i*'))
        ok_(rule_grammar['quantified'].parse('thing+'))
        ok_(rule_grammar['quantified'].parse('"hi"?'))

        ok_(rule_grammar['term'].parse('this'))
        ok_(rule_grammar['term'].parse('that+'))

        ok_(rule_grammar['sequence'].parse('this that? other'))

        ok_(rule_grammar['ored'].parse('this / that+ / "other"'))

        # + is higher precedence than &, so 'anded' should match the whole
        # thing:
        ok_(rule_grammar['lookahead_term'].parse('&this+'))

        ok_(rule_grammar['expression'].parse('this'))
        ok_(rule_grammar['expression'].parse('this? that other*'))
        ok_(rule_grammar['expression'].parse('&this / that+ / "other"'))
        ok_(rule_grammar['expression'].parse('this / that? / "other"+'))
        ok_(rule_grammar['expression'].parse('this? that other*'))

        ok_(rule_grammar['rule'].parse('this = that\r'))
        ok_(rule_grammar['rule'].parse('this = the? that other* \t\r'))
        ok_(rule_grammar['rule'].parse('the=~"hi*"\n'))

        ok_(
            rule_grammar.parse('''
            this = the? that other*
            that = "thing"
            the=~"hi*"
            other = "ahoy hoy"
            '''))
Exemplo n.º 8
0
    def test_successes(self):
        """Make sure the PEG recognition grammar succeeds on various inputs."""
        ok_(rule_grammar['label'].parse('_'))
        ok_(rule_grammar['label'].parse('jeff'))
        ok_(rule_grammar['label'].parse('_THIS_THING'))

        ok_(rule_grammar['atom'].parse('some_label'))
        ok_(rule_grammar['atom'].parse('"some literal"'))
        ok_(rule_grammar['atom'].parse('~"some regex"i'))

        ok_(rule_grammar['quantified'].parse('~"some regex"i*'))
        ok_(rule_grammar['quantified'].parse('thing+'))
        ok_(rule_grammar['quantified'].parse('"hi"?'))

        ok_(rule_grammar['term'].parse('this'))
        ok_(rule_grammar['term'].parse('that+'))

        ok_(rule_grammar['sequence'].parse('this that? other'))

        ok_(rule_grammar['ored'].parse('this / that+ / "other"'))

        # + is higher precedence than &, so 'anded' should match the whole
        # thing:
        ok_(rule_grammar['lookahead_term'].parse('&this+'))

        ok_(rule_grammar['expression'].parse('this'))
        ok_(rule_grammar['expression'].parse('this? that other*'))
        ok_(rule_grammar['expression'].parse('&this / that+ / "other"'))
        ok_(rule_grammar['expression'].parse('this / that? / "other"+'))
        ok_(rule_grammar['expression'].parse('this? that other*'))

        ok_(rule_grammar['rule'].parse('this = that\r'))
        ok_(rule_grammar['rule'].parse('this = the? that other* \t\r'))
        ok_(rule_grammar['rule'].parse('the=~"hi*"\n'))

        ok_(rule_grammar.parse('''
            this = the? that other*
            that = "thing"
            the=~"hi*"
            other = "ahoy hoy"
            '''))
Exemplo n.º 9
0
    def test_successes(self):
        """Make sure the PEG recognition grammar succeeds on various inputs."""
        ok_(rule_grammar['label'].parse('_'))
        ok_(rule_grammar['label'].parse('jeff'))
        ok_(rule_grammar['label'].parse('_THIS_THING'))

        ok_(rule_grammar['atom'].parse('some_label'))
        ok_(rule_grammar['atom'].parse('"some literal"'))
        ok_(rule_grammar['atom'].parse('~"some regex"i'))

        ok_(rule_grammar['quantified'].parse('~"some regex"i*'))
        ok_(rule_grammar['quantified'].parse('thing+'))
        ok_(rule_grammar['quantified'].parse('"hi"?'))

        ok_(rule_grammar['term'].parse('this'))
        ok_(rule_grammar['term'].parse('that+'))

        ok_(rule_grammar['sequence'].parse('this that? other'))

        ok_(rule_grammar['ored'].parse('this / that+ / "other"'))

        ok_(rule_grammar['anded'].parse('this & that+ & "other"'))

        ok_(rule_grammar['poly_term'].parse('this & that+ & "other"'))
        ok_(rule_grammar['poly_term'].parse('this / that? / "other"+'))
        ok_(rule_grammar['poly_term'].parse('this? that other*'))

        ok_(rule_grammar['rhs'].parse('this'))
        ok_(rule_grammar['rhs'].parse('this? that other*'))

        ok_(rule_grammar['rule'].parse('this = that\r'))
        ok_(rule_grammar['rule'].parse('this = the? that other* \t\r'))
        ok_(rule_grammar['rule'].parse('the=~"hi*"\n'))

        ok_(
            rule_grammar.parse('''
            this = the? that other*
            that = "thing"
            the=~"hi*"
            other = "ahoy hoy"
            '''))
Exemplo n.º 10
0
    def test_successes(self):
        """Make sure the PEG recognition grammar succeeds on various inputs."""
        ok_(rule_grammar['label'].parse('_'))
        ok_(rule_grammar['label'].parse('jeff'))
        ok_(rule_grammar['label'].parse('_THIS_THING'))

        ok_(rule_grammar['atom'].parse('some_label'))
        ok_(rule_grammar['atom'].parse('"some literal"'))
        ok_(rule_grammar['atom'].parse('~"some regex"i'))

        ok_(rule_grammar['quantified'].parse('~"some regex"i*'))
        ok_(rule_grammar['quantified'].parse('thing+'))
        ok_(rule_grammar['quantified'].parse('"hi"?'))

        ok_(rule_grammar['term'].parse('this'))
        ok_(rule_grammar['term'].parse('that+'))

        ok_(rule_grammar['sequence'].parse('this that? other'))

        ok_(rule_grammar['ored'].parse('this / that+ / "other"'))

        ok_(rule_grammar['anded'].parse('this & that+ & "other"'))

        ok_(rule_grammar['poly_term'].parse('this & that+ & "other"'))
        ok_(rule_grammar['poly_term'].parse('this / that? / "other"+'))
        ok_(rule_grammar['poly_term'].parse('this? that other*'))

        ok_(rule_grammar['rhs'].parse('this'))
        ok_(rule_grammar['rhs'].parse('this? that other*'))

        ok_(rule_grammar['rule'].parse('this = that\r'))
        ok_(rule_grammar['rule'].parse('this = the? that other* \t\r'))
        ok_(rule_grammar['rule'].parse('the=~"hi*"\n'))

        ok_(rule_grammar.parse('''
            this = the? that other*
            that = "thing"
            the=~"hi*"
            other = "ahoy hoy"
            '''))
Exemplo n.º 11
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)
Exemplo n.º 12
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)