You could set up that parser as a simple state machine, but that’s not very FP and can be a bit tricky to get right for more than just a couple states. I’ve written a small parser combinator POC that produces an immutable AST which might be along the lines of what you’re looking for
https://github.com/copper-leaf/kudzu