diff --git a/README.md b/README.md index 880b6b2..ebc0bd2 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Most importantly, Lark will save you time and prevent you from getting parsing h - [Documentation wiki](https://github.com/erezsh/lark/wiki) - [Tutorial](/docs/json_tutorial.md) for writing a JSON parser. +- [Cheatsheet (PDF)](/docs/lark_cheatsheet.pdf) - Blog post: [How to write a DSL with Lark](http://blog.erezsh.com/how-to-write-a-dsl-in-python-with-lark/) - [Forum @googlegroups](https://groups.google.com/forum/#!forum/lark-parser) (New) @@ -33,16 +34,27 @@ Lark has no dependencies. [![Build Status](https://travis-ci.org/lark-parser/lark.svg?branch=master)](https://travis-ci.org/lark-parser/lark) +### Syntax Highlighting (new) + +Lark now provides syntax highlighting for its grammar files (\*.lark): + +- [Sublime Text & TextMate](https://github.com/lark-parser/lark_syntax) +- [vscode](https://github.com/lark-parser/vscode-lark) + + ### Hello World Here is a little program to parse "Hello, World!" (Or any other similar phrase): ```python from lark import Lark + l = Lark('''start: WORD "," WORD "!" - %import common.WORD - %ignore " " + + %import common.WORD // imports from terminal library + %ignore " " // Disregard spaces in text ''') + print( l.parse("Hello, World!") ) ``` @@ -56,7 +68,7 @@ Notice punctuation doesn't appear in the resulting tree. It's automatically filt ### Fruit flies like bananas -Lark is very good at handling ambiguity. Here's how it parses the phrase "fruit flies like bananas": +Lark is great at handling ambiguity. Let's parse the phrase "fruit flies like bananas": ![fruitflies.png](examples/fruitflies.png) @@ -153,6 +165,7 @@ Lark is currently accepting pull-requests. There are many ways you can help the project: +* Help solve issues * Improve the documentation * Write new grammars for Lark's library * Write a blog post introducing Lark to your audience diff --git a/examples/calc.py b/examples/calc.py index a187571..c4470ef 100644 --- a/examples/calc.py +++ b/examples/calc.py @@ -4,6 +4,7 @@ from lark import Lark, Transformer, v_args + try: input = raw_input # For Python2 compatibility except NameError: @@ -34,7 +35,8 @@ calc_grammar = """ %ignore WS_INLINE """ -@v_args(inline=True) + +@v_args(inline=True) # Affects the signatures of the methods class CalculateTree(Transformer): from operator import add, sub, mul, truediv as div, neg number = float @@ -50,10 +52,10 @@ class CalculateTree(Transformer): return self.vars[name] - calc_parser = Lark(calc_grammar, parser='lalr', transformer=CalculateTree()) calc = calc_parser.parse + def main(): while True: try: @@ -62,11 +64,12 @@ def main(): break print(calc(s)) + def test(): print(calc("a = 1+2")) print(calc("1+a*-3")) + if __name__ == '__main__': # test() main() - diff --git a/examples/conf_earley.py b/examples/conf_earley.py index 71517f0..13b6c8d 100644 --- a/examples/conf_earley.py +++ b/examples/conf_earley.py @@ -19,9 +19,9 @@ parser = Lark(r""" section: "[" NAME "]" _NL item+ item: NAME "=" VALUE? _NL VALUE: /./+ + %import common.CNAME -> NAME %import common.NEWLINE -> _NL - %import common.WS_INLINE %ignore WS_INLINE """, parser="earley") diff --git a/examples/conf_lalr.py b/examples/conf_lalr.py index 417d2af..33d1dc0 100644 --- a/examples/conf_lalr.py +++ b/examples/conf_lalr.py @@ -20,9 +20,9 @@ parser = Lark(r""" section: "[" NAME "]" _NL item+ item: NAME "=" VALUE? _NL VALUE: /./+ + %import common.CNAME -> NAME %import common.NEWLINE -> _NL - %import common.WS_INLINE %ignore WS_INLINE """, parser="lalr") diff --git a/examples/json_parser.py b/examples/json_parser.py index 23378b1..ba1ff1e 100644 --- a/examples/json_parser.py +++ b/examples/json_parser.py @@ -33,6 +33,7 @@ json_grammar = r""" %ignore WS """ + class TreeToJson(Transformer): @v_args(inline=True) def string(self, s): @@ -47,6 +48,7 @@ class TreeToJson(Transformer): true = lambda self, _: True false = lambda self, _: False + # json_parser = Lark(json_grammar, parser='earley', lexer='standard') # def parse(x): # return TreeToJson().transform(json_parser.parse(x)) @@ -54,6 +56,7 @@ class TreeToJson(Transformer): json_parser = Lark(json_grammar, parser='lalr', lexer='standard', transformer=TreeToJson()) parse = json_parser.parse + def test(): test_json = ''' { @@ -71,8 +74,8 @@ def test(): import json assert j == json.loads(test_json) + if __name__ == '__main__': # test() with open(sys.argv[1]) as f: print(parse(f.read())) -