Re: [Pyparsing] Grouping when using asList()
Brought to you by:
ptmcg
From: Joshua J. K. <jk...@sk...> - 2008-03-06 20:47:20
|
On Wed, 2008-03-05 at 21:48 -0600, Paul McGuire wrote: > Ok, here are the changes I made to your code: Wow...just...wow. Really, I wasn't asking anyone to write my parser for me. I just wanted to know how to do it. Wow. Thanks you!! > I changed column_def to: > > col_width_def = (p.Suppress('(') + > p.delimitedList(p.Word(p.nums)).setParseAction( lambda toks: > [int(tok) for tok in toks]) + > p.Suppress(')') > ) That casting to int didn't matter much since we'll be just doing string output, but it does validate the input. :) > paren_quoted was just not the expression, and the "(p.nums + p.Optional(',' > + p.nums)" argument *actually* created a results name for the column_width > field. Gotcha. > I changed create_table to: > > create_table = (p.CaselessKeyword('create').suppress() > + p.CaselessKeyword('table').suppress() > + bracket_quoted.setResultsName('schema') > + "." + bracket_quoted.setResultsName('table_name') > #~ + p.nestedExpr(content=p.delimitedList(p.Or( > #~ [p.Group(column_def.setResultsName('columns')), > #~ p.Group(primary_key), > #~ p.Group(constraint)]))) > + p.Group(p.Suppress('(') + > p.delimitedList(p.Or( > [p.Group(column_def).setResultsName('columns', > listAllMatches=True), > p.Group(primary_key).setResultsName('pkeys', > listAllMatches=True), > p.Group(constraint).setResultsName('constraints', > listAllMatches=True)] > )) > + p.Suppress(')') > )("defs") > + p.CaselessKeyword('on').suppress() > + bracket_quoted.suppress() > ) > > There was no need to use nestedExpr for the list of column/key/constraint > items. Note the use of listAllMatches in the setResultsName calls. Saw that. That makes sense. So, it doesn't change the asList() output any, but I have attributes I can access by name now. Cool. One question: ("defs") is a call to what Groups() returns, but I'm not following. Can you point me to something in the docs that explains what is done when you call a pyparsing expression? Again, thanks so much! I'm much further along to where I need to be. j -- Joshua Kugler VOC/SigNet Provider (aka Web App Programmer) S&K Aerospace Alaska |