Re: [Pyparsing] How can I determine if a fragment is valid?
Brought to you by:
ptmcg
From: Andrew S. <agt...@ya...> - 2008-01-15 09:47:12
|
Paul, Thanks for the quick reply. So far using pe.loc < len(input) works for me. I'll reply to the list if I can find a counter example. Can I count on loc sticking around in the ParseException class? Not to push my luck, but I've got a grammar question too. I'm trying to define a grammar that uses a skipTo(Optional(xxx)) and not having much success. Is there a better way to go about this? An example select where foo = 1 and bar = 2 into result I was hoping to end up with something like the following: into_clause = Keyword('into') + restOfLine Keyword('select') + skipTo(Optional(into_clause)).setResultsName('where_condition') Is there a way to do this without forcing some delimiters onto the where clause? Thanks again for such a useful library. -a. --- Paul McGuire <pt...@au...> wrote: > Andrew - > > You may be able to infer something like this based > on the location of the > raised exception. For instance, consider this > grammar: > > grmr = Literal("A") + ( Literal("B") + Literal("C") > | Literal("D") + > Literal("E") ) > > A full match would require one of these input > strings (with whitespace > allowed, of course): > > ABC > ADE > > So any of these partial strings would indicate that > there could still be a > match: > > A > AB > AD > > but they would raise a ParseException since they are > not complete. The > thing to note is, that the loc field of the raised > exception is equal to the > length of the input string, telling you that the > missing piece would be > found at the end of what was given, but that the > input so far did match the > grammar. > > By contrast, these strings are not partially valid: > > B > AC > ABX > > In these cases, the loc field of the raised > exception is less than the > length of the input string, telling you that the > provided string is not a > partial match. > > This is a very simplistic example, you should test > this idea a bit more > thoroughly with your own specific grammar. I > haven't thought it through > myself for more than about 10 minutes, so please let > me know how it works > out. > > -- Paul > > > > ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs |