From f1b2e1926c25c9886adcf0934b3d554b2eb8dc93 Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Tue, 7 Feb 2017 21:56:09 +0200 Subject: [PATCH] Now supports Python3! --- lark/earley.py | 11 ++++++++--- lark/grammar_analysis.py | 7 ++++--- lark/lexer.py | 5 +++-- lark/load_grammar.py | 9 +++++---- lark/parser.py | 2 +- lark/parser_frontends.py | 2 +- lark/tree.py | 3 ++- lark/utils.py | 2 +- 8 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lark/earley.py b/lark/earley.py index beafe8a..3129a22 100644 --- a/lark/earley.py +++ b/lark/earley.py @@ -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 diff --git a/lark/grammar_analysis.py b/lark/grammar_analysis.py index 51abee9..22c60c0 100644 --- a/lark/grammar_analysis.py +++ b/lark/grammar_analysis.py @@ -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) diff --git a/lark/lexer.py b/lark/lexer.py index 22323cd..6b37916 100644 --- a/lark/lexer.py +++ b/lark/lexer.py @@ -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) diff --git a/lark/load_grammar.py b/lark/load_grammar.py index 0637362..417c164 100644 --- a/lark/load_grammar.py +++ b/lark/load_grammar.py @@ -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__': diff --git a/lark/parser.py b/lark/parser.py index 396eeaf..5d5b964 100644 --- a/lark/parser.py +++ b/lark/parser.py @@ -1,4 +1,4 @@ -from grammar_analysis import ACTION_SHIFT +from .grammar_analysis import ACTION_SHIFT class ParseError(Exception): pass diff --git a/lark/parser_frontends.py b/lark/parser_frontends.py index 5f713de..749e782 100644 --- a/lark/parser_frontends.py +++ b/lark/parser_frontends.py @@ -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: diff --git a/lark/tree.py b/lark/tree.py index d00ac2f..f5f00e5 100644 --- a/lark/tree.py +++ b/lark/tree.py @@ -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): diff --git a/lark/utils.py b/lark/utils.py index b210ee6..4f51a66 100644 --- a/lark/utils.py +++ b/lark/utils.py @@ -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)