Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def minify(compiled):
"""Perform basic minifications.
Fails on non-tabideal indentation or a string with a #.
"""
compiled = compiled.strip()
if compiled:
out = []
for line in compiled.splitlines():
line = line.split("#", 1)[0].rstrip()
if line:
ind = 0
while line.startswith(" "):
line = line[1:]
ind += 1
internal_assert(ind % tabideal == 0, "invalid indentation in", line)
out.append(" " * (ind // tabideal) + line)
compiled = "\n".join(out) + "\n"
return compiled
def attrgetter_atom_split(tokens):
"""Split attrgetter_atom_tokens into (attr_or_method_name, method_args_or_none_if_attr)."""
if len(tokens) == 1: # .attr
return tokens[0], None
elif len(tokens) >= 2 and tokens[1] == "(": # .method(...
if len(tokens) == 2: # .method()
return tokens[0], ""
elif len(tokens) == 3: # .method(args)
return tokens[0], tokens[2]
else:
raise CoconutInternalException("invalid methodcaller literal tokens", tokens)
else:
raise CoconutInternalException("invalid attrgetter literal tokens", tokens)
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
)
def attrgetter_atom_handle(loc, tokens):
"""Process attrgetter literals."""
name, args = attrgetter_atom_split(tokens)
if args is None:
return '_coconut.operator.attrgetter("' + name + '")'
elif "." in name:
raise CoconutDeferredSyntaxError("cannot have attribute access in implicit methodcaller partial", loc)
elif args == "":
return '_coconut.operator.methodcaller("' + tokens[0] + '")'
else:
return '_coconut.operator.methodcaller("' + tokens[0] + '", ' + tokens[2] + ")"
def parse(self, inputstring, parser, preargs, postargs):
"""Use the parser to parse the inputstring with appropriate setup and teardown."""
self.reset()
pre_procd = None
with logger.gather_parsing_stats():
try:
pre_procd = self.pre(inputstring, **preargs)
parsed = parse(parser, pre_procd)
out = self.post(parsed, **postargs)
except ParseBaseException as err:
raise self.make_parse_err(err)
except CoconutDeferredSyntaxError as err:
internal_assert(pre_procd is not None, "invalid deferred syntax error in pre-processing", err)
raise self.make_syntax_err(err, pre_procd)
except RuntimeError as err:
raise CoconutException(
str(err), extra="try again with --recursion-limit greater than the current "
+ str(sys.getrecursionlimit()),
)
if self.strict:
for name in self.unused_imports:
if name != "*":
logger.warn("found unused import", name, extra="disable --strict to dismiss")
return out
),
)
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)
if_stmt = condense(
addspace(keyword("if") - condense(namedexpr_test - suite))
- ZeroOrMore(addspace(keyword("elif") - condense(namedexpr_test - suite)))
- Optional(else_stmt),
)
while_stmt = addspace(keyword("while") - condense(namedexpr_test - suite - Optional(else_stmt)))
for_stmt = addspace(keyword("for") - assignlist - keyword("in") - condense(testlist - suite - Optional(else_stmt)))
except_clause = attach(
keyword("except").suppress() + (
testlist_has_comma("list") | test("test")
) - Optional(keyword("as").suppress() - name),
except_handle,
)
try_stmt = condense(
keyword("try") - suite + (
keyword("finally") - suite
| (
OneOrMore(except_clause - suite) - Optional(keyword("except") - suite)
| keyword("except") - suite
) - Optional(else_stmt) - Optional(keyword("finally") - suite)
),
)
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)