Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
subscriptlist = itemlist(subscript, comma, suppress_trailing=False)
slicetestgroup = Optional(test_no_chain, default="")
sliceopgroup = unsafe_colon.suppress() + slicetestgroup
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
| (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)
decoratable_normal_funcdef_stmt = Forward()
normal_funcdef_stmt = (
funcdef
| math_funcdef
| math_match_funcdef
| match_funcdef
)
)
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:")
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)
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:")
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
passthrough = Combine(backslash + integer + unwrap)
passthrough_block = Combine(fixto(dubbackslash, "\\") + integer + unwrap)
endline = Forward()
endline_ref = condense(OneOrMore(Literal("\n")))
lineitem = Combine(Optional(comment) + endline)
newline = condense(OneOrMore(lineitem))
start_marker = StringStart()
moduledoc_marker = condense(ZeroOrMore(lineitem) - Optional(moduledoc_item))
end_marker = StringEnd()
indent = Literal(openindent)
dedent = Literal(closeindent)
u_string = Forward()
f_string = Forward()
bit_b = Optional(CaselessLiteral("b"))
raw_r = Optional(CaselessLiteral("r"))
b_string = Combine((bit_b + raw_r | raw_r + bit_b) + string_item)
unicode_u = CaselessLiteral("u").suppress()
u_string_ref = Combine((unicode_u + raw_r | raw_r + unicode_u) + string_item)
format_f = CaselessLiteral("f").suppress()
f_string_ref = Combine((format_f + raw_r | raw_r + format_f) + string_item)
string = trace(b_string | u_string | f_string)
moduledoc = string + newline
docstring = condense(moduledoc)
augassign = (
Combine(pipe + equals)
| Combine(back_pipe + equals)
| Combine(star_pipe + equals)
| Combine(back_star_pipe + equals)
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()
+ (
Group(OneOrMore(simple_stmt_item + semicolon.suppress())) + Optional(closing_stmt)
| Group(ZeroOrMore(simple_stmt_item + semicolon.suppress())) + closing_stmt
)
)
| 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()
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(
ellipsis_ref = Literal("...") | Literal("\u2026")
lt = ~Literal("<<") + ~Literal("<=") + ~Literal("<..") + Literal("<")
gt = ~Literal(">>") + ~Literal(">=") + Literal(">")
le = Literal("<=") | fixto(Literal("\u2264"), "<=")
ge = Literal(">=") | fixto(Literal("\u2265"), ">=")
ne = Literal("!=") | fixto(Literal("\xac=") | Literal("\u2260"), "!=")
mul_star = star | fixto(Literal("\xd7"), "*")
exp_dubstar = dubstar | fixto(Literal("\u2191"), "**")
neg_minus = minus | fixto(Literal("\u207b"), "-")
sub_minus = minus | fixto(Literal("\u2212"), "-")
div_slash = slash | fixto(Literal("\xf7") + ~slash, "/")
div_dubslash = dubslash | fixto(Combine(Literal("\xf7") + slash), "//")
matrix_at_ref = at | fixto(Literal("\u22c5"), "@")
matrix_at = Forward()
test = Forward()
test_no_chain, dubcolon = disable_inside(test, unsafe_dubcolon)
test_no_infix, backtick = disable_inside(test, unsafe_backtick)
name = Forward()
base_name = Regex(r"\b(?![0-9])\w+\b", re.U)
for k in keywords + const_vars:
base_name = ~keyword(k) + base_name
for k in reserved_vars:
base_name |= backslash.suppress() + keyword(k)
dotted_base_name = condense(base_name + ZeroOrMore(dot + base_name))
dotted_name = condense(name + ZeroOrMore(dot + name))
integer = Combine(Word(nums) + ZeroOrMore(underscore.suppress() + Word(nums)))
binint = Combine(Word("01") + ZeroOrMore(underscore.suppress() + Word("01")))
| set_literal
| set_letter_literal
| lazy_list,
)
func_atom = (
name
| op_atom
| paren_atom
)
atom = (
known_atom
| passthrough_atom
| func_atom
)
typedef_atom = Forward()
typedef_atom_ref = ( # use special type signifier for item_handle
Group(fixto(lbrack + rbrack, "type:[]"))
| Group(fixto(dollar + lbrack + rbrack, "type:$[]"))
| Group(fixto(questionmark + ~questionmark, "type:?"))
)
simple_trailer = (
condense(lbrack + subscriptlist + rbrack)
| condense(dot + name)
)
call_trailer = (
function_call
| Group(dollar + ~lparen + ~lbrack + ~questionmark) # keep $ for item_handle
)
known_trailer = typedef_atom | (
Group(condense(dollar + lbrack) + subscriptgroup + rbrack.suppress()) # $[
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,
),
)
case_stmt_ref = (
keyword("case").suppress() + test - colon.suppress() - newline.suppress()
- indent.suppress() - Group(OneOrMore(case_match))
- dedent.suppress() - Optional(keyword("else").suppress() - suite)
)
exec_stmt = Forward()
assert_stmt = addspace(keyword("assert") - testlist)