def test_superApply(self): one = t.Action("1") x = t.Action("x") a = t.Apply("super", "main", [one, x]) self.assertEqual(writeBytecode(a), [t.Python('1'), t.Push(), t.Python('x'), t.Push(), t.SuperCall('main')])
def test_apply(self): one = t.Action("1") x = t.Action("x") a = t.Apply("foo", "main", [one, x]) self.assertEqual(writeBytecode(a), [t.Python('1'), t.Push(), t.Python('x'), t.Push(), t.Call('foo')])
def test_foreignApply(self): one = t.Action("1") x = t.Action("x") a = t.ForeignApply("thegrammar", "foo", "main", [one, x]) self.assertEqual(writeBytecode(a), [t.Python('1'), t.Push(), t.Python('x'), t.Push(), t.ForeignCall('thegrammar', 'foo')])
def semanticActionExpr(self): """ Find and generate code for a Python expression terminated by a close-paren, whose return value is ignored. """ val = t.Action(self.pythonExpr(')')[0][0]) self.exactly(')') return val
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 semanticPredicateExpr(self): """ Find and generate code for a Python expression terminated by a close-paren, whose return value determines the success of the pattern it's in. """ expr = t.Action(self.pythonExpr(')')[0][0]) self.exactly(')') return t.Predicate(expr)
def ruleValueExpr(self, singleLine): """ Find and generate code for a Python expression terminated by a close paren/brace or end of line. """ (expr, endchar), err = self.pythonExpr(endChars="\r\n)]") # if str(endchar) in ")]" or (singleLine and endchar): # self.input = self.input.prev() return t.Action(expr)
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_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_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 applicationArgs(self, finalChar): """ Collect rule arguments, a list of Python expressions separated by spaces. """ args = [] while True: try: (arg, endchar), err = self.pythonExpr(" " + finalChar) if not arg: break args.append(t.Action(arg)) if endchar == finalChar: break if endchar == ' ': self.rule_anything() except ParseError: break if args: return args else: raise self.input.nullError()
def test_pred(self): x = t.Predicate(t.Action("doStuff()")) self.assertEqual( writeBytecode(x), [t.Python('doStuff()'), t.Predicate()])