Re: [Pyparsing] results typing
Brought to you by:
ptmcg
From: Paul M. <pt...@au...> - 2008-11-16 18:52:29
|
Denis - Thanks for your contributions on this list - please don't be discouraged if you don't get many replies to your messages. The readers here are mostly lurkers (not that there's anything wrong with that!), or folks who post with particular questions they need answered. I too look at the list as something of an archive of past pyparsing discussions. I've had a chance to look over your notes in brief, but I really want to give them more thought and consideration than I can spare just now. In general, I would say that the best way to start prototyping the integration of your ideas with pyparsing as it exists is through parse actions (for embellishing parse results) and helper methods (to simplify the construction of expressions, or linking them to parse actions). Whatever you do, I *strongly* suggest that you make these enhancements under the control of the developer, and not automatically applied across the board. Pyparsing creates many intermediate parse expressions when building the grammar, and parse results when parsing the source text, many of which get either discarded or absorbed into a larger expressions. Also, if your enhancements stay in the realm of extensions that users may add or not of their own choosing, then forward compatibility of existing code will be preserved, and it will be easier to add your ideas to the core pyparsing code. Here is one idea that might address your question about linking results to the original pyparsing expression (untested): from pyparsing import * def linkToResults(expr): def parseAction(tokens): tokens["expr"] = expr expr.addParseAction(parseAction) integer = Word(nums).setName("integer") decimal = Combine(integer("int_part") + '.' + integer("dec_part")) linkToResults(decimal) print decimal.parseString("3.14159").dump() Which prints: ['3.14159'] - dec_part: 14159 - expr: Combine:({integer "." integer}) - int_part: 3 Now the parse results that get created by expr will contain an added field named "expr" that points back to the original expression (as shown by the dump() output). If this works well as a prototype, then it may be just as easy to add it as a member function of ParserElement, so that any expression can get linked back to from its results. -- Paul |