Browse Source

Fixed examples (Issue #669)

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.10.0
Erez Sh 4 years ago
parent
commit
1ad46b2a9a
5 changed files with 81 additions and 13 deletions
  1. +64
    -0
      examples/advanced/_json_parser.py
  2. +2
    -2
      examples/advanced/error_puppet.py
  3. +1
    -1
      examples/advanced/error_reporting_lalr.py
  4. +1
    -1
      examples/advanced/reconstruct_json.py
  5. +13
    -9
      examples/lark_grammar.py

+ 64
- 0
examples/advanced/_json_parser.py View File

@@ -0,0 +1,64 @@
"""
Simple JSON Parser
==================

The code is short and clear, and outperforms every other parser (that's written in Python).
For an explanation, check out the JSON parser tutorial at /docs/json_tutorial.md

(this is here for use by the other examples)
"""
import sys

from lark import Lark, Transformer, v_args

json_grammar = r"""
?start: value

?value: object
| array
| string
| SIGNED_NUMBER -> number
| "true" -> true
| "false" -> false
| "null" -> null

array : "[" [value ("," value)*] "]"
object : "{" [pair ("," pair)*] "}"
pair : string ":" value

string : ESCAPED_STRING

%import common.ESCAPED_STRING
%import common.SIGNED_NUMBER
%import common.WS

%ignore WS
"""


class TreeToJson(Transformer):
@v_args(inline=True)
def string(self, s):
return s[1:-1].replace('\\"', '"')

array = list
pair = tuple
object = dict
number = v_args(inline=True)(float)

null = lambda self, _: None
true = lambda self, _: True
false = lambda self, _: False


### Create the JSON parser with Lark, using the LALR algorithm
json_parser = Lark(json_grammar, parser='lalr',
# Using the standard lexer isn't required, and isn't usually recommended.
# But, it's good enough for JSON, and it's slightly faster.
lexer='standard',
# Disabling propagate_positions and placeholders slightly improves speed
propagate_positions=False,
maybe_placeholders=False,
# Using an internal transformer is faster and more memory efficient
transformer=TreeToJson())


+ 2
- 2
examples/advanced/error_puppet.py View File

@@ -10,9 +10,9 @@ to proceed step-by-step. When you've achieved the correct parse-state,
you can resume the run by returning True.
"""

from lark import UnexpectedToken, Token
from lark import Token

from .json_parser import json_parser
from _json_parser import json_parser

def ignore_errors(e):
if e.token.type == 'COMMA':


+ 1
- 1
examples/advanced/error_reporting_lalr.py View File

@@ -7,7 +7,7 @@ A demonstration of example-driven error reporting with the LALR parser
"""
from lark import Lark, UnexpectedInput

from .json_parser import json_grammar # Using the grammar from the json_parser example
from _json_parser import json_grammar # Using the grammar from the json_parser example

json_parser = Lark(json_grammar, parser='lalr')



+ 1
- 1
examples/advanced/reconstruct_json.py View File

@@ -14,7 +14,7 @@ import json
from lark import Lark
from lark.reconstruct import Reconstructor

from .json_parser import json_grammar
from _json_parser import json_grammar

test_json = '''
{


+ 13
- 9
examples/lark_grammar.py View File

@@ -4,18 +4,22 @@ Lark Grammar

A reference implementation of the Lark grammar (using LALR(1))
"""
from lark import Lark
import lark
from pathlib import Path

parser = Lark(open('examples/lark.lark'), parser="lalr")
parser = lark.Lark.open('lark.lark', rel_to=__file__, parser="lalr")

examples_path = Path(__file__).parent
lark_path = Path(lark.__file__).parent

grammar_files = [
'examples/python2.lark',
'examples/python3.lark',
'examples/lark.lark',
'examples/relative-imports/multiples.lark',
'examples/relative-imports/multiple2.lark',
'examples/relative-imports/multiple3.lark',
'lark/grammars/common.lark',
examples_path / 'lark.lark',
examples_path / 'advanced/python2.lark',
examples_path / 'advanced/python3.lark',
examples_path / 'relative-imports/multiples.lark',
examples_path / 'relative-imports/multiple2.lark',
examples_path / 'relative-imports/multiple3.lark',
lark_path / 'grammars/common.lark',
]

def test():


Loading…
Cancel
Save