Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_infix_items(tokens, callback=infix_error):
"""Performs infix token processing."""
if len(tokens) < 3:
raise CoconutException("invalid infix tokens", tokens)
else:
items = []
for item in tokens[0]:
items.append(item)
for item in tokens[2]:
items.append(item)
if len(tokens) > 3:
items.append(callback([[]]+tokens[3:]))
args = []
for arg in items:
if arg:
args.append(arg)
return tokens[1], args
def match_proc(tokens):
"""Processes match blocks."""
if len(tokens) == 3:
matches, item, stmts = tokens
cond = None
elif len(tokens) == 4:
matches, item, cond, stmts = tokens
else:
raise CoconutException("invalid outer match tokens", tokens)
matching = matcher()
matching.match(matches, match_to_var)
if cond:
matching.increment(True)
matching.add_check(cond)
out = match_check_var + " = False\n"
out += match_to_var + " = " + item + "\n"
out += matching.out()
if stmts is not None:
out += "if "+match_check_var+":" + "\n" + openindent + "".join(stmts) + closeindent
return out
def match_trailer(self, original, item):
"""Matches typedefs and as patterns."""
if len(original) <= 1 or len(original) % 2 != 1:
raise CoconutException("invalid trailer match tokens", original)
else:
match, trailers = original[0], original[1:]
for i in range(0, len(trailers), 2):
op, arg = trailers[i], trailers[i+1]
if op == "is":
self.checks.append("_coconut.isinstance("+item+", "+arg+")")
elif op == "as":
if arg in self.names:
self.checks.append(self.names[arg]+" == "+item)
elif arg != wildcard:
self.defs.append(arg+" = "+item)
self.names[arg] = item
else:
raise CoconutException("invalid trailer match operation", op)
self.match(match, item)
def post(self, tokens, **kwargs):
"""Performs post-processing."""
if len(tokens) == 1:
out = tokens[0]
for proc in self.postprocs:
out = proc(out, **kwargs)
self.todebug(proc.__name__, out)
return out
else:
raise CoconutException("multiple tokens leftover", tokens)
def get_ref(self, index):
"""Retrieves a reference."""
try:
return self.refs[int(index)]
except (IndexError, ValueError):
raise CoconutException("invalid reference", index)
def full_match_funcdef_proc(tokens):
"""Processes full match function definition."""
if len(tokens) == 2:
return tokens[0] + "".join(tokens[1]) + closeindent
else:
raise CoconutException("invalid pattern-matching function definition tokens", tokens)
def get_infix_items(tokens, callback=infix_error):
"""Performs infix token processing."""
if len(tokens) < 3:
raise CoconutException("invalid infix tokens", tokens)
else:
items = []
for item in tokens[0]:
items.append(item)
for item in tokens[2]:
items.append(item)
if len(tokens) > 3:
items.append(callback([[]]+tokens[3:]))
args = []
for arg in items:
if arg:
args.append(arg)
return tokens[1], args
def string_repl(self, tokens):
"""Replaces string references."""
if len(tokens) == 1:
ref = self.refs[int(tokens[0])]
if isinstance(ref, tuple):
string, strchar, multiline = ref
if multiline:
string = strchar*3+string+strchar*3
else:
string = strchar+string+strchar
return string
else:
raise CoconutException("string marker points to comment/passthrough")
else:
raise CoconutException("invalid string marker", tokens)
def endline_handle(self, original, location, tokens):
"""Inserts line number comments when in linenumbers mode."""
if len(tokens) != 1:
raise CoconutException("invalid endline tokens", tokens)
out = tokens[0]
if self.minify:
out = out[0]
if self.linenumbers:
out = self.wrap_linenumber(self.adjust(lineno(location, original))) + out
return out
def set_letter_literal_convert(self, tokens):
"""Processes set literals."""
if len(tokens) == 1:
set_type = tokens[0]
if set_type == "s":
return "__builtins__.set()"
elif set_type == "f":
return "__builtins__.frozenset()"
else:
raise CoconutException("invalid set type", set_type)
elif len(tokens) == 2:
set_type, set_items = tokens
if len(set_items) != 1:
raise CoconutException("invalid set literal item", tokens[0])
elif set_type == "s":
if self.version == "3":
return "{" + set_items[0] + "}"
else:
return "__builtins__.set(" + set_to_tuple(set_items) + ")"
elif set_type == "f":
return "__builtins__.frozenset(" + set_to_tuple(set_items) + ")"
else:
raise CoconutException("invalid set type", set_type)
else:
raise CoconutException("invalid set literal tokens", tokens)