Commit d471ad91 authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

- Fehlerkorrektur DHParser.transform.error_on

parent c3dc9adf
...@@ -758,7 +758,7 @@ class Node(collections.abc.Sized): ...@@ -758,7 +758,7 @@ class Node(collections.abc.Sized):
""" """
Picks the first descendant with one of the given tag_names. Picks the first descendant with one of the given tag_names.
This function is just syntactic sugar for This function is mostly just syntactic sugar for
``next(node.select_by_tag(tag_names, False))``. However, rather than ``next(node.select_by_tag(tag_names, False))``. However, rather than
raising a StopIterationError if no descendant with the given tag-name raising a StopIterationError if no descendant with the given tag-name
exists, it returns None. exists, it returns None.
......
...@@ -32,7 +32,7 @@ import inspect ...@@ -32,7 +32,7 @@ import inspect
import fnmatch import fnmatch
from functools import partial, reduce, singledispatch from functools import partial, reduce, singledispatch
from DHParser.error import Error from DHParser.error import Error, ErrorCode
from DHParser.syntaxtree import Node, WHITESPACE_PTYPE, TOKEN_PTYPE, ParserBase, MockParser, \ from DHParser.syntaxtree import Node, WHITESPACE_PTYPE, TOKEN_PTYPE, ParserBase, MockParser, \
ZOMBIE_NODE, parse_sxpr, flatten_sxpr ZOMBIE_NODE, parse_sxpr, flatten_sxpr
from DHParser.toolkit import issubtype, isgenerictype, expand_table, smart_list, re, typing from DHParser.toolkit import issubtype, isgenerictype, expand_table, smart_list, re, typing
...@@ -101,7 +101,6 @@ __all__ = ('TransformationDict', ...@@ -101,7 +101,6 @@ __all__ = ('TransformationDict',
'require', 'require',
'assert_content', 'assert_content',
'error_on', 'error_on',
'warn_on',
'assert_has_children', 'assert_has_children',
'peek') 'peek')
...@@ -957,37 +956,42 @@ def remove_content(context: List[Node], regexp: str): ...@@ -957,37 +956,42 @@ def remove_content(context: List[Node], regexp: str):
######################################################################## ########################################################################
@transformation_factory(collections.abc.Callable) @transformation_factory(collections.abc.Callable)
def error_on(context: List[Node], condition: Callable, error_msg: str = ''): def error_on(context: List[Node],
condition: Callable,
error_msg: str = '',
error_code: ErrorCode = Error.ERROR):
""" """
Checks for `condition`; adds an error message if condition is not met. Checks for `condition`; adds an error or warning message if condition is not met.
""" """
node = context[-1] node = context[-1]
if not condition(context): if not condition(context):
if error_msg: if error_msg:
node.add_error(error_msg % node.tag_name if error_msg.find("%s") > 0 else error_msg) context[0].new_error(node, error_msg % node.tag_name if error_msg.find("%s") > 0
else error_msg, error_code)
else: else:
cond_name = condition.__name__ if hasattr(condition, '__name__') \ cond_name = condition.__name__ if hasattr(condition, '__name__') \
else condition.__class__.__name__ if hasattr(condition, '__class__') \ else condition.__class__.__name__ if hasattr(condition, '__class__') \
else '<unknown>' else '<unknown>'
node.add_error("transform.error_on: Failed to meet condition " + cond_name) context[0].new_error(node, "transform.error_on: Failed to meet condition " + cond_name,
error_code)
#
@transformation_factory(collections.abc.Callable) # @transformation_factory(collections.abc.Callable)
def warn_on(context: List[Node], condition: Callable, warning: str = ''): # def warn_on(context: List[Node], condition: Callable, warning: str = ''):
""" # """
Checks for `condition`; adds an warning message if condition is not met. # Checks for `condition`; adds an warning message if condition is not met.
""" # """
node = context[-1] # node = context[-1]
if not condition(context): # if not condition(context):
if warning: # if warning:
node.add_error(warning % node.tag_name if warning.find("%s") > 0 else warning, # node.add_error(warning % node.tag_name if warning.find("%s") > 0 else warning,
Error.WARNING) # Error.WARNING)
else: # else:
cond_name = condition.__name__ if hasattr(condition, '__name__') \ # cond_name = condition.__name__ if hasattr(condition, '__name__') \
else condition.__class__.__name__ if hasattr(condition, '__class__') \ # else condition.__class__.__name__ if hasattr(condition, '__class__') \
else '<unknown>' # else '<unknown>'
node.add_error("transform.warn_on: Failed to meet condition " + cond_name, # context[0].new_error(node, "transform.warn_on: Failed to meet condition " + cond_name,
Error.WARNING) # Error.WARNING)
assert_has_children = error_on(lambda nd: nd.children, 'Element "%s" has no children') assert_has_children = error_on(lambda nd: nd.children, 'Element "%s" has no children')
......
...@@ -96,8 +96,8 @@ class TestNode: ...@@ -96,8 +96,8 @@ class TestNode:
tree['d'].result = "x" tree['d'].result = "x"
assert tree != tree_copy assert tree != tree_copy
assert tree_copy == parse_sxpr('(a (b c) (d (e f) (h i)))') assert tree_copy == parse_sxpr('(a (b c) (d (e f) (h i)))')
print(tree.as_sxpr()) # print(tree.as_sxpr())
print(parse_sxpr('(a (b c) (d x))').as_sxpr()) # print(parse_sxpr('(a (b c) (d x))').as_sxpr())
assert tree == parse_sxpr('(a (b c) (d x))') assert tree == parse_sxpr('(a (b c) (d x))')
# this also checks for errors equality... # this also checks for errors equality...
......
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