Beispiel #1
0
    def test_singleOr(self):
        """
        Test code generation for a sequence of alternatives.
        """

        x1 = self.builder._or([self.builder.exactly("x")])
        x = self.builder.exactly("x")
        self.assertEqual(writePython(x), writePython(x1))
Beispiel #2
0
    def test_singleOr(self):
        """
        Test code generation for a sequence of alternatives.
        """

        x1 = self.builder._or([self.builder.exactly("x")])
        x = self.builder.exactly("x")
        self.assertEqual(writePython(x), writePython(x1))
Beispiel #3
0
    def test_grammar(self):
        """
        Test generation of an entire grammar.
        """
        r1 = self.builder.rule("foo", self.builder.exactly("x"))
        r2 = self.builder.rule("baz", self.builder.exactly("y"))
        x = self.builder.makeGrammar([r1, r2])
        self.assertEqual(
            writePython(x),
            dd("""
                            class BuilderTest(GrammarBase):
                                def rule_foo(self):
                                    _locals = {'self': self}
                                    self.locals['foo'] = _locals
                                    _G_exactly_1, lastError = self.exactly('x')
                                    self.considerError(lastError)
                                    return (_G_exactly_1, self.currentError)


                                def rule_baz(self):
                                    _locals = {'self': self}
                                    self.locals['baz'] = _locals
                                    _G_exactly_1, lastError = self.exactly('y')
                                    self.considerError(lastError)
                                    return (_G_exactly_1, self.currentError)
                            """))
Beispiel #4
0
    def test_grammar(self):
        """
        Test generation of an entire grammar.
        """
        r1 = self.builder.rule("foo", self.builder.exactly("x"))
        r2 = self.builder.rule("baz", self.builder.exactly("y"))
        x = self.builder.makeGrammar([r1, r2])
        self.assertEqual(writePython(x),
                         dd("""
                            class BuilderTest(GrammarBase):
                                def rule_foo(self):
                                    _locals = {'self': self}
                                    self.locals['foo'] = _locals
                                    _G_exactly_1, lastError = self.exactly('x')
                                    self.considerError(lastError)
                                    return (_G_exactly_1, self.currentError)


                                def rule_baz(self):
                                    _locals = {'self': self}
                                    self.locals['baz'] = _locals
                                    _G_exactly_1, lastError = self.exactly('y')
                                    self.considerError(lastError)
                                    return (_G_exactly_1, self.currentError)
                            """))
Beispiel #5
0
 def test_expr(self):
     """
     Test code generation for semantic predicates.
     """
     x = self.builder.expr("returnStuff()")
     self.assertEqual(writePython(x),
                      dd("""
                         _G_python_1, lastError = eval('returnStuff()', self.globals, _locals), None
                         self.considerError(lastError)
                         _G_python_1
                         """))
Beispiel #6
0
 def test_action(self):
     """
     Test code generation for semantic actions.
     """
     x = self.builder.action("doStuff()")
     self.assertEqual(writePython(x),
                      dd("""
                         _G_python_1, lastError = eval('doStuff()', self.globals, _locals), None
                         self.considerError(lastError)
                         _G_python_1
                         """))
Beispiel #7
0
 def test_action(self):
     """
     Test code generation for semantic actions.
     """
     x = self.builder.action("doStuff()")
     self.assertEqual(
         writePython(x),
         dd("""
                         _G_python_1, lastError = eval('doStuff()', self.globals, _locals), None
                         self.considerError(lastError)
                         _G_python_1
                         """))
Beispiel #8
0
 def test_expr(self):
     """
     Test code generation for semantic predicates.
     """
     x = self.builder.expr("returnStuff()")
     self.assertEqual(
         writePython(x),
         dd("""
                         _G_python_1, lastError = eval('returnStuff()', self.globals, _locals), None
                         self.considerError(lastError)
                         _G_python_1
                         """))
Beispiel #9
0
    def test_exactly(self):
        """
        Test generation of code for the 'exactly' pattern.
        """

        x = self.builder.exactly("x")
        self.assertEqual(writePython(x),
                         dd("""
                            _G_exactly_1, lastError = self.exactly('x')
                            self.considerError(lastError)
                            _G_exactly_1
                            """))
Beispiel #10
0
    def test_exactly(self):
        """
        Test generation of code for the 'exactly' pattern.
        """

        x = self.builder.exactly("x")
        self.assertEqual(
            writePython(x),
            dd("""
                            _G_exactly_1, lastError = self.exactly('x')
                            self.considerError(lastError)
                            _G_exactly_1
                            """))
Beispiel #11
0
 def test_bind(self):
     """
     Test code generation for variable assignment.
     """
     x = self.builder.exactly("x")
     b = self.builder.bind(x, "var")
     self.assertEqual(writePython(b),
                      dd("""
                         _G_exactly_1, lastError = self.exactly('x')
                         self.considerError(lastError)
                         _locals['var'] = _G_exactly_1
                         _locals['var']
                         """))
Beispiel #12
0
 def test_bind(self):
     """
     Test code generation for variable assignment.
     """
     x = self.builder.exactly("x")
     b = self.builder.bind(x, "var")
     self.assertEqual(
         writePython(b),
         dd("""
                         _G_exactly_1, lastError = self.exactly('x')
                         self.considerError(lastError)
                         _locals['var'] = _G_exactly_1
                         _locals['var']
                         """))
Beispiel #13
0
    def test_rule(self):
        """
        Test generation of entire rules.
        """

        x = self.builder.rule("foo", self.builder.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)
                                return (_G_exactly_1, self.currentError)
                            """))
Beispiel #14
0
 def test_sequence(self):
     """
     Test generation of code for sequence patterns.
     """
     x = self.builder.exactly("x")
     y = self.builder.exactly("y")
     z = self.builder.sequence([x, y])
     self.assertEqual(writePython(z),
                      dd("""
                         _G_exactly_1, lastError = self.exactly('x')
                         self.considerError(lastError)
                         _G_exactly_2, lastError = self.exactly('y')
                         self.considerError(lastError)
                         _G_exactly_2
                         """))
Beispiel #15
0
 def test_not(self):
     """
     Test code generation for negated terms.
     """
     x = self.builder._not(self.builder.exactly("x"))
     self.assertEqual(writePython(x),
                      dd("""
                         def _G_not_1():
                             _G_exactly_1, lastError = self.exactly('x')
                             self.considerError(lastError)
                             return (_G_exactly_1, self.currentError)
                         _G_not_2, lastError = self._not(_G_not_1)
                         self.considerError(lastError)
                         _G_not_2
                         """))
Beispiel #16
0
 def test_pred(self):
     """
     Test code generation for predicate expressions.
     """
     x = self.builder.pred(self.builder.exactly("x"))
     self.assertEqual(writePython(x),
                      dd("""
                         def _G_pred_1():
                             _G_exactly_1, lastError = self.exactly('x')
                             self.considerError(lastError)
                             return (_G_exactly_1, self.currentError)
                         _G_pred_2, lastError = self.pred(_G_pred_1)
                         self.considerError(lastError)
                         _G_pred_2
                         """))
Beispiel #17
0
 def test_listpattern(self):
     """
     Test code generation for list patterns.
     """
     x = self.builder.listpattern(self.builder.exactly("x"))
     self.assertEqual(writePython(x),
                      dd("""
                         def _G_listpattern_1():
                             _G_exactly_1, lastError = self.exactly('x')
                             self.considerError(lastError)
                             return (_G_exactly_1, self.currentError)
                         _G_listpattern_2, lastError = self.listpattern(_G_listpattern_1)
                         self.considerError(lastError)
                         _G_listpattern_2
                         """))
Beispiel #18
0
 def test_pred(self):
     """
     Test code generation for predicate expressions.
     """
     x = self.builder.pred(self.builder.exactly("x"))
     self.assertEqual(
         writePython(x),
         dd("""
                         def _G_pred_1():
                             _G_exactly_1, lastError = self.exactly('x')
                             self.considerError(lastError)
                             return (_G_exactly_1, self.currentError)
                         _G_pred_2, lastError = self.pred(_G_pred_1)
                         self.considerError(lastError)
                         _G_pred_2
                         """))
Beispiel #19
0
 def test_not(self):
     """
     Test code generation for negated terms.
     """
     x = self.builder._not(self.builder.exactly("x"))
     self.assertEqual(
         writePython(x),
         dd("""
                         def _G_not_1():
                             _G_exactly_1, lastError = self.exactly('x')
                             self.considerError(lastError)
                             return (_G_exactly_1, self.currentError)
                         _G_not_2, lastError = self._not(_G_not_1)
                         self.considerError(lastError)
                         _G_not_2
                         """))
Beispiel #20
0
 def test_sequence(self):
     """
     Test generation of code for sequence patterns.
     """
     x = self.builder.exactly("x")
     y = self.builder.exactly("y")
     z = self.builder.sequence([x, y])
     self.assertEqual(
         writePython(z),
         dd("""
                         _G_exactly_1, lastError = self.exactly('x')
                         self.considerError(lastError)
                         _G_exactly_2, lastError = self.exactly('y')
                         self.considerError(lastError)
                         _G_exactly_2
                         """))
Beispiel #21
0
    def test_rule(self):
        """
        Test generation of entire rules.
        """

        x = self.builder.rule("foo", self.builder.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)
                                return (_G_exactly_1, self.currentError)
                            """))
