How to use the lark.Tree function in lark

To help you get started, we’ve selected a few lark examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nightblade9 / dragon / tests / transpiler / lark / validators / test_lark_validator.py View on Github external
def test_is_fully_parsed_fails_if_token_remains(self):
        # raw tokens: file_input, compound_stmt, classdef, suite, pass_stmt
        tree = Tree("file_input", [Tree("compound_stmt", [Tree("classdef",
            [Token("NAME", 'AssetPaths'), Tree("suite", [Tree("pass_stmt", [])])])])])

        validator = LarkValidator(TestLarkValidator._GRAMMAR_LOCATION)
        self.assertFalse(validator.is_fully_parsed(tree))
github nightblade9 / dragon / tests / transpiler / lark / transformers / test_haxe_transformer.py View on Github external
def test_import_stmt_transforms_simple_imports(self):
        h = HaxeTransformer()

        node = [Tree("import_name", [Tree("dotted_as_names", [Tree("dotted_as_name",
            [Tree("dotted_name", [Token("NAME", 'PlayState')])])])])]

        output = h.import_stmt(node)
        self.assertEqual("import PlayState;", output)
github nightblade9 / dragon / tests / generators / test_haxe_generator.py View on Github external
def test_arguments_returns_arguments(self):
        args = [124, Tree("test", [])]
        output = haxe_generator.arguments(args)
        self.assertEqual(args, output)
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / expression.py View on Github external
def format_comma_separated_list(
    a_list: List[Node], expression_context: ExpressionContext, context: Context
) -> FormattedLines:
    elements = [node for node in a_list if not is_any_comma(node)]
    child_context = context.create_child_context(expression_context.prefix_line)
    fake_expression = Tree("fake", a_list)
    multiline_mode_forced = is_expression_forcing_multiple_lines(fake_expression)
    if not multiline_mode_forced:
        strings_to_join = list(map(standalone_expression_to_str, elements))
        single_line_expression = "{}{}{}".format(
            expression_context.prefix_string,
            ", ".join(strings_to_join),
            expression_context.suffix_string,
        )
        single_line_length = len(single_line_expression) + context.indent
        if single_line_length <= context.max_line_length:
            return [
                (
                    expression_context.prefix_line,
                    "{}{}".format(context.indent_string, single_line_expression),
                )
            ]
github Scony / godot-gdscript-toolkit / gdtoolkit / linter / ast.py View on Github external
def _load_data_from_node_children(self, node: Tree) -> None:
        for stmt in node.children:
            if not isinstance(stmt, Tree):
                continue
            if stmt.data == "class_def":
                self.sub_classes.append(Class(stmt))
            if stmt.data == "func_def":
                self.functions.append(Function(stmt))
github systemslab / popper / cli / popper / commands / cmd_workflow.py View on Github external
def _pretty(self, level, tree):
        if len(tree.children) == 1 and not isinstance(tree.children[0], Tree):
            return ['%s' % (tree.children[0],), ' ']

        ls = ''
        for n in tree.children:
            if isinstance(n, Tree):
                ls += self._pretty(level + 1, n)
            else:
                ls += ['%s' % (n,), ' ']

        return ls
github remorses / skema / skema_lark / splitter.py View on Github external
def root_pair(self, children):
        key, *_ = children
        self.types[key] = Tree("root_pair", children)

        # get generic references
        refs_parents = Tree("", children).find_pred(is_reference_parent)
        for child in refs_parents:
            parentname, ref = child.children  #  TODO first child might be annotation
            refname, = ref.children
            self.child_of[refname].add(str(key))
            self.child_of[refname].add(str(parentname))

        # get list references
        list_refs_parents = list(Tree("", children).find_pred(is_reference_list_parent))
        for list_parent in list_refs_parents:
            parentname, list_node, = list_parent.children
            ref, = list_node.children
            refname, = ref.children
            self.child_of[refname].add(str(key))
            self.child_of[refname].add(str(parentname))

        # if refs_parents:
        #     pretty(self.child_of)
        #     pretty(list(self.types.keys()))
        return Tree("root_pair", children)
github Scony / godot-gdscript-toolkit / gdtoolkit / linter / class_checks.py View on Github external
"tool_stmt": "tools",
        "signal_stmt": "signals",
        "extends_stmt": "extends",
        "classname_stmt": "classnames",
        "const_stmt": "consts",
        "export_stmt": "exports",
        "enum_def": "enums",
    }
    visibility_dependent_stmt_to_section_mapping = {
        "class_var_stmt": {"pub": "pubvars", "prv": "prvvars"},
        "onready_stmt": {"pub": "onreadypubvars", "prv": "onreadyprvvars"},
    }
    problems = []
    current_section = order[0]
    for class_child in class_children:
        if not isinstance(class_child, Tree):
            continue
        stmt = class_child.data
        if stmt == "class_var_stmt":
            visibility = _class_var_stmt_visibility(class_child)
            section = visibility_dependent_stmt_to_section_mapping[stmt][visibility]
        elif stmt == "onready_stmt":
            class_var_stmt = class_child.children[0]
            visibility = _class_var_stmt_visibility(class_var_stmt)
            section = visibility_dependent_stmt_to_section_mapping[stmt][visibility]
        else:
            section = stmt_to_section_mapping.get(stmt, "others")
        section_rank = order.index(section)
        if section_rank >= order.index(current_section):
            current_section = section
        else:
            problems.append(
github protective / stationeers_pymips / compiler / InstBuilder.py View on Github external
def reduce_expr(self, tree, store_dst=None, **kwargs):
        lst = tree.children.copy()
        expr_eval_dst = store_dst

        if LAExprRearrange(store_dst, tree).tree is None:
            # store_dst is an id hence check if it can be used directly else
            # use a tmp register
            expr_eval_dst = None
        with self.free_register(store_dst=expr_eval_dst) as t0:
            eval_store = t0
            while len(lst) >= 3:
                eval_store = t0 if len(lst) >= 5 else store_dst  # use store_dst if final eval
                eval_store = self.operator(*lst[0:3], store_dst=eval_store, **kwargs)
                lst = lst[3:]
                lst.insert(0, Tree('loc', [Token('loc', t0)]))
            return eval_store