|
@@ -2,6 +2,7 @@ |
|
|
|
|
|
|
|
|
import os.path |
|
|
import os.path |
|
|
import sys |
|
|
import sys |
|
|
|
|
|
import codecs |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from lark import Lark, InlineTransformer, Transformer |
|
|
from lark import Lark, InlineTransformer, Transformer |
|
@@ -113,7 +114,7 @@ def _nearley_to_lark(g, builtin_path, n2l, js_code, folder_path, includes): |
|
|
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.add(path) |
|
|
includes.add(path) |
|
|
with open(path) as f: |
|
|
|
|
|
|
|
|
with codecs.open(path, encoding='utf8') as f: |
|
|
text = f.read() |
|
|
text = f.read() |
|
|
rule_defs += _nearley_to_lark(text, builtin_path, n2l, js_code, os.path.abspath(os.path.dirname(path)), includes) |
|
|
rule_defs += _nearley_to_lark(text, builtin_path, n2l, js_code, os.path.abspath(os.path.dirname(path)), includes) |
|
|
else: |
|
|
else: |
|
@@ -168,17 +169,18 @@ def create_code_for_nearley_grammar(g, start, builtin_path, folder_path): |
|
|
|
|
|
|
|
|
return ''.join(emit_code) |
|
|
return ''.join(emit_code) |
|
|
|
|
|
|
|
|
def main(): |
|
|
|
|
|
if len(sys.argv) < 3: |
|
|
|
|
|
print("Reads Nearley grammar (with js functions) outputs an equivalent lark parser.") |
|
|
|
|
|
print("Usage: %s <nearley_grammar_path> <start_rule> <nearley_lib_path>" % sys.argv[0]) |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
fn, start, nearley_lib = sys.argv[1:] |
|
|
|
|
|
with open(fn) as f: |
|
|
|
|
|
|
|
|
def main(fn, start, nearley_lib): |
|
|
|
|
|
with codecs.open(fn, encoding='utf8') as f: |
|
|
grammar = f.read() |
|
|
grammar = f.read() |
|
|
print(create_code_for_nearley_grammar(grammar, start, os.path.join(nearley_lib, 'builtin'), os.path.abspath(os.path.dirname(fn)))) |
|
|
print(create_code_for_nearley_grammar(grammar, start, os.path.join(nearley_lib, 'builtin'), os.path.abspath(os.path.dirname(fn)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
if __name__ == '__main__': |
|
|
main() |
|
|
|
|
|
|
|
|
if len(sys.argv) < 4: |
|
|
|
|
|
print("Reads Nearley grammar (with js functions) outputs an equivalent lark parser.") |
|
|
|
|
|
print("Usage: %s <nearley_grammar_path> <start_rule> <nearley_lib_path>" % sys.argv[0]) |
|
|
|
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
|
|
|
fn, start, nearley_lib = sys.argv[1:] |
|
|
|
|
|
|
|
|
|
|
|
main(fn, start, nearley_lib) |