本文整理汇总了Python中lark.Token方法的典型用法代码示例。如果您正苦于以下问题:Python lark.Token方法的具体用法?Python lark.Token怎么用?Python lark.Token使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lark
的用法示例。
在下文中一共展示了lark.Token方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _private_method_call_check
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [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
示例2: scan_resource_conf
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def scan_resource_conf(self, conf):
"""
Looks for ACL configuration at aws_s3_bucket and Tag values:
https://www.terraform.io/docs/providers/aws/r/s3_bucket.html
:param conf: aws_s3_bucket configuration
:return: <CheckResult>
"""
if 'tags' in conf.keys():
environment_tag = Token("IDENTIFIER", "Scope")
if environment_tag in conf['tags'][0].keys():
if conf['tags'][0][environment_tag] == "PCI":
if 'acl' in conf.keys():
acl_block = conf['acl']
if acl_block in [["public-read"], ["public-read-write"], ["website"]]:
return CheckResult.FAILED
return CheckResult.PASSED
示例3: fuzzy_string_op_rhs
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def fuzzy_string_op_rhs(self, arg):
# fuzzy_string_op_rhs: CONTAINS value | STARTS [ WITH ] value | ENDS [ WITH ] value
# The WITH keyword may be omitted.
if isinstance(arg[1], Token) and arg[1].type == "WITH":
pattern = arg[2]
else:
pattern = arg[1]
# CONTAINS
if arg[0] == "CONTAINS":
regex = f"{pattern}"
elif arg[0] == "STARTS":
regex = f"^{pattern}"
elif arg[0] == "ENDS":
regex = f"{pattern}$"
return {"$regex": regex}
示例4: is_foldable
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def is_foldable(expression: Node) -> bool:
if _is_multiline_string(expression):
return True
return not isinstance(expression, Token) and expression.data not in [
"string",
"node_path",
"get_node",
]
示例5: is_expression_forcing_multiple_lines
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def is_expression_forcing_multiple_lines(expression: Node) -> bool:
if has_trailing_comma(expression):
return True
if _is_multiline_string(expression):
return True
if isinstance(expression, Token):
return False
for child in expression.children:
if is_expression_forcing_multiple_lines(child):
return True
return False
示例6: is_any_comma
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def is_any_comma(expression: Node) -> bool:
return (isinstance(expression, Tree) and expression.data == "trailing_comma") or (
isinstance(expression, Token) and expression.type == "COMMA"
)
示例7: is_any_parentheses
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def is_any_parentheses(expression: Node) -> bool:
return isinstance(expression, Token) and expression.type in ["LPAR", "RPAR"]
示例8: _load_name
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def _load_name(enum_def: Tree) -> Optional[str]:
if len(enum_def.children[0].children) == 0:
return None
node = enum_def.children[0].children[0]
if isinstance(node, Token) and node.type == "NAME":
return node.value
return None
示例9: _unused_argument_check
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def _unused_argument_check(parse_tree: Tree) -> List[Problem]:
problems = []
for func_def in parse_tree.find_data("func_def"):
if (
isinstance(func_def.children[1], Tree)
and func_def.children[1].data == "func_args"
):
argument_definitions = {} # type: Dict[str, int]
argument_tokens = {}
func_args = func_def.children[1]
for func_arg in func_args.children:
arg_name_token = find_name_token_among_children(func_arg)
arg_name = arg_name_token.value
argument_definitions[arg_name] = (
argument_definitions.get(arg_name, 0) + 1
)
argument_tokens[arg_name] = arg_name_token
name_occurances = {} # type: Dict[str, int]
for xnode in func_def.iter_subtrees():
for node in xnode.children:
if isinstance(node, Token) and node.type == "NAME":
name = node.value
name_occurances[name] = name_occurances.get(name, 0) + 1
for argument in argument_definitions:
if argument_definitions[argument] == name_occurances[
argument
] and not argument.startswith("_"):
problems.append(
Problem(
name="unused-argument",
description="unused function argument '{}'".format(
argument
),
line=argument_tokens[argument].line,
column=argument_tokens[argument].column,
)
)
return problems
示例10: find_name_token_among_children
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def find_name_token_among_children(tree):
for child in tree.children:
if isinstance(child, Token) and child.type == "NAME":
return child
return None
示例11: ignore_errors
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def ignore_errors(e):
if e.token.type == 'COMMA':
# Skip comma
return True
elif e.token.type == 'SIGNED_NUMBER':
# Try to feed a comma and retry the number
e.puppet.feed_token(Token('COMMA', ','))
e.puppet.feed_token(e.token)
return True
# Unhandled error. Will stop parse and raise exception
return False
示例12: extern_func
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def extern_func(self, node):
name, params, returns, newline = self.visit_children(node)
name = name.value
if isinstance(params, Token):
params = self.token_to_val(params)
if isinstance(returns, Token):
returns = self.token_to_val(returns)
from .variables import VarType
params = [(VarType._init_from_parser(params[i*2]), params[(i*2)+1].value) \
for i in range(len(params or []) // 2)]
returns = [VarType._init_from_parser(r) for r in returns or []]
existing = self.top.lookup_func(name)
extern = ExternFunction(name, params, returns)
if existing:
existing.expect_signature(extern)
if not existing.finished and \
not isinstance(existing, ExternFunction):
# This is a forward-referenced function, replace references
# with our extern
mapping = {existing: extern}
for var in self.top.scope.values():
if isinstance(var, IRFunction):
var.preamble.apply_mapping(IRFunction, mapping)
for block in var.blocks:
block.apply_mapping(IRFunction, mapping)
# Direct replacement
self.top.scope[name] = extern
else:
self.top.store(name, extern)
示例13: block
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def block(self, node):
if not isinstance(node.children[0], Token):
# Modifier is first child
name = node.children[1].value[:-1]
else:
name = node.children[0].value[:-1]
self.curr_seq = self.func.get_or_create_block(name)
self.visit_children(node)
self.curr_seq.defined = True
self.curr_seq = None
示例14: operand
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def operand(self, node):
token = node.children[0]
if isinstance(token, Token):
return self.token_to_val(token)
# must be tuple
tup, = self.visit_children(node)
return tup
示例15: expression_to_str
# 需要导入模块: import lark [as 别名]
# 或者: from lark import Token [as 别名]
def expression_to_str(expression: Node) -> str:
if isinstance(expression, Token):
return expression.value
return {
"expr": lambda e: standalone_expression_to_str(e.children[0]),
"assnmnt_expr": _operator_chain_based_expression_to_str,
"test_expr": _operator_chain_based_expression_to_str,
"or_test": _operator_chain_based_expression_to_str,
"and_test": _operator_chain_based_expression_to_str,
"not_test": lambda e: "{} {}".format(
expression_to_str(e.children[0]), expression_to_str(e.children[1])
),
"content_test": _operator_chain_based_expression_to_str,
"comparison": _operator_chain_based_expression_to_str,
"bitw_or": _operator_chain_based_expression_to_str,
"bitw_xor": _operator_chain_based_expression_to_str,
"bitw_and": _operator_chain_based_expression_to_str,
"shift_expr": _operator_chain_based_expression_to_str,
"arith_expr": _operator_chain_based_expression_to_str,
"mdr_expr": _operator_chain_based_expression_to_str,
"neg_expr": lambda e: "-{}".format(expression_to_str(e.children[1])),
"bitw_not": lambda e: "~{}".format(expression_to_str(e.children[1])),
"type_test": _operator_chain_based_expression_to_str,
"type_cast": _operator_chain_based_expression_to_str,
"standalone_call": _standalone_call_to_str,
"getattr_call": _getattr_call_to_str,
"getattr": lambda e: "".join(map(expression_to_str, e.children)),
"subscr_expr": _subscription_to_str,
"par_expr": lambda e: "({})".format(
standalone_expression_to_str(e.children[0])
),
"array": _array_to_str,
"dict": _dict_to_str,
"kv_pair": lambda e: _dict_element_to_str(e.children[0]),
"c_dict_element": _dict_element_to_str,
"eq_dict_element": _dict_element_to_str,
"string": lambda e: e.children[0].value,
"node_path": lambda e: "@{}".format(expression_to_str(e.children[0])),
"get_node": lambda e: "${}".format(expression_to_str(e.children[0])),
"path": lambda e: "/".join([name_token.value for name_token in e.children]),
# fake expressions:
"func_arg_regular": lambda e: "{}{}".format(
e.children[0].value,
" = {}".format(standalone_expression_to_str(e.children[1]))
if len(e.children) > 1
else "",
),
"func_arg_inf": lambda e: "{} := {}".format(
e.children[0].value, standalone_expression_to_str(e.children[1])
),
"func_arg_typed": lambda e: "{}: {}{}".format(
e.children[0].value,
e.children[1].value,
" = {}".format(standalone_expression_to_str(e.children[2]))
if len(e.children) > 2
else "",
),
# patterns (fake expressions):
"wildcard_pattern": lambda _: "_",
"attr_pattern": lambda e: ".".join(map(expression_to_str, e.children[::2])),
}[expression.data](expression)