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

Commit bebeac02 authored by eckhart's avatar eckhart
Browse files

- Grammar is not included in the docstring of parser classes by default, any more

parent c322ad0a
......@@ -4,8 +4,8 @@ DHParser Version 0.8.4 (10.11.2018)
- lookahead parsers can now be unit-tested (experimental)
- customized (faster) deepcopy of syntax trees
- better customization of XML-serialization of syntax-trees
- tollkit.GLOBALS as container for globel (actually thread-local)
variables and parameters added
- support for thread-safe globals and configuration data
via toolkit-module
DHParser Version 0.8.3 (20.8.2018)
......
......@@ -35,7 +35,7 @@ from DHParser.parse import Grammar, mixin_comment, Forward, RegExp, Whitespace,
from DHParser.preprocess import nil_preprocessor, PreprocessorFunc
from DHParser.syntaxtree import Node, RootNode, WHITESPACE_PTYPE, TOKEN_PTYPE
from DHParser.toolkit import load_if_file, escape_re, md5, sane_parser_name, re, expand_table, \
GLOBALS, typing
GLOBALS, CONFIG_PRESET, get_config_value, typing
from DHParser.transform import TransformationFunc, traverse, remove_brackets, \
reduce_single_child, replace_by_single_child, remove_expendables, \
remove_tokens, flatten, forbid, assert_content, remove_infix_operator
......@@ -577,17 +577,19 @@ class EBNFCompiler(Compiler):
# add EBNF grammar to the doc string of the parser class
article = 'an ' if self.grammar_name[0:1] in "AaEeIiOoUu" else 'a ' # what about 'hour', 'universe' etc.?
show_source = get_config_value('add_grammar_source_to_parser_docstring')
declarations = ['class ' + self.grammar_name
+ 'Grammar(Grammar):',
'r"""Parser for ' + article + self.grammar_name
+ ' source file'
+ (', with this grammar:' if self.grammar_source else '.')]
+ ('. Grammar:' if self.grammar_source and show_source else '.')]
definitions.append(('parser_initialization__', '"upon instantiation"'))
if self.grammar_source:
definitions.append(('source_hash__',
'"%s"' % md5(self.grammar_source, __version__)))
declarations.append('')
declarations += [line for line in self.grammar_source.split('\n')]
if show_source:
declarations += [line for line in self.grammar_source.split('\n')]
while declarations[-1].strip() == '':
declarations = declarations[:-1]
declarations.append('"""')
......@@ -1013,3 +1015,6 @@ def get_ebnf_compiler(grammar_name="", grammar_source="") -> EBNFCompiler:
compiler.set_grammar_name(grammar_name, grammar_source)
GLOBALS.ebnf_compiler_singleton = compiler
return compiler
CONFIG_PRESET['add_grammar_source_to_parser_docstring'] = False
......@@ -58,18 +58,62 @@ __all__ = ('escape_re',
'compile_python_object',
'smart_list',
'sane_parser_name',
'GLOBALS')
'GLOBALS',
'CONFIG_PRESET',
'get_config_value',
'set_config_value')
#######################################################################
#
# miscellaneous (generic)
# Thread local globals and configuration
#
#######################################################################
GLOBALS = threading.local()
GLOBALS.config = {}
CONFIG_PRESET = {}
def get_config_value(key):
"""
Retrieves a configuration value thread-safely.
:param key: the key (an immutable, usually a string)
:return: the value
"""
try:
cfg = GLOBALS.config
except AttributeError:
GLOBALS.config = {}
cfg = GLOBALS.config
try:
return cfg[key]
except KeyError:
value = CONFIG_PRESET[key]
GLOBALS.config[key] = value
return value
def set_config_value(key, value):
"""
Changes a configuration value thread-safely. The configuration
value will be set only for the current thread. In order to
set configuration values for any new thread, add the key and value
to CONFIG_PRESET, before the thread is started.
:param key: the key (an immutable, usually a string)
:param value: the value
"""
try:
_ = GLOBALS.config
except AttributeError:
GLOBALS.config = {}
GLOBALS.config[key] = value
#######################################################################
#
# miscellaneous (generic)
#
#######################################################################
def escape_re(strg: str) -> str:
......
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