The simplest way to parse case insensitive operators with FParsec's OperatorPrecedenceParser is to add operator definitions for every casing you want to support. If you only need to support short operator names, such as "and" or "or", you could simply add all possible case combinations. If you want to use operator names that are too long for this approach, you might consider only supporting the sane casings, i.e. lowercase, UPPERCASE, camelCase and PascalCase. When you want to support multiple casings, it is usually convenient to write a helper function that automatically generates all the needed casings for you from a standard one.
If you have long operator names and you really want to support all casings, the OperatorPrecedenceParser
's dynamic configurability also allows the following approach, which should be easier and more efficient than transforming the input:
- Search the input for all case insensitive occurrences of the supported operators. This search shouldn't miss any occurrences, but it's no problem if it finds false positives if e.g. the operator name is used inside a function name or inside a string literal.
- Add all unique casings you found in step 1 to the
OperatorPrecedenceParser
. (Usually there won't be many casings of the same operator.) - Parse the input with the configured
OperatorPrecedenceParser
.
When you parse multiple inputs, you can keep the OperatorPrecedenceParser
instance around and just lazily add new operators casings as you need them.