소스 검색

Removed some of the Python2 compatibility code

gm/2021-09-23T00Z/github.com--lark-parser-lark/1.0b
Erez Sh 3 년 전
부모
커밋
8a00284700
6개의 변경된 파일25개의 추가작업 그리고 69개의 파일을 삭제
  1. +2
    -2
      lark/exceptions.py
  2. +6
    -10
      lark/lark.py
  3. +5
    -4
      lark/lexer.py
  4. +4
    -3
      lark/load_grammar.py
  5. +1
    -0
      lark/tools/standalone.py
  6. +7
    -50
      lark/utils.py

+ 2
- 2
lark/exceptions.py 파일 보기

@@ -1,6 +1,6 @@
from warnings import warn

from .utils import STRING_TYPE, logger, NO_VALUE
from .utils import logger, NO_VALUE


###{standalone
@@ -90,7 +90,7 @@ class UnexpectedInput(LarkError):

candidate = (None, False)
for i, (label, example) in enumerate(examples):
assert not isinstance(example, STRING_TYPE)
assert not isinstance(example, str), "Expecting a list"

for j, malformed in enumerate(example):
try:


+ 6
- 10
lark/lark.py 파일 보기

@@ -1,14 +1,13 @@
from __future__ import absolute_import


from lark.exceptions import ConfigurationError, assert_config

from abc import ABC, abstractmethod
import sys, os, pickle, hashlib
from io import open
import tempfile
from warnings import warn

from .utils import STRING_TYPE, Serialize, SerializeMemoizer, FS, isascii, logger, ABC, abstractmethod
from .exceptions import ConfigurationError, assert_config
from .utils import Serialize, SerializeMemoizer, FS, isascii, logger
from .load_grammar import load_grammar, FromPackageLoader, Grammar, verify_used_files
from .tree import Tree
from .common import LexerConf, ParserConf
@@ -153,7 +152,7 @@ class LarkOptions(Serialize):

options[name] = value

if isinstance(options['start'], STRING_TYPE):
if isinstance(options['start'], str):
options['start'] = [options['start']]

self.__dict__['options'] = options
@@ -247,14 +246,11 @@ class Lark(Serialize):

cache_fn = None
cache_md5 = None
if isinstance(grammar, STRING_TYPE):
if isinstance(grammar, str):
self.source_grammar = grammar
if self.options.use_bytes:
if not isascii(grammar):
raise ConfigurationError("Grammar must be ascii only, when use_bytes=True")
if sys.version_info[0] == 2 and self.options.use_bytes != 'force':
raise ConfigurationError("`use_bytes=True` may have issues on python2."
"Use `use_bytes='force'` to use it at your own risk.")

if self.options.cache:
if self.options.parser != 'lalr':
@@ -266,7 +262,7 @@ class Lark(Serialize):
s = grammar + options_str + __version__ + str(sys.version_info[:2])
cache_md5 = hashlib.md5(s.encode('utf8')).hexdigest()

if isinstance(self.options.cache, STRING_TYPE):
if isinstance(self.options.cache, str):
cache_fn = self.options.cache
else:
if self.options.cache is not True:


+ 5
- 4
lark/lexer.py 파일 보기

@@ -1,8 +1,9 @@
# Lexer Implementation

import re
from contextlib import suppress

from .utils import Str, classify, get_regexp_width, Py36, Serialize, suppress
from .utils import classify, get_regexp_width, Py36, Serialize
from .exceptions import UnexpectedCharacters, LexError, UnexpectedToken

###{standalone
@@ -110,7 +111,7 @@ class TerminalDef(Serialize):
return self.name


class Token(Str):
class Token(str):
"""A string with meta-information, that is produced by the lexer.

When parsing text, the resulting chunks of the input that haven't been discarded,
@@ -177,9 +178,9 @@ class Token(Str):
if isinstance(other, Token) and self.type != other.type:
return False

return Str.__eq__(self, other)
return str.__eq__(self, other)

__hash__ = Str.__hash__
__hash__ = str.__hash__


class LineCounter:


+ 4
- 3
lark/load_grammar.py 파일 보기

@@ -8,6 +8,7 @@ from io import open
import pkgutil
from ast import literal_eval
from numbers import Integral
from contextlib import suppress

from .utils import bfs, Py36, logger, classify_bool, is_id_continue, is_id_start, bfs_all_unique
from .lexer import Token, TerminalDef, PatternStr, PatternRE
@@ -16,7 +17,7 @@ from .parse_tree_builder import ParseTreeBuilder
from .parser_frontends import ParsingFrontend
from .common import LexerConf, ParserConf
from .grammar import RuleOptions, Rule, Terminal, NonTerminal, Symbol
from .utils import classify, suppress, dedup_list, Str
from .utils import classify, dedup_list
from .exceptions import GrammarError, UnexpectedCharacters, UnexpectedToken, ParseError

from .tree import Tree, SlottedTree as ST
@@ -539,9 +540,9 @@ class PrepareSymbols(Transformer_InPlace):
if isinstance(v, Tree):
return v
elif v.type == 'RULE':
return NonTerminal(Str(v.value))
return NonTerminal(str(v.value))
elif v.type == 'TERMINAL':
return Terminal(Str(v.value), filter_out=v.startswith('_'))
return Terminal(str(v.value), filter_out=v.startswith('_'))
assert False




+ 1
- 0
lark/tools/standalone.py 파일 보기

@@ -27,6 +27,7 @@ from __future__ import print_function
#

from io import open
from abc import ABC, abstractmethod
###}

import sys


+ 7
- 50
lark/utils.py 파일 보기

@@ -1,4 +1,3 @@
import hashlib
import unicodedata
import os
from functools import reduce
@@ -14,14 +13,6 @@ logger.addHandler(logging.StreamHandler())
# By default, we should not output any log messages
logger.setLevel(logging.CRITICAL)

if sys.version_info[0]>2:
from abc import ABC, abstractmethod
else:
from abc import ABCMeta, abstractmethod
class ABC(object): # Provide Python27 compatibility
__slots__ = ()
__metclass__ = ABCMeta


Py36 = (sys.version_info[:2] >= (3, 6))

@@ -120,28 +111,16 @@ class SerializeMemoizer(Serialize):
return _deserialize(data, namespace, memo)


try:
STRING_TYPE = basestring
except NameError: # Python 3
STRING_TYPE = str


import types
from functools import wraps, partial
from contextlib import contextmanager

Str = type(u'')
try:
classtype = types.ClassType # Python2
except AttributeError:
classtype = type # Python3


def smart_decorator(f, create_decorator):
if isinstance(f, types.FunctionType):
return wraps(f)(create_decorator(f, True))

elif isinstance(f, (classtype, type, types.BuiltinFunctionType)):
elif isinstance(f, (type, types.BuiltinFunctionType)):
return wraps(f)(create_decorator(f, False))

elif isinstance(f, types.MethodType):
@@ -222,34 +201,12 @@ def dedup_list(l):
return [x for x in l if not (x in dedup or dedup.add(x))]


try:
from contextlib import suppress # Python 3
except ImportError:
@contextmanager
def suppress(*excs):
'''Catch and dismiss the provided exception

>>> x = 'hello'
>>> with suppress(IndexError):
... x = x[10]
>>> x
'hello'
'''
try:
yield
except excs:
pass


try:
compare = cmp
except NameError:
def compare(a, b):
if a == b:
return 0
elif a > b:
return 1
return -1
def compare(a, b):
if a == b:
return 0
elif a > b:
return 1
return -1


class Enumerator(Serialize):


불러오는 중...
취소
저장