Browse Source

Examples: Better doc for create_ast

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.11.3
Erez Sh 3 years ago
parent
commit
385c35f506
2 changed files with 13 additions and 2 deletions
  1. +11
    -1
      examples/advanced/create_ast.py
  2. +2
    -1
      lark/ast_utils.py

+ 11
- 1
examples/advanced/create_ast.py View File

@@ -1,6 +1,9 @@
""" """
This example demonstrates how to transform a parse-tree into an AST using `lark.ast_utils`. This example demonstrates how to transform a parse-tree into an AST using `lark.ast_utils`.


create_transformer() collects every subclass of `Ast` subclass from the module,
and creates a Lark transformer that builds the AST with no extra code.

This example only works with Python 3. This example only works with Python 3.
""" """


@@ -17,9 +20,11 @@ this_module = sys.modules[__name__]
# Define AST # Define AST
# #
class _Ast(ast_utils.Ast): class _Ast(ast_utils.Ast):
# This will be skipped by create_transformer(), because it starts with an underscore
pass pass


class _Statement(_Ast): class _Statement(_Ast):
# This will be skipped by create_transformer(), because it starts with an underscore
pass pass


@dataclass @dataclass
@@ -32,6 +37,7 @@ class Name(_Ast):


@dataclass @dataclass
class CodeBlock(_Ast, ast_utils.AsList): class CodeBlock(_Ast, ast_utils.AsList):
# Corresponds to code_block in the grammar
statements: List[_Statement] statements: List[_Statement]


@dataclass @dataclass
@@ -41,6 +47,7 @@ class If(_Statement):


@dataclass @dataclass
class SetVar(_Statement): class SetVar(_Statement):
# Corresponds to set_var in the grammar
name: str name: str
value: Value value: Value


@@ -50,6 +57,8 @@ class Print(_Statement):




class ToAst(Transformer): class ToAst(Transformer):
# Define extra transformation functions, for rules that don't correspond to an AST class.

def STRING(self, s): def STRING(self, s):
# Remove quotation marks # Remove quotation marks
return s[1:-1] return s[1:-1]
@@ -89,7 +98,8 @@ parser = Lark("""
transformer = ast_utils.create_transformer(this_module, ToAst()) transformer = ast_utils.create_transformer(this_module, ToAst())


def parse(text): def parse(text):
return transformer.transform(parser.parse(text))
tree = parser.parse(text)
return transformer.transform(tree)


# #
# Test # Test


+ 2
- 1
lark/ast_utils.py View File

@@ -33,9 +33,10 @@ def create_transformer(ast_module, transformer=None):
For each class, we create a corresponding rule in the transformer, with a matching name. For each class, we create a corresponding rule in the transformer, with a matching name.
CamelCase names will be converted into snake_case. Example: "CodeBlock" -> "code_block". CamelCase names will be converted into snake_case. Example: "CodeBlock" -> "code_block".


Classes starting with an underscore (`_`) will be skipped.

Parameters: Parameters:
ast_module - A Python module containing all the subclasses of `ast_utils.Ast` ast_module - A Python module containing all the subclasses of `ast_utils.Ast`
Classes starting with an underscore (`_`) will be skipped.
transformer (Optional[Transformer]) - An initial transformer. Its attributes may be overwritten. transformer (Optional[Transformer]) - An initial transformer. Its attributes may be overwritten.
""" """
t = transformer or Transformer() t = transformer or Transformer()


Loading…
Cancel
Save