Commit 31da9e96 authored by eckhart's avatar eckhart
Browse files

- UnknownParserError added to parser.py

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