Esempio n. 1
0
 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')])
Esempio n. 2
0
 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')])
Esempio n. 3
0
 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')])
Esempio n. 4
0
 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
Esempio n. 5
0
 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
                         """))
Esempio n. 6
0
 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)
Esempio n. 7
0
 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)
Esempio n. 8
0
 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
                         """))
Esempio n. 9
0
    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
                            """))
Esempio n. 10
0
 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
                         """))
Esempio n. 11
0
 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
                         """))
Esempio n. 12
0
 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()])