| @@ -101,25 +101,21 @@ class NearleyToLark(InlineTransformer): | |||||
| def start(self, *rules): | def start(self, *rules): | ||||
| return '\n'.join(filter(None, rules)) | return '\n'.join(filter(None, rules)) | ||||
| def _nearley_to_lark(g, builtin_path, n2l, js_code, folder_path, includes=None): | |||||
| if includes is None: | |||||
| includes = [] | |||||
| def _nearley_to_lark(g, builtin_path, n2l, js_code, folder_path, includes): | |||||
| rule_defs = [] | rule_defs = [] | ||||
| tree = nearley_grammar_parser.parse(g) | tree = nearley_grammar_parser.parse(g) | ||||
| for statement in tree.children: | for statement in tree.children: | ||||
| if statement.data == 'directive': | if statement.data == 'directive': | ||||
| directive, arg = statement.children | directive, arg = statement.children | ||||
| if directive == 'builtin' or directive == 'include': | |||||
| if directive in ('builtin', 'include'): | |||||
| folder = builtin_path if directive == 'builtin' else folder_path | folder = builtin_path if directive == 'builtin' else folder_path | ||||
| path = os.path.join(folder, arg[1:-1]) | path = os.path.join(folder, arg[1:-1]) | ||||
| if path not in includes: | if path not in includes: | ||||
| includes.append(path) | |||||
| includes.add(path) | |||||
| with open(path) as f: | with open(path) as f: | ||||
| text = f.read() | text = f.read() | ||||
| [included_rule_defs, includes] = _nearley_to_lark(text, builtin_path, n2l, js_code, os.path.abspath(os.path.dirname(path)), includes) | |||||
| rule_defs += included_rule_defs | |||||
| rule_defs += _nearley_to_lark(text, builtin_path, n2l, js_code, os.path.abspath(os.path.dirname(path)), includes) | |||||
| else: | else: | ||||
| assert False, directive | assert False, directive | ||||
| elif statement.data == 'js_code': | elif statement.data == 'js_code': | ||||
| @@ -133,7 +129,7 @@ def _nearley_to_lark(g, builtin_path, n2l, js_code, folder_path, includes=None): | |||||
| else: | else: | ||||
| raise Exception("Unknown statement: %s" % statement) | raise Exception("Unknown statement: %s" % statement) | ||||
| return [rule_defs, includes] | |||||
| return rule_defs | |||||
| def create_code_for_nearley_grammar(g, start, builtin_path, folder_path): | def create_code_for_nearley_grammar(g, start, builtin_path, folder_path): | ||||
| @@ -147,7 +143,7 @@ def create_code_for_nearley_grammar(g, start, builtin_path, folder_path): | |||||
| js_code = ['function id(x) {return x[0];}'] | js_code = ['function id(x) {return x[0];}'] | ||||
| n2l = NearleyToLark() | n2l = NearleyToLark() | ||||
| [rule_defs, _] = _nearley_to_lark(g, builtin_path, n2l, js_code, folder_path) | |||||
| rule_defs = _nearley_to_lark(g, builtin_path, n2l, js_code, folder_path, set()) | |||||
| lark_g = '\n'.join(rule_defs) | lark_g = '\n'.join(rule_defs) | ||||
| lark_g += '\n'+'\n'.join('!%s: %s' % item for item in n2l.extra_rules.items()) | lark_g += '\n'+'\n'.join('!%s: %s' % item for item in n2l.extra_rules.items()) | ||||