本文整理汇总了Python中lark.Tree方法的典型用法代码示例。如果您正苦于以下问题:Python lark.Tree方法的具体用法?Python lark.Tree怎么用?Python lark.Tree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lark
的用法示例。
在下文中一共展示了lark.Tree方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: int_value
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def int_value(self, tree: "Tree") -> "Tree":
"""
Replaces the children of the tree with a custom INT_VALUE token and
casts the string value to an int value.
:param tree: the Tree instance to update
:type tree: Tree
:return: the three with its INT_VALUE token child
:rtype: Tree
"""
# pylint: disable=no-self-use
token = tree.children[0]
return _override_tree_children(
tree,
Token(
"INT_VALUE",
int(token.value),
token.pos_in_stream,
token.line,
token.column,
),
)
示例2: float_value
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def float_value(self, tree: "Tree") -> "Tree":
"""
Replaces the children of the tree with a custom FLOAT_VALUE token and
casts the string value to a float value.
:param tree: the Tree instance to update
:type tree: Tree
:return: the three with its FLOAT_VALUE token child
:rtype: Tree
"""
# pylint: disable=no-self-use
first_token = tree.children[0]
return _override_tree_children(
tree,
Token(
"FLOAT_VALUE",
float("".join([child.value for child in tree.children])),
first_token.pos_in_stream,
first_token.line,
first_token.column,
),
)
示例3: null_value
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def null_value(self, tree: "Tree") -> "Tree":
"""
Replaces the children of the tree with a custom NULL_VALUE token and
force its value to None.
:param tree: the Tree instance to update
:type tree: Tree
:return: the three with its NULL_VALUE token child
:rtype: Tree
"""
# pylint: disable=no-self-use
token = tree.children[0]
return _override_tree_children(
tree,
Token(
"NULL_VALUE",
None,
token.pos_in_stream,
token.line,
token.column,
),
)
示例4: enum_value
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def enum_value(self, tree: "Tree") -> "Tree":
"""
Replaces the children of the tree with a custom ENUM token and set its
value to the underlying name's token value.
:param tree: the Tree instance to update
:type tree: Tree
:return: the three with its ENUM token child
:rtype: Tree
"""
# pylint: disable=no-self-use
token = _find_token(tree.children, _ENUM_VALUE_TOKEN_TYPE)
return _override_tree_children(
tree,
Token(
"ENUM",
token.value,
token.pos_in_stream,
token.line,
token.column,
),
)
示例5: name
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def name(self, tree: "Tree") -> "Tree":
"""
Replaces the children of the tree with a custom NAME token and set its
value to the underlying anonymous's token value.
:param tree: the Tree instance to update
:type tree: Tree
:return: the three with its NAME token child
:rtype: Tree
"""
# pylint: disable=no-self-use
token = tree.children[0]
return _override_tree_children(
tree,
Token(
"NAME",
token.value,
token.pos_in_stream,
token.line,
token.column,
),
)
示例6: description
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def description(self, tree: "Tree") -> "Tree":
"""
Replaces the children of the tree with a custom DESCRIPTION token and
set its value to the underlying string value's token value.
:param tree: the Tree instance to update
:type tree: Tree
:return: the three with its DESCRIPTION token child
:rtype: Tree
"""
# pylint: disable=no-self-use
token = _find_token(tree, _STRING_VALUE_TOKEN_TYPE)
return _override_tree_children(
tree,
Token(
"DESCRIPTION",
token.value,
token.pos_in_stream,
token.line,
token.column,
),
)
示例7: check_tree_invariant
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def check_tree_invariant(
given_code: str,
formatted_code: str,
given_code_parse_tree: Optional[Tree] = None,
formatted_code_parse_tree: Optional[Tree] = None,
) -> None:
given_code_parse_tree = (
given_code_parse_tree
if given_code_parse_tree is not None
else parser.parse(given_code)
)
formatted_code_parse_tree = (
formatted_code_parse_tree
if formatted_code_parse_tree is not None
else parser.parse(formatted_code)
)
loosen_tree_transformer = LoosenTreeTransformer()
given_code_parse_tree = loosen_tree_transformer.transform(given_code_parse_tree)
formatted_code_parse_tree = loosen_tree_transformer.transform(
formatted_code_parse_tree
)
if given_code_parse_tree != formatted_code_parse_tree:
raise TreeInvariantViolation
示例8: check_formatting_stability
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def check_formatting_stability(
formatted_code: str,
max_line_length: int,
parse_tree: Optional[Tree] = None,
comment_parse_tree: Optional[Tree] = None,
) -> None:
code_formatted_again = format_code(
formatted_code,
max_line_length,
parse_tree=parse_tree,
comment_parse_tree=comment_parse_tree,
)
if formatted_code != code_formatted_again:
raise FormattingStabilityViolation
# TODO: boost algorithm
示例9: check_comment_persistence
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def check_comment_persistence(
given_code: str,
formatted_code: str,
given_code_comment_parse_tree: Optional[Tree] = None,
formatted_code_comment_parse_tree: Optional[Tree] = None,
) -> None:
original_comments = gather_comments_from_code(
given_code, comment_tree=given_code_comment_parse_tree,
)
comments_after_formatting = gather_comments_from_code(
formatted_code, comment_tree=formatted_code_comment_parse_tree,
)
for original_comment in original_comments:
if not any(
original_comment in comment_after_formatting
for comment_after_formatting in comments_after_formatting
):
raise CommentPersistenceViolation
示例10: _format_string_to_multiple_lines
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def _format_string_to_multiple_lines(
string: Tree, expression_context: ExpressionContext, context: Context
) -> Outcome:
long_string = string.children[0]
lines = long_string.value.splitlines()
formatted_lines = [
(
expression_context.prefix_line,
"{}{}{}".format(
context.indent_string, expression_context.prefix_string, lines[0]
),
)
] # type: FormattedLines
for middle_line in lines[1:-1]:
formatted_lines.append((string.line, middle_line))
formatted_lines.append(
(string.line, "{}{}".format(lines[-1], expression_context.suffix_string))
)
return (formatted_lines, string.line)
示例11: _format_subscription_to_multiple_lines
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def _format_subscription_to_multiple_lines(
expression: Tree, expression_context: ExpressionContext, context: Context,
) -> Outcome:
subscriptee_expression_context = ExpressionContext(
expression_context.prefix_string, expression_context.prefix_line, ""
)
subscriptee = expression.children[0]
subscriptee_lines, last_line = _format_concrete_expression(
subscriptee, subscriptee_expression_context, context
)
subscript_expression_context = ExpressionContext(
"{}[".format(subscriptee_lines[-1][1].strip()),
last_line,
"]{}".format(expression_context.suffix_string),
)
subscript = expression.children[1]
subscript_lines, _ = _format_standalone_expression(
subscript, subscript_expression_context, context
)
return (subscriptee_lines[:-1] + subscript_lines, expression.end_line)
示例12: _unnecessary_pass_check
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def _unnecessary_pass_check(parse_tree: Tree) -> List[Problem]:
problems = []
for node in parse_tree.iter_subtrees():
if isinstance(node, Tree):
pass_stmts = _find_stmts_among_children(tree=node, suffix="pass_stmt")
all_stmts = _find_stmts_among_children(tree=node, suffix="_stmt")
if len(pass_stmts) < len(all_stmts):
for pass_stmt in pass_stmts:
problems.append(
Problem(
name="unnecessary-pass",
description='"pass" statement not necessary',
line=pass_stmt.line,
column=pass_stmt.column,
)
)
return problems
示例13: lint
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def lint(parse_tree: Tree, config: MappingProxyType) -> List[Problem]:
disable = config["disable"]
checks_to_run_w_tree = [
(
"function-arguments-number",
partial(_function_args_num_check, config["function-arguments-number"]),
),
]
problem_clusters = map(
lambda x: x[1](parse_tree) if x[0] not in disable else [], checks_to_run_w_tree
)
problems = [problem for cluster in problem_clusters for problem in cluster]
checks_to_run_w_ast = [
(
"max-public-methods",
partial(_max_public_methods_check, config["max-public-methods"]),
),
]
ast = AbstractSyntaxTree(parse_tree)
problem_clusters = map(
lambda x: x[1](ast) if x[0] not in disable else [], checks_to_run_w_ast
)
problems += [problem for cluster in problem_clusters for problem in cluster]
return problems
示例14: _gather_rule_name_tokens
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def _gather_rule_name_tokens(
parse_tree: Tree, rules, predicate=lambda _: True
) -> Dict[str, List[str]]:
name_tokens_per_rule = {rule: [] for rule in rules} # type: Dict[str, List[str]]
for node in parse_tree.iter_subtrees():
if isinstance(node, Tree) and node.data in rules:
rule_name = node.data
name_token = find_name_token_among_children(node)
if name_token is None:
name_token = find_name_token_among_children(node.children[0])
predicate_outcome = predicate(node.children[0])
else:
predicate_outcome = predicate(node)
assert name_token is not None
if predicate_outcome:
name_tokens_per_rule[rule_name].append(name_token)
return name_tokens_per_rule
示例15: _private_method_call_check
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Tree [as 别名]
def _private_method_call_check(parse_tree: Tree) -> List[Problem]:
problems = []
for getattr_call in parse_tree.find_data("getattr_call"):
_getattr = getattr_call.children[0]
callee_name_token = _getattr.children[-1]
callee_name = callee_name_token.value
called = _getattr.children[-3]
if (
isinstance(called, Token)
and called.type == "NAME"
and called.value == "self"
):
continue
if not _is_method_private(callee_name):
continue
problems.append(
Problem(
name="private-method-call",
description='Private method "{}" has been called'.format(callee_name),
line=callee_name_token.line,
column=callee_name_token.column,
)
)
return problems