[TransWarp] fmtparse - named rules
Phillip J. Eby
pje at telecommunity.com
Mon Jun 16 12:32:36 EDT 2003
At 08:19 PM 6/16/03 +0400, Oleg Broytmann wrote:
>On Mon, Jun 16, 2003 at 11:37:56AM -0400, Phillip J. Eby wrote:
> > At 01:15 PM 6/16/03 +0400, Oleg Broytmann wrote:
> > >between_expr = Named("between", ExtractString(Sequence("between", '(',
> > >Named("name"), ',', Named("value1"), ',', Named("value2"), ')')))
> > >
> > >test5 = "between(month,1,12)"
> > >print parse(test5, between_expr)
> > >
> > > {'between': 'between(month,1,12)'}. ExtractString extracted too much...
> >
> > What did you want it to extract? You told it to extract
> > Sequence("between", '(', ... ')'), so that's what you got. I'm having
> > trouble understanding what you *want* to do here.
>
> I want to get back the tree
>
>{'between': {"name": "month", "value1": "1", "value2": "12"}}
Ah. This is what Tuple is for...
between_expr = Named("between",
Tuple(
"between(",
Named("name"), ",",
Named("value1"), ",",
Named("value2"), ")"
)
)
Should give you:
{'between': (
("name","month"), ("value1","1"), ("value2": "12")
)
}
Or, you can do this, which is shorter, if you don't care about the names:
es = ExtractString()
between_expr = Named("between", Tuple("between(", es, "," es, ",", es, ")"))
Which gives:
{'between': ("month", "1", "12")}
And is faster, too.
More information about the PEAK
mailing list