Ejemplo n.º 1
0
 def __init__(self, remove_indices=None, multi_remove=False, max_tokens=3):
     Transformer.__init__(self)
     AbstractTransformationsIterator.__init__(self, multi_remove=False)
     if remove_indices is None:
         remove_indices = []
     self.remove_indices = remove_indices
     self.token_index = 0
     self.stmt_index = 0
     self.num_options_per_stmt = []
     self.max_tokens = max_tokens
Ejemplo n.º 2
0
def create_transformer(ast_module: types.ModuleType,
                       transformer: Optional[Transformer] = None,
                       decorator_factory: Callable = v_args) -> Transformer:
    """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.
    CamelCase names will be converted into snake_case. Example: "CodeBlock" -> "code_block".

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

    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):
                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
Ejemplo n.º 3
0
 def transform(self, tree, facts):
     """transform(self, tree, facts)
        'tree' - tree to transform
        'facts' - dict to get info about
        facts from
     """
     self._facts = facts
     return Transformer.transform(self, tree)
Ejemplo n.º 4
0
 def __init__(
     self,
     log=True,
     start_end_par_pattern="{}{}",
     left_par=[],
     right_par=[],
     visit_tokens=False,
 ):
     Transformer.__init__(self, visit_tokens=visit_tokens)
     formatted_left_parenthesis = "|".join(left_par)
     formatted_right_parenthesis = "|".join(right_par)
     self.start_end_par_pattern = re.compile(
         start_end_par_pattern.format(
             formatted_left_parenthesis,
             formatted_right_parenthesis,
         ))
     self._logger_func = logging.info
     if not log:
         self._logger_func = lambda x: x
     self._logger = Log(logger_func=self._logger_func)
Ejemplo n.º 5
0
def create_transformer(ast_module, transformer=None):
    """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.
    CamelCase names will be converted into snake_case. Example: "CodeBlock" -> "code_block".

    Parameters:
        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.
    """
    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)

                setattr(t, camel_to_snake(name), obj)

    return t
Ejemplo n.º 6
0
 def __init__(self, console, *args):
     Transformer.__init__(self, *args)
     self.console = console
     self.history = self.console.history
     self.ps1 = self.console.ps1
     self.ps2 = self.console.ps2
Ejemplo n.º 7
0
 def __init__(self, filename):
     Transformer.__init__(self, visit_tokens=False)
     self.filename = filename
Ejemplo n.º 8
0
 def __init__(self):
     Transformer.__init__(self)
     self.tmpcount = -1
Ejemplo n.º 9
0
 def transform(self, tree):
     return self._tree_to_criteria(Transformer.transform(self, tree)).criterion
Ejemplo n.º 10
0
 def transform(self, tree):
     return self._tree_tostring(Transformer.transform(self, tree))
Ejemplo n.º 11
0
 def transform(self, tree):
     return Transformer.transform(self, tree)
Ejemplo n.º 12
0
        return "stream attribute: %s [%s, %s]\n" % (self.name, self.datatype,
                                                    self.streamtype)


class Transformer(object):
    def __init__(self):
        pass

    def string(self, token):
        return str(token).strip("'")

    def variable(self, token):
        return str(token.children[0].value)


ts = Transformer()


class LiveVariables(object):
    def __init__(self):
        self.sa = {}
        self.wp = {}

    def regist_sa(self, ast):
        assert ast.data == 'stream_attribute'
        filename, name = ast.children
        filename, name = ts.string(filename), ts.string(name)
        with open(FILE_PATH + filename) as f:
            data = json.load(f)
            attr = data[name]
        assert 'datatype' in attr
Ejemplo n.º 13
0
 def __init__(self, conversation, turn):
     self.graph = Graph()
     Transformer.__init__(self)
     self.conversation = conversation
     self.turn = turn
     self.instances = {}
Ejemplo n.º 14
0
 def __init__(self, remove_list=None, multi_remove=True):
     Transformer.__init__(self)
     AbstractTransformationsIterator.__init__(self, remove_list=remove_list, multi_remove=multi_remove)
Ejemplo n.º 15
0
 def __init__(self, extras=None, *args, **kwargs):
     self.extras = extras or {}
     Transformer.__init__(self, *args, **kwargs)
Ejemplo n.º 16
0
    def __init__(self):
        Transformer.__init__(self, visit_tokens=True)

        self.dcfile = DCFile()
Ejemplo n.º 17
0
 def __init__(self):
     Transformer.__init__(self)
     self.lyl_scope = Pt.Module(None, None, [Pt.STD])
Ejemplo n.º 18
0
 def DurationTransformer(self):
     Transformer(self)
     self.last_unit = None
Ejemplo n.º 19
0
 def __init__(self, db, filters, default_namespace):
     Transformer.__init__(self)
     self.Filters = filters
     self.DB = db
     self.DefaultNamespace = default_namespace
     self.CurrentParams = {}
Ejemplo n.º 20
0
    @log
    def stmt(self, items):
        return self.recursive_join(items)


asciimath_parser = Lark(
    r"""
    stmt: _csl
    exp: list
        | mat
        | NUMBER
    mat: "[:" _csl? ":]"
    list: (L _csl? R | DOT_L _csl? R | L _csl? DOT_R) -> list
    L.0: "(" | "[" | "{{"
    R.0: ")" | "]" | "}}"
    DOT_L.1: "[:"
    DOT_R.1: ":]"
    _csl: exp (/,/? exp)* /,/?
    %import common.WS
    %import common.NUMBER
    %ignore WS
""",
    start="stmt",
    parser="lalr",
    debug=True,
)
text = """[:[1,3,[2,3,[1,[2,7]]]]:]"""
parsed_text = asciimath_parser.parse(text)
print(parsed_text.pretty())
print(Transformer().transform(parsed_text))
Ejemplo n.º 21
0
 def __init__(self):
     Transformer.__init__(self, visit_tokens=True)