Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
9.2.2023: Due to updates GitLab will be unavailable for some minutes between 9:00 and 11:00.
Open sidebar
badw-it
DHParser
Commits
31da9e96
Commit
31da9e96
authored
Jan 24, 2018
by
eckhart
Browse files
- UnknownParserError added to parser.py
parent
e99bec91
Changes
4
Hide whitespace changes
Inline
Side-by-side
DHParser/parse.py
View file @
31da9e96
...
...
@@ -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
Key
Error
(
'Unknown parser "%s" !'
%
key
)
raise
UnknownParser
Error
(
'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
...
...
DHParser/syntaxtree.py
View file @
31da9e96
...
...
@@ -35,6 +35,7 @@ __all__ = ('ParserBase',
'ZOMBIE_PARSER'
,
'Node'
,
'mock_syntax_tree'
,
'flatten_sxpr'
,
'TransformationFunc'
)
...
...
DHParser/testing.py
View file @
31da9e96
...
...
@@ -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
))
...
...
test/test_parse.py
View file @
31da9e96
...
...
@@ -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
):
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment