Пример #1
0
 def beginReplacer(expr):
     if isinstance(expr, Ref):
         if expr.name in ("name", "instance",
                          "metadata") or (version is not None
                                          and expr.name == "version"):
             return CellGet(expr.name, [], expr.pos)
         else:
             return expr
     else:
         return genericReplacer(expr, beginReplacer)
Пример #2
0
 def actionReplacer(expr):
     if isinstance(expr, Ref):
         if expr.name in ("name", "instance", "metadata",
                          "actionsStarted", "actionsFinished") or (
                              version is not None
                              and expr.name == "version"):
             return CellGet(expr.name, [], expr.pos)
         else:
             return expr
     else:
         return genericReplacer(expr, actionReplacer)
Пример #3
0
 def genericReplacer(expr, self):
     if isinstance(expr, FcnDef):
         return FcnDef(
             [{
                 list(t.keys())[0]: newPlaceholder(
                     i,
                     list(t.values())[0])
             } for t in expr.params],
             newPlaceholder(i, expr.ret),
             [
                 x.replace(lazyFcnReplacer) for x in expr.body
             ],  # this is the one place where we should pass down fcnReplacer rather than self
             expr.pos)
     elif isinstance(expr, FcnRef):
         return FcnRef(prefixFcnRef(i, pfa, expr.name), expr.pos)
     elif isinstance(expr, FcnRefFill):
         return FcnRefFill(
             prefixFcnRef(i, pfa, expr.name),
             dict((k, v.replace(self))
                  for k, v in list(expr.fill.items())), expr.pos)
     elif isinstance(
             expr, CallUserFcn
     ):  # TODO: need to change the symbols of the corresponding enum
         return CallUserFcn(expr.name.replace(self),
                            [x.replace(self) for x in expr.args],
                            expr.pos)
     elif isinstance(expr, Call):
         if pfa.method == Method.EMIT and i + 1 < len(
                 pfas) and expr.name == "emit":
             return Call("u." + nextActionFcnName,
                         [x.replace(self) for x in expr.args], expr.pos)
         else:
             return Call(prefixFcnRef(i, pfa, expr.name),
                         [x.replace(self) for x in expr.args], expr.pos)
     elif isinstance(expr, Literal):
         return Literal(newPlaceholder(i, expr.avroType), expr.value,
                        expr.pos)
     elif isinstance(expr, NewObject):
         return NewObject(
             dict((k, v.replace(self))
                  for k, v in list(expr.fields.items())),
             newPlaceholder(i, expr.avroType), expr.pos)
     elif isinstance(expr, NewArray):
         return NewArray([x.replace(self) for x in expr.items],
                         newPlaceholder(i, expr.avroType), expr.pos)
     elif isinstance(expr, CellGet):
         return CellGet(prefixCell(i, pfa, expr.cell),
                        [x.replace(self) for x in expr.path], expr.pos)
     elif isinstance(expr, CellTo):
         return CellTo(prefixCell(i, pfa, expr.cell),
                       [x.replace(self) for x in expr.path],
                       expr.to.replace(self), expr.pos)
     elif isinstance(expr, PoolGet):
         return PoolGet(prefixPool(i, pfa, expr.pool),
                        [x.replace(self) for x in expr.path], expr.pos)
     elif isinstance(expr, PoolTo):
         return PoolTo(prefixPool(i, pfa, expr.pool),
                       [x.replace(self) for x in expr.path],
                       expr.to.replace(self), expr.init.replace(self),
                       expr.pos)
     elif isinstance(expr, CastCase):
         return CastCase(newPlaceholder(i, expr.avroType), expr.named,
                         [x.replace(self) for x in expr.body], expr.pos)
     elif isinstance(expr, Upcast):
         return Upcast(expr.expr.replace(self),
                       newPlaceholder(i, expr.avroType), expr.pos)
