| @@ -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) | - [Documentation wiki](https://github.com/erezsh/lark/wiki) | ||||
| - [Tutorial](/docs/json_tutorial.md) for writing a JSON parser. | - [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/) | - 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) | - [Forum @googlegroups](https://groups.google.com/forum/#!forum/lark-parser) (New) | ||||
| @@ -33,16 +34,27 @@ Lark has no dependencies. | |||||
| [](https://travis-ci.org/lark-parser/lark) | [](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 | ### Hello World | ||||
| Here is a little program to parse "Hello, World!" (Or any other similar phrase): | Here is a little program to parse "Hello, World!" (Or any other similar phrase): | ||||
| ```python | ```python | ||||
| from lark import Lark | from lark import Lark | ||||
| l = Lark('''start: WORD "," WORD "!" | 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!") ) | 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 | ### 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": | |||||
|  |  | ||||
| @@ -153,6 +165,7 @@ Lark is currently accepting pull-requests. | |||||
| There are many ways you can help the project: | There are many ways you can help the project: | ||||
| * Help solve issues | |||||
| * Improve the documentation | * Improve the documentation | ||||
| * Write new grammars for Lark's library | * Write new grammars for Lark's library | ||||
| * Write a blog post introducing Lark to your audience | * Write a blog post introducing Lark to your audience | ||||
| @@ -4,6 +4,7 @@ | |||||
| from lark import Lark, Transformer, v_args | from lark import Lark, Transformer, v_args | ||||
| try: | try: | ||||
| input = raw_input # For Python2 compatibility | input = raw_input # For Python2 compatibility | ||||
| except NameError: | except NameError: | ||||
| @@ -34,7 +35,8 @@ calc_grammar = """ | |||||
| %ignore WS_INLINE | %ignore WS_INLINE | ||||
| """ | """ | ||||
| @v_args(inline=True) | |||||
| @v_args(inline=True) # Affects the signatures of the methods | |||||
| class CalculateTree(Transformer): | class CalculateTree(Transformer): | ||||
| from operator import add, sub, mul, truediv as div, neg | from operator import add, sub, mul, truediv as div, neg | ||||
| number = float | number = float | ||||
| @@ -50,10 +52,10 @@ class CalculateTree(Transformer): | |||||
| return self.vars[name] | return self.vars[name] | ||||
| calc_parser = Lark(calc_grammar, parser='lalr', transformer=CalculateTree()) | calc_parser = Lark(calc_grammar, parser='lalr', transformer=CalculateTree()) | ||||
| calc = calc_parser.parse | calc = calc_parser.parse | ||||
| def main(): | def main(): | ||||
| while True: | while True: | ||||
| try: | try: | ||||
| @@ -62,11 +64,12 @@ def main(): | |||||
| break | break | ||||
| print(calc(s)) | print(calc(s)) | ||||
| def test(): | def test(): | ||||
| print(calc("a = 1+2")) | print(calc("a = 1+2")) | ||||
| print(calc("1+a*-3")) | print(calc("1+a*-3")) | ||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||
| # test() | # test() | ||||
| main() | main() | ||||
| @@ -19,9 +19,9 @@ parser = Lark(r""" | |||||
| section: "[" NAME "]" _NL item+ | section: "[" NAME "]" _NL item+ | ||||
| item: NAME "=" VALUE? _NL | item: NAME "=" VALUE? _NL | ||||
| VALUE: /./+ | VALUE: /./+ | ||||
| %import common.CNAME -> NAME | %import common.CNAME -> NAME | ||||
| %import common.NEWLINE -> _NL | %import common.NEWLINE -> _NL | ||||
| %import common.WS_INLINE | %import common.WS_INLINE | ||||
| %ignore WS_INLINE | %ignore WS_INLINE | ||||
| """, parser="earley") | """, parser="earley") | ||||
| @@ -20,9 +20,9 @@ parser = Lark(r""" | |||||
| section: "[" NAME "]" _NL item+ | section: "[" NAME "]" _NL item+ | ||||
| item: NAME "=" VALUE? _NL | item: NAME "=" VALUE? _NL | ||||
| VALUE: /./+ | VALUE: /./+ | ||||
| %import common.CNAME -> NAME | %import common.CNAME -> NAME | ||||
| %import common.NEWLINE -> _NL | %import common.NEWLINE -> _NL | ||||
| %import common.WS_INLINE | %import common.WS_INLINE | ||||
| %ignore WS_INLINE | %ignore WS_INLINE | ||||
| """, parser="lalr") | """, parser="lalr") | ||||
| @@ -33,6 +33,7 @@ json_grammar = r""" | |||||
| %ignore WS | %ignore WS | ||||
| """ | """ | ||||
| class TreeToJson(Transformer): | class TreeToJson(Transformer): | ||||
| @v_args(inline=True) | @v_args(inline=True) | ||||
| def string(self, s): | def string(self, s): | ||||
| @@ -47,6 +48,7 @@ class TreeToJson(Transformer): | |||||
| true = lambda self, _: True | true = lambda self, _: True | ||||
| false = lambda self, _: False | false = lambda self, _: False | ||||
| # json_parser = Lark(json_grammar, parser='earley', lexer='standard') | # json_parser = Lark(json_grammar, parser='earley', lexer='standard') | ||||
| # def parse(x): | # def parse(x): | ||||
| # return TreeToJson().transform(json_parser.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()) | json_parser = Lark(json_grammar, parser='lalr', lexer='standard', transformer=TreeToJson()) | ||||
| parse = json_parser.parse | parse = json_parser.parse | ||||
| def test(): | def test(): | ||||
| test_json = ''' | test_json = ''' | ||||
| { | { | ||||
| @@ -71,8 +74,8 @@ def test(): | |||||
| import json | import json | ||||
| assert j == json.loads(test_json) | assert j == json.loads(test_json) | ||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||
| # test() | # test() | ||||
| with open(sys.argv[1]) as f: | with open(sys.argv[1]) as f: | ||||
| print(parse(f.read())) | print(parse(f.read())) | ||||