Re: [Pyparsing] keepOriginalText problem
Brought to you by:
ptmcg
From: Stefaan H. <ste...@gm...> - 2008-07-03 15:08:05
|
Hello Paul, and thank you for the swift reply! Because this problem was rather urgent I had already reworked the grammar to avoid using keepOriginalText, and that solved the problem in my case (but I also had to modify the client program that uses the parser...) Your alternative to keepOriginalText looks interesting, and I will keep it in mind to play with the next time I am trying to parse something using pyparsing (which is quite often these days ;) ) Best regards, and thanks again, Stefaan. On Thu, Jul 3, 2008 at 2:58 PM, Paul McGuire <pt...@au...> wrote: > Stefaan - > > I suspect the problem is with the inspect module usage, don't know how to > work around that per se. > > However, I did come up with something interesting. I defined an Empty() > with a parse action that just returns the current parse location. I then > bracketed your remove_lines expression with one of these at the beginning > and at the end, with appropriate results names. Then instead of attaching > keepOriginalText, I attached a simple lambda that returns the slice of the > input string from the given begin and end values. See below: > > # a dummy expression that just returns the current parse location > get_cur_locn = p.Empty().setParseAction(lambda s,l,t: l) > > remove_line = p.lineStart + p.Literal("<").suppress() + \ > p.restOfLine.setResultsName("LineContents") + p.lineEnd > > # put get_cur_locn exprs at front and back of remove_lines, with > # useful results names - (Combine is not really necessary, I don't think) > remove_lines = get_cur_locn("begin") + \ > p.OneOrMore(remove_line) + \ > get_cur_locn("end") > > # now replace keepOriginalText with simple string slice - should be > # faster, too! > remove_lines.setParseAction(lambda s,l,t:s[t.begin:t.end]) > > > HTH, > -- Paul > > > > -----Original Message----- > From: pyp...@li... > [mailto:pyp...@li...] On Behalf Of > stefaan > Sent: Thursday, July 03, 2008 6:20 AM > To: pyp...@li... > Subject: [Pyparsing] keepOriginalText problem > > Hello, > > I have a problem with keepOriginalText: when I try to parse a grammar using > the > keepOriginalText parse action inside a cherrypy 2.2.1-based web > application, > the > getTokensEndLoc() fails and I get a traceback: > > File "C:\shi\webapp\comparefiles\comparefiles.py", line 273, in doCompare > NormalDiffParser.remove_lines.parseString("< line 1" + os.linesep + "< > line > 2" + os.linesep) > File "C:\shi\webapp\comparefiles\diffparser\pyparsing.py", line 981, in > parseString > loc, tokens = self._parse( instring, 0 ) > File "C:\shi\webapp\comparefiles\diffparser\pyparsing.py", line 886, in > _parseNoCache > tokens = fn( instring, tokensStart, retTokens ) > File "C:\shi\webapp\comparefiles\diffparser\pyparsing.py", line 3156, in > keepOriginalText > endloc = getTokensEndLoc() > File "C:\shi\webapp\comparefiles\diffparser\pyparsing.py", line 3167, in > getTokensEndLoc > fstack = inspect.stack() > File "D:\Python24\lib\inspect.py", line 819, in stack > return getouterframes(sys._getframe(1), context) > File "D:\Python24\lib\inspect.py", line 800, in getouterframes > framelist.append((frame,) + getframeinfo(frame, context)) > File "D:\Python24\lib\inspect.py", line 769, in getframeinfo > raise TypeError('arg is not a frame or traceback object') > TypeError: arg is not a frame or traceback object > > The grammar for remove_lines: > > import pyparsing as > remove_line = p.lineStart + p.Literal("<").suppress() + \ > p.restOfLine.setResultsName("LineContents") + p.lineEnd > remove_lines = p.Combine(p.OneOrMore(remove_line)) > remove_lines.setParseAction(p.keepOriginalText) > > > The same program, with the same input, works flawlessly in a "normal" (i.e. > not > webapplication) script. > > > > ------------------------------------------------------------------------- > Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! > Studies have shown that voting for your favorite open source project, > along with a healthy diet, reduces your potential for chronic lameness > and boredom. Vote Now at http://www.sourceforge.net/community/cca08 > _______________________________________________ > Pyparsing-users mailing list > Pyp...@li... > https://lists.sourceforge.net/lists/listinfo/pyparsing-users > > |