Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
| set_literal
| set_letter_literal
| lazy_list,
)
func_atom = (
name
| op_atom
| paren_atom
)
atom = (
known_atom
| (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()
case_match = trace(
| 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()
case_match = trace(
Group(
keyword("match").suppress() - match - Optional(keyword("if").suppress() - test) - full_suite,
),
)
item_handle,
)
simple_assignlist = maybeparens(lparen, itemlist(simple_assign, comma, suppress_trailing=False), rparen)
assignlist = Forward()
star_assign_item = Forward()
base_assign_item = condense(simple_assign | lparen + assignlist + rparen | lbrack + assignlist + rbrack)
star_assign_item_ref = condense(star + base_assign_item)
assign_item = star_assign_item | base_assign_item
assignlist <<= trace(itemlist(assign_item, comma, suppress_trailing=False))
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
base_match_funcdef = trace(op_match_funcdef | name_match_funcdef)
def_match_funcdef = trace(
attach(
base_match_funcdef
+ 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)
maybeparens(
lparen,
(name | passthrough_atom)
+ ZeroOrMore(ZeroOrMore(complex_trailer) + OneOrMore(simple_trailer)),
rparen,
),
item_handle,
)
simple_assignlist = maybeparens(lparen, itemlist(simple_assign, comma, suppress_trailing=False), rparen)
assignlist = Forward()
star_assign_item = Forward()
base_assign_item = condense(simple_assign | lparen + assignlist + rparen | lbrack + assignlist + rbrack)
star_assign_item_ref = condense(star + base_assign_item)
assign_item = star_assign_item | base_assign_item
assignlist <<= trace(itemlist(assign_item, comma, suppress_trailing=False))
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
decoratable_func_stmt = decoratable_normal_funcdef_stmt | decoratable_async_funcdef_stmt
class_stmt = classdef | datadef | match_datadef
decoratable_class_stmt = trace(condense(Optional(decorators) + class_stmt))
passthrough_stmt = condense(passthrough_block - (base_suite | newline))
simple_compound_stmt = trace(
if_stmt
| try_stmt
| case_stmt
| match_stmt
| passthrough_stmt,
)
compound_stmt = trace(
decoratable_class_stmt
| decoratable_func_stmt
| with_stmt
| while_stmt
| for_stmt
| async_stmt
| where_stmt
| simple_compound_stmt,
)
endline_semicolon = Forward()
endline_semicolon_ref = semicolon.suppress() + newline
keyword_stmt = trace(
del_stmt
| pass_stmt
| flow_stmt
| import_stmt
simple_stmt <<= trace(
condense(
simple_stmt_item
+ ZeroOrMore(fixto(semicolon, "\n") + simple_stmt_item)
+ (newline | endline_semicolon),
),
)
stmt <<= final(trace(compound_stmt | simple_stmt))
base_suite <<= condense(newline + indent - OneOrMore(stmt) - dedent)
simple_suite = attach(stmt, make_suite_handle)
nocolon_suite <<= trace(base_suite | simple_suite)
suite <<= condense(colon + nocolon_suite)
line = trace(newline | stmt)
single_input = trace(condense(Optional(line) - ZeroOrMore(newline)))
file_input = trace(condense(moduledoc_marker - ZeroOrMore(line)))
eval_input = trace(condense(testlist - ZeroOrMore(newline)))
single_parser = condense(start_marker - single_input - end_marker)
file_parser = condense(start_marker - file_input - end_marker)
eval_parser = condense(start_marker - eval_input - end_marker)
# end: MAIN GRAMMAR
# -----------------------------------------------------------------------------------------------------------------------
# EXTRA GRAMMAR:
# -----------------------------------------------------------------------------------------------------------------------
parens = originalTextFor(nestedExpr("(", ")"))
brackets = originalTextFor(nestedExpr("[", "]"))
braces = originalTextFor(nestedExpr("{", "}"))
any_char = Regex(r".", re.U | re.DOTALL)
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()
yield_classic = addspace(keyword("yield") + Optional(testlist))
yield_from_ref = keyword("yield").suppress() + keyword("from").suppress() + test
yield_expr = yield_from | yield_classic
dict_comp_ref = lbrace.suppress() + (test + colon.suppress() + test | dubstar_expr) + comp_for + rbrace.suppress()
dict_item = condense(
lbrace
+ Optional(
itemlist(
addspace(condense(test + colon) + test) | dubstar_expr,
comma,
),
match_lazy = lbanana + matchlist_list + rbanana.suppress()
series_match = (
(match_list + plus.suppress() + name + plus.suppress() + match_list)("mseries")
| (match_tuple + plus.suppress() + name + plus.suppress() + match_tuple)("mseries")
| ((match_list | match_tuple) + Optional(plus.suppress() + name))("series")
| (name + plus.suppress() + (match_list | match_tuple))("rseries")
)
iter_match = (
((match_list | match_tuple | match_lazy) + unsafe_dubcolon.suppress() + name)
| match_lazy
)("iter")
star_match = (
lbrack.suppress() + matchlist_star + rbrack.suppress()
| lparen.suppress() + matchlist_star + rparen.suppress()
)("star")
base_match = trace(
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)