verarbeite_Beispiele.py 1.5 KB
Newer Older
di68kap's avatar
di68kap committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import os
import sys

sys.path.extend(['../../', '../', './'])

from DHParser.toolkit import logging
from DHParser.parser import compile_source
from DHParser.dsl import recompile_grammar

if not recompile_grammar('MLW.ebnf', force=False):  # recompiles Grammar only if it has changed
    with open('MLW_ebnf_ERRORS.txt') as f:
        print(f.read())
    sys.exit(1)


from MLWCompiler import get_preprocessor, get_grammar, get_transformer, get_compiler

save_path = os.getcwd()
os.chdir("Beispiele")
for entry in os.listdir():
    if entry.lower().endswith('.mlw'):
di68kap's avatar
di68kap committed
22
        print('\n Parse: ' + entry)
di68kap's avatar
di68kap committed
23
24
25
26
27
28
29
        raw_name = os.path.splitext(entry)[0]
        with logging(True):
            result, messages, AST = compile_source(entry,
                                                   get_preprocessor(),
                                                   get_grammar(),
                                                   get_transformer(),
                                                   get_compiler())
di68kap's avatar
di68kap committed
30
31
32
        if AST:
            with open(raw_name + '.ast', 'w', encoding='utf-8') as f:
                f.write(AST.as_sxpr(compact=False))
di68kap's avatar
di68kap committed
33
34
35
36
37
38
39
40
41
42
43
        if messages:
            print("Errors in: " + entry)
            with open(raw_name + '.messages', 'w', encoding='utf-8') as f:
                for m in messages:
                    s = str(m)
                    print(s)
                    f.write(s)
                    f.write('\n')
        else:
            print("\nParsing of %s successfull :-)\n" % entry)
os.chdir(save_path)