In this tutorial we'll build a parser for simple CLI commands. Our parser will be able to parse numbers, words, and basic quoted strings, since those are the most common types one might encounter in simple CLI applications.
All our parser functions will follow the same pattern. They'll accept a string as input and return an array of token objects, plus any remaining input that didn't get consumed. Our finished function will take something like this:
echo "Hello World" 3.14
into this:
[ { type: 'word', value: 'echo' }, { type: 'string', value: 'Hello World' }, { type: 'number', value: 3.14 } ]
Let's do numbers first, because they are easiest!