Ver código fonte

Fixed examples. They didn't work with python3

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.5.1
Erez Shinan 7 anos atrás
pai
commit
7ba98c46f6
3 arquivos alterados com 11 adições e 9 exclusões
  1. +3
    -3
      examples/calc.py
  2. +6
    -4
      examples/indented_tree.py
  3. +2
    -2
      examples/json_parser.py

+ 3
- 3
examples/calc.py Ver arquivo

@@ -22,7 +22,7 @@ calc_grammar = """
"""

class CalculateTree(InlineTransformer):
from operator import add, sub, mul, div, neg
from operator import add, sub, mul, truediv as div, neg
number = float

def __init__(self):
@@ -50,8 +50,8 @@ def main():

def test():
# print calc("a=(1+2)")
print calc("a = 1+2")
print calc("1+a*-3")
print(calc("a = 1+2"))
print(calc("1+a*-3"))

if __name__ == '__main__':
test()


+ 6
- 4
examples/indented_tree.py Ver arquivo

@@ -7,15 +7,17 @@ It is crucial for the indenter that the NL_type matches the spaces (and tabs) af
from lark.lark import Lark
from lark.indenter import Indenter

tree_grammar = """
tree_grammar = r"""
?start: _NL* tree

tree: /\w+/ _NL [_INDENT tree+ _DEDENT]
tree: NAME _NL [_INDENT tree+ _DEDENT]

NAME: /\w+/

WS.ignore: /\s+/
_NL.newline: /(\r?\n[\t ]*)+/
_NL: /(\r?\n[\t ]*)+/
_INDENT: "<INDENT>"
_DEDENT: "<DEDENT>"
"""

class TreeIndenter(Indenter):
@@ -39,7 +41,7 @@ a
"""

def test():
print parser.parse(test_tree).pretty()
print(parser.parse(test_tree).pretty())

if __name__ == '__main__':
test()


+ 2
- 2
examples/json_parser.py Ver arquivo

@@ -53,12 +53,12 @@ def test():
'''

j = parse(test_json)
print j
print(j)
import json
assert j == json.loads(test_json)

if __name__ == '__main__':
test()
with open(sys.argv[1]) as f:
print parse(f.read())
print(parse(f.read()))


Carregando…
Cancelar
Salvar