@@ -1,6 +1,11 @@ | |||||
"My name is Earley" | "My name is Earley" | ||||
from .utils import classify | |||||
from .utils import classify, STRING_TYPE | |||||
try: | |||||
xrange | |||||
except NameError: | |||||
xrange = range | |||||
class MatchFailed(object): | class MatchFailed(object): | ||||
pass | pass | ||||
@@ -27,7 +32,7 @@ class State(object): | |||||
self.is_literal = isinstance(self.expect_symbol, dict) | self.is_literal = isinstance(self.expect_symbol, dict) | ||||
if self.is_literal: | if self.is_literal: | ||||
self.expect_symbol = self.expect_symbol['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): | def next_state(self, data): | ||||
return State(self.rule, self.expect+1, self.reference, self.data + [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 | if (t.rule.name == self.start | ||||
and t.expect == len(t.rule.symbols) | and t.expect == len(t.rule.symbols) | ||||
and t.reference == 0 | and t.reference == 0 | ||||
and t.data != MatchFailed): | |||||
and t.data is not MatchFailed): | |||||
yield t.data | yield t.data | ||||
@@ -1,6 +1,7 @@ | |||||
from collections import defaultdict, deque | 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 | ACTION_SHIFT = 0 | ||||
@@ -105,7 +106,7 @@ class GrammarAnalyzer(object): | |||||
"""Calculate FOLLOW sets. | """Calculate FOLLOW sets. | ||||
Adapted from: http://lara.epfl.ch/w/cc09:algorithm_for_first_and_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? | symbols.add('$root') # what about other unused rules? | ||||
# foreach grammar rule X ::= Y(1) ... Y(k) | # foreach grammar rule X ::= Y(1) ... Y(k) | ||||
@@ -1,5 +1,6 @@ | |||||
## Lexer Implementation | ## Lexer Implementation | ||||
from utils import Str | |||||
from .utils import Str | |||||
class LexError(Exception): | class LexError(Exception): | ||||
pass | pass | ||||
@@ -52,7 +53,7 @@ class Lexer(object): | |||||
self.mres = [] | self.mres = [] | ||||
self.name_from_index = [] | self.name_from_index = [] | ||||
x = tokens | |||||
x = list(tokens) | |||||
while x: | while x: | ||||
mre = re.compile(u'|'.join(u'(?P<%s>%s)'%t for t in x[:LIMIT])) | mre = re.compile(u'|'.join(u'(?P<%s>%s)'%t for t in x[:LIMIT])) | ||||
self.mres.append(mre) | self.mres.append(mre) | ||||
@@ -185,7 +185,7 @@ class SimplifyRule_Visitor(Visitor): | |||||
expansions = _flatten | expansions = _flatten | ||||
def dict_update_safe(d1, d2): | def dict_update_safe(d1, d2): | ||||
for k, v in d2.iteritems(): | |||||
for k, v in d2.items(): | |||||
assert k not in d1 | assert k not in d1 | ||||
d1[k] = v | d1[k] = v | ||||
@@ -195,7 +195,8 @@ class RuleTreeToText(Transformer): | |||||
return x | return x | ||||
def expansion(self, symbols): | def expansion(self, symbols): | ||||
return [sym.value for sym in symbols], None | 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) | assert _alias is None, (alias, expansion, '-', _alias) | ||||
return expansion, alias.value | return expansion, alias.value | ||||
@@ -294,7 +295,7 @@ class GrammarLoader: | |||||
tokens2 = [] | tokens2 = [] | ||||
for name, token, flags in tokens: | for name, token, flags in tokens: | ||||
value = token.value[1:-1] | 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 | # XXX for now, you can't mix unicode escaping and unicode characters at the same token | ||||
value = unicode_escape(value)[0] | value = unicode_escape(value)[0] | ||||
tokens2.append((name, token.type, value, flags)) | tokens2.append((name, token.type, value, flags)) | ||||
@@ -406,7 +407,7 @@ def test(): | |||||
d: "+" | "-" | d: "+" | "-" | ||||
""" | """ | ||||
# print load_grammar(g) | # print load_grammar(g) | ||||
print GrammarLoader().load_grammar2(g) | |||||
print(GrammarLoader().load_grammar2(g)) | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
@@ -1,4 +1,4 @@ | |||||
from grammar_analysis import ACTION_SHIFT | |||||
from .grammar_analysis import ACTION_SHIFT | |||||
class ParseError(Exception): | class ParseError(Exception): | ||||
pass | pass | ||||
@@ -1,5 +1,5 @@ | |||||
from .grammar_analysis import GrammarAnalyzer | from .grammar_analysis import GrammarAnalyzer | ||||
from common import is_terminal | |||||
from .common import is_terminal | |||||
from . import parser, earley | from . import parser, earley | ||||
class LALR: | class LALR: | ||||
@@ -1,5 +1,6 @@ | |||||
from copy import deepcopy | from copy import deepcopy | ||||
from utils import inline_args | |||||
from .utils import inline_args | |||||
class Tree(object): | class Tree(object): | ||||
def __init__(self, data, children): | def __init__(self, data, children): | ||||
@@ -58,7 +58,7 @@ def inline_args(f): | |||||
def _f_func(self, args): | def _f_func(self, args): | ||||
return f(self, *args) | return f(self, *args) | ||||
return _f_func | return _f_func | ||||
elif isinstance(f, (types.TypeType, types.BuiltinFunctionType)): | |||||
elif isinstance(f, (type, types.BuiltinFunctionType)): | |||||
@functools.wraps(f) | @functools.wraps(f) | ||||
def _f_builtin(self, args): | def _f_builtin(self, args): | ||||
return f(*args) | return f(*args) | ||||