Parcourir la source

Example: Friendlier reconstruct_python.py

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.11.2
Erez Sh il y a 3 ans
Parent
révision
f32014cc7b
1 fichiers modifiés avec 25 ajouts et 11 suppressions
  1. +25
    -11
      examples/advanced/reconstruct_python.py

+ 25
- 11
examples/advanced/reconstruct_python.py Voir le fichier

@@ -1,23 +1,32 @@
"""
Reconstruct Python
==================

Demonstrates how Lark's experimental text-reconstruction feature can recreate
functional Python code from its parse-tree, using just the correct grammar and
a small formatter.

"""

from lark import Token
from lark.reconstruct import Reconstructor

from python_parser import python_parser3


test_python = open(__file__).read()
SPACE_AFTER = set(',+-*/~@<>="|:')
SPACE_BEFORE = (SPACE_AFTER - set(',:')) | set('\'')


def special(sym):
return Token('SPECIAL', sym.name)

SPACE_AFTER = set(',+-*/~@<>="|:')
SPACE_BEFORE = (SPACE_AFTER - set(',:')) | set('\'')

def postproc(items):
stack = ['\n']
actions = []
last_was_whitespace = True
for item in items:
if isinstance(item, Token) and item.type == 'SPECIAL':
if isinstance(item, Token) and item.type == 'SPECIAL':
actions.append(item.value)
else:
if actions:
@@ -44,15 +53,20 @@ def postproc(items):
yield "\n"


tree = python_parser3.parse(test_python)
python_reconstruct = Reconstructor(python_parser3, {'_NEWLINE': special, '_DEDENT': special, '_INDENT': special})


python_reconstruct = Reconstructor(python_parser3, {'_NEWLINE': special, '_DEDENT': special, '_INDENT': special})
def test():
self_contents = open(__file__).read()

tree = python_parser3.parse(self_contents+'\n')
output = python_reconstruct.reconstruct(tree, postproc)

output = python_reconstruct.reconstruct(tree, postproc)
tree_new = python_parser3.parse(output)
assert tree == tree_new

print(output)
print(output)

tree_new = python_parser3.parse(output)

assert tree == tree_new
if __name__ == '__main__':
test()

Chargement…
Annuler
Enregistrer