sprache / Sprache

A tiny, friendly, C# parser construction library
MIT License
2.35k stars 215 forks source link

Escape unescaped characters #170

Open rizengie opened 3 years ago

rizengie commented 3 years ago

I've found this article on Stack Overflow which doesn't seem to help enable escaping non-escaped characters, in particular, parenthesis. The case I'm looking for here is to be able to parse Any Character until a set of characters (such as parenthesis and comma "),", and/or parenthesis and end of string.

My test case:

where=and(eq(someString,Call Trace (*54)),eq(someString,Call Trace (*54)))

The way our parser currently works, we use Parse.AnyChar.Except( '(', ')' ) I think it'd be more helpful to be able to say Until( "),").Or(")" + EndOfString)

Is there a current implementation on how to do this, or can this be added?

nblumhardt commented 3 years ago

Hi! I haven't used Sprache in a long time, but I think:

Parse.AnyChar.Until(Parse.String("),").Or(Parse.String(")").AtEnd()))

is roughly the kind of thing you're looking for. There's something a bit awkward about constructing the grammar this way, though - it's rare for a parser to have to "care" about what comes after its matched input. In particular, an expression parser should usually take care of matching parentheses etc. --- Is the grammar something you have control over, or are you parsing the output of some other system that produces irregular strings?