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

Commit 31da9e96 authored by eckhart's avatar eckhart

- UnknownParserError added to parser.py

parent e99bec91
......@@ -72,6 +72,7 @@ from typing import Any, Callable, cast, Dict, List, Set, Tuple, Union, Optional
__all__ = ('HistoryRecord',
'Parser',
'UnknownParserError',
'Grammar',
'PreprocessorToken',
'RegExp',
......@@ -526,6 +527,12 @@ def mixin_comment(whitespace: str, comment: str) -> str:
return wspc
class UnknownParserError(KeyError):
"""UnknownParserError is raised if a Grammer object is called with a
parser that does not exist or if in the course of parsing a parser
is reffered to that does not exist."""
class Grammar:
r"""
Class Grammar directs the parsing process and stores global state
......@@ -801,7 +808,7 @@ class Grammar:
parser.apply(self._add_parser__)
# assert self[key] == parser
return self[key]
raise KeyError('Unknown parser "%s" !' % key)
raise UnknownParserError('Unknown parser "%s" !' % key)
def _reset__(self):
......@@ -1004,8 +1011,8 @@ class Grammar:
if not is_logging():
raise AssertionError("Cannot log history when logging is turned off!")
assert self.history__, \
"Parser did not yet run or logging was turned off when running parser!"
# assert self.history__, \
# "Parser did not yet run or logging was turned off when running parser!"
if not log_file_name:
name = self.__class__.__name__
log_file_name = name[:-7] if name.lower().endswith('grammar') else name
......
......@@ -35,6 +35,7 @@ __all__ = ('ParserBase',
'ZOMBIE_PARSER',
'Node',
'mock_syntax_tree',
'flatten_sxpr',
'TransformationFunc')
......
......@@ -24,7 +24,8 @@ import json
import os
from DHParser.toolkit import is_logging, clear_logs, re
from DHParser.syntaxtree import mock_syntax_tree, flatten_sxpr
from DHParser.syntaxtree import Node, mock_syntax_tree, flatten_sxpr, ZOMBIE_PARSER
from DHParser.parse import UnknownParserError
from DHParser.error import is_error, adjust_error_locations
__all__ = ('unit_from_configfile',
......@@ -172,7 +173,10 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report=True, ve
if verbose:
infostr = ' match-test "' + test_name + '" ... '
errflag = len(errata)
cst = parser(test_code, parser_name)
try:
cst = parser(test_code, parser_name)
except UnknownParserError as upe:
cst = Node(ZOMBIE_PARSER, "").add_error(str(upe)).init_pos(0)
cst.log("match_%s_%s.cst" % (parser_name, test_name))
tests.setdefault('__cst__', {})[test_name] = cst
if "ast" in tests or report:
......@@ -210,7 +214,11 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report=True, ve
if verbose:
infostr = ' fail-test "' + test_name + '" ... '
errflag = len(errata)
cst = parser(test_code, parser_name)
# cst = parser(test_code, parser_name)
try:
cst = parser(test_code, parser_name)
except UnknownParserError as upe:
cst = Node(ZOMBIE_PARSER, "").add_error(str(upe)).init_pos(0)
if not is_error(cst.error_flag):
errata.append('Fail test "%s" for parser "%s" yields match instead of '
'expected failure!' % (test_name, parser_name))
......
......@@ -29,7 +29,8 @@ from DHParser.stringview import StringView
from DHParser.error import Error
from DHParser.syntaxtree import mock_syntax_tree
from DHParser.parse import compile_source, Retrieve, Grammar, Forward, Token, ZeroOrMore, RE, \
RegExp, Lookbehind, NegativeLookahead, OneOrMore, Series, Alternative, AllOf, SomeOf, Compiler
RegExp, Lookbehind, NegativeLookahead, OneOrMore, Series, Alternative, AllOf, SomeOf, Compiler, \
UnknownParserError
from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compiler
from DHParser.dsl import grammar_provider, DHPARSER_IMPORTS
......@@ -562,6 +563,16 @@ class TestCompilerClass:
assert D.error_flag
class TestUnknownParserError:
def test_unknown_parser_error(self):
gr = Grammar()
try:
gr("", "NonExistantParser")
assert False, "UnknownParserError expected!"
except UnknownParserError:
pass
if __name__ == "__main__":
from DHParser.testing import runner
with logging(False):
......
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