Simplify inspection of matched sequences
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_matchToCur
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(
"""
MyGrammar = style2.
parser = MyGrammar(in2)
actual = parser.apply('aa')
expected = ('ab', ('cd', '[cd]'), 'ef')
in1 = ["ab", ["c", "d"], "ef"]
g = """aa ::= "ab":a1 <a2>:a2 "ef":a3 <end> => a1,a2,a3
a2 ::= <pos>:p1 [ "c" "d" ] => 'cd', self.matchToCur
"""
MyGrammar2 = style2.
parser = MyGrammar2(in1)
actual = parser.apply('aa')
expected = ('ab', ('cd', [['c', 'd']]), 'ef')
and style2.
class EnhancedGrammar
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.