Skip to content

Commit 9f45875

Browse files
committed
Added tests for old custom lexer interface (future_interface=1)
1 parent 9ce40fe commit 9f45875

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

tests/test_parser.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,19 @@ def lex(self, lexer_state, parser_state):
10081008

10091009
__future_interface__ = 2
10101010

1011-
class CustomLexerOld(Lexer):
1011+
class CustomLexerOld1(Lexer):
1012+
"""
1013+
Purpose of this custom lexer is to test the integration,
1014+
so it uses the traditionalparser as implementation without custom lexing behaviour.
1015+
"""
1016+
def __init__(self, lexer_conf):
1017+
self.lexer = BasicLexer(copy(lexer_conf))
1018+
def lex(self, lexer_state, parser_state):
1019+
return self.lexer.lex(lexer_state, parser_state)
1020+
1021+
__future_interface__ = 1
1022+
1023+
class CustomLexerOld0(Lexer):
10121024
"""
10131025
Purpose of this custom lexer is to test the integration,
10141026
so it uses the traditionalparser as implementation without custom lexing behaviour.
@@ -1095,7 +1107,8 @@ def load(self,f):
10951107
def _make_parser_test(LEXER, PARSER):
10961108
lexer_class_or_name = {
10971109
'custom_new': CustomLexerNew,
1098-
'custom_old': CustomLexerOld,
1110+
'custom_old1': CustomLexerOld1,
1111+
'custom_old0': CustomLexerOld0,
10991112
}.get(LEXER, LEXER)
11001113

11011114
def _Lark(grammar, **kwargs):
@@ -1649,7 +1662,7 @@ def test_token_flags(self):
16491662
tree = l.parse('AB,a')
16501663
self.assertEqual(tree.children, ['AB'])
16511664

1652-
@unittest.skipIf(LEXER in ('basic', 'custom_old', 'custom_new'), "Requires context sensitive terminal selection")
1665+
@unittest.skipIf(LEXER in ('basic', 'custom_old0', 'custom_old1', 'custom_new'), "Requires context sensitive terminal selection")
16531666
def test_token_flags_collision(self):
16541667

16551668
g = """!start: "a"i "a"
@@ -2408,7 +2421,7 @@ def test_meddling_unused(self):
24082421
parser = _Lark(grammar)
24092422

24102423

2411-
@unittest.skipIf(PARSER!='lalr' or LEXER == 'custom_old', "Serialize currently only works for LALR parsers without custom lexers (though it should be easy to extend)")
2424+
@unittest.skipIf(PARSER!='lalr' or LEXER == 'custom_old0', "Serialize currently only works for LALR parsers without custom lexers (though it should be easy to extend)")
24122425
def test_serialize(self):
24132426
grammar = """
24142427
start: _ANY b "C"
@@ -2454,7 +2467,7 @@ def test_lexer_detect_newline_tokens(self):
24542467
self.assertEqual(a.line, 1)
24552468
self.assertEqual(b.line, 2)
24562469

2457-
@unittest.skipIf(PARSER=='cyk' or LEXER=='custom_old', "match_examples() not supported for CYK/old custom lexer")
2470+
@unittest.skipIf(PARSER=='cyk' or LEXER=='custom_old0', "match_examples() not supported for CYK/old custom lexer")
24582471
def test_match_examples(self):
24592472
p = _Lark(r"""
24602473
start: "a" "b" "c"
@@ -2668,7 +2681,7 @@ def test_strict(self):
26682681
"""
26692682
self.assertRaises(GrammarError, _Lark, grammar, strict=True)
26702683

2671-
@unittest.skipIf(LEXER in ('dynamic', 'dynamic_complete', 'custom_old'),
2684+
@unittest.skipIf(LEXER in ('dynamic', 'dynamic_complete', 'custom_old0', 'custom_old1'),
26722685
"start_pos and end_pos not compatible with old style custom/dynamic lexer ")
26732686
def test_parse_textslice(self):
26742687
grammar = r"""
@@ -2712,7 +2725,7 @@ def test_parse_textslice(self):
27122725
assert t.line == 9
27132726

27142727

2715-
@unittest.skipIf(LEXER not in ('dynamic', 'dynamic_complete', 'custom_old'),
2728+
@unittest.skipIf(LEXER not in ('dynamic', 'dynamic_complete', 'custom_old0', 'custom_old1'),
27162729
"start_pos and end_pos not compatible with old style custom/dynamic lexer ")
27172730
def test_parse_textslice_fails(self):
27182731
parser = _Lark("start: ")
@@ -2738,7 +2751,8 @@ def test_parse_textslice_fails(self):
27382751

27392752
('custom_new', 'lalr'),
27402753
('custom_new', 'cyk'),
2741-
('custom_old', 'earley'),
2754+
('custom_old0', 'earley'),
2755+
('custom_old1', 'earley'),
27422756
]
27432757

27442758
for _LEXER, _PARSER in _TO_TEST:

0 commit comments

Comments
 (0)