def ast(pfas, check=True, name=None, randseed=None, doc=None, version=None, metadata={}, options={}, tryYaml=False, verbose=False): """Create a single PFA from a chained workflow, returning the result as an abstract syntax tree. :type pfas: list of titus.pfaast.EngineConfig, Pythonized JSON, or JSON strings :param pfas: PFA documents for which the output of document *i* is the input to document *i + 1* :type check: bool :param check: test the chained PFA for validity :type name: string or ``None`` :param name: optional name for the chained PFA :type randseed: integer or ``None`` :param randseed: optional random number seed for the chained PFA :type doc: string or ``None`` :param doc: optional documentation string for the chained PFA :type version: integer or ``None`` :param version: optional version number for the chained PFA :type metadata: dict of strings :param metadata: metadata for the chained PFA (default is ``{}``) :type options: dict of Pythonized JSON :param options: implementation options for the chained PFA (default is ``{}``) :type tryYaml: bool :param tryYaml: if ``True``, attempt to interpret ``pfas`` as YAML (assuming they fail as JSON) :type verbose: bool :param verbose: if ``True``, write status messages to standard output :rtype: titus.pfaast.EngineConfig :return: a PFA document representing the chained workflow """ # normalize all input forms to ASTs if verbose: sys.stderr.write(time.asctime() + " Converting all inputs to ASTs\n") asts = [] for i, src in enumerate(pfas): if verbose: sys.stderr.write(time.asctime() + " step {0}\n".format(i + 1)) if isinstance(src, EngineConfig): ast = src elif isinstance(src, dict): ast = titus.reader.jsonToAst(src) else: try: ast = titus.reader.jsonToAst(src) except ValueError: if tryYaml: ast = titus.reader.yamlToAst(src) else: raise asts.append(ast) pfas = asts # helper functions for transforming names def split(t): if "." in t: return t[:t.rindex(".")], t[t.rindex(".") + 1:] else: return None, t def join(ns, n): if ns is None or ns == "": return n else: return ns + "." + n def prefixType(i, pfa, t): ns, n = split(t) return join(ns, "Step{0:d}_{1}_{2}".format(i + 1, pfa.name, n)) def prefixAction(i, pfa): return "step{0:d}_{1}_action".format(i + 1, pfa.name) def prefixFcnRef(i, pfa, x): if x.startswith("u."): return "u.step{0:d}_{1}_fcn_{2}".format(i + 1, pfa.name, x[2:]) else: return x def prefixFcnDef(i, pfa, x): return "step{0:d}_{1}_fcn_{2}".format(i + 1, pfa.name, x) def prefixCell(i, pfa, x): return "step{0:d}_{1}_{2}".format(i + 1, pfa.name, x) def prefixPool(i, pfa, x): return "step{0:d}_{1}_{2}".format(i + 1, pfa.name, x) # define new names for all types to avoid type name collisions if verbose: sys.stderr.write(time.asctime() + " Changing type names to avoid collisions\n") originalNameToNewName = {} for i, pfa in enumerate(pfas): originalNameToNewName[i] = {} for typeName in list(pfa.inputPlaceholder.parser.names.names.keys()): keyTypeName = typeName if (typeName[0] == "."): keyTypeName = keyTypeName[1:] originalNameToNewName[i][keyTypeName] = prefixType( i, pfa, typeName) # but any names in the input to the first and the output from the last should not be changed def trivialName(i, avroType, memo): if isinstance(avroType, AvroArray): trivialName(i, avroType.items, memo) elif isinstance(avroType, AvroMap): trivialName(i, avroType.values, memo) elif isinstance(avroType, AvroUnion): for t in avroType.types: trivialName(i, t, memo) elif isinstance(avroType, (AvroFixed, AvroEnum)): t = avroType.fullName originalNameToNewName[i][t] = t elif isinstance(avroType, AvroRecord): t = avroType.fullName if t not in memo: memo.add(t) for f in avroType.fields: trivialName(i, f.avroType, memo) originalNameToNewName[i][t] = t trivialName(0, pfas[0].input, set()) trivialName(len(pfas) - 1, pfas[-1].output, set()) # ensure that chained types match and will be given the same names if verbose: sys.stderr.write( time.asctime() + " Verifying that input/output schemas match along the chain\n") def chainPair(i, first, second, memo): if isinstance(first, AvroNull) and isinstance(second, AvroNull): return True elif isinstance(first, AvroBoolean) and isinstance( second, AvroBoolean): return True elif isinstance(first, AvroInt) and isinstance(second, AvroInt): return True elif isinstance(first, AvroLong) and isinstance(second, AvroLong): return True elif isinstance(first, AvroFloat) and isinstance(second, AvroFloat): return True elif isinstance(first, AvroDouble) and isinstance(second, AvroDouble): return True elif isinstance(first, AvroBytes) and isinstance(second, AvroBytes): return True elif isinstance(first, AvroFixed) and isinstance(second, AvroFixed): if first.size == second.size: originalNameToNewName[i + 1][ second.fullName] = originalNameToNewName[i][first.fullName] return True else: return False elif isinstance(first, AvroString) and isinstance(second, AvroString): return True elif isinstance(first, AvroEnum) and isinstance(second, AvroEnum): if first.symbols == second.symbols: originalNameToNewName[i + 1][ second.fullName] = originalNameToNewName[i][first.fullName] return True else: return False elif isinstance(first, AvroArray) and isinstance(second, AvroArray): return chainPair(i, first.items, second.items, memo) elif isinstance(first, AvroMap) and isinstance(second, AvroMap): return chainPair(i, first.values, second.values, memo) elif isinstance(first, AvroRecord) and isinstance(second, AvroRecord): if first.fullName not in memo: memo.add(first.fullName) if len(first.fields) != len(second.fields): return False for f1, f2 in zip(first.fields, second.fields): if f1.name != f2.name: return False elif not chainPair(i, f1.avroType, f2.avroType, memo): return False originalNameToNewName[i + 1][ second.fullName] = originalNameToNewName[i][first.fullName] return True elif isinstance(first, AvroUnion) and isinstance(second, AvroUnion): for yt in second.types: if not any(chainPair(i, xt, yt, memo) for xt in first.types): return False return True else: return False for i in range(len(pfas) - 1): first = pfas[i].output second = pfas[i + 1].input if not chainPair(i, first, second, set()): raise PFAChainError( "output of engine {0}: {1} not compatible with input of engine {2}: {3}" .format(i + 1, ts(first), i + 2, ts(second))) def rename(i, avroType, memo): if isinstance(avroType, AvroArray): return {"type": "array", "items": rename(i, avroType.items, memo)} elif isinstance(avroType, AvroMap): return {"type": "map", "values": rename(i, avroType.values, memo)} elif isinstance(avroType, AvroUnion): return [rename(i, t, memo) for t in avroType.types] elif isinstance(avroType, AvroFixed): ns, n = split(originalNameToNewName[i][avroType.fullName]) out = {"type": "fixed", "name": n, "size": avroType.size} if ns is not None: out["namespace"] = ns return out elif isinstance(avroType, AvroEnum): ns, n = split(originalNameToNewName[i][avroType.fullName]) out = {"type": "enum", "name": n, "symbols": avroType.symbols} if ns is not None: out["namespace"] = ns return out elif isinstance(avroType, AvroRecord): newName = originalNameToNewName[i][avroType.fullName] if newName in memo: return memo[newName] else: ns, n = split(newName) out = {"type": "record", "name": n, "fields": []} if ns is not None: out["namespace"] = ns memo[newName] = join(ns, n) for f in avroType.fields: newf = { "name": f.name, "type": rename(i, f.avroType, memo) } if f.default is not None: newf["default"] = f.default if f.order is not None: newf["order"] = f.order out["fields"].append(newf) return out else: return jsonlib.loads(repr(avroType)) avroTypeBuilder = AvroTypeBuilder() memo = {} def newPlaceholder(i, oldAvroType): newAvroType = rename(i, oldAvroType, {}) return avroTypeBuilder.makePlaceholder(jsonlib.dumps(newAvroType), memo) # combined name, if not explicitly set if name is None: name = "Chain_" + "_".join(pfa.name for pfa in pfas) # combined method (fold not supported yet, but could be) method = Method.MAP for pfa in pfas: if pfa.method == Method.EMIT: method = Method.EMIT elif pfa.method == Method.FOLD: raise NotImplementedError( "chaining of fold-type scoring engines has not been implemented yet" ) # no zero or merge until we support fold method zero = None merge = None # input/output types from first and last inputPlaceholder = newPlaceholder(0, pfas[0].input) outputPlaceholder = newPlaceholder(len(pfas) - 1, pfas[-1].output) if verbose: sys.stderr.write( time.asctime() + " Adding [name, instance, metadata, actionsStarted, actionsFinished, version] as model parameters\n" ) cells = { "name": Cell(newPlaceholder(0, AvroString()), jsonlib.dumps(""), False, False, CellPoolSource.EMBEDDED), "instance": Cell(newPlaceholder(0, AvroInt()), jsonlib.dumps(0), False, False, CellPoolSource.EMBEDDED), "metadata": Cell(newPlaceholder(0, AvroMap(AvroString())), jsonlib.dumps({}), False, False, CellPoolSource.EMBEDDED), "actionsStarted": Cell(newPlaceholder(0, AvroLong()), jsonlib.dumps(0), False, False, CellPoolSource.EMBEDDED), "actionsFinished": Cell(newPlaceholder(0, AvroLong()), jsonlib.dumps(0), False, False, CellPoolSource.EMBEDDED) } if version is not None: cells["version"] = Cell(newPlaceholder(0, AvroInt()), 0, False, False, CellPoolSource.EMBEDDED) pools = {} if verbose: sys.stderr.write(time.asctime() + " Converting scoring engine algorithm\n") # all code will go into user functions, including begin/action/end fcns = {} begin = [ CellTo("name", [], Ref("name")), CellTo("instance", [], Ref("instance")), CellTo("metadata", [], Ref("metadata")) ] if version is not None: begin.append(CellTo("version", [], Ref("version"))) action = [ CellTo("actionsStarted", [], Ref("actionsStarted")), CellTo("actionsFinished", [], Ref("actionsFinished")) ] end = [ CellTo("actionsStarted", [], Ref("actionsStarted")), CellTo("actionsFinished", [], Ref("actionsFinished")) ] for i, pfa in enumerate(pfas): if verbose: sys.stderr.write(time.asctime() + " step {0}: {1}\n".format(i + 1, pfa.name)) thisActionFcnName = prefixAction(i, pfa) if i + 1 < len(pfas): nextActionFcnName = prefixAction(i + 1, pfas[i + 1]) else: nextActionFcnName = None # this is a closure; it must be defined in the loop to pick up free variables lazyFcnReplacer = None 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) genericReplacer.isDefinedAt = lambda x: isinstance( x, (FcnDef, FcnRef, FcnRefFill, CallUserFcn, Call, Literal, NewObject, CellGet, CellTo, PoolGet, PoolTo, CastCase, Upcast)) def fcnReplacer(expr): return genericReplacer(expr, fcnReplacer) fcnReplacer.isDefinedAt = genericReplacer.isDefinedAt lazyFcnReplacer = fcnReplacer # add statements to begin 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) beginReplacer.isDefinedAt = lambda x: isinstance( x, Ref) or genericReplacer.isDefinedAt(x) begin.extend([x.replace(beginReplacer) for x in pfa.begin]) # add statements to end def endReplacer(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, endReplacer) endReplacer.isDefinedAt = lambda x: isinstance( x, Ref) or genericReplacer.isDefinedAt(x) end.extend([x.replace(endReplacer) for x in pfa.end]) # convert the action into a user function 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) actionReplacer.isDefinedAt = lambda x: isinstance( x, Ref) or genericReplacer.isDefinedAt(x) body = [x.replace(actionReplacer) for x in pfa.action] if method == Method.MAP: # if the overall method is MAP, then we know that all of the individual engines are MAP # the overall action calls a nested chain of engines-as-functions and each engine-as-a-function just does its job and returns (body is unmodified) fcns[thisActionFcnName] = FcnDef( [{ "input": newPlaceholder(i, pfa.input) }], newPlaceholder(i, pfa.output), body) if i == 0: action.append(Call("u." + thisActionFcnName, [Ref("input")])) else: action[-1] = Call("u." + thisActionFcnName, [action[-1]]) elif method == Method.EMIT: # if the overall method is EMIT, then some individual engines might be MAP or might be EMIT # the overall action calls the first engine-as-a-function and the engines-as-functions call each other (body is modified) if pfa.method == Method.MAP and i + 1 < len(pfas): body = [ Call("u." + nextActionFcnName, [Do(body)]), LiteralNull() ] elif pfa.method == Method.MAP: body = [Call("emit", [Do(body)])] elif pfa.method == Method.EMIT: body.append(LiteralNull()) fcns[thisActionFcnName] = FcnDef( [{ "input": newPlaceholder(i, pfa.input) }], newPlaceholder(i, AvroNull()), body) if i == 0: action.append(Call("u." + thisActionFcnName, [Ref("input")])) # convert all of the user functions into user functions for fcnName, fcnDef in list(pfa.fcns.items()): # note: some of these user-defined functions may call emit; if so, they'll call the right emit fcns[prefixFcnDef(i, pfa, fcnName)] = FcnDef( [{ list(t.keys())[0]: newPlaceholder(i, list(t.values())[0]) } for t in fcnDef.paramsPlaceholder], newPlaceholder(i, fcnDef.ret), [x.replace(fcnReplacer) for x in fcnDef.body], fcnDef.pos) if verbose: sys.stderr.write(time.asctime() + " Create types for model parameters\n") for i, pfa in enumerate(pfas): if verbose and len(pfa.cells) > 0: sys.stderr.write(time.asctime() + " step {0}:\n".format(i + 1)) for cellName, cell in list(pfa.cells.items()): if verbose: sys.stderr.write(time.asctime() + " cell {0}\n".format(cellName)) newCell = Cell(newPlaceholder(i, cell.avroType), cell.init, cell.shared, cell.rollback, cell.source, cell.pos) cells[prefixCell(i, pfa, cellName)] = newCell if cell.source == "embedded": def converter(avroType): original = jsonDecoder(cell.avroType, jsonlib.loads(cell.init)) return jsonlib.dumps(jsonEncoder(avroType, original)) newCell.converter = converter for i, pfa in enumerate(pfas): if verbose and len(pfa.pools) > 0: sys.stderr.write(time.asctime() + " step {0}:\n".format(i + 1)) for poolName, pool in list(pfa.pools.items()): if verbose: sys.stderr.write(time.asctime() + " pool {0}\n".format(poolName)) newPool = Pool(newPlaceholder(i, pool.avroType), pool.init, pool.shared, pool.rollback, pool.source, pool.pos) pools[prefixPool(i, pfa, poolName)] = newPool if pool.source == "embedded": def converter(avroType): original = jsonDecoder(pool.avroType, jsonlib.loads(pool.init)) return jsonlib.dumps(jsonEncoder(avroType, original)) newPool.converter = converter # make sure all the types work together if verbose: sys.stderr.write(time.asctime() + " Resolving all types\n") avroTypeBuilder.resolveTypes() if verbose: sys.stderr.write(time.asctime() + " Converting the model parameters themselves\n") for i, pfa in enumerate(pfas): if verbose and len(pfa.cells) > 0: sys.stderr.write(time.asctime() + " step {0}:\n".format(i + 1)) for cellName, cell in list(pfa.cells.items()): if verbose: sys.stderr.write(time.asctime() + " cell {0}\n".format(cellName)) if cell.source == "embedded": newCell = cells[prefixCell(i, pfa, cellName)] newCell.init = newCell.converter(newCell.avroType) for i, pfa in enumerate(pfas): if verbose and len(pfa.pools) > 0: sys.stderr.write(time.asctime() + " step {0}:\n".format(i + 1)) for poolName, pool in list(pfa.pools.items()): if verbose: sys.stderr.write(time.asctime() + " pool {0}\n".format(poolName)) if pool.source == "embedded": newPool = pools[prefixPool(i, pfa, poolName)] newPool.init = newPool.converter(newPool.avroType) # randseed, doc, version, metadata, and options need to be explicitly set # return a (possibly checked) AST out = EngineConfig(name, method, inputPlaceholder, outputPlaceholder, begin, action, end, fcns, zero, merge, cells, pools, randseed, doc, version, metadata, options) if check: if verbose: sys.stderr.write(time.asctime() + " Verifying PFA validity\n") PFAEngine.fromAst(out) if verbose: sys.stderr.write(time.asctime() + " Done\n") return out
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)
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)