datalust / superpower

A C# parser construction toolkit with high-quality error reporting
Apache License 2.0
1.05k stars 98 forks source link

Send parser value into another parser #151

Closed martinkoslof closed 1 year ago

martinkoslof commented 1 year ago

Hello there,

I plan on migrating a work in progress parser to Superpower. Right now, the code I am using is Sprache, but my problem will just migrate over to Superpower, since I don't know how to resolve it (in Sprache or Superpower).

High level, I am trying to parse a query string filter value. This string can have and/ors, some function calls i.e. (startsWith(Property, value). Currently I am doing the following:

_property = Parser that gets the property member expression

_function = Parser that will find a startsWith( ), contains( ) or any valid function

_constant = Parser which gets the value of the filter (Number, String, Boolean) etc

_expr = Parser that looks for nested or sub expressions such as ( term and term) or (...)) and does an XOr on _constant or and XOr on _function.Or(_property)

_comparisonTerm = Parser that looks for the "eq, neq, gt, lt" or the filter comparison component. This is a Parse.ChainOperator with _expr

_binaryTerm = Parser that looks for the "and" "or" and is a Parse.ChainOperator with _comparison term.

With these parsers defined, I now build my recursively parsed Expression via: _binaryTerm.TryParse(filter)

Everything works, but I have one problem. I would like to pass the value or token from the _property parser into the _constant and _comparisonTerm parsers and I can't figure out how to do it. If I change the _constant parser code to be something like from n in NumberConst.Xor(StringConst) from p in _property.Token() select ... the property parser is called again, and it's now on the wrong character in the string, so it's not correct.

I have some additional logic checks I need to do against the filter string which is cached in a dictionary. I need to more or less know "The value of 'Foo' was requested for property Bar" - is this valid. "The gt operator was requested for property Bar" - is this valid. I cannot figure out how to pass the _property value into the other parsers as it is recursively evaluating the string and building the expression.

Can anyone offer any ideas of how to do this?

Thanks for any ideas or assistance.

martinkoslof commented 1 year ago

I am going to try taking a different approach.