Storing allowed state machine transitions

I am writing a parser, and I want to allow only certain types of tokens after certain other types of tokens, like, you can't have a number after another number, only a space or an operator. I have a number of different enums, one for each token, and I can create another enum to wrap all of them into one. Or just merge all those separate enums:

enum WhiteSpace {
    WhiteSpace(Vec<char>),
}
pub enum Number {
    Integer(Vec<char>),
    Decimal(Vec<char>),
}
//etc

Instances of each of those enums are fed characters from the input, and when they get a char that does not belong to that type of token, they return an Error. In that case, I want to try and feed that char to every token type that can follow the current one, and for that I need to have some kind of a table, like:

...
WhiteSpace: [Number, Operator],
Number: [WhiteSpace],
Operator: [WhiteSpace]

But how can I do that?

Maybe look at the nom crate.

There's a good walk though in this video.

Thank you for your reply, but I don't actually need that parser, I am doing this to learn Rust

Not sure, but they may have meant that you may find ideas if you look at how nom is implemented.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.