21.10.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

test_dsl.py 4.33 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.parser import Grammar, Compiler
28
from DHParser.dsl import compile_on_disk, run_compiler, compileEBNF, grammar_provider, \
29
    load_compiler_suite
Eckhart Arnold's avatar
Eckhart Arnold committed
30
31
32
33
34
35
36
37
38
39
40
41

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):
42
        parser_src = compileEBNF(ARITHMETIC_EBNF)
43
        assert isinstance(parser_src, str), str(type(parser_src))
44
        assert parser_src.find('DSLGrammar') >= 0
45
        parser_src = compileEBNF(ARITHMETIC_EBNF, branding="CustomDSL")
46
        assert isinstance(parser_src, str), str(type(parser_src))
47
        assert parser_src.find('CustomDSLGrammar') >= 0
48
        factory = grammar_provider(ARITHMETIC_EBNF, branding="TestDSL")
49
50
        assert callable(factory)
        parser = factory()
51
52
53
54
55
        result = parser("5 + 3 * 4")
        assert not result.error_flag
        result = parser("5A + 4B ** 4C")
        assert result.error_flag

56

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

    def setup(self):
71
72
73
74
75
76
        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):
77
78
79
        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
80
        pass
81

82
83
84
85
86
87
88
89
    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)
90
        assert isinstance(parser, Grammar)
91
        assert callable(transformer)
92
        assert isinstance(compiler, Compiler)
93

94
95
96
97
98
99
100
101
102
103
104
105
106
    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
107
        # assert is_filename(self.text_name)
108
109
110
111
112
        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()
113

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

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


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