Skip to content

Commit 0e9edd1

Browse files
authored
Merge pull request #1120 from gruebel/fix-caching
change MAXREPEAT to int
2 parents bc22192 + 75d65d7 commit 0e9edd1

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lark/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ def get_regexp_width(expr):
137137
# we manually test for the most important info (whether the empty string is matched)
138138
c = regex.compile(regexp_final)
139139
if c.match('') is None:
140-
return 1, sre_constants.MAXREPEAT
140+
# MAXREPEAT is a none pickable subclass of int, therefore needs to be converted to enable caching
141+
return 1, int(sre_constants.MAXREPEAT)
141142
else:
142-
return 0, sre_constants.MAXREPEAT
143+
return 0, int(sre_constants.MAXREPEAT)
143144

144145
###}
145146

tests/test_cache.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import
22

3+
import logging
34
from unittest import TestCase, main
45

56
from lark import Lark, Tree, Transformer
@@ -140,6 +141,24 @@ def test_imports(self):
140141
res = parser.parse("ab")
141142
self.assertEqual(res, Tree('startab', [Tree('expr', ['a', 'b'])]))
142143

144+
def test_recursive_pattern(self):
145+
g = """
146+
start: recursive+
147+
recursive: /\w{3}\d{3}(?R)?/
148+
"""
149+
150+
assert len(self.mock_fs.files) == 0
151+
Lark(g, parser="lalr", regex=True, cache=True)
152+
assert len(self.mock_fs.files) == 1
153+
154+
with self.assertLogs("lark", level="ERROR") as cm:
155+
Lark(g, parser='lalr', regex=True, cache=True)
156+
assert len(self.mock_fs.files) == 1
157+
# need to add an error log, because 'self.assertNoLogs' was added in Python 3.10
158+
logging.getLogger('lark').error("dummy message")
159+
# should only have the dummy log
160+
self.assertCountEqual(cm.output, ["ERROR:lark:dummy message"])
161+
143162

144163

145164

0 commit comments

Comments
 (0)