sebastienros / parlot

Fast and lightweight parser creation tools
BSD 3-Clause "New" or "Revised" License
381 stars 45 forks source link

Can I read the delimiter of a Separated parser? #78

Open deanebarker opened 2 years ago

deanebarker commented 2 years ago

Say I do this:

var booleanDelimiter = OneOf(Terms.Text("and"), Terms.Text("or"));

Then I use that parser as the delimiter for a Separated parser:

Separated(booleanDelimeter, filterClause).Then(v => // config my filter);

As part of the filter, I want to know if it was prefaced by an and or an or. To do this, I need to read from the booleanDelimiter.

Is this possible? Or is there a better way?

sebastienros commented 2 years ago

I would say that Separated is not the best construct here, and you'd better use filterClause.And(ZeroOrMany(booleanDelimiter.And(filterClause)))

Note that this will result in left-precedence for and/or, meaning there won't be a priority on either of them (like + vs *) because you are reading from left to right. Usual grammars use a recursive approach when you get a tree-like structure to define precedence of operators. Liquid for instance does the same thing you are doing here (which is uncommon).

sebastienros commented 2 years ago

So yeah, Separated is more a "forget about me" thing for the separator.