While trying to learn about elm/parser I decided to try and write a parser for urls. Because a lot of parts of a url are optional, I have to be able to look ahead in the given url to decide whether to go down a path or not. For example a url can start with or without a protocol, and as far as I know the deciding factor is whether or not a
// is present.
While digging through the documentation I found out about the
oneOf (docs) function which I thought could be used for this problem. However, this function will only try other parsers if a previous parser hasn’t chomped any characters. Considering the parser for the “protocol” and the “auth” section of a url are both just strings, it will always pick the parser for the “protocol”.
The docs for the
oneOf function also give a link to a document which mentions backtracking. This sounds like exactly the thing I need, however that document also mentions that if you shuffle your parsers around, you do not need any backtrackable parsers at all. Which brings me to my question.
How would one write a parser with signature
Parser (Maybe Protocol) without a backtrackable parser?
- I’ve created an Ellie for this little project which can be found here