Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
    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)
Esempio n. 4
0
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)
Esempio n. 5
0
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)
Esempio n. 6
0
    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
Esempio n. 7
0
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
Esempio n. 8
0
    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
Esempio n. 9
0
    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,))
Esempio n. 10
0
    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, ))
Esempio n. 11
0
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), ())
Esempio n. 12
0
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), ())
Esempio n. 13
0
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), ())