Search code examples
pythontext-processingpyparsing

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


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'

Solution

  • 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.