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

Commit 60c1491d authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

refactorings finished: a) logging is now more or less thread safe + context...

refactorings finished: a) logging is now more or less thread safe + context managers make logging control more flexible; b) factory functions now return (thread local) singleton parser and compiler objects c) compiler suites now rely on factory methods for more orthogonality
parent 761284f8
...@@ -28,7 +28,7 @@ except ImportError: ...@@ -28,7 +28,7 @@ except ImportError:
import re import re
from .ebnf import EBNFGrammar, EBNFTransform, EBNFCompiler, grammar_changed, \ from .ebnf import EBNFGrammar, EBNFTransform, EBNFCompiler, grammar_changed, \
get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compiler get_ebnf_scanner, get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compiler
from .toolkit import logging, load_if_file, is_python_code, compile_python_object from .toolkit import logging, load_if_file, is_python_code, compile_python_object
from .parsers import GrammarBase, CompilerBase, compile_source, nil_scanner from .parsers import GrammarBase, CompilerBase, compile_source, nil_scanner
from .syntaxtree import Node from .syntaxtree import Node
...@@ -266,9 +266,9 @@ def load_compiler_suite(compiler_suite): ...@@ -266,9 +266,9 @@ def load_compiler_suite(compiler_suite):
except ValueError as error: except ValueError as error:
raise AssertionError('File "' + compiler_suite + '" seems to be corrupted. ' raise AssertionError('File "' + compiler_suite + '" seems to be corrupted. '
'Please delete or repair file manually.') 'Please delete or repair file manually.')
scanner = compile_python_object(imports + scanner_py, '\w*Scanner$') scanner = compile_python_object(imports + scanner_py, 'get_\w*_scanner$')
ast = compile_python_object(imports + ast_py, '\w*Transform$') ast = compile_python_object(imports + ast_py, 'get_\w*_transformer$')
compiler = compile_python_object(imports + compiler_py, '\w*Compiler$') compiler = compile_python_object(imports + compiler_py, 'get_\w*_compiler$')
else: else:
# assume source is an ebnf grammar # assume source is an ebnf grammar
with logging(False): with logging(False):
...@@ -276,10 +276,10 @@ def load_compiler_suite(compiler_suite): ...@@ -276,10 +276,10 @@ def load_compiler_suite(compiler_suite):
get_ebnf_grammar(), get_ebnf_transformer(), get_ebnf_compiler()) get_ebnf_grammar(), get_ebnf_transformer(), get_ebnf_compiler())
if errors: if errors:
raise GrammarError('\n\n'.join(errors), source) raise GrammarError('\n\n'.join(errors), source)
scanner = nil_scanner scanner = get_ebnf_scanner
ast = EBNFTransform ast = get_ebnf_transformer
compiler = EBNFCompiler compiler = get_ebnf_compiler
parser = compile_python_object(DHPARSER_IMPORTS + parser_py, '\w*Grammar$') parser = compile_python_object(DHPARSER_IMPORTS + parser_py, 'get_\w*_grammar$')
return scanner, parser, ast, compiler return scanner, parser, ast, compiler
...@@ -304,7 +304,7 @@ def is_outdated(compiler_suite, grammar_source): ...@@ -304,7 +304,7 @@ def is_outdated(compiler_suite, grammar_source):
""" """
try: try:
scanner, grammar, ast, compiler = load_compiler_suite(compiler_suite) scanner, grammar, ast, compiler = load_compiler_suite(compiler_suite)
return grammar_changed(grammar, grammar_source) return grammar_changed(grammar(), grammar_source)
except ValueError: except ValueError:
return True return True
...@@ -329,7 +329,7 @@ def run_compiler(text_or_file, compiler_suite): ...@@ -329,7 +329,7 @@ def run_compiler(text_or_file, compiler_suite):
CompilerError CompilerError
""" """
scanner, parser, ast, compiler = load_compiler_suite(compiler_suite) scanner, parser, ast, compiler = load_compiler_suite(compiler_suite)
return compileDSL(text_or_file, scanner, parser(), ast, compiler()) return compileDSL(text_or_file, scanner(), parser(), ast(), compiler())
def compile_on_disk(source_file, compiler_suite="", extension=".xml"): def compile_on_disk(source_file, compiler_suite="", extension=".xml"):
...@@ -369,19 +369,18 @@ def compile_on_disk(source_file, compiler_suite="", extension=".xml"): ...@@ -369,19 +369,18 @@ def compile_on_disk(source_file, compiler_suite="", extension=".xml"):
rootname = os.path.splitext(filepath)[0] rootname = os.path.splitext(filepath)[0]
compiler_name = os.path.basename(rootname) compiler_name = os.path.basename(rootname)
if compiler_suite: if compiler_suite:
scanner, pclass, trans, cclass = load_compiler_suite(compiler_suite) scanner, parser, trans, cfactory = load_compiler_suite(compiler_suite)
parser = pclass()
compiler1 = cclass()
else: else:
scanner = nil_scanner scanner = get_ebnf_scanner
parser = get_ebnf_grammar() parser = get_ebnf_grammar
trans = get_ebnf_transformer() trans = get_ebnf_transformer
compiler1 = get_ebnf_compiler(compiler_name, source_file) cfactory = get_ebnf_compiler
result, errors, ast = compile_source(source_file, scanner, parser, trans, compiler1) compiler1 = cfactory(compiler_name, source_file)
result, errors, ast = compile_source(source_file, scanner(), parser(), trans(), compiler1)
if errors: if errors:
return errors return errors
elif trans == EBNFTransform: # either an EBNF- or no compiler suite given elif trans == get_ebnf_transformer or trans == EBNFTransform: # either an EBNF- or no compiler suite given
global SECTION_MARKER, RX_SECTION_MARKER, SCANNER_SECTION, PARSER_SECTION, \ global SECTION_MARKER, RX_SECTION_MARKER, SCANNER_SECTION, PARSER_SECTION, \
AST_SECTION, COMPILER_SECTION, END_SECTIONS_MARKER AST_SECTION, COMPILER_SECTION, END_SECTIONS_MARKER
f = None f = None
......
...@@ -46,9 +46,9 @@ class TestCompilerGeneration: ...@@ -46,9 +46,9 @@ class TestCompilerGeneration:
f.write(self.trivial_text) f.write(self.trivial_text)
def teardown(self): def teardown(self):
for name in (self.grammar_name, self.compiler_name, self.text_name, self.result_name): # for name in (self.grammar_name, self.compiler_name, self.text_name, self.result_name):
if os.path.exists(name): # if os.path.exists(name):
os.remove(name) # os.remove(name)
pass pass
def test_compiling_functions(self): def test_compiling_functions(self):
......
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