Python 2.7 Pyparsing:将检查分为两个单独的部分时出现问题,如果第一部分的检查失败,则应继续第二部分的检查
对于我们的客户机,我们定义了一种特定于域的(自动)测试(脚本)语言,以简化测试用例的设置 ASC文件中的每个测试由三部分组成:Python 2.7 Pyparsing:将检查分为两个单独的部分时出现问题,如果第一部分的检查失败,则应继续第二部分的检查,python-2.7,pyparsing,Python 2.7,Pyparsing,对于我们的客户机,我们定义了一种特定于域的(自动)测试(脚本)语言,以简化测试用例的设置 ASC文件中的每个测试由三部分组成: test <name> - <options> # <-- defines the start of a test and some general options <testheader> # <-- contains a number of header commands which need t
test <name> - <options> # <-- defines the start of a test and some general options
<testheader> # <-- contains a number of header commands which need to be always filled in
<testbody> # <-- the real test-actions
对于
命令,此解决方案正在运行。但是现在它在每个
命令上都失败了,因为它们与header\u命令
部分不匹配,如果header\u命令
部分失败,它将继续执行test\u命令
部分
再次注意:在标题和正文部分中都允许使用空格,所以我们不能将它们用作分隔符。我们必须保持向后兼容,因此很难/不可能引入任何其他分隔符
有效的\u header\u命令
部分添加检查,但这不起作用,因为尽管条件_块
定义是块
的一部分,但它也包含块
,因此只有当if
语句的部分已经被处理时,它才会处理剩余的test_命令
部分,该部分包含签入有效的\u头\u命令
。所以在那里处理它已经太晚了\u parseaction\u validate\u mandatory\u header\u commands
方法,但我们如何确保在失败时,它首先进入test\u命令,然后才真正引发错误?
因此,我们目前没有进一步采取这种做法
testcommand
分为两个部分的方法是正确的,但为了让它正常工作,我们已经为此绞尽脑汁了好几天。所以我们最后在这里寻求帮助
-->有没有人知道我们如何确保在验证器发现它不是
命令后,它会在引发错误之前继续检查
命令
注意:实现是在Python2.7和pyparsing 2.3.0中完成的,我的一位同事找到了一个可行的解决方案 他还将块拆分为包含all和仅测试命令部分的部分,并用仅测试命令块替换if语句中的块部分。 他还增加了一些额外的动作:
test_command = NotAny(conditional_construct | eot) - (valid_header_command | valid_test_command | anyotherline)
no_header_command = NotAny(conditional_construct | eot) - (valid_test_command | anyotherline)
block = Forward()
no_header_block = Forward()
if_statement = (Keyword('if') - vp_expression - eol).addParseAction(self._parseaction_in_if_statement)
then_block = ZeroOrMore(no_header_block)
elif_block = Keyword('elif') - vp_expression - eol - ZeroOrMore(no_header_block)
else_block = Keyword('else') - eol - ZeroOrMore(no_header_block)
fi_statement = (Keyword('fi') - eol).addParseAction(self._parseaction_out_if_statement)
conditional_block = if_statement - then_block - ZeroOrMore(elif_block) - Optional(else_block) - fi_statement
block << ( OneOrMore(test_command) | conditional_block ) # pylint: disable=expression-not-assigned
no_header_block << ( OneOrMore(no_header_command) | conditional_block ) # pylint: disable=expression-not-assigned
test_command=NotAny(有条件的_构造| eot)-(有效的_头|命令|有效的| test |命令|任何其他行)
no_header_command=NotAny(条件_构造| eot)-(有效的_test_command |任何其他行)
block=Forward()
无头\u块=正向()
if_语句=(关键字('if')-vp_表达式-eol).addParseAction(self._parseaction_in_if_语句)
然后\u块=零或更多(无\u头\u块)
elif_block=关键字('elif')-vp_表达式-eol-ZeroOrMore(无标题_block)
else_block=关键字('else')-eol-ZeroOrMore(无标题_block)
fi_语句=(关键字('fi')-eol).addParseAction(self.\u parseaction\u out\u if_语句)
条件块=如果语句-那么块-零或更多(elif块)-可选(else块)-fi块语句
阻止好的解决方案。如果您使用
header_command = NotAny(OneOfKeywords('if', 'elif', 'else', 'fi') | eot) - (valid_header_command)
test_command = NotAny(OneOfKeywords('if', 'elif', 'else', 'fi') | eot) - (valid_test_command | anyotherline)
....
test_implementation = OneOrMore(header_command).setParseAction(self._parseaction_validate_mandatory_header_commands) + OneOrMore(block) + eot
test_command = NotAny(conditional_construct | eot) - (valid_header_command | valid_test_command | anyotherline)
no_header_command = NotAny(conditional_construct | eot) - (valid_test_command | anyotherline)
block = Forward()
no_header_block = Forward()
if_statement = (Keyword('if') - vp_expression - eol).addParseAction(self._parseaction_in_if_statement)
then_block = ZeroOrMore(no_header_block)
elif_block = Keyword('elif') - vp_expression - eol - ZeroOrMore(no_header_block)
else_block = Keyword('else') - eol - ZeroOrMore(no_header_block)
fi_statement = (Keyword('fi') - eol).addParseAction(self._parseaction_out_if_statement)
conditional_block = if_statement - then_block - ZeroOrMore(elif_block) - Optional(else_block) - fi_statement
block << ( OneOrMore(test_command) | conditional_block ) # pylint: disable=expression-not-assigned
no_header_block << ( OneOrMore(no_header_command) | conditional_block ) # pylint: disable=expression-not-assigned