def __solve_for_repeated(expr, vars): """Helper: solve 'expr' always returning an IRepeated. If the result of solving 'expr' is a list or a tuple of IStructured objects then treat is as a repeated value of IStructured objects because that's what the called meant to do. This is a convenience helper so users of the API don't have to create IRepeated objects. If the result of solving 'expr' is a scalar then return it as a repeated value of one element. Arguments: expr: Expression to solve. vars: The scope. Returns: IRepeated result of solving 'expr'. A booelan to indicate whether the original was repeating. """ var = solve(expr, vars).value if (var and isinstance(var, (tuple, list)) and protocol.implements(var[0], structured.IStructured)): return repeated.meld(*var), False return var, repeated.isrepeating(var)
def __init__(self, other): super(dict, self).__init__() if not protocol.implements(other, structured.IStructured): raise TypeError("Can only set scope from IStructured.") # Copy the scope locally. for key in structured.getmembers_runtime(other): self[key] = structured.resolve(other, key)
def __nest_scope(expr, outer, inner): try: return scope.ScopeStack(outer, inner) except TypeError: if protocol.implements(inner, applicative.IApplicative): raise errors.EfilterTypeError( root=expr, query=expr.source, message="Attempting to use a function %r as an object." % inner) raise errors.EfilterTypeError( root=expr, query=expr.source, message="Attempting to use %r as an object (IStructured)." % inner)
def __init__(self, *scopes): flattened_scopes = [] for scope in scopes: if isinstance(scope, type(self)): flattened_scopes.extend(scope.scopes) elif isinstance(scope, type): flattened_scopes.append(scope) elif protocol.implements(scope, structured.IStructured): flattened_scopes.append(scope) else: raise TypeError("Scopes must be instances or subclasses of " "IStructured; got %r." % (scope,)) self.scopes = flattened_scopes
def __within_lhs_as_repeated(lhs_expr, vars): """Map/Filter/others support lists and IRepeated on the LHS. If the value of 'lhs_expr' is a list or tuple of IAssociative objects then treat it as an IRepeated of IAssociative objects because that is what the caller meant to do. This is a convenience so that users don't have to create IRepeated objects. """ var = solve(lhs_expr, vars).value if (var and isinstance(var, (tuple, list)) and protocol.implements(var[0], associative.IAssociative)): return repeated.meld(*var) return var
def __init__(self, *scopes): flattened_scopes = [] for scope in scopes: if isinstance(scope, type(self)): flattened_scopes.extend(scope.scopes) elif isinstance(scope, type): flattened_scopes.append(scope) elif protocol.implements(scope, structured.IStructured): flattened_scopes.append(scope) else: raise TypeError("Scopes must be instances or subclasses of " "IStructured; got %r." % (scope, )) self.scopes = flattened_scopes
def visit_Membership(self, expr, **kwargs): symbols = set() lha = self.visit(expr.lhs, **kwargs) rha = self.visit(expr.rhs, **kwargs) symbols.update(lha.symbols) symbols.update(rha.symbols) if (not isinstance(expr.rhs, expression.Literal) or not isinstance(expr.lhs, expression.Binding)): return Analysis(symbols, ()) if not protocol.implements(expr.rhs.value, iset.ISet): # Yup, no can do. raise errors.EfilterTypeError(root=expr.rhs, query=self.query, actual=type(expr.rhs.value), expected=iset.ISet) return Analysis(symbols, (expr.lhs.value,))
def visit_Membership(self, expr, **kwargs): symbols = set() lha = self.visit(expr.lhs, **kwargs) rha = self.visit(expr.rhs, **kwargs) symbols.update(lha.symbols) symbols.update(rha.symbols) if (not isinstance(expr.rhs, expression.Literal) or not isinstance(expr.lhs, expression.Binding)): return Analysis(symbols, ()) if not protocol.implements(expr.rhs.value, iset.ISet): # Yup, no can do. raise errors.EfilterTypeError(root=expr.rhs, query=self.query, actual=type(expr.rhs.value), expected=iset.ISet) return Analysis(symbols, (expr.lhs.value, ))
def solve_isinstance(expr, vars): """Typecheck whether LHS is type on the RHS.""" lhs = solve(expr.lhs, vars) try: t = solve(expr.rhs, vars).value except errors.EfilterKeyError: t = None if t is None: raise errors.EfilterTypeError( root=expr.rhs, query=expr.source, message="Cannot find type named %r." % expr.rhs.value) if not isinstance(t, type): raise errors.EfilterTypeError( root=expr.rhs, query=expr.source, message="%r is not a type and cannot be used with 'isa'." % (t,)) return Result(protocol.implements(lhs.value, t), ())
def solve_isinstance(expr, vars): """Typecheck whether LHS is type on the RHS.""" lhs = solve(expr.lhs, vars) try: t = solve(expr.rhs, vars).value except errors.EfilterKeyError: t = None if t is None: raise errors.EfilterTypeError(root=expr.rhs, query=expr.source, message="Cannot find type named %r." % expr.rhs.value) if not isinstance(t, type): raise errors.EfilterTypeError( root=expr.rhs, query=expr.source, message="%r is not a type and cannot be used with 'isa'." % (t, )) return Result(protocol.implements(lhs.value, t), ())
def solve_isinstance(expr, vars): """Typecheck whether LHS is type on the RHS.""" lhs = solve(expr.lhs, vars) t = structured.reflect(vars, expr.rhs) return Result(protocol.implements(lhs.value, t), ())