[Pyparsing] Required Class?
Brought to you by:
ptmcg
From: Russell D. <Rus...@as...> - 2011-09-28 05:31:41
|
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? |