test_dsl.py 4.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/python3

"""test_dsl.py - tests of the dsl-module of DHParser 


Author: Eckhart Arnold <arnold@badw.de>

Copyright 2017 Bavarian Academy of Sciences and Humanities

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

23
24
import os
import sys
Eckhart Arnold's avatar
Eckhart Arnold committed
25
sys.path.extend(['../', './'])
26

27
from DHParser.parsers import Grammar, Compiler
28
from DHParser.dsl import compile_on_disk, run_compiler, compileEBNF, parser_factory, \
29
    load_compiler_suite
30
from DHParser.toolkit import is_filename
Eckhart Arnold's avatar
Eckhart Arnold committed
31
32
33
34
35
36
37
38
39
40
41
42
43


ARITHMETIC_EBNF = """
    @ whitespace = linefeed
    formula = [ //~ ] expr
    expr = expr ("+"|"-") term | term
    term = term ("*"|"/") factor | factor
    factor = /[0-9]+/~
    # example:  "5 + 3 * 4"
    """

class TestCompileFunctions:
    def test_compileEBNF(self):
44
        parser_src = compileEBNF(ARITHMETIC_EBNF)
45
        assert isinstance(parser_src, str), str(type(parser_src))
46
        assert parser_src.find('DSLGrammar') >= 0
47
        parser_src = compileEBNF(ARITHMETIC_EBNF, branding="CustomDSL")
48
        assert isinstance(parser_src, str), str(type(parser_src))
49
        assert parser_src.find('CustomDSLGrammar') >= 0
50
51
52
        factory = parser_factory(ARITHMETIC_EBNF, branding="TestDSL")
        assert callable(factory)
        parser = factory()
53
54
55
56
57
        result = parser("5 + 3 * 4")
        assert not result.error_flag
        result = parser("5A + 4B ** 4C")
        assert result.error_flag

58

59
60
class TestCompilerGeneration:
    trivial_lang = """
61
        text = { word | WSPC } "." [/\s/]
62
        word = /\w+/
63
        WSPC = /\s+/
64
        """
65
    tmp = 'tmp/' if os.path.isdir('tmp') else ('test/tmp/')
66
    trivial_text = u"""Es war ein Koenig in Thule.\n"""
67
    grammar_name = tmp + "TestCompilerGeneration.ebnf"
68
    compiler_name = tmp + "TestCompilerGenerationCompiler.py"
69
70
    text_name = tmp + "TestCompilerGeneration_text.txt"
    result_name = tmp + "TestCompilerGeneration_text.xml"
71
72

    def setup(self):
73
74
75
76
77
78
        with open(self.grammar_name, "w") as f:
            f.write(self.trivial_lang)
        with open(self.text_name, "w") as f:
            f.write(self.trivial_text)

    def teardown(self):
79
80
81
        for name in (self.grammar_name, self.compiler_name, self.text_name, self.result_name):
            if os.path.exists(name):
                os.remove(name)
Eckhart Arnold's avatar
Eckhart Arnold committed
82
        pass
83

84
85
86
87
88
89
90
91
    def test_load_compiler_suite(self):
        src = compileEBNF(self.trivial_lang, "Trivial")
        scanner, parser, transformer, compiler = load_compiler_suite(src)
        scanner = scanner()
        parser = parser()
        transformer = transformer()
        compiler = compiler()
        assert callable(scanner)
92
        assert isinstance(parser, Grammar)
93
        assert callable(transformer)
94
        assert isinstance(compiler, Compiler)
95

96
97
98
99
100
101
102
103
104
105
106
107
108
    def test_compiling_functions(self):
        # test if cutting and reassembling of compiler suite works:
        errors = compile_on_disk(self.grammar_name)
        assert not errors
        with open(self.compiler_name, 'r') as f:
            compiler_suite = f.read()
        errors = compile_on_disk(self.grammar_name)
        assert not errors
        with open(self.compiler_name, 'r') as f:
            compiler_suite_2nd_run = f.read()
        assert compiler_suite == compiler_suite_2nd_run

        # test compiling with a generated compiler suite
109
        # assert is_filename(self.text_name)
110
111
112
113
114
        errors = compile_on_disk(self.text_name, self.compiler_name)
        assert not errors, str(errors)
        assert os.path.exists(self.result_name)
        with open(self.result_name, 'r') as f:
            output = f.read()
115

116
117
118
        # test compiling in memory
        result = run_compiler(self.trivial_text, self.compiler_name)
        assert output == result.as_xml(), str(result)
119

120
        sys.path.append(self.tmp)
121
122
        from TestCompilerGenerationCompiler import compile_src
        result, errors, ast = compile_src(self.trivial_text)
123
124
125


if __name__ == "__main__":
126
    from DHParser.testing import runner
127
    runner("", globals())