Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
badw-it
DHParser
Commits
a7ea5970
Commit
a7ea5970
authored
Feb 04, 2019
by
Eckhart Arnold
Browse files
- refactor RootNode.errors()-Method
parent
af64dde3
Changes
10
Hide whitespace changes
Inline
Side-by-side
DHParser/compile.py
View file @
a7ea5970
...
...
@@ -246,7 +246,7 @@ def compile_source(source: str,
# messages.extend(syntax_tree.errors())
# syntax_tree.error_flag = max(syntax_tree.error_flag, efl)
messages
=
syntax_tree
.
errors
()
messages
=
syntax_tree
.
errors
_sorted
adjust_error_locations
(
messages
,
original_text
,
source_mapping
)
return
result
,
messages
,
ast
...
...
DHParser/syntaxtree.py
View file @
a7ea5970
...
...
@@ -193,7 +193,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
def
__str__
(
self
):
if
isinstance
(
self
,
RootNode
):
root
=
cast
(
RootNode
,
self
)
errors
=
root
.
errors
()
errors
=
root
.
errors
_sorted
if
errors
:
e_pos
=
errors
[
0
].
pos
return
self
.
content
[:
e_pos
]
+
\
...
...
@@ -743,7 +743,7 @@ PLACEHOLDER = Node('__PLACEHOLDER__', '')
class
RootNode
(
Node
):
"""TODO: Add Documentation!!!
all_
errors (list): A list of all errors that have occured so far during
errors (list): A list of all errors that have occured so far during
processing (i.e. parsing, AST-transformation, compiling)
of this tree.
...
...
@@ -753,7 +753,7 @@ class RootNode(Node):
def
__init__
(
self
,
node
:
Optional
[
Node
]
=
None
):
super
().
__init__
(
'__not_yet_ready__'
,
''
)
self
.
all_
errors
=
[]
# type: List[Error]
self
.
errors
=
[]
# type: List[Error]
self
.
error_nodes
=
dict
()
# type: Dict[int, List[Error]] # id(node) -> error list
self
.
error_positions
=
dict
()
# type: Dict[int, Set[int]] # pos -> set of id(node)
self
.
error_flag
=
0
...
...
@@ -777,7 +777,7 @@ class RootNode(Node):
if
self
.
attr_active
():
duplicate
.
attr
.
update
(
copy
.
deepcopy
(
self
.
_xml_attr
))
# duplicate._xml_attr = copy.deepcopy(self._xml_attr) # this is blocked by cython
duplicate
.
all_
errors
=
copy
.
copy
(
self
.
all_
errors
)
duplicate
.
errors
=
copy
.
copy
(
self
.
errors
)
duplicate
.
error_nodes
=
copy
.
copy
(
self
.
error_nodes
)
duplicate
.
error_positions
=
copy
.
deepcopy
(
self
.
error_positions
)
duplicate
.
error_flag
=
self
.
error_flag
...
...
@@ -818,11 +818,7 @@ class RootNode(Node):
"""
assert
not
isinstance
(
node
,
FrozenNode
)
assert
node
.
pos
==
error
.
pos
# self.all_errors.append(error)
for
i
in
range
(
len
(
self
.
all_errors
)):
if
node
.
pos
<
self
.
all_errors
[
i
].
pos
:
break
self
.
all_errors
.
insert
(
i
,
error
)
self
.
errors
.
append
(
error
)
self
.
error_flag
=
max
(
self
.
error_flag
,
error
.
code
)
self
.
error_nodes
.
setdefault
(
id
(
node
),
[]).
append
(
error
)
self
.
error_positions
.
setdefault
(
node
.
pos
,
set
()).
add
(
id
(
node
))
...
...
@@ -863,12 +859,12 @@ class RootNode(Node):
return
errors
@
property
def
errors
(
self
)
->
List
[
Error
]:
def
errors
_sorted
(
self
)
->
List
[
Error
]:
"""
Returns the list of errors, ordered bv their position.
"""
#
self.
all_
errors.sort(key=lambda e: e.pos)
return
self
.
all_
errors
self
.
errors
.
sort
(
key
=
lambda
e
:
e
.
pos
)
return
self
.
errors
def
customized_XML
(
self
):
"""
...
...
DHParser/testing.py
View file @
a7ea5970
...
...
@@ -410,7 +410,7 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report=True, ve
transform
(
ast
)
tests
.
setdefault
(
'__ast__'
,
{})[
test_name
]
=
ast
# log_ST(ast, "match_%s_%s.ast" % (parser_name, clean_test_name))
raw_errors
=
cst
.
errors
()
raw_errors
=
cst
.
errors
_sorted
if
is_error
(
cst
.
error_flag
)
and
not
lookahead_artifact
(
parser
,
raw_errors
):
errors
=
adjust_error_locations
(
raw_errors
,
test_code
)
errata
.
append
(
'Match test "%s" for parser "%s" failed:
\n\t
Expr.: %s
\n\n\t
%s
\n\n
'
%
...
...
@@ -454,7 +454,7 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report=True, ve
cst
=
RootNode
(
node
).
new_error
(
node
,
str
(
upe
))
errata
.
append
(
'Unknown parser "{}" in fail test "{}"!'
.
format
(
parser_name
,
test_name
))
tests
.
setdefault
(
'__err__'
,
{})[
test_name
]
=
errata
[
-
1
]
if
not
is_error
(
cst
.
error_flag
)
and
not
lookahead_artifact
(
parser
,
cst
.
errors
()
):
if
not
is_error
(
cst
.
error_flag
)
and
not
lookahead_artifact
(
parser
,
cst
.
errors
_sorted
):
errata
.
append
(
'Fail test "%s" for parser "%s" yields match instead of '
'expected failure!'
%
(
test_name
,
parser_name
))
tests
.
setdefault
(
'__err__'
,
{})[
test_name
]
=
errata
[
-
1
]
...
...
@@ -463,7 +463,7 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report=True, ve
log_parsing_history
(
parser
,
"fail_%s_%s.log"
%
(
parser_name
,
test_name
))
if
cst
.
error_flag
:
tests
.
setdefault
(
'__msg__'
,
{})[
test_name
]
=
\
"
\n
"
.
join
(
str
(
e
)
for
e
in
cst
.
errors
()
)
"
\n
"
.
join
(
str
(
e
)
for
e
in
cst
.
errors
_sorted
)
if
verbose
:
write
(
infostr
+
(
"OK"
if
len
(
errata
)
==
errflag
else
"FAIL"
))
...
...
examples/CommonMark/markdown.py
View file @
a7ea5970
...
...
@@ -207,4 +207,4 @@ syntax_tree = parser(markdown_text)
ASTTransform
(
syntax_tree
,
MDTransTable
)
print
(
syntax_tree
.
as_sxpr
())
print
(
error_messages
(
markdown_text
,
syntax_tree
.
errors
()
))
print
(
error_messages
(
markdown_text
,
syntax_tree
.
errors
_sorted
))
examples/CommonMark/markdown_old.py
View file @
a7ea5970
...
...
@@ -283,4 +283,4 @@ syntax_tree = parse(markdown_text, parser)
ASTTransform
(
syntax_tree
,
MDTransTable
)
print
(
syntax_tree
.
as_sxpr
())
print
(
error_messages
(
markdown_text
,
syntax_tree
.
errors
()))
print
(
error_messages
(
markdown_text
,
syntax_tree
.
errors
_sorted
()))
examples/LaTeX/tst_LaTeX_docs.py
View file @
a7ea5970
...
...
@@ -50,7 +50,7 @@ compiler = get_compiler()
def
fail_on_error
(
src
,
result
):
if
result
.
error_flag
:
print
(
result
.
as_sxpr
())
for
e
in
result
.
errors
()
:
for
e
in
result
.
errors
_sorted
:
print
(
str
(
e
))
sys
.
exit
(
1
)
...
...
test/test_dsl.py
View file @
a7ea5970
...
...
@@ -52,7 +52,7 @@ class TestCompileFunctions:
assert
callable
(
factory
)
parser
=
factory
()
result
=
parser
(
"5 + 3 * 4"
)
assert
not
result
.
error_flag
,
str
(
result
.
errors
()
)
assert
not
result
.
error_flag
,
str
(
result
.
errors
_sorted
)
result
=
parser
(
"5A + 4B ** 4C"
)
assert
is_error
(
result
.
error_flag
)
...
...
test/test_ebnf.py
View file @
a7ea5970
...
...
@@ -52,36 +52,36 @@ class TestDirectives:
assert
parser
syntax_tree
=
parser
(
"3 + 4 * 12"
)
# parser.log_parsing_history("WSP")
assert
not
syntax_tree
.
errors
()
assert
not
syntax_tree
.
errors
_sorted
syntax_tree
=
parser
(
"3 + 4
\n
* 12"
)
# parser.log_parsing_history("WSPLF")
assert
not
syntax_tree
.
errors
()
assert
not
syntax_tree
.
errors
_sorted
syntax_tree
=
parser
(
"3 + 4
\n
\n
* 12"
)
assert
syntax_tree
.
errors
()
assert
syntax_tree
.
errors
_sorted
syntax_tree
=
parser
(
"3 + 4
\n\n
* 12"
)
assert
syntax_tree
.
errors
()
assert
syntax_tree
.
errors
_sorted
def
test_whitespace_vertical
(
self
):
lang
=
"@ whitespace = vertical
\n
"
+
self
.
mini_language
parser
=
grammar_provider
(
lang
)()
assert
parser
syntax_tree
=
parser
(
"3 + 4 * 12"
)
assert
not
syntax_tree
.
errors
()
assert
not
syntax_tree
.
errors
_sorted
syntax_tree
=
parser
(
"3 + 4
\n
* 12"
)
assert
not
syntax_tree
.
errors
()
assert
not
syntax_tree
.
errors
_sorted
syntax_tree
=
parser
(
"3 + 4
\n
\n
* 12"
)
assert
not
syntax_tree
.
errors
()
assert
not
syntax_tree
.
errors
_sorted
syntax_tree
=
parser
(
"3 + 4
\n\n
* 12"
)
assert
not
syntax_tree
.
errors
()
assert
not
syntax_tree
.
errors
_sorted
def
test_whitespace_horizontal
(
self
):
lang
=
"@ whitespace = horizontal
\n
"
+
self
.
mini_language
parser
=
grammar_provider
(
lang
)()
assert
parser
syntax_tree
=
parser
(
"3 + 4 * 12"
)
assert
not
syntax_tree
.
errors
()
assert
not
syntax_tree
.
errors
_sorted
syntax_tree
=
parser
(
"3 + 4
\n
* 12"
)
assert
syntax_tree
.
errors
()
assert
syntax_tree
.
errors
_sorted
class
TestReservedSymbols
:
...
...
@@ -176,7 +176,7 @@ class TestParserNameOverwriteBug:
get_ebnf_transformer
()(
st
)
# print(st.as_sxpr())
result
=
get_ebnf_compiler
()(
st
)
messages
=
st
.
errors
()
messages
=
st
.
errors
_sorted
assert
not
has_errors
(
messages
),
str
(
messages
)
def
test_single_mandatory_bug
(
self
):
...
...
@@ -194,9 +194,9 @@ class TestSemanticValidation:
def
check
(
self
,
minilang
,
bool_filter
=
lambda
x
:
x
):
grammar
=
get_ebnf_grammar
()
st
=
grammar
(
minilang
)
assert
not
st
.
errors
()
assert
not
st
.
errors
_sorted
EBNFTransform
()(
st
)
assert
bool_filter
(
st
.
errors
()
)
assert
bool_filter
(
st
.
errors
_sorted
)
def
test_illegal_nesting
(
self
):
self
.
check
(
'impossible = { [ "an optional requirement" ] }'
)
...
...
@@ -477,12 +477,12 @@ class TestErrorCustomization:
st
=
parser
(
"ABCD"
);
assert
not
st
.
error_flag
st
=
parser
(
"A_CD"
);
assert
not
st
.
error_flag
st
=
parser
(
"AB_D"
);
assert
st
.
error_flag
assert
st
.
errors
()
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
()
[
0
].
message
==
"a user defined error message"
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
message
==
"a user defined error message"
# transitivity of mandatory-operator
st
=
parser
(
"ABC_"
);
assert
st
.
error_flag
assert
st
.
errors
()
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
()
[
0
].
message
==
"a user defined error message"
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
message
==
"a user defined error message"
def
test_customized_error_case_sensitive
(
self
):
lang
=
"""
...
...
@@ -493,8 +493,8 @@ class TestErrorCustomization:
"""
parser
=
grammar_provider
(
lang
)()
st
=
parser
(
"ABC_"
);
assert
st
.
error_flag
assert
st
.
errors
()
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
()
[
0
].
message
==
"a user defined error message"
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
message
==
"a user defined error message"
def
test_multiple_error_messages
(
self
):
lang
=
"""
...
...
@@ -509,21 +509,21 @@ class TestErrorCustomization:
"""
parser
=
grammar_provider
(
lang
)()
st
=
parser
(
"AB*D"
);
assert
st
.
error_flag
assert
st
.
errors
()
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
()
[
0
].
message
==
"the asterix is wrong in this place"
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
message
==
"the asterix is wrong in this place"
# transitivity of mandatory-operator
st
=
parser
(
"ABC_"
);
assert
st
.
error_flag
assert
st
.
errors
()
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
()
[
0
].
message
==
"the underscore is wrong in this place"
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
message
==
"the underscore is wrong in this place"
st
=
parser
(
"ABiD"
);
assert
st
.
error_flag
assert
st
.
errors
()
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
()
[
0
].
message
.
startswith
(
'wrong letter'
)
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
message
.
startswith
(
'wrong letter'
)
st
=
parser
(
"AB+D"
);
assert
st
.
error_flag
assert
st
.
errors
()
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
()
[
0
].
message
==
"fallback error message"
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
message
==
"fallback error message"
st
=
parser
(
"ABCi"
);
assert
st
.
error_flag
assert
st
.
errors
()
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
()
[
0
].
message
.
startswith
(
'C cannot be followed by'
)
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
message
.
startswith
(
'C cannot be followed by'
)
class
TestErrorCustomizationErrors
:
...
...
@@ -600,7 +600,7 @@ class TestCustomizedResumeParsing:
assert
cst
.
content
==
content
assert
cst
.
pick
(
'alpha'
).
content
.
startswith
(
'ALPHA'
)
# because of resuming, there should be only on error message
assert
len
(
cst
.
errors
()
)
==
1
assert
len
(
cst
.
errors
_sorted
)
==
1
content
=
'ALPHA acb BETA bad GAMMA cab .'
cst
=
gr
(
content
)
...
...
@@ -609,7 +609,7 @@ class TestCustomizedResumeParsing:
assert
cst
.
content
==
content
assert
cst
.
pick
(
'alpha'
).
content
.
startswith
(
'ALPHA'
)
# because of resuming, there should be only on error message
assert
len
(
cst
.
errors
()
)
==
2
assert
len
(
cst
.
errors
_sorted
)
==
2
content
=
'ALPHA acb GAMMA cab .'
cst
=
gr
(
content
)
...
...
@@ -618,7 +618,7 @@ class TestCustomizedResumeParsing:
assert
cst
.
content
==
content
assert
cst
.
pick
(
'alpha'
).
content
.
startswith
(
'ALPHA'
)
# because of resuming, there should be only on error message
assert
len
(
cst
.
errors
()
)
==
1
assert
len
(
cst
.
errors
_sorted
)
==
1
class
TestInSeriesResume
:
...
...
@@ -634,29 +634,29 @@ class TestInSeriesResume:
st
=
self
.
gr
(
'ABCDEFG'
)
assert
not
st
.
error_flag
st
=
self
.
gr
(
'AB XYZ CDEFG'
)
errors
=
st
.
errors
()
errors
=
st
.
errors
_sorted
assert
len
(
errors
)
==
1
and
errors
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
st
=
self
.
gr
(
'AB XYZ CDE XYZ FG'
)
errors
=
st
.
errors
()
errors
=
st
.
errors
_sorted
assert
len
(
errors
)
==
2
and
all
(
err
.
code
==
Error
.
MANDATORY_CONTINUATION
for
err
in
errors
)
st
=
self
.
gr
(
'AB XYZ CDE XNZ FG'
)
# fails to resume parsing
errors
=
st
.
errors
()
errors
=
st
.
errors
_sorted
assert
len
(
errors
)
>=
1
and
errors
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
def
test_series_gap
(
self
):
st
=
self
.
gr
(
'ABDEFG'
)
errors
=
st
.
errors
()
errors
=
st
.
errors
_sorted
assert
len
(
errors
)
==
1
and
errors
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
st
=
self
.
gr
(
'ABXEFG'
)
# two missing, one wrong element added
errors
=
st
.
errors
()
errors
=
st
.
errors
_sorted
assert
len
(
errors
)
==
2
and
all
(
err
.
code
==
Error
.
MANDATORY_CONTINUATION
for
err
in
errors
)
st
=
self
.
gr
(
'AB_DE_G'
)
errors
=
st
.
errors
()
errors
=
st
.
errors
_sorted
assert
len
(
errors
)
==
2
and
all
(
err
.
code
==
Error
.
MANDATORY_CONTINUATION
for
err
in
errors
)
def
test_series_permutation
(
self
):
st
=
self
.
gr
(
'ABEDFG'
)
errors
=
st
.
errors
()
errors
=
st
.
errors
_sorted
assert
len
(
errors
)
>=
1
# cannot really recover from permutation errors
...
...
@@ -674,7 +674,7 @@ class TestAllOfResume:
st
=
self
.
gr
(
'GFCBAED'
)
assert
not
st
.
error_flag
st
=
self
.
gr
(
'GFCB XYZ AED'
)
errors
=
st
.
errors
()
errors
=
st
.
errors
_sorted
assert
errors
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
str
(
errors
[
0
]).
find
(
':-('
)
>=
0
...
...
@@ -697,9 +697,9 @@ class TestAllOfResume:
assert
not
st
.
error_flag
st
=
gr
(
'EDXYZ.'
)
assert
st
.
error_flag
assert
len
(
st
.
errors
()
)
==
1
assert
len
(
st
.
errors
_sorted
)
==
1
st
=
gr
(
'FCB_GAED.'
)
assert
len
(
st
.
errors
()
)
==
1
assert
len
(
st
.
errors
_sorted
)
==
1
def
test_complex_resume_task
(
self
):
...
...
@@ -722,11 +722,11 @@ class TestAllOfResume:
assert
not
st
.
error_flag
st
=
gr
(
'EDXYZ.'
)
assert
st
.
error_flag
assert
len
(
st
.
errors
()
)
==
1
assert
len
(
st
.
errors
_sorted
)
==
1
st
=
gr
(
'FCB_GAED.'
)
assert
len
(
st
.
errors
()
)
==
2
assert
len
(
st
.
errors
_sorted
)
==
2
st
=
gr
(
'EXY EXYZ.'
)
assert
len
(
st
.
errors
()
)
==
1
assert
len
(
st
.
errors
_sorted
)
==
1
...
...
test/test_parse.py
View file @
a7ea5970
...
...
@@ -69,7 +69,7 @@ class TestInfiLoopsAndRecursion:
parser
=
grammar_provider
(
minilang
)()
assert
parser
syntax_tree
=
parser
(
snippet
)
assert
not
syntax_tree
.
error_flag
,
str
(
syntax_tree
.
errors
()
)
assert
not
syntax_tree
.
error_flag
,
str
(
syntax_tree
.
errors
_sorted
)
assert
snippet
==
str
(
syntax_tree
)
if
is_logging
():
log_ST
(
syntax_tree
,
"test_LeftRecursion_direct.cst"
)
...
...
@@ -86,7 +86,7 @@ class TestInfiLoopsAndRecursion:
parser
=
grammar_provider
(
minilang
)()
assert
parser
syntax_tree
=
parser
(
snippet
)
assert
not
syntax_tree
.
error_flag
,
syntax_tree
.
errors
()
assert
not
syntax_tree
.
error_flag
,
syntax_tree
.
errors
_sorted
assert
snippet
==
str
(
syntax_tree
)
def
test_indirect_left_recursion1
(
self
):
...
...
@@ -100,13 +100,13 @@ class TestInfiLoopsAndRecursion:
assert
parser
snippet
=
"8 * 4"
syntax_tree
=
parser
(
snippet
)
assert
not
syntax_tree
.
error_flag
,
syntax_tree
.
errors
()
assert
not
syntax_tree
.
error_flag
,
syntax_tree
.
errors
_sorted
snippet
=
"7 + 8 * 4"
syntax_tree
=
parser
(
snippet
)
assert
not
syntax_tree
.
error_flag
,
syntax_tree
.
errors
()
assert
not
syntax_tree
.
error_flag
,
syntax_tree
.
errors
_sorted
snippet
=
"9 + 8 * (4 + 3)"
syntax_tree
=
parser
(
snippet
)
assert
not
syntax_tree
.
error_flag
,
syntax_tree
.
errors
()
assert
not
syntax_tree
.
error_flag
,
syntax_tree
.
errors
_sorted
assert
snippet
==
str
(
syntax_tree
)
if
is_logging
():
log_ST
(
syntax_tree
,
"test_LeftRecursion_indirect.cst"
)
...
...
@@ -325,10 +325,10 @@ class TestSeries:
st
=
parser
(
"A_CD"
);
assert
not
st
.
error_flag
st
=
parser
(
"AB_D"
);
assert
st
.
error_flag
# print(st.errors())
assert
st
.
errors
()
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
# transitivity of mandatory-operator
st
=
parser
(
"ABC_"
);
assert
st
.
error_flag
assert
st
.
errors
()
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
def
test_series_composition
(
self
):
TA
,
TB
,
TC
,
TD
,
TE
=
(
TKN
(
b
)
for
b
in
"ABCDE"
)
...
...
@@ -340,9 +340,9 @@ class TestSeries:
st
=
parser
(
"ABCDE"
);
assert
not
st
.
error_flag
st
=
parser
(
"A_CDE"
);
assert
not
st
.
error_flag
st
=
parser
(
"AB_DE"
);
assert
st
.
error_flag
assert
st
.
errors
()
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
st
=
parser
(
"ABC_E"
);
assert
st
.
error_flag
assert
st
.
errors
()
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
combined
=
Alternative
(
s2
+
s1
,
RegExp
(
'.*'
))
parser
=
Grammar
(
combined
)
...
...
@@ -352,7 +352,7 @@ class TestSeries:
st
=
parser
(
"DE_BC"
);
assert
not
st
.
error_flag
st
=
parser
(
"DEA_C"
);
assert
not
st
.
error_flag
st
=
parser
(
"DEAB_"
);
assert
st
.
error_flag
assert
st
.
errors
()
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MANDATORY_CONTINUATION
# def test_boundary_cases(self):
# lang = """
...
...
@@ -463,23 +463,23 @@ class TestPopRetrieve:
def
test_stackhandling
(
self
):
ambigous_opening
=
"<ABCnormal> normal tag <ABCnormal*>"
syntax_tree
=
self
.
minilang_parser3
(
ambigous_opening
)
assert
not
syntax_tree
.
error_flag
,
str
(
syntax_tree
.
errors
()
)
assert
not
syntax_tree
.
error_flag
,
str
(
syntax_tree
.
errors
_sorted
)
ambigous_opening
=
"<ABCnormal> normal tag <ABCnormal/>"
syntax_tree
=
self
.
minilang_parser3
(
ambigous_opening
)
assert
not
syntax_tree
.
error_flag
,
str
(
syntax_tree
.
errors
()
)
assert
not
syntax_tree
.
error_flag
,
str
(
syntax_tree
.
errors
_sorted
)
forgot_closing_tag
=
"<em> where is the closing tag?"
syntax_tree
=
self
.
minilang_parser3
(
forgot_closing_tag
)
assert
syntax_tree
.
error_flag
,
str
(
syntax_tree
.
errors
()
)
assert
syntax_tree
.
error_flag
,
str
(
syntax_tree
.
errors
_sorted
)
proper
=
"<em> has closing tag <em/>"
syntax_tree
=
self
.
minilang_parser3
(
proper
)
assert
not
syntax_tree
.
error_flag
,
str
(
syntax_tree
.
errors
()
)
assert
not
syntax_tree
.
error_flag
,
str
(
syntax_tree
.
errors
_sorted
)
proper
=
"<em> has closing tag <em*>"
syntax_tree
=
self
.
minilang_parser3
(
proper
)
assert
not
syntax_tree
.
error_flag
,
str
(
syntax_tree
.
errors
()
)
assert
not
syntax_tree
.
error_flag
,
str
(
syntax_tree
.
errors
_sorted
)
def
test_cache_neutrality
(
self
):
"""Test that packrat-caching does not interfere with the variable-
...
...
@@ -496,12 +496,12 @@ class TestPopRetrieve:
case
=
"(secret)*. secret"
gr
=
grammar_provider
(
lang
)()
st
=
gr
(
case
)
assert
not
st
.
error_flag
,
str
(
st
.
errors
()
)
assert
not
st
.
error_flag
,
str
(
st
.
errors
_sorted
)
def
test_single_line
(
self
):
teststr
=
"Anfang ```code block `` <- keine Ende-Zeichen ! ``` Ende"
syntax_tree
=
self
.
minilang_parser
(
teststr
)
assert
not
syntax_tree
.
errors
()
assert
not
syntax_tree
.
errors
_sorted
delim
=
str
(
next
(
syntax_tree
.
select
(
partial
(
self
.
opening_delimiter
,
name
=
"delimiter"
))))
pop
=
str
(
next
(
syntax_tree
.
select
(
self
.
closing_delimiter
)))
assert
delim
==
pop
...
...
@@ -518,7 +518,7 @@ class TestPopRetrieve:
Mehrzeliger ```code block
"""
syntax_tree
=
self
.
minilang_parser
(
teststr
)
assert
not
syntax_tree
.
errors
()
assert
not
syntax_tree
.
errors
_sorted
delim
=
str
(
next
(
syntax_tree
.
select
(
partial
(
self
.
opening_delimiter
,
name
=
"delimiter"
))))
pop
=
str
(
next
(
syntax_tree
.
select
(
self
.
closing_delimiter
)))
assert
delim
==
pop
...
...
@@ -528,7 +528,7 @@ class TestPopRetrieve:
def
test_single_line_complement
(
self
):
teststr
=
"Anfang {{{code block }} <- keine Ende-Zeichen ! }}} Ende"
syntax_tree
=
self
.
minilang_parser2
(
teststr
)
assert
not
syntax_tree
.
errors
()
assert
not
syntax_tree
.
errors
_sorted
delim
=
str
(
next
(
syntax_tree
.
select
(
partial
(
self
.
opening_delimiter
,
name
=
"braces"
))))
pop
=
str
(
next
(
syntax_tree
.
select
(
self
.
closing_delimiter
)))
assert
len
(
delim
)
==
len
(
pop
)
and
delim
!=
pop
...
...
@@ -545,7 +545,7 @@ class TestPopRetrieve:
Mehrzeliger }}}code block
"""
syntax_tree
=
self
.
minilang_parser2
(
teststr
)
assert
not
syntax_tree
.
errors
()
assert
not
syntax_tree
.
errors
_sorted
delim
=
str
(
next
(
syntax_tree
.
select
(
partial
(
self
.
opening_delimiter
,
name
=
"braces"
))))
pop
=
str
(
next
(
syntax_tree
.
select
(
self
.
closing_delimiter
)))
assert
len
(
delim
)
==
len
(
pop
)
and
delim
!=
pop
...
...
@@ -597,7 +597,7 @@ class TestErrorReporting:
testcode2
=
"XYZ"
testcode3
=
"hallo "
cst
=
self
.
parser
(
testcode1
)
assert
not
cst
.
error_flag
,
str
(
cst
.
errors
()
)
assert
not
cst
.
error_flag
,
str
(
cst
.
errors
_sorted
)
cst
=
self
.
parser
(
testcode2
)
assert
not
cst
.
error_flag
cst
=
self
.
parser
(
testcode3
)
...
...
@@ -611,9 +611,9 @@ class TestBorderlineCases:
cst
=
gr
(
'X'
,
'parser'
)
assert
not
cst
.
error_flag
cst
=
gr
(
' '
,
'parser'
)
assert
cst
.
error_flag
and
cst
.
errors
()
[
0
].
code
==
Error
.
PARSER_DID_NOT_MATCH
assert
cst
.
error_flag
and
cst
.
errors
_sorted
[
0
].
code
==
Error
.
PARSER_DID_NOT_MATCH
cst
=
gr
(
''
,
'parser'
)
assert
cst
.
error_flag
and
cst
.
errors
()
[
0
].
code
==
Error
.
PARSER_DID_NOT_MATCH
assert
cst
.
error_flag
and
cst
.
errors
_sorted
[
0
].
code
==
Error
.
PARSER_DID_NOT_MATCH
def
test_matching
(
self
):
minilang
=
"""parser = /.?/"""
...
...
@@ -621,7 +621,7 @@ class TestBorderlineCases:
cst
=
gr
(
' '
,
'parser'
)
assert
not
cst
.
error_flag
cst
=
gr
(
' '
,
'parser'
)
assert
cst
.
error_flag
and
cst
.
errors
()
[
0
].
code
==
Error
.
PARSER_STOPPED_BEFORE_END
assert
cst
.
error_flag
and
cst
.
errors
_sorted
[
0
].
code
==
Error
.
PARSER_STOPPED_BEFORE_END
cst
=
gr
(
''
,
'parser'
)
assert
not
cst
.
error_flag
...
...
@@ -669,7 +669,7 @@ class TestReentryAfterError:
assert
cst
.
content
==
content
assert
cst
.
pick
(
'alpha'
).
content
.
startswith
(
'ALPHA'
)
# because of resuming, there should be only on error message
assert
len
(
cst
.
errors
()
)
==
1
assert
len
(
cst
.
errors
_sorted
)
==
1
def
test_failing_resume_rule
(
self
):
gr
=
self
.
gr
;
gr
.
resume_rules
=
dict
()
...
...
@@ -692,7 +692,7 @@ class TestReentryAfterError:
assert
cst
.
content
==
content
assert
cst
.
pick
(
'alpha'
).
content
.
startswith
(
'ALPHA'
)
# because of resuming, there should be only on error message
assert
len
(
cst
.
errors
()
)
==
1
assert
len
(
cst
.
errors
_sorted
)
==
1
def
test_several_reentry_points_second_point_matching
(
self
):
gr
=
self
.
gr
;
gr
.
resume_rules
=
dict
()
...
...
@@ -704,7 +704,7 @@ class TestReentryAfterError:
assert
cst
.
content
==
content
assert
cst
.
pick
(
'alpha'
).
content
.
startswith
(
'ALPHA'
)
# because of resuming, there should be only on error message
assert
len
(
cst
.
errors
()
)
==
1
assert
len
(
cst
.
errors
_sorted
)
==
1
def
test_several_resume_rules_innermost_rule_matching
(
self
):
gr
=
self
.
gr
;
gr
.
resume_rules
=
dict
()
...
...
@@ -718,7 +718,7 @@ class TestReentryAfterError:
assert
cst
.
content
==
content
assert
cst
.
pick
(
'alpha'
).
content
.
startswith
(
'ALPHA'
)
# because of resuming, there should be only on error message
assert
len
(
cst
.
errors
()
)
==
1
assert
len
(
cst
.
errors
_sorted
)
==
1
# multiple failures
content
=
'ALPHA acb BETA bad GAMMA cab .'
cst
=
gr
(
content
)
...
...
@@ -727,7 +727,7 @@ class TestReentryAfterError:
assert
cst
.
content
==
content
assert
cst
.
pick
(
'alpha'
).
content
.
startswith
(
'ALPHA'
)
# because of resuming, there should be only on error message
assert
len
(
cst
.
errors
()
)
==
2
assert
len
(
cst
.
errors
_sorted
)
==
2
class
TestConfiguredErrorMessages
:
...
...
@@ -740,8 +740,8 @@ class TestConfiguredErrorMessages:
"""
parser
=
grammar_provider
(
lang
)()
st
=
parser
(
"AB_D"
);
assert
st
.
error_flag
assert
st
.
errors
()
[
0
].
code
==
Error
.
MALFORMED_ERROR_STRING
assert
st
.
errors
()
[
1
].
code
==
Error
.
MANDATORY_CONTINUATION
assert
st
.
errors
_sorted
[
0
].
code
==
Error
.
MALFORMED_ERROR_STRING
assert
st
.
errors
_sorted
[
1
].
code
==
Error
.
MANDATORY_CONTINUATION
# print(st.errors())
...
...
test/test_syntaxtree.py
View file @
a7ea5970
...
...
@@ -91,7 +91,7 @@ class TestNode:
assert
tree_copy
.
as_sxpr
()
==
parse_sxpr
(
'(a (b c) (d (e f) (h i)))'
).
as_sxpr
()
tree
.
add_error
(
tree
,
Error
(
'Test Error'
,
0
))
assert
not
tree_copy
.
all_
errors
assert
not
tree_copy
.
errors
assert
tree
.
as_sxpr
()
!=
parse_sxpr
(
'(a (b c) (d (e f) (h i)))'
).
as_sxpr
()
assert
tree_copy
.
as_sxpr
()
==
parse_sxpr
(
'(a (b c) (d (e f) (h i)))'
).
as_sxpr
()
...
...
@@ -198,7 +198,7 @@ class TestRootNode:
root
.
new_error
(
tree
.
children
[
0
],
"error B"
)
root
.
swallow
(
tree
)
assert
root
.
error_flag
errors
=
root
.
errors
()
errors
=
root
.
errors
_sorted
assert
root
.
error_flag
# assert errors == root.errors(True)
# assert not root.error_flag and not root.errors()
...
...
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