Пример #4
0
def _readArgument(data, dot, avroTypeBuilder):
    if data is None:
        return LiteralNull(dot)
    elif isinstance(data, bool):
        return LiteralBoolean(data, dot)
    elif isinstance(data, int):
        if -2147483648 <= data <= 2147483647:
            return LiteralInt(data, dot)
        elif -9223372036854775808 <= data <= 9223372036854775807:
            return LiteralLong(data, dot)
        else:
            raise PFASyntaxException("integer out of range: " + str(data), dot)
    elif isinstance(data, float):
        return LiteralDouble(data, dot)
    elif isinstance(data, str):
        if "." in data:
            words = data.split(".")
            ref = words[0]
            rest = words[1:]
            if not validSymbolName(ref):
                raise PFASyntaxException("\"{0}\" is not a valid symbol name".format(ref), dot)
            for i in range(len(rest)):
                try:
                    asint = int(rest[i])
                except ValueError:
                    rest[i] = LiteralString(rest[i], dot)
                else:
                    rest[i] = LiteralInt(asint, dot)
            return AttrGet(Ref(ref), rest, dot)
        elif validSymbolName(data):
            return Ref(data, dot)
        else:
            raise PFASyntaxException("\"{0}\" is not a valid symbol name".format(data), dot)

    elif isinstance(data, (list, tuple)):
        if len(data) == 1 and isinstance(data[0], str):
            return LiteralString(data[0], dot)
        else:
            raise PFASyntaxException("expecting expression, which may be [\"string\"], but no other array can be used as an expression", dot)

    elif isinstance(data, dict):
        at = data.get("@")
        keys = set(x for x in data.keys() if x != "@")

        _path = []
        _seq = True
        _partial = False
        _code = 0
        _newObject = None
        _newArray = None
        _filter = None

        for key in keys:
            if key == "int": _int = _readInt(data[key], dot + " -> " + key)
            elif key == "long": _long = _readLong(data[key], dot + " -> " + key)
            elif key == "float": _float = _readFloat(data[key], dot + " -> " + key)
            elif key == "double": _double = _readDouble(data[key], dot + " -> " + key)
            elif key == "string": _string = _readString(data[key], dot + " -> " + key)
            elif key == "base64": _bytes = _readBase64(data[key], dot + " -> " + key)
            elif key == "type": _avroType = _readAvroPlaceholder(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "value": _value = _readJsonToString(data[key], dot + " -> " + key)

            elif key == "let": _let = _readExpressionMap(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "set": _set = _readExpressionMap(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "for": _forlet = _readExpressionMap(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "step": _forstep = _readExpressionMap(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "ifnotnull": _ifnotnull = _readExpressionMap(data[key], dot + " -> " + key, avroTypeBuilder)

            elif key == "do":
                if isinstance(data[key], (list, tuple)):
                    _body = _readExpressionArray(data[key], dot + " -> " + key, avroTypeBuilder)
                else:
                    _body = [_readExpression(data[key], dot + " -> " + key, avroTypeBuilder)]
            elif key == "then":
                if isinstance(data[key], (list, tuple)):
                    _thenClause = _readExpressionArray(data[key], dot + " -> " + key, avroTypeBuilder)
                else:
                    _thenClause = [_readExpression(data[key], dot + " -> " + key, avroTypeBuilder)]
            elif key == "else":
                if isinstance(data[key], (list, tuple)):
                    _elseClause = _readExpressionArray(data[key], dot + " -> " + key, avroTypeBuilder)
                else:
                    _elseClause = [_readExpression(data[key], dot + " -> " + key, avroTypeBuilder)]
            elif key == "log":
                if isinstance(data[key], (list, tuple)):
                    _log = _readExpressionArray(data[key], dot + " -> " + key, avroTypeBuilder)
                else:
                    _log = [_readExpression(data[key], dot + " -> " + key, avroTypeBuilder)]
            elif key == "path":
                _path = _readExpressionArray(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "args":
                _callwithargs = _readExpressionArray(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "try":
                if isinstance(data[key], (list, tuple)):
                    _trycatch = _readExpressionArray(data[key], dot + " -> " + key, avroTypeBuilder)
                else:
                    _trycatch = [_readExpression(data[key], dot + " -> " + key, avroTypeBuilder)]

            elif key == "attr": _attr = _readExpression(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "if": _ifPredicate = _readExpression(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "while": _whilePredicate = _readExpression(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "until": _until = _readExpression(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "unpack": _unpack = _readExpression(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "del": _dell = _readExpression(data[key], dot + " -> " + key, avroTypeBuilder)

            elif key == "cond":
                _cond = _readExpressionArray(data[key], dot + " -> " + key, avroTypeBuilder)
                if any(x.elseClause is not None for x in _cond):
                    raise PFASyntaxException("cond expression must only contain else-less if expressions", pos(dot, at))

            elif key == "cases": _cases = _readCastCaseArray(data[key], dot + " -> " + key, avroTypeBuilder)

            elif key == "foreach": _foreach = _readString(data[key], dot + " -> " + key)
            elif key == "forkey": _forkey = _readString(data[key], dot + " -> " + key)
            elif key == "forval": _forval = _readString(data[key], dot + " -> " + key)
            elif key == "fcn": _fcnref = _readString(data[key], dot + " -> " + key)
            elif key == "cell": _cell = _readString(data[key], dot + " -> " + key)
            elif key == "pool": _pool = _readString(data[key], dot + " -> " + key)

            elif key == "in": _in = _readExpression(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "cast": _cast = _readExpression(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "upcast": _upcast = _readExpression(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "init": _init = _readExpression(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "call": _callwith = _readExpression(data[key], dot + " -> " + key, avroTypeBuilder)

            elif key == "seq": _seq = _readBoolean(data[key], dot + " -> " + key)
            elif key == "partial": _partial = _readBoolean(data[key], dot + " -> " + key)

            elif key == "doc": _doc = _readString(data[key], dot + " -> " + key)
            elif key == "error": _error = _readString(data[key], dot + " -> " + key)
            elif key == "code": _code = _readInt(data[key], dot + " -> " + key)
            elif key == "namespace": _namespace = _readString(data[key], dot + " -> " + key)

            elif key == "new":
                if isinstance(data[key], dict):
                    _newObject = _readExpressionMap(data[key], dot + " -> " + key, avroTypeBuilder)
                elif isinstance(data[key], (list, tuple)):
                    _newArray = _readExpressionArray(data[key], dot + " -> " + key, avroTypeBuilder)
                else:
                    raise PFASyntaxException("\"new\" must be an object (map, record) or an array", pos(dot, at))

            elif key == "params": _params = _readParams(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "ret": _ret = _readAvroPlaceholder(data[key], dot + " -> " + key, avroTypeBuilder)
            elif key == "as": _as = _readAvroPlaceholder(data[key], dot + " -> " + key, avroTypeBuilder)

            elif key == "to": _to = _readArgument(data[key], dot + " -> " + key, avroTypeBuilder)

            elif key == "fill": _fill = _readArgumentMap(data[key], dot + " -> " + key, avroTypeBuilder)

            elif key == "filter": _filter = _readStringOrIntArray(data[key], dot + " -> " + key)

            elif key == "format": _format = _readStringPairs(data[key], dot + " -> " + key)
            elif key == "pack": _pack = _readStringExpressionPairs(data[key], dot + " -> " + key, avroTypeBuilder)

            else:
                _callName = key
                if isinstance(data[key], (list, tuple)):
                    _callArgs = _readArgumentArray(data[key], dot + " -> " + key, avroTypeBuilder)
                else:
                    _callArgs = [_readArgument(data[key], dot + " -> " + key, avroTypeBuilder)]

        if "foreach" in keys and not validSymbolName(_foreach):
            raise PFASyntaxException("\"{0}\" is not a valid symbol name".format(data[keys]), pos(dot, at))
        if "forkey" in keys and not validSymbolName(_forkey):
            raise PFASyntaxException("\"{0}\" is not a valid symbol name".format(data[keys]), pos(dot, at))
        if "forval" in keys and not validSymbolName(_forval):
            raise PFASyntaxException("\"{0}\" is not a valid symbol name".format(data[keys]), pos(dot, at))
        if "fcn" in keys and not validFunctionName(_fcnref):
            raise PFASyntaxException("\"{0}\" is not a valid function name".format(data[keys]), pos(dot, at))

        if keys == set(["int"]):                             return LiteralInt(_int, pos(dot, at))
        elif keys == set(["long"]):                          return LiteralLong(_long, pos(dot, at))
        elif keys == set(["float"]):                         return LiteralFloat(_float, pos(dot, at))
        elif keys == set(["double"]):                        return LiteralDouble(_double, pos(dot, at))
        elif keys == set(["string"]):                        return LiteralString(_string, pos(dot, at))
        elif keys == set(["base64"]):                        return LiteralBase64(_bytes, pos(dot, at))
        elif keys == set(["type", "value"]):                 return Literal(_avroType, _value, pos(dot, at))

        elif keys == set(["new", "type"]) and _newObject is not None:
                                                             return NewObject(_newObject, _avroType, pos(dot, at))
        elif keys == set(["new", "type"]) and _newArray is not None:
                                                             return NewArray(_newArray, _avroType, pos(dot, at))

        elif keys == set(["do"]):                            return Do(_body, pos(dot, at))
        elif keys == set(["let"]):                           return Let(_let, pos(dot, at))
        elif keys == set(["set"]):                           return SetVar(_set, pos(dot, at))

        elif keys == set(["attr", "path"]):                  return AttrGet(_attr, _path, pos(dot, at))
        elif keys == set(["attr", "path", "to"]):            return AttrTo(_attr, _path, _to, pos(dot, at))
        elif keys == set(["cell"]) or \
             keys == set(["cell", "path"]):                  return CellGet(_cell, _path, pos(dot, at))
        elif keys == set(["cell", "to"]) or \
             keys == set(["cell", "path", "to"]):            return CellTo(_cell, _path, _to, pos(dot, at))
        elif keys == set(["pool", "path"]):                  return PoolGet(_pool, _path, pos(dot, at))
        elif keys == set(["pool", "path", "to", "init"]):    return PoolTo(_pool, _path, _to, _init, pos(dot, at))
        elif keys == set(["pool", "del"]):                   return PoolDel(_pool, _dell, pos(dot, at))

        elif keys == set(["if", "then"]):                    return If(_ifPredicate, _thenClause, None, pos(dot, at))
        elif keys == set(["if", "then", "else"]):            return If(_ifPredicate, _thenClause, _elseClause, pos(dot, at))
        elif keys == set(["cond"]):                          return Cond(_cond, None, pos(dot, at))
        elif keys == set(["cond", "else"]):                  return Cond(_cond, _elseClause, pos(dot, at))

        elif keys == set(["while", "do"]):                   return While(_whilePredicate, _body, pos(dot, at))
        elif keys == set(["do", "until"]):                   return DoUntil(_body, _until, pos(dot, at))
        elif keys == set(["for", "while", "step", "do"]):    return For(_forlet, _whilePredicate, _forstep, _body, pos(dot, at))

        elif keys == set(["foreach", "in", "do"]) or \
             keys == set(["foreach", "in", "do", "seq"]):    return Foreach(_foreach, _in, _body, _seq, pos(dot, at))
        elif keys == set(["forkey", "forval", "in", "do"]):  return Forkeyval(_forkey, _forval, _in, _body, pos(dot, at))

        elif keys == set(["cast", "cases"]) or \
             keys == set(["cast", "cases", "partial"]):      return CastBlock(_cast, _cases, _partial, pos(dot, at))
        elif keys == set(["upcast", "as"]):                  return Upcast(_upcast, _as, pos(dot, at))

        elif keys == set(["ifnotnull", "then"]):             return IfNotNull(_ifnotnull, _thenClause, None, pos(dot, at))
        elif keys == set(["ifnotnull", "then", "else"]):     return IfNotNull(_ifnotnull, _thenClause, _elseClause, pos(dot, at))

        elif keys == set(["pack"]):                          return Pack(_pack, pos(dot, at))
        elif keys == set(["unpack", "format", "then"]):      return Unpack(_unpack, _format, _thenClause, None, pos(dot, at))
        elif keys == set(["unpack", "format", "then", "else"]):
                                                             return Unpack(_unpack, _format, _thenClause, _elseClause, pos(dot, at))

        elif keys == set(["doc"]):                           return Doc(_doc, pos(dot, at))

        elif keys == set(["error"]):                         return Error(_error, None, pos(dot, at))
        elif keys == set(["error", "code"]):                 return Error(_error, _code, pos(dot, at))
        elif keys == set(["try"]) or \
             keys == set(["try", "filter"]):                 return Try(_trycatch, _filter, pos(dot, at))
        elif keys == set(["log"]):                           return Log(_log, None, pos(dot, at))
        elif keys == set(["log", "namespace"]):              return Log(_log, _namespace, pos(dot, at))

        elif keys == set(["params", "ret", "do"]):           return FcnDef(_params, _ret, _body, pos(dot, at))
        elif keys == set(["fcn"]):                           return FcnRef(_fcnref, pos(dot, at))
        elif keys == set(["fcn", "fill"]):                   return FcnRefFill(_fcnref, _fill, pos(dot, at))
        elif keys == set(["call", "args"]):                  return CallUserFcn(_callwith, _callwithargs, pos(dot, at))

        elif len(keys) == 1 and list(keys)[0] not in \
             set(["args", "as", "attr", "base64", "call", "cases", "cast", "cell", "code", "cond", "do", "doc", "double", "else",
                  "error", "fcn", "fill", "filter", "float", "for", "foreach", "forkey", "format", "forval", "if", "ifnotnull", "in", "init",
                  "int", "let", "log", "long", "namespace", "new", "pack", "params", "partial", "path", "pool", "ret", "seq",
                  "set", "step", "string", "then", "to", "try", "type", "unpack", "until", "upcast", "value", "while"]):
                                                             return Call(_callName, _callArgs, pos(dot, at))

        else: raise PFASyntaxException("unrecognized special form: {0} (not enough arguments? too many?)".format(", ".join(keys)), pos(dot, at))

    else:
        raise PFASyntaxException("expected expression, not " + _trunc(repr(data)), dot)