Skip to content

Commit

Permalink
Added basic tests, added comment
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaIng committed Mar 2, 2023
1 parent 718c738 commit 61bd562
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lark/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
###{standalone
from copy import copy

try:
try: # For the standalone parser, we need to make sure that interegular is set to None to avoid NameErrors later on
interegular
except NameError:
interegular = None
Expand Down Expand Up @@ -498,8 +498,8 @@ def __init__(self, conf: 'LexerConf', comparator=None) -> None:
assert a.priority == b.priority
# Mark this pair to not repeat warnings when multiple different BasicLexers see the same collision
comparator.mark(a, b)
# leave it as a warning for the moment

# leave it as a warning for the moment
# raise LexError("Collision between Terminals %s and %s" % (a.name, b.name))
logger.warning("Collision between Terminals %r and %r: %r" %
(a.name, b.name, comparator.get_example_overlap(a, b)))
Expand Down Expand Up @@ -612,6 +612,7 @@ def __init__(self, conf: 'LexerConf', states: Dict[str, Collection[str]], always
self.lexers[state] = lexer

assert trad_conf.terminals is terminals
trad_conf.skip_validation = True # We don't need to verify all terminals again
self.root_lexer = BasicLexer(trad_conf, comparator)

def lex(self, lexer_state: LexerState, parser_state: Any) -> Iterator[Token]:
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Js2Py==0.68
regex
interegular>=0.2.2
39 changes: 38 additions & 1 deletion tests/test_logger.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import logging
from contextlib import contextmanager
from lark import Lark, logger
from unittest import TestCase, main
from unittest import TestCase, main, skipIf

try:
from StringIO import StringIO
except ImportError:
from io import StringIO

try:
import interegular
except ImportError:
interegular = None

@contextmanager
def capture_log():
stream = StringIO()
Expand Down Expand Up @@ -61,5 +66,37 @@ def test_loglevel_higher(self):
# no log message
self.assertEqual(len(log), 0)

@skipIf(interegular is None, "interegular is not installed, can't test regex collisions")
def test_regex_collision(self):
logger.setLevel(logging.WARNING)
collision_grammar = '''
start: A | B
A: /a+/
B: /(a|b)+/
'''
with capture_log() as log:
Lark(collision_grammar, parser='lalr')

log = log.getvalue()
# since there are conflicts between A and B
# symbols A and B should appear in the log message
self.assertIn("A", log)
self.assertIn("B", log)

@skipIf(interegular is None, "interegular is not installed, can't test regex collisions")
def test_regex_no_collision(self):
logger.setLevel(logging.WARNING)
collision_grammar = '''
start: A " " B
A: /a+/
B: /(a|b)+/
'''
with capture_log() as log:
Lark(collision_grammar, parser='lalr')

log = log.getvalue()
self.assertEqual(log, "")


if __name__ == '__main__':
main()

0 comments on commit 61bd562

Please sign in to comment.