Browse Source

Change LOGGER to logger

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.10.0
pwwang 4 years ago
parent
commit
2a73afd355
10 changed files with 37 additions and 37 deletions
  1. +3
    -3
      docs/how_to_use.md
  2. +1
    -1
      lark/__init__.py
  3. +3
    -3
      lark/common.py
  4. +3
    -3
      lark/lark.py
  5. +2
    -2
      lark/parsers/earley.py
  6. +3
    -3
      lark/parsers/lalr_analysis.py
  7. +4
    -4
      tests/__main__.py
  8. +13
    -13
      tests/test_logger.py
  9. +3
    -3
      tests/test_nearley/test_nearley.py
  10. +2
    -2
      tests/test_parser.py

+ 3
- 3
docs/how_to_use.md View File

@@ -30,13 +30,13 @@ Use the reference pages for more in-depth explanations. (links in the [main page


## LALR usage ## LALR usage


By default Lark silently resolves Shift/Reduce conflicts as Shift. To enable warnings pass `debug=True`. To get the messages printed you have to configure the `LOGGER` beforehand. For example:
By default Lark silently resolves Shift/Reduce conflicts as Shift. To enable warnings pass `debug=True`. To get the messages printed you have to configure the `logger` beforehand. For example:


```python ```python
import logging import logging
from lark import Lark, LOGGER
from lark import Lark, logger


LOGGER.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)


collision_grammar = ''' collision_grammar = '''
start: as as start: as as


+ 1
- 1
lark/__init__.py View File

@@ -1,4 +1,4 @@
from .common import LOGGER
from .common import logger
from .tree import Tree from .tree import Tree
from .visitors import Transformer, Visitor, v_args, Discard from .visitors import Transformer, Visitor, v_args, Discard
from .visitors import InlineTransformer, inline_args # XXX Deprecated from .visitors import InlineTransformer, inline_args # XXX Deprecated


+ 3
- 3
lark/common.py View File

@@ -2,11 +2,11 @@ import logging
from .utils import Serialize from .utils import Serialize
from .lexer import TerminalDef from .lexer import TerminalDef


LOGGER = logging.getLogger("lark")
LOGGER.addHandler(logging.StreamHandler())
logger = logging.getLogger("lark")
logger.addHandler(logging.StreamHandler())
# Set to highest level, since we have some warnings amongst the code # Set to highest level, since we have some warnings amongst the code
# By default, we should not output any log messages # By default, we should not output any log messages
LOGGER.setLevel(logging.CRITICAL)
logger.setLevel(logging.CRITICAL)


###{standalone ###{standalone




+ 3
- 3
lark/lark.py View File

@@ -7,7 +7,7 @@ from io import open
from .utils import STRING_TYPE, Serialize, SerializeMemoizer, FS from .utils import STRING_TYPE, Serialize, SerializeMemoizer, FS
from .load_grammar import load_grammar from .load_grammar import load_grammar
from .tree import Tree from .tree import Tree
from .common import LexerConf, ParserConf, LOGGER
from .common import LexerConf, ParserConf, logger


from .lexer import Lexer, TraditionalLexer, TerminalDef, UnexpectedToken from .lexer import Lexer, TraditionalLexer, TerminalDef, UnexpectedToken
from .parse_tree_builder import ParseTreeBuilder from .parse_tree_builder import ParseTreeBuilder
@@ -205,7 +205,7 @@ class Lark(Serialize):
cache_fn = '.lark_cache_%s.tmp' % md5 cache_fn = '.lark_cache_%s.tmp' % md5


if FS.exists(cache_fn): if FS.exists(cache_fn):
LOGGER.debug('Loading grammar from cache: %s', cache_fn)
logger.debug('Loading grammar from cache: %s', cache_fn)
with FS.open(cache_fn, 'rb') as f: with FS.open(cache_fn, 'rb') as f:
self._load(f, self.options.transformer, self.options.postlex) self._load(f, self.options.transformer, self.options.postlex)
return return
@@ -284,7 +284,7 @@ class Lark(Serialize):
self.lexer = self._build_lexer() self.lexer = self._build_lexer()


if cache_fn: if cache_fn:
LOGGER.debug('Saving grammar to cache: %s', cache_fn)
logger.debug('Saving grammar to cache: %s', cache_fn)
with FS.open(cache_fn, 'wb') as f: with FS.open(cache_fn, 'wb') as f:
self.save(f) self.save(f)




+ 2
- 2
lark/parsers/earley.py View File

@@ -14,7 +14,7 @@ from collections import deque


from ..visitors import Transformer_InPlace, v_args from ..visitors import Transformer_InPlace, v_args
from ..exceptions import UnexpectedEOF, UnexpectedToken from ..exceptions import UnexpectedEOF, UnexpectedToken
from ..common import LOGGER
from ..common import logger
from .grammar_analysis import GrammarAnalyzer from .grammar_analysis import GrammarAnalyzer
from ..grammar import NonTerminal from ..grammar import NonTerminal
from .earley_common import Item, TransitiveItem from .earley_common import Item, TransitiveItem
@@ -301,7 +301,7 @@ class Parser:
try: try:
debug_walker = ForestToPyDotVisitor() debug_walker = ForestToPyDotVisitor()
except ImportError: except ImportError:
LOGGER.warning("Cannot find dependency 'pydot', will not generate sppf debug image")
logger.warning("Cannot find dependency 'pydot', will not generate sppf debug image")
else: else:
debug_walker.visit(solutions[0], "sppf.png") debug_walker.visit(solutions[0], "sppf.png")




+ 3
- 3
lark/parsers/lalr_analysis.py View File

@@ -10,7 +10,7 @@ from collections import defaultdict, deque


from ..utils import classify, classify_bool, bfs, fzset, Serialize, Enumerator from ..utils import classify, classify_bool, bfs, fzset, Serialize, Enumerator
from ..exceptions import GrammarError from ..exceptions import GrammarError
from ..common import LOGGER
from ..common import logger


from .grammar_analysis import GrammarAnalyzer, Terminal, LR0ItemSet from .grammar_analysis import GrammarAnalyzer, Terminal, LR0ItemSet
from ..grammar import Rule from ..grammar import Rule
@@ -256,8 +256,8 @@ class LALR_Analyzer(GrammarAnalyzer):
raise GrammarError('Reduce/Reduce collision in %s between the following rules: %s' % (la, ''.join([ '\n\t\t- ' + str(r) for r in rules ]))) raise GrammarError('Reduce/Reduce collision in %s between the following rules: %s' % (la, ''.join([ '\n\t\t- ' + str(r) for r in rules ])))
if la in actions: if la in actions:
if self.debug: if self.debug:
LOGGER.warning('Shift/Reduce conflict for terminal %s: (resolving as shift)', la.name)
LOGGER.warning(' * %s', list(rules)[0])
logger.warning('Shift/Reduce conflict for terminal %s: (resolving as shift)', la.name)
logger.warning(' * %s', list(rules)[0])
else: else:
actions[la] = (Reduce, list(rules)[0]) actions[la] = (Reduce, list(rules)[0])
m[state] = { k.name: v for k, v in actions.items() } m[state] = { k.name: v for k, v in actions.items() }


+ 4
- 4
tests/__main__.py View File

@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function


import unittest import unittest
import logging import logging
from lark import LOGGER
from lark import logger


from .test_trees import TestTrees from .test_trees import TestTrees
from .test_tools import TestStandalone from .test_tools import TestStandalone
@@ -12,12 +12,12 @@ from .test_reconstructor import TestReconstructor
try: try:
from .test_nearley.test_nearley import TestNearley from .test_nearley.test_nearley import TestNearley
except ImportError: except ImportError:
LOGGER.warning("Warning: Skipping tests for Nearley grammar imports (js2py required)")
logger.warning("Warning: Skipping tests for Nearley grammar imports (js2py required)")


# from .test_selectors import TestSelectors # from .test_selectors import TestSelectors
# from .test_grammars import TestPythonG, TestConfigG # from .test_grammars import TestPythonG, TestConfigG


from .test_logger import TestLogger
from .test_logger import Testlogger


from .test_parser import ( from .test_parser import (
TestLalrStandard, TestLalrStandard,
@@ -34,7 +34,7 @@ from .test_parser import (
TestParsers, TestParsers,
) )


LOGGER.setLevel(logging.INFO)
logger.setLevel(logging.INFO)


if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

+ 13
- 13
tests/test_logger.py View File

@@ -1,6 +1,6 @@
import logging import logging
from contextlib import contextmanager from contextlib import contextmanager
from lark import Lark, LOGGER
from lark import Lark, logger
from unittest import TestCase, main from unittest import TestCase, main


try: try:
@@ -11,17 +11,17 @@ except ImportError:
@contextmanager @contextmanager
def capture_log(): def capture_log():
stream = StringIO() stream = StringIO()
orig_handler = LOGGER.handlers[0]
del LOGGER.handlers[:]
LOGGER.addHandler(logging.StreamHandler(stream))
orig_handler = logger.handlers[0]
del logger.handlers[:]
logger.addHandler(logging.StreamHandler(stream))
yield stream yield stream
del LOGGER.handlers[:]
LOGGER.addHandler(orig_handler)
del logger.handlers[:]
logger.addHandler(orig_handler)


class TestLogger(TestCase):
class Testlogger(TestCase):


def test_debug(self): def test_debug(self):
LOGGER.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
collision_grammar = ''' collision_grammar = '''
start: as as start: as as
as: a* as: a*
@@ -31,12 +31,12 @@ class TestLogger(TestCase):
Lark(collision_grammar, parser='lalr', debug=True) Lark(collision_grammar, parser='lalr', debug=True)


log = log.getvalue() log = log.getvalue()
self.assertIn("Shift/Reduce conflict for terminal", log)
self.assertIn("A: (resolving as shift)", log)
self.assertIn("Shift/Reduce conflict for terminal A: (resolving as shift)", log)
# since there are conflicts about A
# symbol A should appear in the log message for hint
self.assertIn("A", log)


def test_non_debug(self): def test_non_debug(self):
LOGGER.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
collision_grammar = ''' collision_grammar = '''
start: as as start: as as
as: a* as: a*
@@ -49,7 +49,7 @@ class TestLogger(TestCase):
self.assertEqual(len(log), 0) self.assertEqual(len(log), 0)


def test_loglevel_higher(self): def test_loglevel_higher(self):
LOGGER.setLevel(logging.ERROR)
logger.setLevel(logging.ERROR)
collision_grammar = ''' collision_grammar = '''
start: as as start: as as
as: a* as: a*


+ 3
- 3
tests/test_nearley/test_nearley.py View File

@@ -6,17 +6,17 @@ import logging
import os import os
import codecs import codecs


from lark import LOGGER
from lark import logger
from lark.tools.nearley import create_code_for_nearley_grammar, main as nearley_tool_main from lark.tools.nearley import create_code_for_nearley_grammar, main as nearley_tool_main


LOGGER.setLevel(logging.INFO)
logger.setLevel(logging.INFO)


TEST_PATH = os.path.abspath(os.path.dirname(__file__)) TEST_PATH = os.path.abspath(os.path.dirname(__file__))
NEARLEY_PATH = os.path.join(TEST_PATH, 'nearley') NEARLEY_PATH = os.path.join(TEST_PATH, 'nearley')
BUILTIN_PATH = os.path.join(NEARLEY_PATH, 'builtin') BUILTIN_PATH = os.path.join(NEARLEY_PATH, 'builtin')


if not os.path.exists(NEARLEY_PATH): if not os.path.exists(NEARLEY_PATH):
LOGGER.warn("Nearley not installed. Skipping Nearley tests!")
logger.warn("Nearley not installed. Skipping Nearley tests!")
raise ImportError("Skipping Nearley tests!") raise ImportError("Skipping Nearley tests!")


import js2py # Ensures that js2py exists, to avoid failing tests import js2py # Ensures that js2py exists, to avoid failing tests


+ 2
- 2
tests/test_parser.py View File

@@ -24,7 +24,7 @@ try:
except ImportError: except ImportError:
regex = None regex = None


from lark import LOGGER
from lark import logger
from lark.lark import Lark from lark.lark import Lark
from lark.exceptions import GrammarError, ParseError, UnexpectedToken, UnexpectedInput, UnexpectedCharacters from lark.exceptions import GrammarError, ParseError, UnexpectedToken, UnexpectedInput, UnexpectedCharacters
from lark.tree import Tree from lark.tree import Tree
@@ -32,7 +32,7 @@ from lark.visitors import Transformer, Transformer_InPlace, v_args
from lark.grammar import Rule from lark.grammar import Rule
from lark.lexer import TerminalDef, Lexer, TraditionalLexer from lark.lexer import TerminalDef, Lexer, TraditionalLexer


LOGGER.setLevel(logging.INFO)
logger.setLevel(logging.INFO)




__path__ = os.path.dirname(__file__) __path__ = os.path.dirname(__file__)


Loading…
Cancel
Save