How to use pyparsing Group with SkipTo for a file parsing?

折月煮酒 提交于 2019-12-11 06:14:19

问题


When I used pyparsing SkipTo together with other parser, the file parsing seems hang.

unexpected = pp.SkipTo(pp.LineEnd())('unexpected*')
rules = pp.Group(predefined_parser) | unexpected

parser = pp.Dict(pp.OneOrMore(rules)
parser.ignore('*' + pp.restOfLine)
parser.parseFile(filename, True)

I turned on setDebug, here's the debug message. Any insights is highly appreciated.

Match Dict:([{Group:({Combine:({"something" W:(_) Combine:({W:(ABCD...) [W:(0123...)]}) W:(_) {"sth1" | "sth2"}}) Suppress:("=") W:(0123...)}) | SkipTo:(LineEnd)}]...) at loc 0(1,1)

After I changed to the following, AttributeError thrown.

unexpected = pp.SkipTo(pp.LineEnd(), include=True)('unexpected*')

Here's the AttributeError message.

File "/nfs/pdx/disks/icf_gwa_002/ylim13/anaconda2/lib/python2.7/site-packages/pyparsing.py", line 1599, in parseFile
  return self.parseString(file_contents, parseAll)
File "/nfs/pdx/disks/icf_gwa_002/ylim13/anaconda2/lib/python2.7/site-packages/pyparsing.py", line 1078, in _parseNoCache
  tokens = self.postParse( instring, loc, tokens )
File "/nfs/pdx/disks/icf_gwa_002/ylim13/anaconda2/lib/python2.7/site-packages/pyparsing.py", line 3249, in postParse
  dictvalue = tok.copy() #ParseResults(i)
AttributeError: 'str' object has no attribute 'copy'

回答1:


Short answer - change:

    | pp.SkipTo(pp.LineEnd(), include=True)

to:

    | pp.SkipTo(pp.LineEnd(), include=True).suppress()

When this is passed to Dict, it sees the two values created by SkipTo, so assumes it is the parsed results from a key-value pair, to be added into the cumulative dict. By suppressing this result, Dict gets an empty string, which it knows to ignore.



来源:https://stackoverflow.com/questions/55798690/how-to-use-pyparsing-group-with-skipto-for-a-file-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!