# Newline and indentation issues in elm/parser

**URL:** https://discourse.elm-lang.org/t/newline-and-indentation-issues-in-elm-parser/4869
**Category:** Learn
**Created:** [December 23, 2019, 2:07am UTC](https://discourse.elm-lang.org/t/newline-and-indentation-issues-in-elm-parser/4869 "2019-12-23T02:07:08Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![AlienKevin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/alienkevin/32/2727_2.png) [@AlienKevin](https://discourse.elm-lang.org/u/AlienKevin)
#### Post date: [December 23, 2019, 2:07am UTC](https://discourse.elm-lang.org/t/newline-and-indentation-issues-in-elm-parser/4869/1 "2019-12-23T02:07:08Z")

</div>

Hi guys, I was experimenting with [elm/parser](https://package.elm-lang.org/packages/elm/parser/1.1.0/) 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: [https://ellie-app.com/7yBjqh8sqmMa1](https://ellie-app.com/7yBjqh8sqmMa1)

---

<div class="post-metadata">

### Author: ![jxxcarlson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jxxcarlson/32/866_2.png) [@jxxcarlson](https://discourse.elm-lang.org/u/jxxcarlson)
#### Post date: [December 24, 2019, 7:34am UTC](https://discourse.elm-lang.org/t/newline-and-indentation-issues-in-elm-parser/4869/2 "2019-12-24T07:34:29Z")

</div>

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:

[Example](https://ellie-app.com/7z7YFf9MZ4sa1)

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.

---

<div class="post-metadata">

### Author: ![AlienKevin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/alienkevin/32/2727_2.png) [@AlienKevin](https://discourse.elm-lang.org/u/AlienKevin)
#### Post date: [December 24, 2019, 11:52pm UTC](https://discourse.elm-lang.org/t/newline-and-indentation-issues-in-elm-parser/4869/3 "2019-12-24T23:52:15Z")

</div>

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:

```elm
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?

---

<div class="post-metadata">

### Author: ![wolfadex](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/wolfadex/32/2461_2.png) [@wolfadex](https://discourse.elm-lang.org/u/wolfadex)
#### Post date: [December 25, 2019, 1:25am UTC](https://discourse.elm-lang.org/t/newline-and-indentation-issues-in-elm-parser/4869/4 "2019-12-25T01:25:15Z")

</div>

[https://github.com/elm-in-elm/compiler](https://github.com/elm-in-elm/compiler) uses elm/parser

---

<div class="post-metadata">

### Author: ![jxxcarlson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jxxcarlson/32/866_2.png) [@jxxcarlson](https://discourse.elm-lang.org/u/jxxcarlson)
#### Post date: [December 25, 2019, 8:56am UTC](https://discourse.elm-lang.org/t/newline-and-indentation-issues-in-elm-parser/4869/5 "2019-12-25T08:56:08Z")

</div>

I’ll try to look at this later today. Re other projects using `elm/parser`, there is [MiniLaTeX](https://package.elm-lang.org/packages/jxxcarlson/meenylatex/latest/), which parses LaTeX and renders it to `Html msg`. Also [MathMarkdown](https://package.elm-lang.org/packages/jxxcarlson/elm-markdown/latest/), 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](https://minilatex.io/) 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](https://github.com/elm-in-elm/compiler) uses `elm/parser`. It is far more sophisticated than my projects.

---

<div class="post-metadata">

### Author: ![AlienKevin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/alienkevin/32/2727_2.png) [@AlienKevin](https://discourse.elm-lang.org/u/AlienKevin)
#### Post date: [December 25, 2019, 12:47pm UTC](https://discourse.elm-lang.org/t/newline-and-indentation-issues-in-elm-parser/4869/6 "2019-12-25T12:47:33Z")

</div>

@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

```elm
succeed identity
    |= block

```

within `ifStmt : MyParser Stmt`  
to

```elm
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](https://ellie-app.com/7zGzckkkqcLa1)

---

<div class="post-metadata">

### Author: ![jxxcarlson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jxxcarlson/32/866_2.png) [@jxxcarlson](https://discourse.elm-lang.org/u/jxxcarlson)
#### Post date: [December 26, 2019, 4:26am UTC](https://discourse.elm-lang.org/t/newline-and-indentation-issues-in-elm-parser/4869/7 "2019-12-26T04:26:44Z")

</div>

Great work on your parser!

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)
#### Post date: [January 5, 2020, 4:26am UTC](https://discourse.elm-lang.org/t/newline-and-indentation-issues-in-elm-parser/4869/8 "2020-01-05T04:26:44Z")

</div>

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