ソースを参照

Added test for match_examples

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.11.2
Erez Sh 3年前
コミット
1aff84391a
2個のファイルの変更25行の追加5行の削除
  1. +1
    -1
      lark/exceptions.py
  2. +24
    -4
      tests/test_parser.py

+ 1
- 1
lark/exceptions.py ファイルの表示

@@ -101,7 +101,7 @@ class UnexpectedInput(LarkError):

except AttributeError:
pass
if not candidate[0]:
if candidate[0] is None:
logger.debug("Same State match at example [%s][%s]" % (i, j))
candidate = label, False



+ 24
- 4
tests/test_parser.py ファイルの表示

@@ -323,7 +323,7 @@ class TestParsers(unittest.TestCase):

def test_alias(self):
Lark("""start: ["a"] "b" ["c"] "e" ["f"] ["g"] ["h"] "x" -> d """)
def test_backwards_custom_lexer(self):
class OldCustomLexer(Lexer):
def __init__(self, lexer_conf):
@@ -331,12 +331,12 @@ class TestParsers(unittest.TestCase):

def lex(self, text):
yield Token('A', 'A')
p = Lark("""
start: A
%declare A
""", parser='lalr', lexer=OldCustomLexer)
r = p.parse('')
self.assertEqual(r, Tree('start', [Token('A', 'A')]))

@@ -866,7 +866,7 @@ class CustomLexer(Lexer):
self.lexer = TraditionalLexer(copy(lexer_conf))
def lex(self, *args, **kwargs):
return self.lexer.lex(*args, **kwargs)
__future_interface__ = True

def _tree_structure_check(a, b):
@@ -2342,6 +2342,26 @@ def _make_parser_test(LEXER, PARSER):
self.assertEqual(a.line, 1)
self.assertEqual(b.line, 2)

@unittest.skipIf(LEXER=='standard' and PARSER!='lalr', "Puppet error handling only works with LALR for now")
def test_match_examples(self):
p = _Lark(r"""
start: "a" "b" "c"
""")

def match_error(s):
try:
_ = p.parse(s)
except UnexpectedInput as u:
return u.match_examples(p.parse, {
0: ['abe'],
1: ['ab'],
})
assert False

assert match_error("abe") == 0
assert match_error("ab") == 1


@unittest.skipIf(not regex or sys.version_info[0] == 2, 'Unicode and Python 2 do not place nicely together.')
def test_unicode_class(self):
"Tests that character classes from the `regex` module work correctly."


読み込み中…
キャンセル
保存