def bytecodeToPython(expr): print "Gonna emit", expr from ometa.grammar import TreeTransformerGrammar from ometa.runtime import TreeTransformerBase Emitter = TreeTransformerGrammar.makeGrammar(open("ometa/vm_emit.parsley").read(), "Emitter").createParserClass(TreeTransformerBase, {"t": t}) return Emitter.transform(expr)[0]
def writeBytecode(expr): print "Gonna compile", expr from ometa.grammar import TreeTransformerGrammar from ometa.runtime import TreeTransformerBase Compiler = TreeTransformerGrammar.makeGrammar(open("../ometa/vm.parsley").read(), "Compiler").createParserClass(TreeTransformerBase, {"t": t}) return Compiler.transform(expr)[0]
def bytecodeToPython(expr): print("Gonna emit %s" % (expr, )) from ometa.grammar import TreeTransformerGrammar from ometa.runtime import TreeTransformerBase Emitter = TreeTransformerGrammar.makeGrammar( open("ometa/vm_emit.parsley").read(), "Emitter").createParserClass(TreeTransformerBase, {"t": t}) return Emitter.transform(expr)[0]
def writeBytecode(expr): print "Gonna compile", expr from ometa.grammar import TreeTransformerGrammar from ometa.runtime import TreeTransformerBase Compiler = TreeTransformerGrammar.makeGrammar( open("../ometa/vm.parsley").read(), "Compiler").createParserClass(TreeTransformerBase, {"t": t}) return Compiler.transform(expr)[0]
def writeBytecode(expr): print("Gonna compile %s" % (expr,)) from ometa.grammar import TreeTransformerGrammar from ometa.runtime import TreeTransformerBase path = os.path.join(HERE, 'vm.parsley') Compiler = TreeTransformerGrammar.makeGrammar(open(path).read(), "Compiler").createParserClass(TreeTransformerBase, {"t": t}) return Compiler.transform(expr)[0]
def writeBytecode(expr): print("Gonna compile %s" % (expr, )) from ometa.grammar import TreeTransformerGrammar from ometa.runtime import TreeTransformerBase path = os.path.join(HERE, 'vm.parsley') Compiler = TreeTransformerGrammar.makeGrammar( open(path).read(), "Compiler").createParserClass(TreeTransformerBase, {"t": t}) return Compiler.transform(expr)[0]
def compile(self, grammar, namespace=None): """ Produce an object capable of parsing via this grammar. @param grammar: A string containing an OMeta grammar. """ if namespace is None: namespace = globals() g = TreeTransformerGrammar.makeGrammar( dedent(grammar), 'TestGrammar').createParserClass( TreeTransformerBase, namespace) return g
NounExpr(@name) (?(name in self.renamings) -> self.renamings[name] | -> t.NounExpr(name)) """ def expand(term, scope=None): e = Expander([term]) e.scope = scope e.nouns = set() e.counter = 0 expanded = e.apply("transform")[0] r = Reifier([expanded]) r.nouns = set(e.nouns) r.cache = {} r.id = 0 reified = r.apply("transform")[0] return reified def mktemp(self, name): self.counter += 1 return t.TempNounExpr(name, self.counter) StaticScopeTransformer = TreeTransformerGrammar.makeGrammar(computeStaticScopeRules, "StaticScopeTransformer").createParserClass(TreeTransformerBase, globals()) Expander = TreeTransformerGrammar.makeGrammar(expander, name="EExpander").createParserClass(TreeTransformerBase, globals()) Expander.mktemp = mktemp Reifier = TreeTransformerGrammar.makeGrammar(reifier, name="Reifier").createParserClass(TreeTransformerBase, globals()) CycleRenamer = TreeTransformerGrammar.makeGrammar(cycleRenamer, name="CycleRenamer").createParserClass(TreeTransformerBase, globals())
e.counter = 0 expanded = e.apply("transform")[0] r = Reifier([expanded]) r.nouns = set(e.nouns) r.cache = {} r.id = 0 reified = r.apply("transform")[0] return reified def mktemp(self, name): self.counter += 1 return t.TempNounExpr(name, self.counter) StaticScopeTransformer = TreeTransformerGrammar.makeGrammar( computeStaticScopeRules, "StaticScopeTransformer").createParserClass(TreeTransformerBase, globals()) Expander = TreeTransformerGrammar.makeGrammar( expander, name="EExpander").createParserClass(TreeTransformerBase, globals()) Expander.mktemp = mktemp Reifier = TreeTransformerGrammar.makeGrammar( reifier, name="Reifier").createParserClass(TreeTransformerBase, globals()) CycleRenamer = TreeTransformerGrammar.makeGrammar( cycleRenamer, name="CycleRenamer").createParserClass(TreeTransformerBase, globals())
output = [] for chunk in template.args: if chunk.tag.name == ".String.": output.append(chunk.data) elif chunk.tag.name == "QuasiExprHole": v = vals[chunk.args[0].data] output.append(v) else: raise TypeError("didn't expect %r in string template" % chunk) pt = PrecedenceTemplate(self.nextPrecedence, output) self.nextPrecedence = None return pt, None PythonExpressionUnparser = TreeTransformerGrammar.makeGrammar( open("expression_unparser.parsley").read(), 'PythonExpressionUnparser').createParserClass(PrecedenceTransformer, globals()) PythonStatementUnparser = TreeTransformerGrammar.makeGrammar( open("statement_unparser.parsley").read(), 'PythonStatementUnparser').createParserClass(TreeTransformerBase, globals()) if __name__ == "__main__": from terml.parser import parseTerm as term f = open(sys.argv[1]).read() g = wrapGrammar(PythonParser) stmts = g(f).file_input() from pprint import pprint pprint(stmts)
for chunk in template.args: if chunk.tag.name == ".String.": output.append(chunk.data) elif chunk.tag.name == "QuasiExprHole": v = vals[chunk.args[0].data] output.append(v) else: raise TypeError("didn't expect %r in string template" % chunk) pt = PrecedenceTemplate(self.nextPrecedence, output) self.nextPrecedence = None return pt, None Desugarer = TreeTransformerGrammar.makeGrammar( open("desugar_augmented_assignment.parsley").read(), "Desugarer" ).createParserClass(TreeTransformerBase, {"t": t}) PythonExpressionUnparser = TreeTransformerGrammar.makeGrammar( open("expression_unparser.parsley").read(), "PythonExpressionUnparser" ).createParserClass(PrecedenceTransformer, globals()) PythonStatementUnparser = TreeTransformerGrammar.makeGrammar( open("statement_unparser.parsley").read(), "PythonStatementUnparser" ).createParserClass(TreeTransformerBase, globals()) if __name__ == "__main__": from terml.parser import parseTerm as term
for chunk in template.args: if chunk.tag.name == ".String.": output.append(chunk.data) elif chunk.tag.name == "QuasiExprHole": v = vals[chunk.args[0].data] output.append(v) else: raise TypeError("didn't expect %r in string template" % chunk) pt = PrecedenceTemplate(self.nextPrecedence, output) self.nextPrecedence = None return pt, None Desugarer = TreeTransformerGrammar.makeGrammar( open("desugar_augmented_assignment.parsley").read(), "Desugarer").createParserClass(TreeTransformerBase, {"t": t}) PythonExpressionUnparser = TreeTransformerGrammar.makeGrammar( open("expression_unparser.parsley").read(), 'PythonExpressionUnparser').createParserClass(PrecedenceTransformer, globals()) PythonStatementUnparser = TreeTransformerGrammar.makeGrammar( open("statement_unparser.parsley").read(), 'PythonStatementUnparser').createParserClass(TreeTransformerBase, globals()) if __name__ == "__main__": from terml.parser import parseTerm as term