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 get_ref(self, reftype, index):
"""Retrieve a reference."""
try:
got_reftype, data = self.refs[int(index)]
except (IndexError, ValueError):
raise CoconutInternalException("no reference at invalid index", index)
internal_assert(got_reftype == reftype, "wanted " + reftype + " reference; got " + got_reftype + " reference")
return data
def math_funcdef_handle(tokens):
"""Process assignment function definition."""
internal_assert(len(tokens) == 2, "invalid assignment function definition tokens", tokens)
return tokens[0] + ("" if tokens[1].startswith("\n") else " ") + tokens[1]
def set_letter_literal_handle(self, tokens):
"""Process set literals."""
if len(tokens) == 1:
set_type = tokens[0]
if set_type == "s":
return "_coconut.set()"
elif set_type == "f":
return "_coconut.frozenset()"
else:
raise CoconutInternalException("invalid set type", set_type)
elif len(tokens) == 2:
set_type, set_items = tokens
internal_assert(len(set_items) == 1, "invalid set literal item", tokens[0])
if set_type == "s":
return self.set_literal_handle([set_items])
elif set_type == "f":
return "_coconut.frozenset(" + set_to_tuple(set_items) + ")"
else:
raise CoconutInternalException("invalid set type", set_type)
else:
raise CoconutInternalException("invalid set literal tokens", tokens)
def parse_package(self, inputstring, package_level=0, addhash=True):
"""Parse package code."""
internal_assert(package_level >= 0, "invalid package level", package_level)
if addhash:
use_hash = self.genhash(inputstring, package_level)
else:
use_hash = None
return self.parse(inputstring, self.file_parser, {"nl_at_eof_check": True}, {"header": "package:" + str(package_level), "use_hash": use_hash})
def with_stmt_handle(self, tokens):
"""Process with statements."""
internal_assert(len(tokens) == 2, "invalid with statement tokens", tokens)
withs, body = tokens
if len(withs) == 1 or self.target_info >= (2, 7):
return "with " + ", ".join(withs) + body
else:
return (
"".join("with " + expr + ":\n" + openindent for expr in withs[:-1])
+ "with " + withs[-1] + body
+ closeindent * (len(withs) - 1)
)
found = None # store of characters that might be the start of a string
hold = None
# hold = [_comment]:
_comment = 0 # the contents of the comment so far
# hold = [_contents, _start, _stop]:
_contents = 0 # the contents of the string so far
_start = 1 # the string of characters that started the string
_stop = 2 # store of characters that might be the end of the string
skips = self.copy_skips()
x = 0
while x <= len(inputstring):
try:
c = inputstring[x]
except IndexError:
internal_assert(x == len(inputstring), "invalid index in str_proc", x)
c = "\n"
if hold is not None:
if len(hold) == 1: # hold == [_comment]
if c == "\n":
if self.minify:
if out:
lines = "".join(out).splitlines()
lines[-1] = lines[-1].rstrip()
out = ["\n".join(lines)]
out.append(c)
else:
out.append(self.wrap_comment(hold[_comment], reformat=False) + c)
hold = None
else:
hold[_comment] += c
def class_suite_handle(tokens):
"""Process implicit pass in class suite."""
internal_assert(len(tokens) == 1, "invalid implicit pass in class suite tokens", tokens)
return ": pass" + tokens[0]
def check_strict(self, name, original, loc, tokens):
"""Check that syntax meets --strict requirements."""
internal_assert(len(tokens) == 1, "invalid " + name + " tokens", tokens)
if self.strict:
raise self.make_err(CoconutStyleError, "found " + name, original, loc)
else:
return tokens[0]