Hello!
I myself wrote an elm parser in rust, using the lalrpop library (my parser). It can parse pretty much any elm code, but doesn’t do much beside that.
When starting my project I considered nom, but at the time, the documentation was not good enough for me, and I found the error messages confusing. I went with Lalrpop because I’m familiar with parser generators, it has neat error messages and has a grammar close to bnf, this was of fantastic help when starting out.
I’ve not much experience with rust outside of that one project, so take what I say with a grain of salt .
First, you should use clippy, it gives tips on how to improve your code, it is especially useful for beginners. I see for example in your code clippy identifies a lot of if vec.len() == 0
where you could do if vec.is_empty()
. I also see that clippy is a bit iffy with nom macros.
This is an elm forum, so I feel uncomfortable giving more rust tips . Otherwise I would tell you how to put your test code in its own module and how to use include_str!
to test your parser over whole elm files.
I wrote a formal grammar of the elm syntax to help for my own parser, I hope it can be useful to anyone, but again, it is mostly for myself and it might not follow exactly the actual grammar I use. the grammar
A formal grammar is more useful with a parser generator. Plus, the way I handle indentation is questionable at best and a typical workaround due to the architecture of parser generators.
A parser combinator library like nom is much closer to what the original elm compiler uses. And in there, I don’t see any special handling of indentation. The indentation detection code seems to lie with the parsing code with let
and case branch parsing.
A parser is also disappointingly useless. The hardest part (but also the most fulfilling) is in fact using the resulting AST to do something (for example, say code validity checking). I’m myself a little disappointed in what I made. It’s easy to test though, because of the very simple input -> output concept of them.
If you have any question about rust in general, or parsing elm, I am open to them. If you have questions about nom you should ask in their chat channels (they are linked in the nom github page)