我有一个由“语句”列表组成的文件,每个语句都包含一个以逗号分隔的可变长度“列”列表,其中语句的结尾用分号表示:
head(
col1,
col2
);
head2(
col1,
col2,
col3
);
我有一个完整文件的工作语法,但是如果其中一个列中存在语法错误,则错误消息的信息量不大 - 它基本上表示整个语句在开头语中都不好。
所以我想分别解析每一列,以便更有信息地报告该列中的错误。以下内容将解析整个文件(两个语句)并从解析操作中打印每个语句和列列表:
from pyparsing import Group, Literal, ParseException
from pyparsing import SkipTo, Suppress, commaSeparatedList
from pyparsing import Word, ZeroOrMore, alphas, alphanums
def parse_stmt(strng, loc, toks):
print("++",toks,"++",loc,"++")
def parse_col(strng, loc, toks):
# cols = commaSeparatedList(toks)
print("==",toks,"===",loc,"==")
specs = '$_-'
name = Word(alphas+specs, alphanums+specs)
term = Literal(')') + Literal(';')
init = name
statement = Group(init.setParseAction(parse_stmt) + Suppress(Literal('(')) + \
SkipTo(term).setParseAction(parse_col) + Suppress(term))
script = ZeroOrMore(statement)
ss = '\
abc(d 45, e, f g h); \
s2("much" "stuff", 34 48); \
'
print(ss)
try:
print(script.parseString(ss))
except ParseException as err:
print(" "*(err.column-1) + "^")
print(err)
导致:
abc(d 45, e, f g h); s2("much" "stuff", 34 48);
++ ['abc'] ++ 0 ++
== ['d 45, e, f g h'] === 4 ==
++ ['s2'] ++ 21 ++
== ['"much" "stuff", 34 48'] === 24 ==
[['abc', 'd 45, e, f g h'], ['s2', '"much" "stuff", 34 48']]
但是一旦我尝试通过取消注释cols = commaSeparatedList(toks)
来解析列列表,一切都会爆炸:
abc(d 45, e, f g h); s2("much" "stuff", 34 48);
++ ['abc'] ++ 0 ++
Traceback (most recent call last):
File "pt.py", line 27, in <module>
print(script.parseString(ss))
File "pyparsing.py", line 1115, in parseString
loc, tokens = self._parse( instring, 0 )
File "pyparsing.py", line 989, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "pyparsing.py", line 2732, in parseImpl
loc, tokens = self.expr._parse( instring, loc, doActions, callPreParse=False )
File "pyparsing.py", line 989, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "pyparsing.py", line 2624, in parseImpl
return self.expr._parse( instring, loc, doActions, callPreParse=False )
File "pyparsing.py", line 989, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "pyparsing.py", line 2378, in parseImpl
loc, exprtokens = e._parse( instring, loc, doActions )
File "pyparsing.py", line 1015, in _parseNoCache
tokens = fn( instring, tokensStart, retTokens )
File "pyparsing.py", line 779, in wrapper
ret = func(*args[limit[0]:])
TypeError: parse_col() missing 3 required positional arguments: 'strng', 'loc', and 'toks'
我的问题不涉及多线程或递归,但它似乎表明了对可能与context in pyparsing parse actions besides globals有关的可重入调用pyparsing的问题。
是否有一种简单的方法可以从一个动作中调用pyparsing例程,这个方法比该问题的答案更简单?
答案 0 :(得分:2)
更改
cols = commaSeparatedList(toks)
到
cols = commaSeparatedList.parseString(toks[0])
似乎几乎可以修复它,至少对于您提供的数据示例。但是&#34;很多&#34; &#34;东西&#34;似乎打破了csl解析器,可能是因为它希望在关闭后使用逗号#34; &#34;很多&#34;。