dhparser.py 2.83 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
22
#TODO: This is still a stub...

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

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

32
33
34
35
36
37

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])
Eckhart Arnold's avatar
Eckhart Arnold committed
38
39
40
    parser = get_ebnf_grammar()
    transformer = get_ebnf_transformer()
    compiler = get_ebnf_compiler(compiler_name, grammar)
Eckhart Arnold's avatar
Eckhart Arnold committed
41
    result, errors, syntax_tree = compile_source(grammar, None, parser,
Eckhart Arnold's avatar
Eckhart Arnold committed
42
                                                 transformer, compiler)
43
44
    print(result)
    if errors:
45
        print('\n\n'.join(errors))
46
47
        sys.exit(1)
    else:
48
49
        # compile the grammar again using the result of the previous
        # compilation as parser
50
        print(type(result))
Eckhart Arnold's avatar
Eckhart Arnold committed
51
        result = compileDSL(grammar, nil_scanner, result, transformer, compiler)
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
        print(result)
    return result


def profile(func):
    import cProfile
    pr = cProfile.Profile()
    pr.enable()
    func()
    pr.disable()
    # after your program ends
    pr.print_stats(sort="tottime")


# # 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:
77
78
        _errors = compile_on_disk(sys.argv[1],
                                  sys.argv[2] if len(sys.argv) > 2 else "")
Eckhart Arnold's avatar
Eckhart Arnold committed
79
        if _errors:
80
            print('\n\n'.join(_errors))
81
82
83
84
            sys.exit(1)
    else:
        # run self test
        # selftest('EBNF/EBNF.ebnf')
Eckhart Arnold's avatar
Eckhart Arnold committed
85
86
        with logging(False):
            profile(partial(selftest, file_name='EBNF/EBNF.ebnf'))