Bläddra i källkod

Now supports Python3!

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.5.1
Erez Shinan 7 år sedan
förälder
incheckning
f1b2e1926c
8 ändrade filer med 25 tillägg och 16 borttagningar
  1. +8
    -3
      lark/earley.py
  2. +4
    -3
      lark/grammar_analysis.py
  3. +3
    -2
      lark/lexer.py
  4. +5
    -4
      lark/load_grammar.py
  5. +1
    -1
      lark/parser.py
  6. +1
    -1
      lark/parser_frontends.py
  7. +2
    -1
      lark/tree.py
  8. +1
    -1
      lark/utils.py

+ 8
- 3
lark/earley.py Visa fil

@@ -1,6 +1,11 @@
"My name is Earley"

from .utils import classify
from .utils import classify, STRING_TYPE

try:
xrange
except NameError:
xrange = range

class MatchFailed(object):
pass
@@ -27,7 +32,7 @@ class State(object):
self.is_literal = isinstance(self.expect_symbol, dict)
if self.is_literal:
self.expect_symbol = self.expect_symbol['literal']
assert isinstance(self.expect_symbol, (str, unicode)), self.expect_symbol
assert isinstance(self.expect_symbol, STRING_TYPE), self.expect_symbol

def next_state(self, data):
return State(self.rule, self.expect+1, self.reference, self.data + [data])
@@ -139,6 +144,6 @@ class Parser(object):
if (t.rule.name == self.start
and t.expect == len(t.rule.symbols)
and t.reference == 0
and t.data != MatchFailed):
and t.data is not MatchFailed):
yield t.data


+ 4
- 3
lark/grammar_analysis.py Visa fil

@@ -1,6 +1,7 @@
from collections import defaultdict, deque
from utils import classify, classify_bool, bfs, fzset
from common import GrammarError, is_terminal

from .utils import classify, classify_bool, bfs, fzset
from .common import GrammarError, is_terminal

ACTION_SHIFT = 0

@@ -105,7 +106,7 @@ class GrammarAnalyzer(object):
"""Calculate FOLLOW sets.

Adapted from: http://lara.epfl.ch/w/cc09:algorithm_for_first_and_follow_sets"""
symbols = {sym for rule in self.rules for sym in rule.expansion}
symbols = {sym for rule in self.rules for sym in rule.expansion} | {rule.origin for rule in self.rules}
symbols.add('$root') # what about other unused rules?

# foreach grammar rule X ::= Y(1) ... Y(k)


+ 3
- 2
lark/lexer.py Visa fil

@@ -1,5 +1,6 @@
## Lexer Implementation
from utils import Str

from .utils import Str

class LexError(Exception):
pass
@@ -52,7 +53,7 @@ class Lexer(object):

self.mres = []
self.name_from_index = []
x = tokens
x = list(tokens)
while x:
mre = re.compile(u'|'.join(u'(?P<%s>%s)'%t for t in x[:LIMIT]))
self.mres.append(mre)


+ 5
- 4
lark/load_grammar.py Visa fil

@@ -185,7 +185,7 @@ class SimplifyRule_Visitor(Visitor):
expansions = _flatten

def dict_update_safe(d1, d2):
for k, v in d2.iteritems():
for k, v in d2.items():
assert k not in d1
d1[k] = v

@@ -195,7 +195,8 @@ class RuleTreeToText(Transformer):
return x
def expansion(self, symbols):
return [sym.value for sym in symbols], None
def alias(self, ((expansion, _alias), alias)):
def alias(self, x):
(expansion, _alias), alias = x
assert _alias is None, (alias, expansion, '-', _alias)
return expansion, alias.value

@@ -294,7 +295,7 @@ class GrammarLoader:
tokens2 = []
for name, token, flags in tokens:
value = token.value[1:-1]
if '\u' in value:
if r'\u' in value:
# XXX for now, you can't mix unicode escaping and unicode characters at the same token
value = unicode_escape(value)[0]
tokens2.append((name, token.type, value, flags))
@@ -406,7 +407,7 @@ def test():
d: "+" | "-"
"""
# print load_grammar(g)
print GrammarLoader().load_grammar2(g)
print(GrammarLoader().load_grammar2(g))


if __name__ == '__main__':


+ 1
- 1
lark/parser.py Visa fil

@@ -1,4 +1,4 @@
from grammar_analysis import ACTION_SHIFT
from .grammar_analysis import ACTION_SHIFT

class ParseError(Exception):
pass


+ 1
- 1
lark/parser_frontends.py Visa fil

@@ -1,5 +1,5 @@
from .grammar_analysis import GrammarAnalyzer
from common import is_terminal
from .common import is_terminal
from . import parser, earley

class LALR:


+ 2
- 1
lark/tree.py Visa fil

@@ -1,5 +1,6 @@
from copy import deepcopy
from utils import inline_args

from .utils import inline_args

class Tree(object):
def __init__(self, data, children):


+ 1
- 1
lark/utils.py Visa fil

@@ -58,7 +58,7 @@ def inline_args(f):
def _f_func(self, args):
return f(self, *args)
return _f_func
elif isinstance(f, (types.TypeType, types.BuiltinFunctionType)):
elif isinstance(f, (type, types.BuiltinFunctionType)):
@functools.wraps(f)
def _f_builtin(self, args):
return f(*args)


Laddar…
Avbryt
Spara