Simplify inspection of matched sequences

Bug #567060 reported by Cory Dodt
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
PyMeta
New
Undecided
Unassigned

Bug Description

I need to capture the parsed text for certain cases. For example, in the template language I'm (re-)implementing:

"[foo]bar"

The first token [foo] is expanded to the value of the template named 'foo', if it exists. If it doesn't, it's supposed to expand to the literal string '[foo]'.

Here's a test and an implementation. In this test, a2 returns both a return value 'cd' and the original matched text.

    def test_matchToCursor(self):
        in2 = "ab[cd]ef"
        g = """aa ::= ('a' 'b' => 'ab'):a1 <a2>:a2 ('e' 'f' => 'ef'):a3 <end> => a1,a2,a3
            a2 ::= <pos>:p1 '[' 'c' 'd' ']' => 'cd', ''.join(self.matchToCursor(p1))
            """
        MyGrammar = style2.EnhancedGrammar.makeGrammar(g, {}, "MyGrammar")
        parser = MyGrammar(in2)
        actual = parser.apply('aa')
        expected = ('ab', ('cd', '[cd]'), 'ef')
        self.assertEqual(actual, expected)

        in1 = ["ab", ["c", "d"], "ef"]
        g = """aa ::= "ab":a1 <a2>:a2 "ef":a3 <end> => a1,a2,a3
            a2 ::= <pos>:p1 [ "c" "d" ] => 'cd', self.matchToCursor(p1)
            """
        MyGrammar2 = style2.EnhancedGrammar.makeGrammar(g, {}, "MyGrammar2")
        parser = MyGrammar2(in1)
        actual = parser.apply('aa')
        expected = ('ab', ('cd', [['c', 'd']]), 'ef')
        self.assertEqual(actual, expected)

and style2.EnhancedGrammar looks like this:

class EnhancedGrammar(OMeta):
    def rule_pos(self):
        """
        Return the position the cursor is currently at
        """
        return self.input.position

    def matchToCursor(self, startpos):
        """
        Return the sequence beginning at startpos and ending at the current
        input cursor
        """
        return self.input.data[startpos:self.input.position]

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.