@@ -37,4 +37,3 @@ class ParserConf: | |||||
self.rules = rules | self.rules = rules | ||||
self.callback = callback | self.callback = callback | ||||
self.start = start | self.start = start | ||||
@@ -1,10 +1,12 @@ | |||||
from __future__ import absolute_import | from __future__ import absolute_import | ||||
import os | import os | ||||
import time | |||||
from collections import defaultdict | |||||
from .utils import STRING_TYPE, inline_args | |||||
from .utils import STRING_TYPE | |||||
from .load_grammar import load_grammar | from .load_grammar import load_grammar | ||||
from .tree import Tree, Transformer | |||||
from .tree import Tree | |||||
from .common import GrammarError, LexerConf, ParserConf | from .common import GrammarError, LexerConf, ParserConf | ||||
from .lexer import Lexer | from .lexer import Lexer | ||||
@@ -44,7 +46,8 @@ class LarkOptions(object): | |||||
assert self.parser in ENGINE_DICT | assert self.parser in ENGINE_DICT | ||||
if self.parser == 'earley' and self.transformer: | if self.parser == 'earley' and self.transformer: | ||||
raise ValueError('Cannot specify an auto-transformer when using the Earley algorithm. Please use your transformer on the resulting parse tree, or use a different algorithm (i.e. lalr)') | |||||
raise ValueError('Cannot specify an auto-transformer when using the Earley algorithm.' | |||||
'Please use your transformer on the resulting parse tree, or use a different algorithm (i.e. lalr)') | |||||
if self.keep_all_tokens: | if self.keep_all_tokens: | ||||
raise NotImplementedError("Not implemented yet!") | raise NotImplementedError("Not implemented yet!") | ||||
@@ -52,8 +55,6 @@ class LarkOptions(object): | |||||
raise ValueError("Unknown options: %s" % o.keys()) | raise ValueError("Unknown options: %s" % o.keys()) | ||||
import time | |||||
from collections import defaultdict | |||||
class Profiler: | class Profiler: | ||||
def __init__(self): | def __init__(self): | ||||
self.total_time = defaultdict(float) | self.total_time = defaultdict(float) | ||||
@@ -67,7 +68,7 @@ class Profiler: | |||||
self.cur_section = name | self.cur_section = name | ||||
def make_wrapper(self, name, f): | def make_wrapper(self, name, f): | ||||
def _f(*args, **kwargs): | |||||
def wrapper(*args, **kwargs): | |||||
last_section = self.cur_section | last_section = self.cur_section | ||||
self.enter_section(name) | self.enter_section(name) | ||||
try: | try: | ||||
@@ -75,7 +76,7 @@ class Profiler: | |||||
finally: | finally: | ||||
self.enter_section(last_section) | self.enter_section(last_section) | ||||
return _f | |||||
return wrapper | |||||
class Lark: | class Lark: | ||||
@@ -13,7 +13,7 @@ class TokenDef(object): | |||||
self.value = value | self.value = value | ||||
def __repr__(self): | def __repr__(self): | ||||
return ('%s(%r, %r)' % (type(self).__name__, self.name, self.value)) | |||||
return '%s(%r, %r)' % (type(self).__name__, self.name, self.value) | |||||
class TokenDef__Str(TokenDef): | class TokenDef__Str(TokenDef): | ||||
def to_regexp(self): | def to_regexp(self): | ||||
@@ -40,16 +40,16 @@ class UnexpectedInput(LexError): | |||||
self.context = context | self.context = context | ||||
class Token(Str): | class Token(Str): | ||||
def __new__(cls, type, value, pos_in_stream=None): | |||||
def __new__(cls, type_, value, pos_in_stream=None): | |||||
inst = Str.__new__(cls, value) | inst = Str.__new__(cls, value) | ||||
inst.type = type | |||||
inst.type = type_ | |||||
inst.pos_in_stream = pos_in_stream | inst.pos_in_stream = pos_in_stream | ||||
inst.value = value | inst.value = value | ||||
return inst | return inst | ||||
@classmethod | @classmethod | ||||
def new_borrow_pos(cls, type, value, borrow_t): | |||||
inst = cls(type, value, borrow_t.pos_in_stream) | |||||
def new_borrow_pos(cls, type_, value, borrow_t): | |||||
inst = cls(type_, value, borrow_t.pos_in_stream) | |||||
inst.line = borrow_t.line | inst.line = borrow_t.line | ||||
inst.column = borrow_t.column | inst.column = borrow_t.column | ||||
return inst | return inst | ||||
@@ -66,11 +66,11 @@ def _regexp_has_newline(r): | |||||
return '\n' in r or '\\n' in r or ('(?s)' in r and '.' in r) | return '\n' in r or '\\n' in r or ('(?s)' in r and '.' in r) | ||||
def _create_unless_callback(strs): | def _create_unless_callback(strs): | ||||
def f(t): | |||||
def unless_callback(t): | |||||
if t in strs: | if t in strs: | ||||
t.type = strs[t] | t.type = strs[t] | ||||
return t | return t | ||||
return f | |||||
return unless_callback | |||||
def _create_unless(tokens): | def _create_unless(tokens): | ||||
tokens_by_type = classify(tokens, type) | tokens_by_type = classify(tokens, type) | ||||
@@ -169,4 +169,3 @@ class Lexer(object): | |||||
if lex_pos < len(stream): | if lex_pos < len(stream): | ||||
raise UnexpectedInput(stream, lex_pos, line, lex_pos - col_start_pos) | raise UnexpectedInput(stream, lex_pos, line, lex_pos - col_start_pos) | ||||
break | break | ||||
@@ -53,14 +53,14 @@ _TOKEN_NAMES = { | |||||
# Grammar Parser | # Grammar Parser | ||||
TOKENS = { | TOKENS = { | ||||
'_LPAR': '\(', | |||||
'_RPAR': '\)', | |||||
'_LBRA': '\[', | |||||
'_RBRA': '\]', | |||||
'_LPAR': r'\(', | |||||
'_RPAR': r'\)', | |||||
'_LBRA': r'\[', | |||||
'_RBRA': r'\]', | |||||
'OP': '[+*?](?![a-z])', | 'OP': '[+*?](?![a-z])', | ||||
'_COLON': ':', | '_COLON': ':', | ||||
'_OR': '\|', | |||||
'_DOT': '\.', | |||||
'_OR': r'\|', | |||||
'_DOT': r'\.', | |||||
'RULE': '[_?*]?[a-z][_a-z0-9]*', | 'RULE': '[_?*]?[a-z][_a-z0-9]*', | ||||
'TOKEN': '_?[A-Z][_A-Z0-9]*', | 'TOKEN': '_?[A-Z][_A-Z0-9]*', | ||||
'STRING': r'".*?[^\\]"', | 'STRING': r'".*?[^\\]"', | ||||
@@ -99,7 +99,7 @@ RULES = { | |||||
'maybe': ['_LBRA expansions _RBRA'], | 'maybe': ['_LBRA expansions _RBRA'], | ||||
'token': ['TOKEN _COLON tokenvalue _NL', | |||||
'token': ['TOKEN _COLON tokenvalue _NL', | |||||
'TOKEN tokenmods _COLON tokenvalue _NL'], | 'TOKEN tokenmods _COLON tokenvalue _NL'], | ||||
'?tokenvalue': ['REGEXP', 'STRING'], | '?tokenvalue': ['REGEXP', 'STRING'], | ||||
@@ -264,7 +264,7 @@ class ExtractAnonTokens(InlineTransformer): | |||||
value = token.value | value = token.value | ||||
self.i += 1 | self.i += 1 | ||||
else: | else: | ||||
assert False, x | |||||
assert False, token | |||||
if token_name not in self.token_set: | if token_name not in self.token_set: | ||||
self.token_set.add(token_name) | self.token_set.add(token_name) | ||||
@@ -373,41 +373,3 @@ class GrammarLoader: | |||||
return tokendefs, rules | return tokendefs, rules | ||||
load_grammar = GrammarLoader().load_grammar | load_grammar = GrammarLoader().load_grammar | ||||
def test(): | |||||
g = """ | |||||
start: add | |||||
// Rules | |||||
add: mul | |||||
| add _add_sym mul | |||||
mul: [mul _add_mul] _atom | |||||
_atom: "-" _atom -> neg | |||||
| NUMBER | |||||
| "(" add ")" | |||||
// Tokens | |||||
NUMBER: /[\d.]+/ | |||||
_add_sym: "+" | "-" | |||||
_add_mul: "*" | "/" | |||||
WS.ignore.newline: /\s+/ | |||||
""" | |||||
g2 = """ | |||||
start: a | |||||
a: "a" (b*|(c d)+) "b"? | |||||
b: "b" | |||||
c: "c" | |||||
d: "+" | "-" | |||||
""" | |||||
# print load_grammar(g) | |||||
print(GrammarLoader().load_grammar2(g)) | |||||
if __name__ == '__main__': | |||||
test() |
@@ -5,12 +5,12 @@ class Callback(object): | |||||
def create_expand1_tree_builder_function(tree_builder): | def create_expand1_tree_builder_function(tree_builder): | ||||
def f(children): | |||||
def expand1(children): | |||||
if len(children) == 1: | if len(children) == 1: | ||||
return children[0] | return children[0] | ||||
else: | else: | ||||
return tree_builder(children) | return tree_builder(children) | ||||
return f | |||||
return expand1 | |||||
def create_rule_handler(expansion, usermethod): | def create_rule_handler(expansion, usermethod): | ||||
to_include = [(i, sym.startswith('_')) for i, sym in enumerate(expansion) | to_include = [(i, sym.startswith('_')) for i, sym in enumerate(expansion) | ||||
@@ -38,9 +38,10 @@ class ParseTreeBuilder: | |||||
def _create_tree_builder_function(self, name): | def _create_tree_builder_function(self, name): | ||||
tree_class = self.tree_class | tree_class = self.tree_class | ||||
def f(children): | |||||
def tree_builder_f(children): | |||||
return tree_class(name, children) | return tree_class(name, children) | ||||
return f | |||||
return tree_builder_f | |||||
def create_tree_builder(self, rules, transformer): | def create_tree_builder(self, rules, transformer): | ||||
@@ -77,5 +78,3 @@ class ParseTreeBuilder: | |||||
new_rules.append(( _origin, expansion, _alias )) | new_rules.append(( _origin, expansion, _alias )) | ||||
return new_rules, callback | return new_rules, callback | ||||
@@ -52,4 +52,3 @@ class Earley(WithLexer): | |||||
ENGINE_DICT = { 'lalr': LALR, 'earley': Earley } | ENGINE_DICT = { 'lalr': LALR, 'earley': Earley } | ||||
@@ -89,7 +89,6 @@ class State(object): | |||||
new_copy = self.consume_nonterminal(r.name) | new_copy = self.consume_nonterminal(r.name) | ||||
if r.postprocess: | if r.postprocess: | ||||
new_copy.data[-1] = r.postprocess([]) | new_copy.data[-1] = r.postprocess([]) | ||||
# new_copy.data[-1] = r.postprocess([], self.reference) | |||||
else: | else: | ||||
new_copy.data[-1] = [] | new_copy.data[-1] = [] | ||||
@@ -154,4 +153,3 @@ class Parser(object): | |||||
and t.reference == 0 | and t.reference == 0 | ||||
and t.data is not MatchFailed): | and t.data is not MatchFailed): | ||||
yield t.data | yield t.data | ||||
@@ -192,7 +192,8 @@ class GrammarAnalyzer(object): | |||||
self.states[state] = {k:v[0] for k, v in lookahead.items()} | self.states[state] = {k:v[0] for k, v in lookahead.items()} | ||||
x = list(bfs([self.init_state], step)) | |||||
for _ in bfs([self.init_state], step): | |||||
pass | |||||
# -- | # -- | ||||
self.enum = list(self.states) | self.enum = list(self.states) | ||||
@@ -207,4 +208,3 @@ class GrammarAnalyzer(object): | |||||
self.init_state_idx = self.enum_rev[self.init_state] | self.init_state_idx = self.enum_rev[self.init_state] | ||||
@@ -40,7 +40,7 @@ class Tree(object): | |||||
if pred(self): | if pred(self): | ||||
yield self | yield self | ||||
else: | else: | ||||
for i, c in enumerate(self.children): | |||||
for c in self.children: | |||||
if isinstance(c, Tree): | if isinstance(c, Tree): | ||||
for t in c.find_pred(pred): | for t in c.find_pred(pred): | ||||
yield t | yield t | ||||
@@ -48,9 +48,6 @@ class Tree(object): | |||||
def find_data(self, data): | def find_data(self, data): | ||||
return self.find_pred(lambda t: t.data == data) | return self.find_pred(lambda t: t.data == data) | ||||
# def clone(self): | |||||
# return Tree(self.data, [c.clone() if isinstance(c, Tree) else c for c in self.children]) | |||||
def __deepcopy__(self, memo): | def __deepcopy__(self, memo): | ||||
return type(self)(self.data, deepcopy(self.children, memo)) | return type(self)(self.data, deepcopy(self.children, memo)) | ||||
@@ -69,7 +66,6 @@ class Transformer(object): | |||||
else: | else: | ||||
return f(items) | return f(items) | ||||
def __default__(self, data, children): | def __default__(self, data, children): | ||||
return Tree(data, children) | return Tree(data, children) | ||||
@@ -79,7 +75,6 @@ class InlineTransformer(Transformer): | |||||
return inline_args(getattr(self, name)).__get__(self) | return inline_args(getattr(self, name)).__get__(self) | ||||
class Visitor(object): | class Visitor(object): | ||||
def visit(self, tree): | def visit(self, tree): | ||||
for child in tree.children: | for child in tree.children: | ||||
@@ -92,5 +87,3 @@ class Visitor(object): | |||||
def __default__(self, tree): | def __default__(self, tree): | ||||
pass | pass | ||||
@@ -1,3 +1,5 @@ | |||||
import functools | |||||
import types | |||||
from collections import deque | from collections import deque | ||||
class fzset(frozenset): | class fzset(frozenset): | ||||
@@ -49,8 +51,6 @@ except NameError: # Python 3 | |||||
Str = type(u'') | Str = type(u'') | ||||
import functools | |||||
import types | |||||
def inline_args(f): | def inline_args(f): | ||||
# print '@@', f.__name__, type(f), isinstance(f, types.FunctionType), isinstance(f, types.TypeType), isinstance(f, types.BuiltinFunctionType) | # print '@@', f.__name__, type(f), isinstance(f, types.FunctionType), isinstance(f, types.TypeType), isinstance(f, types.BuiltinFunctionType) | ||||
if isinstance(f, types.FunctionType): | if isinstance(f, types.FunctionType): | ||||