Selaa lähdekoodia

Merge pull request #982 from lark-parser/astutils_meta

gm/2021-09-23T00Z/github.com--lark-parser-lark/v0.12.0
Erez Shinan 3 vuotta sitten
committed by GitHub
vanhempi
commit
887457859e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 muutettua tiedostoa jossa 16 lisäystä ja 10 poistoa
  1. +4
    -1
      examples/advanced/create_ast.py
  2. +12
    -9
      lark/ast_utils.py

+ 4
- 1
examples/advanced/create_ast.py Näytä tiedosto

@@ -15,6 +15,7 @@ from typing import List
from dataclasses import dataclass

from lark import Lark, ast_utils, Transformer, v_args
from lark.tree import Meta

this_module = sys.modules[__name__]

@@ -31,7 +32,9 @@ class _Statement(_Ast):
pass

@dataclass
class Value(_Ast):
class Value(_Ast, ast_utils.WithMeta):
"Uses WithMeta to include line-number metadata in the meta attribute"
meta: Meta
value: object

@dataclass


+ 12
- 9
lark/ast_utils.py Näytä tiedosto

@@ -19,15 +19,17 @@ class AsList(object):
Subclasses will be instanciated with the parse results as a single list, instead of as arguments.
"""

def camel_to_snake(name):
return re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower()
class WithMeta(object):
"""Abstract class

def _call(func, _data, children, _meta):
return func(*children)
Subclasses will be instanciated with the Meta instance of the tree. (see ``v_args`` for more detail)
"""
pass

inline = v_args(wrapper=_call)
def camel_to_snake(name):
return re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower()

def create_transformer(ast_module, transformer=None):
def create_transformer(ast_module, transformer=None, decorator_factory=v_args):
"""Collects `Ast` subclasses from the given module, and creates a Lark transformer that builds the AST.

For each class, we create a corresponding rule in the transformer, with a matching name.
@@ -38,15 +40,16 @@ def create_transformer(ast_module, transformer=None):
Parameters:
ast_module: A Python module containing all the subclasses of ``ast_utils.Ast``
transformer (Optional[Transformer]): An initial transformer. Its attributes may be overwritten.
decorator_factory (Callable): An optional callable accepting two booleans, inline, and meta,
and returning a decorator for the methods of ``transformer``. (default: ``v_args``).
"""
t = transformer or Transformer()

for name, obj in inspect.getmembers(ast_module):
if not name.startswith('_') and inspect.isclass(obj):
if issubclass(obj, Ast):
if not issubclass(obj, AsList):
obj = inline(obj).__get__(t)

wrapper = decorator_factory(inline=not issubclass(obj, AsList), meta=issubclass(obj, WithMeta))
obj = wrapper(obj).__get__(t)
setattr(t, camel_to_snake(name), obj)

return t

Ladataan…
Peruuta
Tallenna