Beispiel #22
0
 def test_listpattern(self):
     """
     Test code generation for list patterns.
     """
     x = self.builder.listpattern(self.builder.exactly("x"))
     self.assertEqual(
         writePython(x),
         dd("""
                         def _G_listpattern_1():
                             _G_exactly_1, lastError = self.exactly('x')
                             self.considerError(lastError)
                             return (_G_exactly_1, self.currentError)
                         _G_listpattern_2, lastError = self.listpattern(_G_listpattern_1)
                         self.considerError(lastError)
                         _G_listpattern_2
                         """))
Beispiel #23
0
 def test_optional(self):
     """
     Test code generation for optional terms.
     """
     x = self.builder.optional(self.builder.exactly("x"))
     self.assertEqual(writePython(x),
                      dd("""
                         def _G_optional_1():
                             _G_exactly_1, lastError = self.exactly('x')
                             self.considerError(lastError)
                             return (_G_exactly_1, self.currentError)
                         def _G_optional_2():
                             return (None, self.input.nullError())
                         _G_or_3, lastError = self._or([_G_optional_1, _G_optional_2])
                         self.considerError(lastError)
                         _G_or_3
                         """))
Beispiel #24
0
    def test_many1(self):
        """
        Test generation of code for matching one or more instances of
        a pattern.
        """

        xs = self.builder.many1(self.builder.exactly("x"))
        self.assertEqual(writePython(xs),
                         dd("""
                            def _G_many1_1():
                                _G_exactly_1, lastError = self.exactly('x')
                                self.considerError(lastError)
                                return (_G_exactly_1, self.currentError)
                            _G_many1_2, lastError = self.many(_G_many1_1, _G_many1_1())
                            self.considerError(lastError)
                            _G_many1_2
                            """))
Beispiel #25
0
    def test_many1(self):
        """
        Test generation of code for matching one or more instances of
        a pattern.
        """

        xs = self.builder.many1(self.builder.exactly("x"))
        self.assertEqual(
            writePython(xs),
            dd("""
                            def _G_many1_1():
                                _G_exactly_1, lastError = self.exactly('x')
                                self.considerError(lastError)
                                return (_G_exactly_1, self.currentError)
                            _G_many1_2, lastError = self.many(_G_many1_1, _G_many1_1())
                            self.considerError(lastError)
                            _G_many1_2
                            """))
Beispiel #26
0
 def test_optional(self):
     """
     Test code generation for optional terms.
     """
     x = self.builder.optional(self.builder.exactly("x"))
     self.assertEqual(
         writePython(x),
         dd("""
                         def _G_optional_1():
                             _G_exactly_1, lastError = self.exactly('x')
                             self.considerError(lastError)
                             return (_G_exactly_1, self.currentError)
                         def _G_optional_2():
                             return (None, self.input.nullError())
                         _G_or_3, lastError = self._or([_G_optional_1, _G_optional_2])
                         self.considerError(lastError)
                         _G_or_3
                         """))
Beispiel #27
0
 def test_superApply(self):
     """
     Test generation of code for calling the superclass' implementation of
     the current rule.
     """
     one = self.builder.expr("1")
     x = self.builder.expr("x")
     a = self.builder.apply("super", "main", one, x)
     self.assertEqual(writePython(a),
                      dd("""
                         _G_python_1, lastError = eval('1', self.globals, _locals), None
                         self.considerError(lastError)
                         _G_python_2, lastError = eval('x', self.globals, _locals), None
                         self.considerError(lastError)
                         _G_apply_3, lastError = self.superApply("main", _G_python_1, _G_python_2)
                         self.considerError(lastError)
                         _G_apply_3
                         """))
