Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
datadef = Forward()
data_args = Group(
Optional(
lparen.suppress() + ZeroOrMore(
Group(
# everything here must end with arg_comma
(name + arg_comma.suppress())("name")
| (name + equals.suppress() + test + arg_comma.suppress())("default")
| (star.suppress() + name + arg_comma.suppress())("star")
| (name + colon.suppress() + typedef_test + equals.suppress() + test + arg_comma.suppress())("type default")
| (name + colon.suppress() + typedef_test + arg_comma.suppress())("type"),
),
) + rparen.suppress(),
),
) + Optional(keyword("from").suppress() + testlist)
data_suite = Group(
colon.suppress() - (
(newline.suppress() + indent.suppress() + Optional(docstring) + Group(OneOrMore(stmt)) + dedent.suppress())("complex")
| (newline.suppress() + indent.suppress() + docstring + dedent.suppress() | docstring)("docstring")
| simple_stmt("simple")
) | newline("empty"),
)
datadef_ref = keyword("data").suppress() + name + data_args + data_suite
match_datadef = Forward()
match_data_args = lparen.suppress() + Group(
match_args_list + match_guard,
) + rparen.suppress() + Optional(keyword("from").suppress() + testlist)
match_datadef_ref = Optional(keyword("match").suppress()) + keyword("data").suppress() + name + match_data_args + data_suite
simple_decorator = condense(dotted_name + Optional(function_call))("simple")
complex_decorator = test("test")
subscriptgroup = attach(slicetestgroup + sliceopgroup + Optional(sliceopgroup) | test, subscriptgroup_handle)
subscriptgrouplist = itemlist(subscriptgroup, comma)
testlist_comp = addspace((namedexpr_test | star_expr) + comp_for) | testlist_star_namedexpr
list_comp = condense(lbrack + Optional(testlist_comp) + rbrack)
paren_atom = condense(lparen + Optional(yield_expr | testlist_comp) + rparen)
op_atom = lparen.suppress() + op_item + rparen.suppress()
keyword_atom = reduce(lambda acc, x: acc | keyword(x), const_vars)
string_atom = addspace(OneOrMore(string))
passthrough_atom = trace(addspace(OneOrMore(passthrough)))
set_literal = Forward()
set_letter_literal = Forward()
set_s = fixto(CaselessLiteral("s"), "s")
set_f = fixto(CaselessLiteral("f"), "f")
set_letter = set_s | set_f
setmaker = Group(addspace(test + comp_for)("comp") | testlist_has_comma("list") | test("test"))
set_literal_ref = lbrace.suppress() + setmaker + rbrace.suppress()
set_letter_literal_ref = set_letter + lbrace.suppress() + Optional(setmaker) + rbrace.suppress()
lazy_items = Optional(test + ZeroOrMore(comma.suppress() + test) + Optional(comma.suppress()))
lazy_list = attach(lbanana.suppress() + lazy_items + rbanana.suppress(), lazy_list_handle)
const_atom = (
keyword_atom
| number
| string_atom
)
known_atom = trace(
const_atom
| ellipsis
| list_comp
| dict_comp
| dict_item
Optional(
lparen.suppress() + ZeroOrMore(
Group(
# everything here must end with arg_comma
(name + arg_comma.suppress())("name")
| (name + equals.suppress() + test + arg_comma.suppress())("default")
| (star.suppress() + name + arg_comma.suppress())("star")
| (name + colon.suppress() + typedef_test + equals.suppress() + test + arg_comma.suppress())("type default")
| (name + colon.suppress() + typedef_test + arg_comma.suppress())("type"),
),
) + rparen.suppress(),
),
) + Optional(keyword("from").suppress() + testlist)
data_suite = Group(
colon.suppress() - (
(newline.suppress() + indent.suppress() + Optional(docstring) + Group(OneOrMore(stmt)) + dedent.suppress())("complex")
| (newline.suppress() + indent.suppress() + docstring + dedent.suppress() | docstring)("docstring")
| simple_stmt("simple")
) | newline("empty"),
)
datadef_ref = keyword("data").suppress() + name + data_args + data_suite
match_datadef = Forward()
match_data_args = lparen.suppress() + Group(
match_args_list + match_guard,
) + rparen.suppress() + Optional(keyword("from").suppress() + testlist)
match_datadef_ref = Optional(keyword("match").suppress()) + keyword("data").suppress() + name + match_data_args + data_suite
simple_decorator = condense(dotted_name + Optional(function_call))("simple")
complex_decorator = test("test")
decorators = attach(OneOrMore(at.suppress() - Group(longest(simple_decorator, complex_decorator)) - newline.suppress()), decorator_handle)
(newline.suppress() + indent.suppress() + Optional(docstring) + Group(OneOrMore(stmt)) + dedent.suppress())("complex")
| (newline.suppress() + indent.suppress() + docstring + dedent.suppress() | docstring)("docstring")
| simple_stmt("simple")
) | newline("empty"),
)
datadef_ref = keyword("data").suppress() + name + data_args + data_suite
match_datadef = Forward()
match_data_args = lparen.suppress() + Group(
match_args_list + match_guard,
) + rparen.suppress() + Optional(keyword("from").suppress() + testlist)
match_datadef_ref = Optional(keyword("match").suppress()) + keyword("data").suppress() + name + match_data_args + data_suite
simple_decorator = condense(dotted_name + Optional(function_call))("simple")
complex_decorator = test("test")
decorators = attach(OneOrMore(at.suppress() - Group(longest(simple_decorator, complex_decorator)) - newline.suppress()), decorator_handle)
decoratable_normal_funcdef_stmt = Forward()
normal_funcdef_stmt = (
funcdef
| math_funcdef
| math_match_funcdef
| match_funcdef
)
decoratable_normal_funcdef_stmt_ref = Optional(decorators) + normal_funcdef_stmt
decoratable_async_funcdef_stmt = Forward()
async_funcdef_stmt = async_funcdef | async_match_funcdef
decoratable_async_funcdef_stmt_ref = Optional(decorators) + async_funcdef_stmt
decoratable_func_stmt = decoratable_normal_funcdef_stmt | decoratable_async_funcdef_stmt
comp_pipe_handle,
)
)
pipe_op = (
pipe
| back_pipe
| star_pipe
| back_star_pipe
| dubstar_pipe
| back_dubstar_pipe
)
pipe_item = (
# we need the pipe_op since any of the atoms could otherwise be the start of an expression
Group(attrgetter_atom_tokens("attrgetter")) + pipe_op
| Group(itemgetter_atom_tokens("itemgetter")) + pipe_op
| Group(partial_atom_tokens("partial")) + pipe_op
| Group(comp_pipe_expr("expr")) + pipe_op
)
last_pipe_item = Group(
lambdef("expr")
| longest(
# we need longest here because there's no following pipe_op we can use as above
attrgetter_atom_tokens("attrgetter"),
itemgetter_atom_tokens("itemgetter"),
partial_atom_tokens("partial"),
comp_pipe_expr("expr"),
),
)
pipe_expr = (
comp_pipe_expr + ~pipe_op
| attach(OneOrMore(pipe_item) + last_pipe_item, pipe_handle)
),
),
)
call_item = (
dubstar + test
| star + test
| name + default
| namedexpr_test
)
function_call_tokens = lparen.suppress() + (
# everything here must end with rparen
rparen.suppress()
| Group(op_item) + rparen.suppress()
| Group(attach(addspace(test + comp_for), add_paren_handle)) + rparen.suppress()
| tokenlist(Group(call_item), comma) + rparen.suppress()
)
function_call = attach(function_call_tokens, function_call_handle)
questionmark_call_tokens = Group(
tokenlist(
Group(
questionmark
| call_item,
),
comma,
),
)
methodcaller_args = (
itemlist(condense(call_item), comma)
| op_item
)
comp_iter <<= comp_for | comp_if
complex_raise_stmt = Forward()
pass_stmt = keyword("pass")
break_stmt = keyword("break")
continue_stmt = keyword("continue")
return_stmt = addspace(keyword("return") - Optional(testlist))
simple_raise_stmt = addspace(keyword("raise") + Optional(test))
complex_raise_stmt_ref = keyword("raise").suppress() + test + keyword("from").suppress() - test
raise_stmt = complex_raise_stmt | simple_raise_stmt
flow_stmt = break_stmt | continue_stmt | return_stmt | raise_stmt | yield_expr
dotted_as_name = Group(dotted_name - Optional(keyword("as").suppress() - name))
import_as_name = Group(name - Optional(keyword("as").suppress() - name))
import_names = Group(maybeparens(lparen, tokenlist(dotted_as_name, comma), rparen))
from_import_names = Group(maybeparens(lparen, tokenlist(import_as_name, comma), rparen))
basic_import = keyword("import").suppress() - (import_names | Group(star))
from_import = (
keyword("from").suppress()
- condense(ZeroOrMore(unsafe_dot) + dotted_name | OneOrMore(unsafe_dot) | star)
- keyword("import").suppress() - (from_import_names | Group(star))
)
import_stmt = Forward()
import_stmt_ref = from_import | basic_import
nonlocal_stmt = Forward()
namelist = attach(
maybeparens(lparen, itemlist(name, comma), rparen) - Optional(equals.suppress() - test_expr),
namelist_handle,
)
global_stmt = addspace(keyword("global") - namelist)
nonlocal_stmt_ref = addspace(keyword("nonlocal") - namelist)
OneOrMore(none_coalesce_expr + comp_pipe_op) + (lambdef | none_coalesce_expr),
comp_pipe_handle,
)
)
pipe_op = (
pipe
| back_pipe
| star_pipe
| back_star_pipe
| dubstar_pipe
| back_dubstar_pipe
)
pipe_item = (
# we need the pipe_op since any of the atoms could otherwise be the start of an expression
Group(attrgetter_atom_tokens("attrgetter")) + pipe_op
| Group(itemgetter_atom_tokens("itemgetter")) + pipe_op
| Group(partial_atom_tokens("partial")) + pipe_op
| Group(comp_pipe_expr("expr")) + pipe_op
)
last_pipe_item = Group(
lambdef("expr")
| longest(
# we need longest here because there's no following pipe_op we can use as above
attrgetter_atom_tokens("attrgetter"),
itemgetter_atom_tokens("itemgetter"),
partial_atom_tokens("partial"),
comp_pipe_expr("expr"),
),
)
pipe_expr = (
comp_pipe_expr + ~pipe_op
| (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()
destructuring_stmt_ref = Optional(keyword("match").suppress()) + match + equals.suppress() + test_expr
case_stmt = Forward()
)
global_stmt = addspace(keyword("global") - namelist)
nonlocal_stmt_ref = addspace(keyword("nonlocal") - namelist)
del_stmt = addspace(keyword("del") - simple_assignlist)
matchlist_list = Group(Optional(tokenlist(match, comma)))
matchlist_tuple = Group(
Optional(
match + OneOrMore(comma.suppress() + match) + Optional(comma.suppress())
| match + comma.suppress(),
),
)
matchlist_star = (
Optional(Group(OneOrMore(match + comma.suppress())))
+ star.suppress() + name
+ Optional(Group(OneOrMore(comma.suppress() + match)))
+ Optional(comma.suppress())
)
matchlist_data = (
Optional(Group(OneOrMore(match + comma.suppress())), default=())
+ star.suppress() + match
+ Optional(comma.suppress())
) | matchlist_list
match_const = const_atom | condense(equals.suppress() + atom_item)
match_string = (
(string + plus.suppress() + name + plus.suppress() + string)("mstring")
| (string + plus.suppress() + name)("string")
| (name + plus.suppress() + string)("rstring")
)
matchlist_set = Group(Optional(tokenlist(match_const, comma)))
match_pair = Group(match_const + colon.suppress() + match)