Newline and indentation issues in elm/parser

Hi guys, I was experimenting with elm/parser and was quite satisfied after building a simple top-down math parser. However, when I tried to expand the parser for more complex statements, things just do not work. The main issue is that the indentation and newlines are not tracked properly. Can someone help me? Thanks.

link to ellie:

Hi there! Very nice error reporting in your parser. I think the problem is that you have too many calls to newline. Thus the last one called finds nothing because the “\n”'s have already been “eaten.” Here is something that works on your input:


Whether it is a good solution in general, I don’t know. Note the commented-out newLine in line 291.

Handling whitespace is indeed quite tricky (in my experience, anyway). The typical use of elm/parser is for it to act as both a lexer and and a parser, which is what you are doing.

Thank you for your reply! Using your idea that the newline has already been eaten, I tweaked the spaces function to match only whitespaces and not newline characters:

spaces : MyParser ()
spaces =
    chompWhile (\c -> c == ' ')

This yields the same result as commenting out line 291. However, the else branch of the if statement is not parsed at all. Do you know why?
By the way, I can’t find any useful projects written in elm/parser besides elm-markup, CSV, double-quoted string, etc. None of them use indentation very rigorously. Do you know any small language compilers that are written in elm/parser? uses elm/parser


I’ll try to look at this later today. Re other projects using elm/parser, there is MiniLaTeX, which parses LaTeX and renders it to Html msg. Also MathMarkdown, which is pure Elm Markdown parser-renderer that can also handle math written in LaTeX, e.g. $a^2 + b^2 = c^2$. These are two of my own projects. (See this for demos). I’m not particularly proud of my code at this point and am working to make it better. As @wolfadex noted below, the elm-in-elm compiler uses elm/parser. It is far more sophisticated than my projects.

1 Like

@wolfadex @jxxcarlson Thanks for the project recommendations. I finally figured out a solution after modifying the spaces function. It turns out that I need a newline after the else keyword and before the opening of the else block. Simply change

succeed identity
    |= block

within ifStmt : MyParser Stmt

succeed identity
    |. newline
    |= block

I think the reason why previously elm/parser didn’t produce an error but ignored the else branch completely is that: neither of the options provided matched so elm/parser stopped without reaching the end of the string.

See updated code in Ellie


Great work on your parser!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.