Commit 9a4086af authored by di68kap's avatar di68kap
Browse files

- DHParser/parse.py: bug fix. synonym for anonymous whitespace

parent f69ae607
......@@ -318,9 +318,13 @@ CONFIG_PRESET['server_default_port'] = 8888
# Turn on (costly) debugging functionality for any of the respective
# modules or subsystems.
# Default value: always False
# Default value: False
CONFIG_PRESET['debug_compiler'] = False
# Makes DHParser.dsl.grammar_provider() write generated Python code to
# the log-file (if logging is on) or to the console (if logging is off)
# Default value: False
CONFIG_PRESET['compiled_EBNF_log'] = ''
########################################################################
#
......
......@@ -33,7 +33,7 @@ from DHParser.ebnf import EBNFCompiler, grammar_changed, DHPARSER_IMPORTS, \
get_ebnf_preprocessor, get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compiler, \
PreprocessorFactoryFunc, ParserFactoryFunc, TransformerFactoryFunc, CompilerFactoryFunc
from DHParser.error import Error, is_error, has_errors, only_errors
from DHParser.log import suspend_logging, resume_logging
from DHParser.log import suspend_logging, resume_logging, is_logging, append_log
from DHParser.parse import Grammar
from DHParser.preprocess import nil_preprocessor, PreprocessorFunc
from DHParser.syntaxtree import Node
......@@ -243,12 +243,12 @@ def compileEBNF(ebnf_src: str, branding="DSL") -> str:
def grammar_provider(ebnf_src: str, branding="DSL") -> Grammar:
"""
Compiles an EBNF grammar and returns a grammar-parser provider
Compiles an EBNF-grammar and returns a grammar-parser provider
function for that grammar.
Args:
ebnf_src(str): Either the file name of an EBNF grammar or
the EBNF grammar itself as a string.
the EBNF-grammar itself as a string.
branding (str or bool): Branding name for the compiler
suite source code.
......@@ -258,6 +258,12 @@ def grammar_provider(ebnf_src: str, branding="DSL") -> Grammar:
"""
grammar_src = compileDSL(ebnf_src, nil_preprocessor, get_ebnf_grammar(),
get_ebnf_transformer(), get_ebnf_compiler(branding, ebnf_src))
log_name = get_config_value('compiled_EBNF_log')
if log_name:
if is_logging():
append_log(log_name, grammar_src)
else:
print(grammar_src)
grammar_factory = compile_python_object(DHPARSER_IMPORTS + grammar_src, r'get_(?:\w+_)?grammar$')
grammar_factory.python_src__ = grammar_src
return grammar_factory
......
......@@ -797,9 +797,8 @@ class EBNFCompiler(Compiler):
if DROP_WSPC in self.directives.drop:
definitions.append((self.DROP_WHITESPACE_PARSER_KEYWORD,
'DropWhitespace(%s)' % self.WHITESPACE_KEYWORD))
else:
definitions.append((self.WHITESPACE_PARSER_KEYWORD,
'Whitespace(%s)' % self.WHITESPACE_KEYWORD))
definitions.append((self.WHITESPACE_PARSER_KEYWORD,
'Whitespace(%s)' % self.WHITESPACE_KEYWORD))
definitions.append((self.WHITESPACE_KEYWORD,
("mixin_comment(whitespace=" + self.RAW_WS_KEYWORD
+ ", comment=" + self.COMMENT_KEYWORD + ")")))
......
......@@ -2498,6 +2498,9 @@ class Pop(Retrieve):
########################################################################
# TODO: Add a generic Drop-Parser (see DropToken, DropWhitespace)
class Synonym(UnaryParser):
r"""
Simply calls another parser and encapsulates the result in
......@@ -2512,11 +2515,15 @@ class Synonym(UnaryParser):
class, in which case it would be unclear whether the parser
RegExp('\d\d\d\d') carries the name 'JAHRESZAHL' or 'jahr'.
"""
def __init__(self, parser: Parser) -> None:
assert not (isinstance(parser, DropWhitespace) or isinstance(parser, DropToken))
super(Synonym, self).__init__(parser)
def _parse(self, text: StringView) -> Tuple[Optional[Node], StringView]:
node, text = self.parser(text)
assert node != EMPTY_NODE # TODO: add a test-case for this, and then correct the error!!!
if node:
if node == EMPTY_NODE:
return Node(self.tag_name, ''), text
return Node(self.tag_name, node), text
return None, text
......
......@@ -388,6 +388,21 @@ class TestGrammar:
st = gr('eins zwei', complete_match=False)
assert not st.errors
def test_synonym(self):
lang = """
doc = { word | number }
word = /\w+/ S
number = [VZ] /\d+/ S
S = ~ # let S by a synonym for anonymous whitespace
VZ = "-"
"""
gr = grammar_provider(lang)()
st = gr('eins 1 zwei2drei 3')
# set_config_value('compiled_EBNF_log', 'grammar.log')
gr = grammar_provider("@drop = whitespace, token" + lang)()
st = gr('eins 1 zwei2drei 3')
st = gr('-3')
class TestSeries:
def test_non_mandatory(self):
......
Supports Markdown
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