Re: [Pyparsing] How can I determine if a fragment is valid?
Brought to you by:
ptmcg
From: Paul M. <pt...@au...> - 2008-01-14 08:07:21
|
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 |