Currently job artifacts in CI/CD pipelines on LRZ GitLab never expire. Starting from Wed 26.1.2022 the default expiration time will be 30 days (GitLab default). Currently existing artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

dhparser.py 4.14 KB
Newer Older
1
#!/usr/bin/python
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

"""dhparser.py - command line tool for DHParser

Copyright 2016  by Eckhart Arnold (arnold@badw.de)
                Bavarian Academy of Sciences an Humanities (badw.de)

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.
"""

21
#  TODO: This is still a stub...
22

23
24
25
26
import os
import sys
from functools import partial

27
from DHParser.dsl import compileDSL, compile_on_disk
Eckhart Arnold's avatar
Eckhart Arnold committed
28
from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compiler
29
from DHParser.parser import compile_source, nil_preprocessor
30
from DHParser.toolkit import logging
31

32

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# def selftest(file_name):
#     print(file_name)
#     with open('examples/' + file_name, encoding="utf-8") as f:
#         grammar = f.read()
#     compiler_name = os.path.basename(os.path.splitext(file_name)[0])
#     parser = get_ebnf_grammar()
#     print("\nAlphabetical List of Parsers:\n")
#     parser_list = sorted([p for p in parser.all_parsers__ if p.name], key=lambda p: p.name)
#     for p in parser_list:
#         print(p)
#     print('\n\n')
#     transformer = get_ebnf_transformer()
#     compiler = get_ebnf_compiler(compiler_name, grammar)
#     result, errors, syntax_tree = compile_source(grammar, None, parser,
#                                                  transformer, compiler)
#     print(result)
#     if errors:
50
#         print('\n\n'.join_children(errors))
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#         sys.exit(1)
#     else:
#         # compile the grammar again using the result of the previous
#         # compilation as parser
#         for i in range(1):
#             result = compileDSL(grammar, nil_preprocessor, result, transformer, compiler)
#         print(result)
#     return result



def selftest() -> bool:
    print("DHParser selftest...")
    print("\nSTAGE I:  Trying to compile EBNF-Grammar:\n")
    builtin_ebnf_parser = get_ebnf_grammar()
    ebnf_src = builtin_ebnf_parser.__doc__[builtin_ebnf_parser.__doc__.find('#'):]
    ebnf_transformer = get_ebnf_transformer()
    ebnf_compiler = get_ebnf_compiler('EBNF')
    generated_ebnf_parser, errors, ast = compile_source(ebnf_src, None,
        builtin_ebnf_parser, ebnf_transformer, ebnf_compiler)

72
    if errors:
73
74
75
76
77
78
79
80
81
82
83
        print("Selftest FAILED :-(")
        print("\n\n".join(errors))
        return False
    print(generated_ebnf_parser)
    print("\n\nSTAGE 2: Selfhosting-test: Trying to compile EBNF-Grammar with generated parser...\n")
    selfhosted_ebnf_parser = compileDSL(ebnf_src, None, generated_ebnf_parser,
                                        ebnf_transformer, ebnf_compiler)
    print(selfhosted_ebnf_parser)
    print("\n\n Selftest SUCCEEDED :-)\n\n")
    return True

84
85
86
87
88
89


def profile(func):
    import cProfile
    pr = cProfile.Profile()
    pr.enable()
90
91
92
93
    for i in range(1):
        success = func()
        if not success:
            break
94
95
96
    pr.disable()
    # after your program ends
    pr.print_stats(sort="tottime")
97
    return success
98
99
100
101
102
103
104
105
106
107
108
109
110


# # Changes in the EBNF source that are not reflected in this file could be
# # a source of sometimes obscure errors! Therefore, we will check this.
# if (os.path.exists('examples/EBNF/EBNF.ebnf')
#     and source_changed('examples/EBNF/EBNF.ebnf', EBNFGrammar)):
#     assert False, "WARNING: Grammar source has changed. The parser may not " \
#         "represent the actual grammar any more!!!"
#     pass

if __name__ == "__main__":
    print(sys.argv)
    if len(sys.argv) > 1:
111
112
        _errors = compile_on_disk(sys.argv[1],
                                  sys.argv[2] if len(sys.argv) > 2 else "")
Eckhart Arnold's avatar
Eckhart Arnold committed
113
        if _errors:
114
            print('\n\n'.join(_errors))
115
116
117
118
            sys.exit(1)
    else:
        # run self test
        # selftest('EBNF/EBNF.ebnf')
119
        with logging(False):
120
121
122
            if not profile(selftest):
                sys.exit(1)