Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 match_mstring(self, tokens, item, use_bytes=None):
"""Match prefix and suffix string."""
prefix, name, suffix = tokens
if use_bytes is None:
if prefix.startswith("b") or suffix.startswith("b"):
if prefix.startswith("b") and suffix.startswith("b"):
use_bytes = True
else:
raise CoconutDeferredSyntaxError("string literals and byte literals cannot be added in patterns", self.loc)
if use_bytes:
self.add_check("_coconut.isinstance(" + item + ", _coconut.bytes)")
else:
self.add_check("_coconut.isinstance(" + item + ", _coconut.str)")
if prefix is not None:
self.add_check(item + ".startswith(" + prefix + ")")
if suffix is not None:
self.add_check(item + ".endswith(" + suffix + ")")
if name != wildcard:
self.add_def(
name + " = " + item + "["
+ ("" if prefix is None else "_coconut.len(" + prefix + ")") + ":"
+ ("" if suffix is None else "-_coconut.len(" + suffix + ")") + "]",
)
def split_args_list(tokens, loc):
"""Splits function definition arguments."""
pos_only_args = []
req_args = []
def_args = []
star_arg = None
kwd_args = []
dubstar_arg = None
pos = 0
for arg in tokens:
if len(arg) == 1:
if arg[0] == "*":
# star sep (pos = 3)
if pos >= 3:
raise CoconutDeferredSyntaxError("star separator at invalid position in function definition", loc)
pos = 3
elif arg[0] == "/":
# slash sep (pos = 0)
if pos > 0:
raise CoconutDeferredSyntaxError("slash separator at invalid position in function definition", loc)
if pos_only_args:
raise CoconutDeferredSyntaxError("only one slash separator allowed in function definition", loc)
if not req_args:
raise CoconutDeferredSyntaxError("slash separator must come after arguments to mark as positional-only")
pos_only_args = req_args
req_args = []
else:
# pos arg (pos = 0)
if pos > 0:
raise CoconutDeferredSyntaxError("positional arguments must come first in function definition", loc)
req_args.append(arg[0])
def_args = []
star_arg = None
kwd_args = []
dubstar_arg = None
pos = 0
for arg in tokens:
if len(arg) == 1:
if arg[0] == "*":
# star sep (pos = 3)
if pos >= 3:
raise CoconutDeferredSyntaxError("star separator at invalid position in function definition", loc)
pos = 3
elif arg[0] == "/":
# slash sep (pos = 0)
if pos > 0:
raise CoconutDeferredSyntaxError("slash separator at invalid position in function definition", loc)
if pos_only_args:
raise CoconutDeferredSyntaxError("only one slash separator allowed in function definition", loc)
if not req_args:
raise CoconutDeferredSyntaxError("slash separator must come after arguments to mark as positional-only")
pos_only_args = req_args
req_args = []
else:
# pos arg (pos = 0)
if pos > 0:
raise CoconutDeferredSyntaxError("positional arguments must come first in function definition", loc)
req_args.append(arg[0])
elif len(arg) == 2:
if arg[0] == "*":
# star arg (pos = 2)
if pos >= 2:
raise CoconutDeferredSyntaxError("star argument at invalid position in function definition", loc)
kwd_args = []
dubstar_arg = None
pos = 0
for arg in tokens:
if len(arg) == 1:
if arg[0] == "*":
# star sep (pos = 3)
if pos >= 3:
raise CoconutDeferredSyntaxError("star separator at invalid position in function definition", loc)
pos = 3
elif arg[0] == "/":
# slash sep (pos = 0)
if pos > 0:
raise CoconutDeferredSyntaxError("slash separator at invalid position in function definition", loc)
if pos_only_args:
raise CoconutDeferredSyntaxError("only one slash separator allowed in function definition", loc)
if not req_args:
raise CoconutDeferredSyntaxError("slash separator must come after arguments to mark as positional-only")
pos_only_args = req_args
req_args = []
else:
# pos arg (pos = 0)
if pos > 0:
raise CoconutDeferredSyntaxError("positional arguments must come first in function definition", loc)
req_args.append(arg[0])
elif len(arg) == 2:
if arg[0] == "*":
# star arg (pos = 2)
if pos >= 2:
raise CoconutDeferredSyntaxError("star argument at invalid position in function definition", loc)
pos = 2
star_arg = arg[1]
raise CoconutDeferredSyntaxError("star separator at invalid position in function definition", loc)
pos = 3
elif arg[0] == "/":
# slash sep (pos = 0)
if pos > 0:
raise CoconutDeferredSyntaxError("slash separator at invalid position in function definition", loc)
if pos_only_args:
raise CoconutDeferredSyntaxError("only one slash separator allowed in function definition", loc)
if not req_args:
raise CoconutDeferredSyntaxError("slash separator must come after arguments to mark as positional-only")
pos_only_args = req_args
req_args = []
else:
# pos arg (pos = 0)
if pos > 0:
raise CoconutDeferredSyntaxError("positional arguments must come first in function definition", loc)
req_args.append(arg[0])
elif len(arg) == 2:
if arg[0] == "*":
# star arg (pos = 2)
if pos >= 2:
raise CoconutDeferredSyntaxError("star argument at invalid position in function definition", loc)
pos = 2
star_arg = arg[1]
elif arg[0] == "**":
# dub star arg (pos = 4)
if pos == 4:
raise CoconutDeferredSyntaxError("double star argument at invalid position in function definition", loc)
pos = 4
dubstar_arg = arg[1]
else:
# def arg (pos = 1)
def comp_pipe_handle(loc, tokens):
"""Process pipe function composition."""
internal_assert(len(tokens) >= 3 and len(tokens) % 2 == 1, "invalid composition pipe tokens", tokens)
funcs = [tokens[0]]
stars_per_func = []
direction = None
for i in range(1, len(tokens), 2):
op, fn = tokens[i], tokens[i + 1]
new_direction, stars = pipe_info(op)
if direction is None:
direction = new_direction
elif new_direction != direction:
raise CoconutDeferredSyntaxError("cannot mix function composition pipe operators with different directions", loc)
funcs.append(fn)
stars_per_func.append(stars)
if direction == "backwards":
funcs.reverse()
stars_per_func.reverse()
func = funcs.pop(0)
funcstars = zip(funcs, stars_per_func)
return "_coconut_base_compose(" + func + ", " + ", ".join(
"(%s, %s)" % (f, star) for f, star in funcstars
) + ")"
internal_assert(len(arg) == 1)
star, argname = True, arg[0]
elif "type" in arg:
internal_assert(len(arg) == 2)
argname, typedef = arg
elif "type default" in arg:
internal_assert(len(arg) == 3)
argname, typedef, default = arg
else:
raise CoconutInternalException("invalid data arg tokens", arg)
if argname.startswith("_"):
raise CoconutDeferredSyntaxError("data fields cannot start with an underscore", loc)
if star:
if i != len(original_args) - 1:
raise CoconutDeferredSyntaxError("starred data field must come last", loc)
starred_arg = argname
else:
if default:
saw_defaults = True
elif saw_defaults:
raise CoconutDeferredSyntaxError("data fields with defaults must come after data fields without", loc)
else:
req_args += 1
base_args.append(argname)
if typedef:
internal_assert(not star, "invalid typedef in starred data field", typedef)
types[i] = typedef
arg_str = ("*" if star else "") + argname + ("=" + default if default else "")
all_args.append(arg_str)
attr_str = " ".join(base_args)
if pos_only_args:
raise CoconutDeferredSyntaxError("only one slash separator allowed in function definition", loc)
if not req_args:
raise CoconutDeferredSyntaxError("slash separator must come after arguments to mark as positional-only")
pos_only_args = req_args
req_args = []
else:
# pos arg (pos = 0)
if pos > 0:
raise CoconutDeferredSyntaxError("positional arguments must come first in function definition", loc)
req_args.append(arg[0])
elif len(arg) == 2:
if arg[0] == "*":
# star arg (pos = 2)
if pos >= 2:
raise CoconutDeferredSyntaxError("star argument at invalid position in function definition", loc)
pos = 2
star_arg = arg[1]
elif arg[0] == "**":
# dub star arg (pos = 4)
if pos == 4:
raise CoconutDeferredSyntaxError("double star argument at invalid position in function definition", loc)
pos = 4
dubstar_arg = arg[1]
else:
# def arg (pos = 1)
if pos <= 1:
pos = 1
def_args.append((arg[0], arg[1]))
# kwd arg (pos = 3)
elif pos <= 3:
pos = 3
else:
# pos arg (pos = 0)
if pos > 0:
raise CoconutDeferredSyntaxError("positional arguments must come first in function definition", loc)
req_args.append(arg[0])
elif len(arg) == 2:
if arg[0] == "*":
# star arg (pos = 2)
if pos >= 2:
raise CoconutDeferredSyntaxError("star argument at invalid position in function definition", loc)
pos = 2
star_arg = arg[1]
elif arg[0] == "**":
# dub star arg (pos = 4)
if pos == 4:
raise CoconutDeferredSyntaxError("double star argument at invalid position in function definition", loc)
pos = 4
dubstar_arg = arg[1]
else:
# def arg (pos = 1)
if pos <= 1:
pos = 1
def_args.append((arg[0], arg[1]))
# kwd arg (pos = 3)
elif pos <= 3:
pos = 3
kwd_args.append((arg[0], arg[1]))
else:
raise CoconutDeferredSyntaxError("invalid default argument in function definition", loc)
else:
raise CoconutInternalException("invalid function definition argument", arg)
return pos_only_args, req_args, def_args, star_arg, kwd_args, dubstar_arg