diff --git a/lark/lexer.py b/lark/lexer.py index 3464591..a7e5c26 100644 --- a/lark/lexer.py +++ b/lark/lexer.py @@ -12,7 +12,7 @@ import warnings try: import interegular except ImportError: - interegular = None + pass if TYPE_CHECKING: from .common import LexerConf @@ -24,10 +24,10 @@ from .grammar import TOKEN_DEFAULT_PRIORITY ###{standalone from copy import copy -try: # For the standalone parser, we need to make sure that interegular is set to None to avoid NameErrors later on - interegular +try: # For the standalone parser, we need to make sure that has_interegular is False to avoid NameErrors later on + has_interegular = bool(interegular) except NameError: - interegular = None + has_interegular = False class Pattern(Serialize, ABC): @@ -490,7 +490,7 @@ class BasicLexer(Lexer): if not (set(conf.ignore) <= {t.name for t in terminals}): raise LexError("Ignore terminals are not defined: %s" % (set(conf.ignore) - {t.name for t in terminals})) - if interegular: + if has_interegular: if not comparator: comparator = interegular.Comparator.from_regexes(terminal_to_regexp) for group in classify(terminal_to_regexp, lambda t:t.priority).values(): @@ -592,7 +592,7 @@ class ContextualLexer(Lexer): trad_conf = copy(conf) trad_conf.terminals = terminals - if interegular and not conf.skip_validation: + if has_interegular and not conf.skip_validation: comparator = interegular.Comparator.from_regexes({t: t.pattern.to_regexp() for t in terminals}) else: comparator = None diff --git a/lark/tools/__init__.py b/lark/tools/__init__.py index 5daf972..c6995c6 100644 --- a/lark/tools/__init__.py +++ b/lark/tools/__init__.py @@ -8,8 +8,9 @@ import warnings from lark import Lark, logger try: from interegular import logger as interegular_logger + has_interegular = True except ImportError: - interegular_logger = None + has_interegular = False lalr_argparser = ArgumentParser(add_help=False, epilog='Look at the Lark documentation for more info on the options') @@ -44,7 +45,7 @@ for flag in flags: def build_lalr(namespace): logger.setLevel((ERROR, WARN, INFO, DEBUG)[min(namespace.verbose, 3)]) - if interegular_logger is not None: + if has_interegular: interegular_logger.setLevel(logger.getEffectiveLevel()) if len(namespace.start) == 0: namespace.start.append('start') diff --git a/lark/utils.py b/lark/utils.py index 198c352..753610a 100644 --- a/lark/utils.py +++ b/lark/utils.py @@ -3,7 +3,7 @@ import os from functools import reduce from itertools import product from collections import deque -from typing import Callable, Iterator, List, Optional, Tuple, Type, TypeVar, Union, Dict, Any, Sequence +from typing import Callable, Iterator, List, Optional, Tuple, Type, TypeVar, Union, Dict, Any, Sequence, Iterable ###{standalone import sys, re @@ -21,7 +21,7 @@ NO_VALUE = object() T = TypeVar("T") -def classify(seq: Sequence, key: Optional[Callable] = None, value: Optional[Callable] = None) -> Dict: +def classify(seq: Iterable, key: Optional[Callable] = None, value: Optional[Callable] = None) -> Dict: d: Dict[Any, Any] = {} for item in seq: k = key(item) if (key is not None) else item diff --git a/test-requirements.txt b/test-requirements.txt index 85303e9..de2a735 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,3 +1,3 @@ -interegular>=0.2.2 +interegular>=0.2.3 Js2Py==0.68 regex diff --git a/tox.ini b/tox.ini index 0b1426d..0ca1d45 100644 --- a/tox.ini +++ b/tox.ini @@ -23,6 +23,7 @@ skip_install = true recreate = false deps = mypy==0.950 + interegular>=0.2.3 types-atomicwrites types-regex rich