Skip to content

Commit

Permalink
Merge pull request #1252 from MegaIng/fix-1154
Browse files Browse the repository at this point in the history
Fix 1154
  • Loading branch information
erezsh committed Feb 19, 2023
2 parents c9b697e + f845b2e commit 2564232
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lark/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _get_flags(self, value):


class PatternStr(Pattern):
__serialize_fields__ = 'value', 'flags'
__serialize_fields__ = 'value', 'flags', 'raw'

type: ClassVar[str] = "str"

Expand All @@ -80,7 +80,7 @@ def max_width(self) -> int:


class PatternRE(Pattern):
__serialize_fields__ = 'value', 'flags', '_width'
__serialize_fields__ = 'value', 'flags', 'raw', '_width'

type: ClassVar[str] = "re"

Expand Down
21 changes: 20 additions & 1 deletion tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from unittest import TestCase, main, skipIf

from lark import Lark, Tree, Transformer
from lark import Lark, Tree, Transformer, UnexpectedInput
from lark.lexer import Lexer, Token
import lark.lark as lark_module

Expand Down Expand Up @@ -168,7 +168,26 @@ def test_recursive_pattern(self):
self.assertCountEqual(cm.output, ["ERROR:lark:dummy message"])


def test_error_message(self):
# Checks that error message generation works
# This is especially important since sometimes the `str` method fails with
# the mysterious "<unprintable UnexpectedCharacters object>" or similar
g = r"""
start: add+
add: /\d+/ "+" /\d+/
%ignore " "
"""
texts = ("1+", "+1", "", "1 1+1")

parser1 = Lark(g, parser='lalr', cache=True)
parser2 = Lark(g, parser='lalr', cache=True)
assert len(self.mock_fs.files) == 1
for text in texts:
with self.assertRaises((UnexpectedInput)) as cm1:
parser1.parse(text)
with self.assertRaises((UnexpectedInput)) as cm2:
parser2.parse(text)
self.assertEqual(str(cm1.exception), str(cm2.exception))

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion tests/test_nearley/nearley

0 comments on commit 2564232

Please sign in to comment.