def macroexpand1(x, context=None): if isinstance(x, Doc): if context is None: context = MacroContext() return macros.expand1(x, language.getOp(x.tag), context) else: return x, False
def _expand(obj, context, quoteDepth=0, lineno=None): ln = getattr(obj, "lineno", None) if ln != None: lineno = ln if isinstance(obj, Doc): op = language.getOp(obj.tag) if quoteDepth == 0 and op != None and hasattr(op, 'macro'): moduleContext = context.setdefault(op.language.__module__, MacroContext()) moduleContext.beginScope() expanded, _ = expand1(obj, op, moduleContext) assert expanded is not obj res = _expand(expanded, context, lineno=lineno) moduleContext.endScope() else: if obj.tag == rootops.quote: docQuoteDepth = quoteDepth + 1 elif obj.tag == rootops.escape: extra = obj.get("extra") if extra != None: escapeLevel = extra.contentLen() + 1 else: escapeLevel = 1 if escapeLevel > quoteDepth: # TODO: Proper filename in error message raise SyntaxError, ("quote escape greater than quote nesting", (None, lineno,0, "")) elif escapeLevel == quoteDepth: docQuoteDepth = 0 else: docQuoteDepth = quoteDepth else: docQuoteDepth = quoteDepth content = [] for x in obj.content(): ex = _expand(x, context, docQuoteDepth, lineno=lineno) if isinstance(ex, splice): content.extend(ex.items) else: content.append(ex) properties = {} for name, val in obj.properties(): ex = _expand(val, context, docQuoteDepth, lineno=lineno) if isinstance(ex, splice): raise SyntaxError, "cannot splice into named operand '%s'" % name properties[name] = ex res = Doc(obj.tag, content, properties) parser.copySourcePos(obj, res) else: res = obj return res
def _expand(obj, context, quoteDepth=0): if isinstance(obj, Doc): op = language.getOp(obj.tag) if quoteDepth == 0 and op != None and hasattr(op, 'macro'): moduleContext = context.setdefault(op.language.__module__, MacroContext()) moduleContext.beginScope() expanded, _ = expand1(obj, op, moduleContext) assert expanded is not obj res = _expand(expanded, context) moduleContext.endScope() else: if obj.tag == rootops.quote: docQuoteDepth = quoteDepth + 1 elif obj.tag == rootops.escape: extra = obj.get("extra") if extra != None: escapeLevel = extra.contentLen() + 1 else: escapeLevel = 1 if escapeLevel > quoteDepth: raise SyntaxError, "quote escape greater than quote nesting" elif escapeLevel == quoteDepth: docQuoteDepth = 0 else: docQuoteDepth = quoteDepth else: docQuoteDepth = quoteDepth content = [] for x in obj.content(): ex = _expand(x, context, docQuoteDepth) if isinstance(ex, splice): content.extend(ex.items) else: content.append(ex) properties = {} for name, val in obj.properties(): ex = _expand(val, context, docQuoteDepth) if isinstance(ex, splice): raise SyntaxError, "cannot splice into named operand '%s'" % name properties[name] = ex res = Doc(obj.tag, content, properties) parser.copySourcePos(obj, res) else: res = obj return res
def Doc(doc): rootopCompiler = rootopCompilers.get(doc.tag) if rootopCompiler: return rootopCompiler(doc) else: op = language.getOp(doc.tag) if op: pycompile = getattr(op, "pycompile", None) if pycompile: return doc.applyTo(pycompile, op) # Assume it's a function operator - call op.func return compileFunctionCall(ast.Getattr(compileGetOp(doc.tag), "func"), list(doc.content()), dict(doc.properties()))