File: bootstrap_test.py

package info (click to toggle)
python-tatsu 5.13.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: python: 10,202; makefile: 54
file content (177 lines) | stat: -rw-r--r-- 5,966 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# ruff: noqa: PLR0914
from __future__ import annotations

import json
import pickle
import py_compile
import shutil
import sys
import unittest
from pathlib import Path

from tatsu.ngcodegen import codegen
from tatsu.parser import EBNFParser, GrammarGenerator
from tatsu.parser_semantics import EBNFGrammarSemantics
from tatsu.util import asjson
from tatsu.walkers import DepthFirstWalker

tmp = Path('./tmp').resolve()
sys.path.insert(0, str(tmp))


class BootstrapTests(unittest.TestCase):
    def test_bootstrap(self):
        print()

        tmp = Path('./tmp')
        if (tmp / '00.ast').is_file():
            shutil.rmtree('./tmp')
        tmp.mkdir(exist_ok=True)
        print('-' * 20, 'phase 00 - parse using the bootstrap grammar')
        with Path('grammar/tatsu.ebnf').open() as f:
            text = str(f.read())
        g = EBNFParser('EBNFBootstrap')
        grammar0 = g.parse(text, parseinfo=False)
        ast0 = json.dumps(asjson(grammar0), indent=2)
        with Path('./tmp/00.ast').open('w') as f:
            f.write(ast0)

        print('-' * 20, 'phase 01 - parse with parser generator')
        with Path('grammar/tatsu.ebnf').open() as f:
            text = str(f.read())
        g = GrammarGenerator('EBNFBootstrap')
        result = g.parse(text)

        generated_grammar1 = str(result)
        with Path('./tmp/01.ebnf').open('w') as f:
            f.write(generated_grammar1)

        print(
            '-' * 20,
            'phase 02 - parse previous output with the parser generator',
        )
        with Path('./tmp/01.ebnf').open() as f:
            text = str(f.read())
        g = GrammarGenerator('EBNFBootstrap')
        result = g.parse(text)
        generated_grammar2 = str(result)
        with Path('./tmp/02.ebnf').open('w') as f:
            f.write(generated_grammar2)
        self.assertEqual(generated_grammar2, generated_grammar1)

        print('-' * 20, 'phase 03 - repeat')
        with Path('./tmp/02.ebnf').open() as f:
            text = f.read()
        g = EBNFParser('EBNFBootstrap')
        ast3 = g.parse(text)
        with Path('./tmp/03.ast').open('w') as f:
            f.write(json.dumps(asjson(ast3), indent=2))

        print('-' * 20, 'phase 04 - repeat')
        with Path('./tmp/02.ebnf').open() as f:
            text = f.read()
        g = GrammarGenerator('EBNFBootstrap')
        parser = g.parse(text)
        generated_grammar4 = str(parser)
        with Path('./tmp/04.ebnf').open('w') as f:
            f.write(generated_grammar4)
        self.assertEqual(generated_grammar4, generated_grammar2)

        print('-' * 20, 'phase 05 - parse using the grammar model')
        with Path('./tmp/04.ebnf').open() as f:
            text = f.read()
        ast5 = parser.parse(text)
        with Path('./tmp/05.ast').open('w') as f:
            f.write(json.dumps(asjson(ast5), indent=2))

        print('-' * 20, 'phase 06 - generate parser code')
        gencode6 = codegen(parser)
        with Path('./tmp/g06.py').open('w') as f:
            f.write(gencode6)

        print('-' * 20, 'phase 07 - import generated code')
        py_compile.compile('./tmp/g06.py', doraise=True)
        # g06 = __import__('g06')
        # GenParser = g06.EBNFBootstrapParser
        # assert GenParser

        # print('-' * 20, 'phase 08 - compile using generated code')
        # parser = GenParser(trace=False)
        # result = parser.parse(original_grammar, semantics=ASTSemantics, parseinfo=False)
        # ast8 = json.dumps(asjson(result), indent=2)
        # open('./tmp/08.ast', 'w').write(ast8)
        # self.assertEqual(ast0, ast8)

        print('-' * 20, 'phase 09 - Generate parser with semantics')
        with Path('grammar/tatsu.ebnf').open() as f:
            text = f.read()
        parser = GrammarGenerator('EBNFBootstrap')
        g9 = parser.parse(text)
        generated_grammar9 = str(g9)
        with Path('./tmp/09.ebnf').open('w') as f:
            f.write(generated_grammar9)
        self.assertEqual(generated_grammar9, generated_grammar1)

        print('-' * 20, 'phase 10 - Parse with a model using a semantics')
        g10 = g9.parse(
            text,
            start_rule='start',
            semantics=EBNFGrammarSemantics('EBNFBootstrap'),
        )
        generated_grammar10 = str(g10)
        with Path('./tmp/10.ebnf').open('w') as f:
            f.write(generated_grammar10)
        gencode10 = codegen(g10)
        with Path('./tmp/g10.py').open('w') as f:
            f.write(gencode10)

        print('-' * 20, 'phase 11 - Pickle the model and try again.')
        with Path('./tmp/11.tatsu').open('wb') as f:
            pickle.dump(g10, f)
        with Path('./tmp/11.tatsu').open('rb') as f:
            g11 = pickle.load(f)
        r11 = g11.parse(
            text,
            start_rule='start',
            semantics=EBNFGrammarSemantics('EBNFBootstrap'),
        )
        with Path('./tmp/11.ebnf').open('w') as f:
            f.write(str(g11))
        gencode11 = codegen(r11)
        with Path('./tmp/bootstrap_g11.py').open('w') as f:
            f.write(gencode11)

        print('-' * 20, 'phase 12 - Walker')

        class PrintNameWalker(DepthFirstWalker):
            def __init__(self):
                super().__init__()
                self.walked = []

            def walk_default(self, o, children):
                self.walked.append(o.__class__.__name__)

        v = PrintNameWalker()
        v.walk(g11)
        with Path('./tmp/12.txt').open('w') as f:
            f.write('\n'.join(v.walked))

        print('-' * 20, 'phase 13 - Graphics')
        try:
            from tatsu.diagrams import draw
        except ImportError:
            print('PyGraphViz not found!')
        else:
            draw('./tmp/13.png', g11)


def suite():
    return unittest.TestLoader().loadTestsFromTestCase(BootstrapTests)


def main():
    unittest.TextTestRunner(verbosity=2).run(suite())


if __name__ == '__main__':
    main()