Using the parser generator nom, how can I write a parser which extracts the difference of the minus sign in the terms 1-2 and 1*-2 ?
In the first example, I expect the tokens 1, - and 2. In the second the "minus" sign specifies the number being negative. The expected tokens are 1, * and -2. Not 1, *, - and 2.
How can I make nom stateful, with user-defined states such as expect_literal: bool?
The best solution I found for now is using nom_locate with a span defined as
Then you can modify the state via
where
TokenPayloadis an enum representing my token content.Now you can write the operator parser:
And the integer parser as:
This might not win a beauty contest but it works. The remaining pieces should be trivial.