Difficulty writing PEG recursive expression grammar with Arpeggio

74 views Asked by At

My input text might have a simple statement like this:

aircraft

In my language I call this a name which represents a set of instances with various properties. It yields an instance_set of all aircraft instances in this example.

I can apply a filter in parenthesis to any instance_set:

aircraft(Altitude < ceiling)

It yields another, possibly reduced instance_set. And since it is an instance set, I can filter it yet again:

aircraft(Altitude < ceiling)(Speed > min_speed)

I foolishly thought I could do something like this in my grammar:

instance_set = expr
expr = source / instance_set
source = name filter?

It parses my first two cases correctly, but chokes on the last one:

 aircraft(Altitude < ceiling)(Speed > min_speed)

The error reported being just before the second open paren.

Why doesn't Arpeggio see that there is just a filtered instance_set which is itself a filtered instance set?

I humbly submit my appeal to the peg parsing gods and await insight...

1

There are 1 answers

1
rici On

Your first two cases both match source. Once source is matched, it's matched; that's the PEG contract. So the parser isn't going to explore the alternative.

But suppose it did. How could that help? The rule says that if an expr is not a source, then it's an instance_set. But an instance_set is just an expr. In other words, an expr is either a source or it's an expr. Clearly the alternative doesn't get us anywhere.

I'm pretty sure Arpeggio has repetitions, which is what you really want here:

source = name filter*