Re: [Pyparsing] Required Class?
Brought to you by:
ptmcg
From: Paul M. <pt...@au...> - 2011-09-28 05:59:08
|
You can disable the backtracking within an a, b, c, or d by using the '-' operator instead of '+': a = a_header - a_data b = b_header - b_data c = c_header - c_data d = d_header - d_data I think this will accomplish what you wanted with Required. -- Paul -----Original Message----- From: Russell Dill [mailto:Rus...@as...] Sent: Wednesday, September 28, 2011 12:32 AM To: pyp...@li... Subject: [Pyparsing] Required Class? I have some text I'm parsing with headers and then data. The header/data groups can be in any order, aba, aabbba, acbd, bdacaa, etc (Where the letter represents a header type followed by its data). So overall, I have something like: a = a_header + a_data b = b_header + b_data c = c_header + c_data d = d_header + d_data ZeroOrMore(a | b | c | d) + EndTag Now, if a_header exists, but a_data is malformed, I get an error that it expected EndTag. For easier debugging, I'd like the error to make more sense, so: a = a_header + Required(a_data) where: class Required(ParseElementEnhance): def __init__(self, exprs): super(Required, self).__init__(exprs) def parseImpl(self, instring, loc, doActions=True): try: loc, tokens = self.expr._parse(instring, loc, doActions, callPreParse=False) except ParseException as e: raise ParseFatalException(e) return loc, tokens Is this the best way to go about this goal? Is there instead a better way of saying any number of any of these expressions in any order? If at least one (or exactly one) 'c' expression is required, is there a way of denoting that? ---------------------------------------------------------------------------- -- All the data continuously generated in your IT infrastructure contains a definitive record of customers, application performance, security threats, fraudulent activity and more. Splunk takes this data and makes sense of it. Business sense. IT sense. Common sense. http://p.sf.net/sfu/splunk-d2dcopy1 _______________________________________________ Pyparsing-users mailing list Pyp...@li... https://lists.sourceforge.net/lists/listinfo/pyparsing-users |