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

Commit 4b26772d authored by Eckhart Arnold's avatar Eckhart Arnold

- split ParserCombinators.py into different modules: version, logging,...

- split ParserCombinators.py into different modules: version, logging, syntaxtree, parser, EBNFcompiler, DSLsupport, dhparser
parent 96853fb9
...@@ -14,6 +14,8 @@ testdata/*.pdf ...@@ -14,6 +14,8 @@ testdata/*.pdf
*~ *~
*.old *.old
DEBUG* DEBUG*
LOGS*
LOGS/
external_resources/ external_resources/
tmp/ tmp/
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/python3
"""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.
"""
import os
import sys
from functools import partial
from DSLsupport import compileDSL, run_compiler
from EBNFcompiler import EBNFGrammar, EBNFTransTable, EBNFCompiler
from parser import full_compilation
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])
compiler = EBNFCompiler(compiler_name, grammar)
parser = EBNFGrammar()
result, errors, syntax_tree = full_compilation(grammar,
parser, EBNFTransTable, compiler)
print(result)
if errors:
print(errors)
sys.exit(1)
else:
result = compileDSL(grammar, result, EBNFTransTable, compiler)
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:
_errors = run_compiler(sys.argv[1],
sys.argv[2] if len(sys.argv) > 2 else "")
if (_errors):
print(_errors)
sys.exit(1)
else:
# run self test
# selftest('EBNF/EBNF.ebnf')
profile(partial(selftest, file_name='EBNF/EBNF.ebnf'))
#!/usr/bin/python3
"""logging.py - basic log file support 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.
Module ``logging`` defines the global variable LOGGING which contains
the name of a directory where log files shall be placed. By setting
its value to the empty string "" logging can be turned off.
To read the directory name function ``LOGS_DIR()`` should be called
rather than reading the variable LOGGING. ``LOGS_DIR()`` makes sure
the directory exists and raises an error if a file with the same name
already exists.
"""
import os
LOGGING: str = "LOGS" # LOGGING = "" turns logging off!
def LOGS_DIR() -> str:
"""Returns a path of a directory where log files will be stored.
The default name of the logging directory is taken from the LOGGING
variabe (default value 'LOGS'). The directory will be created if it
does not exist. If the directory name does not contain a leading
slash '/' it will be created as a subdirectory of the current
directory Any files in the logging directory can be overwritten!
Raises:
AssertionError if logging has been turned off
Returns:
name of the logging directory
"""
global LOGGING
if not LOGGING:
raise AssertionError("Cannot use LOGGING_DIR() if logging is turned off!")
dirname = LOGGING
if os.path.exists(LOGGING):
if not os.path.isdir(LOGGING):
raise IOError('"' + LOGGING + '" cannot be used as log directory, '
'because it is not a directory!')
else:
os.mkdir(LOGGING)
info_file_name = os.path.join(LOGGING, 'info.txt')
if not os.path.exists(info_file_name):
with open(info_file_name, 'w') as f:
f.write("This directory has been created by DHParser to store log files from\n"
"parsing. ANY FILE IN THIS DIRECTORY CAN BE OVERWRITTEN! Therefore,\n"
"do not place any files here or edit existing files in this directory\n"
"manually.\n")
return dirname
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/python3
"""version.py - Version number of 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.
"""
import os
__version__ = '0.5.4' + '_dev' + str(os.stat(__file__).st_mtime)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment