From b0feed8c12838f6e0078b119e2eee97a1fafa66c Mon Sep 17 00:00:00 2001 From: Erez Sh Date: Wed, 5 Aug 2020 16:05:31 +0300 Subject: [PATCH] A few adjustments to Nearley PR (mostly docs) --- README.md | 32 +---------------------------- docs/features.md | 2 +- docs/index.md | 1 + docs/nearley.md | 47 +++++++++++++++++++++++++++++++++++++++++++ lark/tools/nearley.py | 6 +++--- 5 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 docs/nearley.md diff --git a/README.md b/README.md index fb00841..aa19ab0 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ See more [examples here](https://github.com/lark-parser/lark/tree/master/example - **Python 2 & 3** compatible - Automatic line & column tracking - Standard library of terminals (strings, numbers, names, etc.) - - Import grammars from Nearley.js + - Import grammars from Nearley.js ([read more](/docs/nearley.md)) - Extensive test suite [![codecov](https://codecov.io/gh/erezsh/lark/branch/master/graph/badge.svg)](https://codecov.io/gh/erezsh/lark) - MyPy support using type stubs - And much more! @@ -159,36 +159,6 @@ Check out the [JSON tutorial](/docs/json_tutorial.md#conclusion) for more detail Using Lark? Send me a message and I'll add your project! -### How to use Nearley grammars in Lark - -Lark comes with a tool to convert grammars from [Nearley](https://github.com/Hardmath123/nearley), a popular Earley library for Javascript. It uses [Js2Py](https://github.com/PiotrDabkowski/Js2Py) to convert and run the Javascript postprocessing code segments. - -First, ensure you have Lark installed with the `nearley` component included: -```bash -pip install lark-parser[nearley] -``` - -Here's an example: -```bash -git clone https://github.com/Hardmath123/nearley -python -m lark.tools.nearley nearley/examples/calculator/arithmetic.ne main nearley > ncalc.py -``` - -You can use the output as a regular python module: - -```python ->>> import ncalc ->>> ncalc.parse('sin(pi/4) ^ e') -0.38981434460254655 -``` - -The Nearley converter also supports an experimental converter for newer JavaScript (ES6+), using the `--es6` flag: - -```bash -git clone https://github.com/Hardmath123/nearley -python -m lark.tools.nearley nearley/examples/calculator/arithmetic.ne main nearley --es6 > ncalc.py -``` - ## License Lark uses the [MIT license](LICENSE). diff --git a/docs/features.md b/docs/features.md index d8a4340..9346989 100644 --- a/docs/features.md +++ b/docs/features.md @@ -21,7 +21,7 @@ # Extra features - Import rules and tokens from other Lark grammars, for code reuse and modularity. - - Import grammars from Nearley.js + - Import grammars from Nearley.js ([read more](/docs/nearley.md)) - CYK parser ### Experimental features diff --git a/docs/index.md b/docs/index.md index 20257b5..1310be2 100644 --- a/docs/index.md +++ b/docs/index.md @@ -49,6 +49,7 @@ $ pip install lark-parser * [Visitors & Transformers](visitors.md) * [Classes](classes.md) * [Cheatsheet (PDF)](lark_cheatsheet.pdf) + * [Importing grammars from Nearley](nearley.md) * Discussion * [Gitter](https://gitter.im/lark-parser/Lobby) * [Forum (Google Groups)](https://groups.google.com/forum/#!forum/lark-parser) diff --git a/docs/nearley.md b/docs/nearley.md new file mode 100644 index 0000000..4ab8595 --- /dev/null +++ b/docs/nearley.md @@ -0,0 +1,47 @@ +# Importing grammars from Nearley + +Lark comes with a tool to convert grammars from [Nearley](https://github.com/Hardmath123/nearley), a popular Earley library for Javascript. It uses [Js2Py](https://github.com/PiotrDabkowski/Js2Py) to convert and run the Javascript postprocessing code segments. + +## Requirements + +1. Install Lark with the `nearley` component: +```bash +pip install lark-parser[nearley] +``` + +2. Acquire a copy of the nearley codebase. This can be done using: +```bash +git clone https://github.com/Hardmath123/nearley +``` + +## Usage + +Here's an example of how to import nearley's calculator example into Lark: + +```bash +git clone https://github.com/Hardmath123/nearley +python -m lark.tools.nearley nearley/examples/calculator/arithmetic.ne main nearley > ncalc.py +``` + +You can use the output as a regular python module: + +```python +>>> import ncalc +>>> ncalc.parse('sin(pi/4) ^ e') +0.38981434460254655 +``` + +The Nearley converter also supports an experimental converter for newer JavaScript (ES6+), using the `--es6` flag: + +```bash +git clone https://github.com/Hardmath123/nearley +python -m lark.tools.nearley nearley/examples/calculator/arithmetic.ne main nearley --es6 > ncalc.py +``` + +## Notes + +- Lark currently cannot import templates from Nearley + +- Lark currently cannot export grammars to Nearley + +These might get added in the future, if enough users ask for them. \ No newline at end of file diff --git a/lark/tools/nearley.py b/lark/tools/nearley.py index c3df234..0237fcd 100644 --- a/lark/tools/nearley.py +++ b/lark/tools/nearley.py @@ -1,4 +1,4 @@ -"Converts between Lark and Nearley grammars. Work in progress!" +"Converts Nearley grammars to Lark" import os.path import sys @@ -182,7 +182,7 @@ def main(fn, start, nearley_lib, es6=False): grammar = f.read() 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(): +def get_arg_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') @@ -191,6 +191,6 @@ def get_parser(): return parser if __name__ == '__main__': - parser = get_parser() + parser = get_arg_parser() args = parser.parse_args() print(main(fn=args.nearley_grammar, start=args.start_rule, nearley_lib=args.nearley_lib, es6=args.es6))