[TransWarp] fmtparse - recursive grammar
alexander smishlajev
alex at ank-sia.com
Thu Jun 26 15:04:56 EDT 2003
Phillip J. Eby wrote:
>
> I found a simpler way to fix the issue, and have checked it in.
thank you.
we found that Rule and Epsilon classes haven't got the third argument to
getOpening(). this breaks Repeat() without explicit separator.
the following example parses simple function-like boolean expressions.
if there is something wrong with it, i would appreciate your comments.
=== cut ===
from peak.util.fmtparse import *
class Expression(Alternatives):
alternatives = []
def __init__(self, *alternatives):
if alternatives:
super(Expression, self).__init__(*alternatives)
class Function(Tuple):
syntax = ()
def __init__(self, *__args, **__kw):
if not __args:
__args = self.syntax
super(Function, self).__init__(*__args, **__kw)
class Comparison(Function):
syntax = (
ExtractString(Alternatives(
"eq", "neq", "lt", "le", "gt", "ge"
)), "(", Tuple(ExtractString(), ",", ExtractString()), ")",
)
class AndOr(Function):
syntax = (
ExtractString(Alternatives("and", "or")),
"(", Repeat(Expression(), separator=",", minCt=2), ")",
)
Expression.alternatives = (Comparison(), AndOr())
query = Named("query", Expression())
def test():
print parse("eq(a,1)", query)
print parse("and(eq(a,1),lt(b,2))", query)
print parse("or(and(lt(a,1),gt(b,2)))", query)
print parse("or(and(lt(a,1),gt(b,2)),eq(c,3),neq(d,4))", query)
if __name__ == "__main__":
test()
=== cut ===
thanks again,
alex.
More information about the PEAK
mailing list