Kaynağa Gözat

ast_utils: Added support for WithMeta (Initial)

gm/2021-09-23T00Z/github.com--lark-parser-lark/v0.12.0
Erez Sh 3 yıl önce
ebeveyn
işleme
f5115464ec
1 değiştirilmiş dosya ile 14 ekleme ve 1 silme
  1. +14
    -1
      lark/ast_utils.py

+ 14
- 1
lark/ast_utils.py Dosyayı Görüntüle

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

class WithMeta(object):
pass

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

def _call(func, _data, children, _meta):
return func(*children)
def _call_with_meta(func, _data, children, meta):
return func(meta, children)
def _call_with_meta_inline(func, _data, children, meta):
return func(meta, *children)

inline = v_args(wrapper=_call)
with_meta = v_args(wrapper=_call_with_meta)
with_meta_inline = v_args(wrapper=_call_with_meta_inline)

def create_transformer(ast_module, transformer=None):
"""Collects `Ast` subclasses from the given module, and creates a Lark transformer that builds the AST.
@@ -45,7 +54,11 @@ def create_transformer(ast_module, transformer=None):
if not name.startswith('_') and inspect.isclass(obj):
if issubclass(obj, Ast):
if not issubclass(obj, AsList):
obj = inline(obj).__get__(t)
obj = with_meta_inline(obj).__get__(t)
else:
obj = inline(obj).__get__(t)
elif issubclass(obj, WithMeta):
obj = with_meta(obj).__get__(t)

setattr(t, camel_to_snake(name), obj)


Yükleniyor…
İptal
Kaydet