Skip to content

Commit

Permalink
For Nearley: add argparse, extras install, allow es6 compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
multimeric committed Aug 3, 2020
1 parent 9ee8428 commit e6ad86f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
30 changes: 18 additions & 12 deletions lark/tools/nearley.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os.path
import sys
import codecs
import argparse


from lark import Lark, InlineTransformer
Expand Down Expand Up @@ -137,7 +138,7 @@ def _nearley_to_lark(g, builtin_path, n2l, js_code, folder_path, 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, es6=False):
import js2py

emit_code = []
Expand All @@ -160,7 +161,10 @@ def emit(x=None):
for alias, code in n2l.alias_js_code.items():
js_code.append('%s = (%s);' % (alias, code))

emit(js2py.translate_js('\n'.join(js_code)))
if es6:
emit(js2py.translate_js6('\n'.join(js_code)))
else:
emit(js2py.translate_js('\n'.join(js_code)))
emit('class TransformNearley(Transformer):')
for alias in n2l.alias_js_code:
emit(" %s = var.get('%s').to_python()" % (alias, alias))
Expand All @@ -173,18 +177,20 @@ def emit(x=None):

return ''.join(emit_code)

def main(fn, start, nearley_lib):
def main(fn, start, nearley_lib, es6=False):
with codecs.open(fn, encoding='utf8') as f:
grammar = f.read()
return create_code_for_nearley_grammar(grammar, start, os.path.join(nearley_lib, 'builtin'), os.path.abspath(os.path.dirname(fn)))
return create_code_for_nearley_grammar(grammar, start, os.path.join(nearley_lib, 'builtin'), os.path.abspath(os.path.dirname(fn)), es6=es6)

def get_parser():
parser = argparse.ArgumentParser('Reads Nearley grammar (with js functions) outputs an equivalent lark parser.')
parser.add_argument('nearley_grammar', help='Path to the file containing the nearley grammar')
parser.add_argument('start_rule', help='Rule within the nearley grammar to make the base rule')
parser.add_argument('nearley_lib', help='')
parser.add_argument('--es6', help='Enable experimental ES6 support', action='store_true')
return parser

if __name__ == '__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:]

print(main(fn, start, nearley_lib))
parser = get_parser()
args = parser.parse_args()
print(main(fn=args.nearley_grammar, start=args.start_rule, nearley_lib=args.nearley_lib, es6=args.es6))
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
install_requires = [],

extras_require = {
"regex": ["regex"]
"regex": ["regex"],
"nearley": ["js2py"]
},

package_data = {'': ['*.md', '*.lark'], 'lark-stubs': ['*.pyi']},
Expand Down

0 comments on commit e6ad86f

Please sign in to comment.