Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
augassign_stmt = Forward()
typed_assign_stmt = Forward()
augassign_stmt_ref = simple_assign + augassign + test_expr
typed_assign_stmt_ref = simple_assign + colon.suppress() + typedef_test + Optional(equals.suppress() + test_expr)
basic_stmt = trace(addspace(ZeroOrMore(assignlist + equals) + test_expr))
compose_item = attach(atom_item + ZeroOrMore(dotdot.suppress() + atom_item), compose_item_handle)
impl_call_arg = (
keyword_atom
| number
| name
)
for k in reserved_vars:
impl_call_arg = ~keyword(k) + impl_call_arg
impl_call_item = attach(compose_item + ZeroOrMore(impl_call_arg), impl_call_item_handle)
await_item = Forward()
await_item_ref = keyword("await").suppress() + impl_call_item
power_item = await_item | impl_call_item
factor = Forward()
unary = plus | neg_minus | tilde
power = trace(condense(power_item + Optional(exp_dubstar + factor)))
factor <<= trace(condense(ZeroOrMore(unary) + power))
mulop = mul_star | div_dubslash | div_slash | percent | matrix_at
addop = plus | sub_minus
shift = lshift | rshift
term = exprlist(factor, mulop)
| Combine(percent + equals)
| Combine(amp + equals)
| Combine(bar + equals)
| Combine(caret + equals)
| Combine(lshift + equals)
| Combine(rshift + equals)
| Combine(matrix_at + equals)
| Combine(dubquestion + equals)
)
comp_op = (
le | ge | ne | lt | gt | eq
| addspace(keyword("not") + keyword("in"))
| keyword("in")
| addspace(keyword("is") + keyword("not"))
| keyword("is")
)
expr = Forward()
star_expr = Forward()
dubstar_expr = Forward()
comp_for = Forward()
test_no_cond = Forward()
namedexpr_test = Forward()
testlist = trace(itemlist(test, comma, suppress_trailing=False))
testlist_star_expr = trace(itemlist(test | star_expr, comma, suppress_trailing=False))
testlist_star_namedexpr = trace(itemlist(namedexpr_test | star_expr, comma, suppress_trailing=False))
testlist_has_comma = trace(addspace(OneOrMore(condense(test + comma)) + Optional(test)))
yield_from = Forward()
dict_comp = Forward()
Group(
match_string
| match_const("const")
| (lparen.suppress() + match + rparen.suppress())("paren")
| (lbrace.suppress() + matchlist_dict + Optional(dubstar.suppress() + name) + rbrace.suppress())("dict")
| (Optional(set_s.suppress()) + lbrace.suppress() + matchlist_set + rbrace.suppress())("set")
| iter_match
| series_match
| star_match
| (name + lparen.suppress() + matchlist_data + rparen.suppress())("data")
| name("var"),
),
)
matchlist_trailer = base_match + OneOrMore(keyword("as") + name | keyword("is") + atom_item)
as_match = Group(matchlist_trailer("trailer")) | base_match
matchlist_and = as_match + OneOrMore(keyword("and").suppress() + as_match)
and_match = Group(matchlist_and("and")) | as_match
matchlist_or = and_match + OneOrMore(keyword("or").suppress() + and_match)
or_match = Group(matchlist_or("or")) | and_match
match <<= trace(or_match)
else_stmt = condense(keyword("else") - suite)
full_suite = colon.suppress() + Group((newline.suppress() + indent.suppress() + OneOrMore(stmt) + dedent.suppress()) | simple_stmt)
full_match = trace(
attach(
keyword("match").suppress() + match + addspace(Optional(keyword("not")) + keyword("in")) - test - match_guard - full_suite,
match_handle,
),
)
match_stmt = condense(full_match - Optional(else_stmt))
destructuring_stmt = Forward()
),
)
match_funcdef = addspace(match_def_modifiers + def_match_funcdef)
where_suite = colon.suppress() - Group(
newline.suppress() + indent.suppress() - OneOrMore(simple_stmt) - dedent.suppress()
| simple_stmt,
)
where_stmt = attach(unsafe_simple_stmt_item + keyword("where").suppress() - where_suite, where_stmt_handle)
implicit_return = (
attach(return_stmt, invalid_return_stmt_handle)
| attach(testlist, implicit_return_handle)
)
implicit_return_stmt = (
attach(implicit_return + keyword("where").suppress() - where_suite, where_stmt_handle)
| condense(implicit_return + newline)
)
math_funcdef_body = condense(ZeroOrMore(~(implicit_return_stmt + dedent) + stmt) - implicit_return_stmt)
math_funcdef_suite = (
attach(implicit_return_stmt, make_suite_handle)
| condense(newline - indent - math_funcdef_body - dedent)
)
end_func_equals = return_typedef + equals.suppress() | fixto(equals, ":")
math_funcdef = trace(
attach(
condense(addspace(keyword("def") + base_funcdef) + end_func_equals) - math_funcdef_suite,
math_funcdef_handle,
),
)
math_match_funcdef = addspace(
match_def_modifiers
comp_pipe_expr("expr"),
),
)
pipe_expr = (
comp_pipe_expr + ~pipe_op
| attach(OneOrMore(pipe_item) + last_pipe_item, pipe_handle)
)
expr <<= pipe_expr
star_expr_ref = condense(star + expr)
dubstar_expr_ref = condense(dubstar + expr)
comparison = exprlist(expr, comp_op)
not_test = addspace(ZeroOrMore(keyword("not")) + comparison)
and_test = exprlist(not_test, keyword("and"))
test_item = trace(exprlist(and_test, keyword("or")))
simple_stmt_item = Forward()
unsafe_simple_stmt_item = Forward()
simple_stmt = Forward()
stmt = Forward()
suite = Forward()
nocolon_suite = Forward()
base_suite = Forward()
classlist = Forward()
classic_lambdef = Forward()
classic_lambdef_params = maybeparens(lparen, var_args_list, rparen)
new_lambdef_params = lparen.suppress() + var_args_list + rparen.suppress() | name
classic_lambdef_ref = addspace(keyword("lambda") + condense(classic_lambdef_params + colon))
new_lambdef = attach(new_lambdef_params + arrow.suppress(), lambdef_handle)
+ colon.suppress()
+ (
attach(simple_stmt, make_suite_handle)
| newline.suppress() + indent.suppress()
+ Optional(docstring)
+ attach(condense(OneOrMore(stmt)), make_suite_handle)
+ dedent.suppress()
),
join_match_funcdef,
),
)
match_def_modifiers = trace(
Optional(
# we don't suppress addpattern so its presence can be detected later
keyword("match").suppress() + Optional(keyword("addpattern"))
| keyword("addpattern") + Optional(keyword("match")).suppress(),
),
)
match_funcdef = addspace(match_def_modifiers + def_match_funcdef)
where_suite = colon.suppress() - Group(
newline.suppress() + indent.suppress() - OneOrMore(simple_stmt) - dedent.suppress()
| simple_stmt,
)
where_stmt = attach(unsafe_simple_stmt_item + keyword("where").suppress() - where_suite, where_stmt_handle)
implicit_return = (
attach(return_stmt, invalid_return_stmt_handle)
| attach(testlist, implicit_return_handle)
)
implicit_return_stmt = (
attach(implicit_return + keyword("where").suppress() - where_suite, where_stmt_handle)
),
)
pipe_expr = (
comp_pipe_expr + ~pipe_op
| attach(OneOrMore(pipe_item) + last_pipe_item, pipe_handle)
)
expr <<= pipe_expr
star_expr_ref = condense(star + expr)
dubstar_expr_ref = condense(dubstar + expr)
comparison = exprlist(expr, comp_op)
not_test = addspace(ZeroOrMore(keyword("not")) + comparison)
and_test = exprlist(not_test, keyword("and"))
test_item = trace(exprlist(and_test, keyword("or")))
simple_stmt_item = Forward()
unsafe_simple_stmt_item = Forward()
simple_stmt = Forward()
stmt = Forward()
suite = Forward()
nocolon_suite = Forward()
base_suite = Forward()
classlist = Forward()
classic_lambdef = Forward()
classic_lambdef_params = maybeparens(lparen, var_args_list, rparen)
new_lambdef_params = lparen.suppress() + var_args_list + rparen.suppress() | name
classic_lambdef_ref = addspace(keyword("lambda") + condense(classic_lambdef_params + colon))
new_lambdef = attach(new_lambdef_params + arrow.suppress(), lambdef_handle)
implicit_lambdef = fixto(arrow, "lambda _=None:")
and_test = exprlist(not_test, keyword("and"))
test_item = trace(exprlist(and_test, keyword("or")))
simple_stmt_item = Forward()
unsafe_simple_stmt_item = Forward()
simple_stmt = Forward()
stmt = Forward()
suite = Forward()
nocolon_suite = Forward()
base_suite = Forward()
classlist = Forward()
classic_lambdef = Forward()
classic_lambdef_params = maybeparens(lparen, var_args_list, rparen)
new_lambdef_params = lparen.suppress() + var_args_list + rparen.suppress() | name
classic_lambdef_ref = addspace(keyword("lambda") + condense(classic_lambdef_params + colon))
new_lambdef = attach(new_lambdef_params + arrow.suppress(), lambdef_handle)
implicit_lambdef = fixto(arrow, "lambda _=None:")
lambdef_base = classic_lambdef | new_lambdef | implicit_lambdef
stmt_lambdef = Forward()
match_guard = Optional(keyword("if").suppress() + test)
closing_stmt = longest(testlist("tests"), unsafe_simple_stmt_item)
stmt_lambdef_params = Optional(
attach(name, add_paren_handle)
| parameters
| Group(lparen.suppress() + match_args_list + match_guard + rparen.suppress()),
default="(_=None)",
)
stmt_lambdef_ref = (
keyword("def").suppress() + stmt_lambdef_params + arrow.suppress()
+ (
attach(return_stmt, invalid_return_stmt_handle)
| attach(testlist, implicit_return_handle)
)
implicit_return_stmt = (
attach(implicit_return + keyword("where").suppress() - where_suite, where_stmt_handle)
| condense(implicit_return + newline)
)
math_funcdef_body = condense(ZeroOrMore(~(implicit_return_stmt + dedent) + stmt) - implicit_return_stmt)
math_funcdef_suite = (
attach(implicit_return_stmt, make_suite_handle)
| condense(newline - indent - math_funcdef_body - dedent)
)
end_func_equals = return_typedef + equals.suppress() | fixto(equals, ":")
math_funcdef = trace(
attach(
condense(addspace(keyword("def") + base_funcdef) + end_func_equals) - math_funcdef_suite,
math_funcdef_handle,
),
)
math_match_funcdef = addspace(
match_def_modifiers
+ trace(
attach(
base_match_funcdef
+ equals.suppress()
- Optional(docstring)
- (
attach(implicit_return_stmt, make_suite_handle)
| newline.suppress() - indent.suppress()
- Optional(docstring)
- attach(math_funcdef_body, make_suite_handle)
- dedent.suppress()