Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def cast(self, t):
r = self.types & t
if not r:
raise HplTypeError("expected ({}) but found ({}): {}".format(
type_name(t), type_name(self.types), self))
self.types = r
def _parse_property(self, text, topic_types, pns=""):
ast = self.property_parser.parse(text)
refs = {}
events = (ast.scope.activator, ast.pattern.trigger,
ast.pattern.behaviour, ast.scope.terminator)
for event in events:
if event is None or not event.is_publish:
continue
topic = RosName.resolve(event.topic, private_ns=pns)
if topic not in topic_types:
raise HplTypeError(
"No type information for topic '{}'".format(topic))
rostype = topic_types[topic]
if event.alias:
assert event.alias not in refs, "duplicate event alias"
assert rostype.is_message, "topic type is not a message"
# update refs without worries about temporal order of events
# prop.sanity_check() already checks that
refs[event.alias] = rostype
for event in events:
if event is None or not event.is_publish:
continue
topic = RosName.resolve(event.topic, private_ns=pns)
rostype = topic_types[topic]
event.predicate.refine_types(rostype, **refs)
return ast
def __init__(self, expr):
if not expr.is_expression:
raise TypeError("not an expression: " + str(expr))
if not expr.can_be_bool:
raise HplTypeError("not a boolean expression: " + str(expr))
self.condition = expr
self._static_checks()
expr = expr.message
assert expr.is_value and (expr.is_this_msg or expr.is_variable)
if expr.is_this_msg:
t = rostype
else:
if expr.name not in kwargs:
raise HplSanityError(
"undefined message alias: '{}'".format(expr.name))
t = kwargs[expr.name]
assert t.is_message
expr.ros_type = t
while stack:
expr = stack.pop()
if expr.is_field:
if not t.is_message or expr.field not in t.fields:
raise HplTypeError.ros_field(t, expr.field, expr)
t = t.fields[expr.field]
else:
assert expr.is_indexed
if not t.is_array:
raise HplTypeError.ros_array(t, expr)
i = expr.index
if (i.is_value and i.is_literal
and not t.contains_index(i.value)):
raise HplTypeError.ros_index(t, expr.index, expr)
t = t.type_token
if t.is_message:
accessor._type_check(expr, T_MSG)
elif t.is_number:
accessor._type_check(expr, T_NUM)
# TODO check that values fit within types
elif t.is_bool:
def rem_type(self, t):
self.types = self.types & ~t
if not self.types:
raise HplTypeError("no types left: " + str(self))
def _parse_assumption(self, text, topic_types, pns=""):
ast = self.assumption_parser.parse(text)
# assumptions, like events, also have .topic and .predicate
topic = RosName.resolve(ast.topic, private_ns=pns)
if topic not in topic_types:
raise HplTypeError(
"No type information for topic '{}'".format(topic))
ast.predicate.refine_types(topic_types[topic])
return ast
assert t.is_message
expr.ros_type = t
while stack:
expr = stack.pop()
if expr.is_field:
if not t.is_message or expr.field not in t.fields:
raise HplTypeError.ros_field(t, expr.field, expr)
t = t.fields[expr.field]
else:
assert expr.is_indexed
if not t.is_array:
raise HplTypeError.ros_array(t, expr)
i = expr.index
if (i.is_value and i.is_literal
and not t.contains_index(i.value)):
raise HplTypeError.ros_index(t, expr.index, expr)
t = t.type_token
if t.is_message:
accessor._type_check(expr, T_MSG)
elif t.is_number:
accessor._type_check(expr, T_NUM)
# TODO check that values fit within types
elif t.is_bool:
accessor._type_check(expr, T_BOOL)
elif t.is_string:
accessor._type_check(expr, T_STR)
elif t.is_array:
accessor._type_check(expr, T_ARR)
expr.ros_type = t
def parse_config_specs(self, config):
# changes config.hpl_properties and config.hpl_assumptions
# outputs only what can be parsed and type checked
# might end up with fewer properties or none at all
topic_types = self._get_config_topics(config)
specs = []
for text in config.hpl_properties:
assert isinstance(text, basestring)
try:
ast = self._parse_property(text, topic_types)
specs.append(ast)
except (HplSanityError, HplTypeError) as e:
self.log.error(
("Error in configuration '%s' when parsing property\n"
"'%s'\n\n%s"), config.name, text, e)
config.hpl_properties = specs
specs = {}
for text in config.hpl_assumptions:
assert isinstance(text, basestring)
try:
ast = self._parse_assumption(text, topic_types)
if ast.topic in specs:
specs[ast.topic] = False
self.log.error("Multiple assumptions for '%s'", ast.topic)
else:
specs[ast.topic] = ast
except (HplSanityError, HplTypeError) as e:
self.log.error(
def __init__(self, fun, args):
try:
tin, tout = self._BUILTINS[fun]
except KeyError:
raise HplTypeError("undefined function '{}'".format(fun))
HplExpression.__init__(self, types=tout)
self.function = fun # string
self.arguments = args # [HplValue]
for arg in args:
self._type_check(arg, tin)