Beispiel #28
0
    def test_apply(self):
        """
        Test generation of code for rule application.
        """

        one = self.builder.expr("1")
        x = self.builder.expr("x")
        a = self.builder.apply("foo", "main", one, x)
        self.assertEqual(writePython(a),
                         dd("""
                            _G_python_1, lastError = eval('1', self.globals, _locals), None
                            self.considerError(lastError)
                            _G_python_2, lastError = eval('x', self.globals, _locals), None
                            self.considerError(lastError)
                            _G_apply_3, lastError = self.apply("foo", _G_python_1, _G_python_2)
                            self.considerError(lastError)
                            _G_apply_3
                            """))
Beispiel #29
0
    def test_apply(self):
        """
        Test generation of code for rule application.
        """

        one = self.builder.expr("1")
        x = self.builder.expr("x")
        a = self.builder.apply("foo", "main", one, x)
        self.assertEqual(
            writePython(a),
            dd("""
                            _G_python_1, lastError = eval('1', self.globals, _locals), None
                            self.considerError(lastError)
                            _G_python_2, lastError = eval('x', self.globals, _locals), None
                            self.considerError(lastError)
                            _G_apply_3, lastError = self._apply(self.rule_foo, "foo", [_G_python_1, _G_python_2])
                            self.considerError(lastError)
                            _G_apply_3
                            """))
Beispiel #30
0
 def test_superApply(self):
     """
     Test generation of code for calling the superclass' implementation of
     the current rule.
     """
     one = self.builder.expr("1")
     x = self.builder.expr("x")
     a = self.builder.apply("super", "main", one, x)
     self.assertEqual(
         writePython(a),
         dd("""
                         _G_python_1, lastError = eval('1', self.globals, _locals), None
                         self.considerError(lastError)
                         _G_python_2, lastError = eval('x', self.globals, _locals), None
                         self.considerError(lastError)
                         _G_apply_3, lastError = self.superApply("main", _G_python_1, _G_python_2)
                         self.considerError(lastError)
                         _G_apply_3
                         """))
Beispiel #31
0
    def generate_parser_module(self):
        from pymeta.grammar import OMetaGrammar
        from pymeta import builder

        tree = OMetaGrammar(self.grammar).parseGrammar(self.classname,
                                                       builder.TreeBuilder)
        with open(os.path.join(self.src_dir, 'pymeta', 'runtime.py')) as fp:
            runtime_str = fp.read()
        with open(os.path.join(self.src_dir, self.filename), 'w') as fp:
            fp.write('# pylint: disable=C0103,C0301,C0302,R0201,'
                     'R0903,R0904,R0912,R0914\n\n')
            fp.write(runtime_str)
            fp.write('\n\n')
            fp.write('%s = """\n%s\n"""\n\n' % (self.grammar_constant_name,
                                                self.grammar))
            fp.write('GrammarBase = OMetaBase\n')
            fp.write('\n\n')

            parser_cls_code = builder.writePython(tree)
            fp.write(parser_cls_code)
Beispiel #32
0
    def test_or(self):
        """
        Test code generation for a sequence of alternatives.
        """

        xy = self.builder._or([self.builder.exactly("x"),
                               self.builder.exactly("y")])
        self.assertEqual(writePython(xy),
                         dd("""
                            def _G_or_1():
                                _G_exactly_1, lastError = self.exactly('x')
                                self.considerError(lastError)
                                return (_G_exactly_1, self.currentError)
                            def _G_or_2():
                                _G_exactly_1, lastError = self.exactly('y')
                                self.considerError(lastError)
                                return (_G_exactly_1, self.currentError)
                            _G_or_3, lastError = self._or([_G_or_1, _G_or_2])
                            self.considerError(lastError)
                            _G_or_3
                            """))
Beispiel #33
0
    def test_or(self):
        """
        Test code generation for a sequence of alternatives.
        """

        xy = self.builder._or(
            [self.builder.exactly("x"),
             self.builder.exactly("y")])
        self.assertEqual(
            writePython(xy),
            dd("""
                            def _G_or_1():
                                _G_exactly_1, lastError = self.exactly('x')
                                self.considerError(lastError)
                                return (_G_exactly_1, self.currentError)
                            def _G_or_2():
                                _G_exactly_1, lastError = self.exactly('y')
                                self.considerError(lastError)
                                return (_G_exactly_1, self.currentError)
                            _G_or_3, lastError = self._or([_G_or_1, _G_or_2])
                            self.considerError(lastError)
                            _G_or_3
                            """))