-
-
Notifications
You must be signed in to change notification settings - Fork 296
Description
I am having a lot of difficulties getting any decent error messages when using validators. Here is an example:
import pyparsing as pp
ppc = pp.pyparsing_common
def validate_option_id(tokens):
option_id: str = tokens[0]
if option_id not in ["one", "two"]:
print("yes i was here")
raise pp.ParseException(f"Invalid option id: {option_id}")
return tokens
def validate_task_id(tokens):
task_id: str = tokens[0]
if "task" not in task_id:
raise pp.ParseException(f"Invalid task id: {task_id}")
return tokens
option_id = ppc.identifier.copy()\
.set_name("option id")\
.set_parse_action(validate_option_id)
option = pp.Group(
option_id -
pp.Suppress("=") -
ppc.number
).set_name("option")
task_id = ppc.identifier.copy()\
.set_name("task id")\
.set_parse_action(validate_task_id)
task = pp.Group(
pp.Keyword("do") +
task_id
).set_name("task")
component = pp.Group(
pp.Keyword("component") -
pp.IndentedBlock(
pp.OneOrMore(option) -
pp.OneOrMore(task)
)
)
test_str = """
component
three = 1
do task
"""
result = component.parse_string(test_str)
print(result.as_list())
I can't seem to get the error message generated by the validator. When I step into the code I see that the exception message is completely ignored and overwritten by something like "Expected option, found 'three'".
Changing the raised exception to ParseSyntaxException will show the message but fail parsing even for valid files because then it fails when "do" is not a valid option. Since my actual grammar is more complex I cannot easily create negative cases so that it will go into option rule only if there are no "do" keywords used. At the very least the raised exception message should not be ignored. Because for me error messages are more important and I can try to create "syntactical traps" with error stop to ensure early failure but the error message needs to be helpful in that case.
PS: the validators need to be dynamic, so ignore the checks in the example above. And please let me know if this should instead be posted on stackoverflow.