datalust / superpower

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

Negative test / equivalent of Token.EqualTo #114

Closed straatvark closed 3 years ago

straatvark commented 4 years ago

Hi - how would one approach the scenario where you would like to find a certain token - but only when it is NOT following another token type (or types)?

I already tokenised the text and are parsing the tokens into my Abstract Syntax Tree classes. I only want to continue with an instance of "MyWordClass" when it is NOT following one or more other token types.

Currently my example return an instance of "MyWordClass" - only when it is following token 1/ token 2, which is the opposite of what i'm trying to achieve.

Any assistance on the pattern to be used (if possible) would be much appreciated. - i was hoping for soming like "Token.NotEqualTo" ?

public static TokenListParser<TestNameToken, MyWordClass> WordNotAfaterToken1OrToken2 => from unwantedPrefix in Token.EqualTo(TestNameToken.Token1).Try() .Or(Token.EqualTo(TestNameToken.Token2).Try()) from word in Token.EqualTo(TestNameToken.Word) select new MyWordClass(word);

nblumhardt commented 4 years ago

Hi! Thanks for getting in touch. Usually this kind of thing requires a rethink/reorganizing of the grammar. It's a bit tricky to offer quick advice without a better picture of what your input and intended parse tree look like. Posting the full grammar and input to Stack Overflow, with the #superpower tag, will get more eyes on it (please feel free to post a link here, too, and we'll check it out as soon as we are able to!). Hope this helps - NB