def test_singleOr(self): """ Test code generation for a sequence of alternatives. """ x1 = t.Or([t.Exactly("x")]) x = t.Exactly("x") self.assertEqual(writePython(x, ""), writePython(x1, ""))
def makeGrammar(cls, grammar, name='Grammar'): """ Define a new parser class with the rules in the given grammar. @param grammar: A string containing a PyMeta grammar. @param name: The name of the class to be generated. @param superclass: The class the generated class is a child of. """ g = cls(grammar) if TIMING: start = time.time() tree = g.parseGrammar(name) if TIMING: print "Grammar %r parsed in %g secs" % (name, time.time() - start) def cnt(n): count = sum(cnt(a) for a in n.args) + 1 return count print "%d nodes." % (cnt(tree)) start = time.time() modname = "pymeta_grammar__" + name filename = "/pymeta_generated_code/" + modname + ".py" source = writePython(tree, grammar) if TIMING: print "Grammar %r generated in %g secs" % (name, time.time() - start) return moduleFromGrammar(source, name, modname, filename)
def makeGrammar(cls, grammar, name='Grammar'): """ Define a new parser class with the rules in the given grammar. @param grammar: A string containing a PyMeta grammar. @param name: The name of the class to be generated. @param superclass: The class the generated class is a child of. """ g = cls(grammar) if TIMING: start = time.time() tree = g.parseGrammar(name) if TIMING: print "Grammar %r parsed in %g secs" % (name, time.time() - start) def cnt(n): count = sum(cnt(a) for a in n.args) + 1 return count print "%d nodes." % (cnt(tree)) start = time.time() modname = "pymeta_grammar__" + name filename = "/pymeta_generated_code/" + modname + ".py" source = writePython(tree) if TIMING: print "Grammar %r generated in %g secs" % (name, time.time() - start) return moduleFromGrammar(source, name, modname, filename)
def test_markAsTree(self): """ Grammars containing list patterns are marked as taking tree-shaped input rather than character streams. """ x = t.Rule("foo", t.List(t.Exactly("x"))) g = t.Grammar("TestGrammar", True, [x]) self.assert_("\n tree = True\n" in writePython(g, ""))
def test_markAsTree(self): """ Grammars containing list patterns are marked as taking tree-shaped input rather than character streams. """ x = t.Rule("foo", t.List( t.Exactly("x"))) g = t.Grammar("TestGrammar", True, [x]) self.assertIn("\n tree = True\n", writePython(g, ""))
def test_action(self): """ Test code generation for semantic actions. """ x = t.Action("doStuff()") self.assertEqual(writePython(x), dd(""" _G_python_1, lastError = eval('doStuff()', self.globals, _locals), None self.considerError(lastError, None) _G_python_1 """))
def test_expr(self): """ Test code generation for semantic predicates. """ x = t.Action("returnStuff()") self.assertEqual(writePython(x, ""), dd(""" _G_python_1, lastError = eval('returnStuff()', self.globals, _locals), None self.considerError(lastError, None) _G_python_1 """))
def test_action(self): """ Test code generation for semantic actions. """ x = t.Action("doStuff()") self.assertEqual( writePython(x), dd(""" _G_python_1, lastError = eval('doStuff()', self.globals, _locals), None self.considerError(lastError, None) _G_python_1 """))
def test_expr(self): """ Test code generation for semantic predicates. """ x = t.Action("returnStuff()") self.assertEqual( writePython(x, ""), dd(""" _G_python_1, lastError = eval('returnStuff()', self.globals, _locals), None self.considerError(lastError, None) _G_python_1 """))
def test_exactly(self): """ Test generation of code for the 'exactly' pattern. """ x = t.Exactly("x") self.assertEqual(writePython(x ,""), dd(""" _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, None) _G_exactly_1 """))
def test_exactly(self): """ Test generation of code for the 'exactly' pattern. """ x = t.Exactly("x") self.assertEqual( writePython(x, ""), dd(""" _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, None) _G_exactly_1 """))
def test_bind(self): """ Test code generation for variable assignment. """ x = t.Exactly("x") b = t.Bind("var", x) self.assertEqual(writePython(b, ""), dd(""" _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, None) _locals['var'] = _G_exactly_1 _locals['var'] """))
def test_bind(self): """ Test code generation for variable assignment. """ x = t.Exactly("x") b = t.Bind("var", x) self.assertEqual( writePython(b, ""), dd(""" _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, None) _locals['var'] = _G_exactly_1 _G_exactly_1 """))
def test_not(self): """ Test code generation for negated terms. """ x = t.Not(t.Exactly("x")) self.assertEqual(writePython(x ,""), dd(""" def _G_not_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_not_3, lastError = self._not(_G_not_1) self.considerError(lastError, None) _G_not_3 """))
def test_listpattern(self): """ Test code generation for list patterns. """ x = t.List(t.Exactly("x")) self.assertEqual(writePython(x, ""), dd(""" def _G_listpattern_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_listpattern_3, lastError = self.listpattern(_G_listpattern_1) self.considerError(lastError, None) _G_listpattern_3 """))
def test_sequence(self): """ Test generation of code for sequence patterns. """ x = t.Exactly("x") y = t.Exactly("y") z = t.And([x, y]) self.assertEqual(writePython(z, ""), dd(""" _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, None) _G_exactly_2, lastError = self.exactly('y') self.considerError(lastError, None) _G_exactly_2 """))
def test_rule(self): """ Test generation of entire rules. """ x = t.Rule("foo", t.Exactly("x")) self.assertEqual(writePython(x, ""), dd(""" def rule_foo(self): _locals = {'self': self} self.locals['foo'] = _locals _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, 'foo') return (_G_exactly_1, self.currentError) """))
def test_lookahead(self): """ Test code generation for lookahead expressions. """ x = t.Lookahead(t.Exactly("x")) self.assertEqual(writePython(x), dd(""" def _G_lookahead_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_lookahead_3, lastError = self.lookahead(_G_lookahead_1) self.considerError(lastError, None) _G_lookahead_3 """))
def test_pred(self): """ Test code generation for predicate expressions. """ x = t.Predicate(t.Exactly("x")) self.assertEqual(writePython(x, ""), dd(""" def _G_pred_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_pred_3, lastError = self.pred(_G_pred_1) self.considerError(lastError, None) _G_pred_3 """))
def test_label(self): """ Test code generation for custom labels. """ xs = t.Label(t.Exactly("x"), 'CustomLabel') self.assertEqual(writePython(xs, ""), dd(""" def _G_label_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_label_3, lastError = self.label(_G_label_1, "CustomLabel") self.considerError(lastError, None) _G_label_3 """))
def test_not(self): """ Test code generation for negated terms. """ x = t.Not(t.Exactly("x")) self.assertEqual( writePython(x, ""), dd(""" def _G_not_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_not_3, lastError = self._not(_G_not_1) self.considerError(lastError, None) _G_not_3 """))
def test_pred(self): """ Test code generation for predicate expressions. """ x = t.Predicate(t.Exactly("x")) self.assertEqual( writePython(x, ""), dd(""" def _G_pred_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_pred_3, lastError = self.pred(_G_pred_1) self.considerError(lastError, None) _G_pred_3 """))
def test_label(self): """ Test code generation for custom labels. """ xs = t.Label(t.Exactly("x"), 'CustomLabel') self.assertEqual( writePython(xs, ""), dd(""" def _G_label_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_label_3, lastError = self.label(_G_label_1, "CustomLabel") self.considerError(lastError, None) _G_label_3 """))
def test_listpattern(self): """ Test code generation for list patterns. """ x = t.List(t.Exactly("x")) self.assertEqual( writePython(x, ""), dd(""" def _G_listpattern_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_listpattern_3, lastError = self.listpattern(_G_listpattern_1) self.considerError(lastError, None) _G_listpattern_3 """))
def test_rule(self): """ Test generation of entire rules. """ x = t.Rule("foo", t.Exactly("x")) self.assertEqual( writePython(x, ""), dd(""" def rule_foo(self): _locals = {'self': self} self.locals['foo'] = _locals _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, 'foo') return (_G_exactly_1, self.currentError) """))
def test_sequence(self): """ Test generation of code for sequence patterns. """ x = t.Exactly("x") y = t.Exactly("y") z = t.And([x, y]) self.assertEqual( writePython(z, ""), dd(""" _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, None) _G_exactly_2, lastError = self.exactly('y') self.considerError(lastError, None) _G_exactly_2 """))
def test_lookahead(self): """ Test code generation for lookahead expressions. """ x = t.Lookahead(t.Exactly("x")) self.assertEqual( writePython(x), dd(""" def _G_lookahead_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_lookahead_3, lastError = self.lookahead(_G_lookahead_1) self.considerError(lastError, None) _G_lookahead_3 """))
def test_optional(self): """ Test code generation for optional terms. """ x = t.Optional(t.Exactly("x")) self.assertEqual(writePython(x, ""), dd(""" def _G_optional_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) def _G_optional_3(): return (None, self.input.nullError()) _G_or_4, lastError = self._or([_G_optional_1, _G_optional_3]) self.considerError(lastError, None) _G_or_4 """))
def test_many(self): """ Test generation of code for matching zero or more instances of a pattern. """ xs = t.Many(t.Exactly("x")) self.assertEqual(writePython(xs, ""), dd(""" def _G_many_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_many_3, lastError = self.many(_G_many_1) self.considerError(lastError, None) _G_many_3 """))
def test_foreignApply(self): """ Test generation of code for calling foreign grammar's rules. """ one = t.Action("1") x = t.Action("x") a = t.ForeignApply("thegrammar", "foo", "main", [one, x]) self.assertEqual(writePython(a, ""), dd(""" _G_python_1, lastError = 1, None self.considerError(lastError, None) _G_python_2, lastError = eval('x', self.globals, _locals), None self.considerError(lastError, None) _G_apply_3, lastError = self.foreignApply("thegrammar", "foo", self.globals, _locals, _G_python_1, _G_python_2) self.considerError(lastError, None) _G_apply_3 """))
def test_superApply(self): """ Test generation of code for calling the superclass' implementation of the current rule. """ one = t.Action("1") x = t.Action("x") a = t.Apply("super", "main", [one, x]) self.assertEqual(writePython(a, ""), dd(""" _G_python_1, lastError = 1, None self.considerError(lastError, None) _G_python_2, lastError = eval('x', self.globals, _locals), None self.considerError(lastError, None) _G_apply_3, lastError = self.superApply("main", _G_python_1, _G_python_2) self.considerError(lastError, None) _G_apply_3 """))
def test_optional(self): """ Test code generation for optional terms. """ x = t.Optional(t.Exactly("x")) self.assertEqual( writePython(x, ""), dd(""" def _G_optional_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) def _G_optional_3(): return (None, self.input.nullError()) _G_or_4, lastError = self._or([_G_optional_1, _G_optional_3]) self.considerError(lastError, None) _G_or_4 """))
def test_apply(self): """ Test generation of code for rule application. """ one = t.Action("1") x = t.Action("x") a = t.Apply("foo", "main", [one, x]) self.assertEqual(writePython(a, ""), dd(""" _G_python_1, lastError = 1, None self.considerError(lastError, None) _G_python_2, lastError = eval('x', self.globals, _locals), None self.considerError(lastError, None) _G_apply_3, lastError = self._apply(self.rule_foo, "foo", [_G_python_1, _G_python_2]) self.considerError(lastError, None) _G_apply_3 """))
def test_foreignApply(self): """ Test generation of code for calling foreign grammar's rules. """ one = t.Action("1") x = t.Action("x") a = t.ForeignApply("thegrammar", "foo", "main", [one, x]) self.assertEqual( writePython(a, ""), dd(""" _G_python_1, lastError = (1), None self.considerError(lastError, None) _G_python_2, lastError = eval('x', self.globals, _locals), None self.considerError(lastError, None) _G_apply_3, lastError = self.foreignApply("thegrammar", "foo", self.globals, _locals, _G_python_1, _G_python_2) self.considerError(lastError, None) _G_apply_3 """))
def test_many(self): """ Test generation of code for matching zero or more instances of a pattern. """ xs = t.Many(t.Exactly("x")) self.assertEqual( writePython(xs, ""), dd(""" def _G_many_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) _G_many_3, lastError = self.many(_G_many_1) self.considerError(lastError, None) _G_many_3 """))
def test_apply(self): """ Test generation of code for rule application. """ one = t.Action("1") x = t.Action("x") a = t.Apply("foo", "main", [one, x]) self.assertEqual( writePython(a, ""), dd(""" _G_python_1, lastError = (1), None self.considerError(lastError, None) _G_python_2, lastError = eval('x', self.globals, _locals), None self.considerError(lastError, None) _G_apply_3, lastError = self._apply(self.rule_foo, "foo", [_G_python_1, _G_python_2]) self.considerError(lastError, None) _G_apply_3 """))
def test_superApply(self): """ Test generation of code for calling the superclass' implementation of the current rule. """ one = t.Action("1") x = t.Action("x") a = t.Apply("super", "main", [one, x]) self.assertEqual( writePython(a, ""), dd(""" _G_python_1, lastError = (1), None self.considerError(lastError, None) _G_python_2, lastError = eval('x', self.globals, _locals), None self.considerError(lastError, None) _G_apply_3, lastError = self.superApply("main", _G_python_1, _G_python_2) self.considerError(lastError, None) _G_apply_3 """))
def test_or(self): """ Test code generation for a sequence of alternatives. """ xy = t.Or([t.Exactly("x"), t.Exactly("y")]) self.assertEqual( writePython(xy, ""), dd(""" def _G_or_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) def _G_or_3(): _G_exactly_4, lastError = self.exactly('y') self.considerError(lastError, None) return (_G_exactly_4, self.currentError) _G_or_5, lastError = self._or([_G_or_1, _G_or_3]) self.considerError(lastError, None) _G_or_5 """))
def test_or(self): """ Test code generation for a sequence of alternatives. """ xy = t.Or([t.Exactly("x"), t.Exactly("y")]) self.assertEqual(writePython(xy, ""), dd(""" def _G_or_1(): _G_exactly_2, lastError = self.exactly('x') self.considerError(lastError, None) return (_G_exactly_2, self.currentError) def _G_or_3(): _G_exactly_4, lastError = self.exactly('y') self.considerError(lastError, None) return (_G_exactly_4, self.currentError) _G_or_5, lastError = self._or([_G_or_1, _G_or_3]) self.considerError(lastError, None) _G_or_5 """))
def test_grammar(self): """ Test generation of an entire grammar. """ r1 = t.Rule("foo", t.Exactly("x")) r2 = t.Rule("baz", t.Exactly("y")) x = t.Grammar("BuilderTest", False, [r1, r2]) self.assertEqual( writePython(x, ""), dd(""" def createParserClass(GrammarBase, ruleGlobals): if ruleGlobals is None: ruleGlobals = {} class BuilderTest(GrammarBase): def rule_foo(self): _locals = {'self': self} self.locals['foo'] = _locals _G_exactly_1, lastError = self.exactly('x') self.considerError(lastError, 'foo') return (_G_exactly_1, self.currentError) def rule_baz(self): _locals = {'self': self} self.locals['baz'] = _locals _G_exactly_2, lastError = self.exactly('y') self.considerError(lastError, 'baz') return (_G_exactly_2, self.currentError) if BuilderTest.globals is not None: BuilderTest.globals = BuilderTest.globals.copy() BuilderTest.globals.update(ruleGlobals) else: BuilderTest.globals = ruleGlobals return BuilderTest """))
import sys from ometa.grammar import OMeta from ometa.builder import writePython if len(sys.argv) != 3: print "Usage: %s grammar-filename python-filename" % (sys.argv[0],) sys.exit(1) with open(sys.argv[1]) as infile: grammar = infile.read() g = OMeta(grammar) tree = g.parseGrammar("Parser") source = writePython(tree, grammar) + '\n' with open(sys.argv[2], 'w') as outfile: outfile.write(source)
import sys from ometa.grammar import OMeta from ometa.builder import writePython if len(sys.argv) != 3: print "Usage: %s grammar-filename python-filename" % (sys.argv[0], ) sys.exit(1) with open(sys.argv[1]) as infile: grammar = infile.read() g = OMeta(grammar) tree = g.parseGrammar("Parser") source = writePython(tree, grammar) + '\n' with open(sys.argv[2], 'w') as outfile: outfile.write(source)