Browse Source

Move PythonIndenter to lark/indenter.py

remotes/origin/gm/2021-09-23T00Z/github.com--lark-parser-lark/tree_templates
Erez Sh 3 years ago
parent
commit
6985acd9f8
4 changed files with 15 additions and 15 deletions
  1. +2
    -1
      examples/advanced/py3to2.py
  2. +1
    -9
      examples/advanced/python_parser.py
  3. +3
    -5
      examples/advanced/reconstruct_python.py
  4. +9
    -0
      lark/indenter.py

+ 2
- 1
examples/advanced/py3to2.py View File

@@ -12,7 +12,8 @@ Uses reconstruct_python.py for generating the final Python 2 code.
from lark import Lark from lark import Lark
from lark.tree_templates import TemplateConf, TemplateTranslator from lark.tree_templates import TemplateConf, TemplateTranslator
from reconstruct_python import PythonIndenter, PythonReconstructor
from lark.indenter import PythonIndenter
from reconstruct_python import PythonReconstructor
# #


+ 1
- 9
examples/advanced/python_parser.py View File

@@ -12,17 +12,9 @@ from io import open
import glob, time import glob, time


from lark import Lark from lark import Lark
from lark.indenter import Indenter
from lark.indenter import PythonIndenter




class PythonIndenter(Indenter):
NL_type = '_NEWLINE'
OPEN_PAREN_types = ['LPAR', 'LSQB', 'LBRACE']
CLOSE_PAREN_types = ['RPAR', 'RSQB', 'RBRACE']
INDENT_type = '_INDENT'
DEDENT_type = '_DEDENT'
tab_len = 8

kwargs = dict(postlex=PythonIndenter(), start='file_input') kwargs = dict(postlex=PythonIndenter(), start='file_input')


# Official Python grammar by Lark # Official Python grammar by Lark


+ 3
- 5
examples/advanced/reconstruct_python.py View File

@@ -10,13 +10,11 @@ a small formatter.


from lark import Lark, Token from lark import Lark, Token
from lark.reconstruct import Reconstructor from lark.reconstruct import Reconstructor

from python_parser import PythonIndenter
from lark.indenter import PythonIndenter


# Official Python grammar by Lark # Official Python grammar by Lark
python_parser3 = Lark.open_from_package('lark', 'python.lark', ['grammars'],
parser='lalr', postlex=PythonIndenter(),
start='file_input', maybe_placeholders=False)
kwargs = dict(parser='lalr', postlex=PythonIndenter(), start='file_input', maybe_placeholders=False)
python_parser3 = Lark.open_from_package('lark', 'python.lark', ['grammars'], **kwargs)




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


+ 9
- 0
lark/indenter.py View File

@@ -101,3 +101,12 @@ class Indenter(PostLex, ABC):
raise NotImplementedError() raise NotImplementedError()


###} ###}


class PythonIndenter(Indenter):
NL_type = '_NEWLINE'
OPEN_PAREN_types = ['LPAR', 'LSQB', 'LBRACE']
CLOSE_PAREN_types = ['RPAR', 'RSQB', 'RBRACE']
INDENT_type = '_INDENT'
DEDENT_type = '_DEDENT'
tab_len = 8

Loading…
Cancel
Save