# elm/parser

> Regular expressions are quite confusing and difficult to use. This library provides a coherent alternative that handles more cases and produces clearer code.
>
> (Readme.md from the [package](https://package.elm-lang.org/packages/elm/parser/latest/))

```
type alias Point =
  { x : Float
  , y : Float
  }

point : Parser Point
point =
  succeed Point
    |. symbol "("
    |. spaces
    |= float
    |. spaces
    |. symbol ","
    |. spaces
    |= float
    |. spaces
    |. symbol ")"
```

This is a summary of the talk [Demystifying Parsers](https://www.youtube.com/watch?v=M9ulswr1z0E) by Tereza Sokol.

## Basics

| Function                                                                                                                    | Description                                                                                                                           |
| --------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| [run](https://package.elm-lang.org/packages/elm/parser/latest/Parser#run) : Parser a -> String -> Result (List DeadEnd) a   | Runs the Parser on a String                                                                                                           |
| [int](https://package.elm-lang.org/packages/elm/parser/latest/Parser#int) : Parser Int                                      | Parser for Int                                                                                                                        |
| [float](https://package.elm-lang.org/packages/elm/parser/latest/Parser#float) : Parser Float                                | Parser for Float                                                                                                                      |
| [oneOf](https://package.elm-lang.org/packages/elm/parser/latest/Parser#oneOf) : List (Parser a) -> Parser a                 | <p>Trys different Parsers. Uses the first that succeeds.</p><p>Once the parser matches the first element, there is no going back!</p> |
| [andThen](https://package.elm-lang.org/packages/elm/parser/latest/Parser#andThen) : (a -> Parser b) -> Parser a -> Parser b | Applys the first parser and if successful applies the second. Fails if one of the parsers fails.                                      |

## Pipeline

{% hint style="info" %}
This subject is explained at [4:48 in the Video](https://youtu.be/M9ulswr1z0E?t=288).
{% endhint %}

| Function                                                                                                                      | Description                                                                                                                                                    |
| ----------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [succeed](https://package.elm-lang.org/packages/elm/parser/latest/Parser#succeed) : a -> Parser a                             | <p>Starts the Pipeline. <code>a -> Parser a</code>  is the constructor.</p><p>For a record <code>Point</code> use the structur called <code>Point .</code></p> |
| [(\|=)](https://package.elm-lang.org/packages/elm/parser/latest/Parser#\(\|=\)) : Parser (a -> b) -> Parser a -> Parser b     | Keeps an element.                                                                                                                                              |
| [(\|.)](https://package.elm-lang.org/packages/elm/parser/latest/Parser#\(\|.\)) : Parser keep -> Parser ignore -> Parser keep | Eats a character and throws it away.                                                                                                                           |
| [spaces](https://package.elm-lang.org/packages/elm/parser/latest/Parser#spaces) : Parser ()                                   | Represents whitespace                                                                                                                                          |

## Strings

{% hint style="info" %}
This subject is explained at [7:20 in the Video](https://youtu.be/M9ulswr1z0E?t=440).
{% endhint %}

| Function                                                                                                                        | Description                                                                                  |
| ------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
| [chompWhile](https://package.elm-lang.org/packages/elm/parser/latest/Parser#chompWhile) : (Char -> Bool) -> Parser ()           | Looks for zero or more characters that succeeds the check. Stops as soon as the check fails. |
| [chompIf](https://package.elm-lang.org/packages/elm/parser/latest/Parser#chompIf) : (Char -> Bool) -> Parser ()                 | Reads one symbols.                                                                           |
| [getChompedString](https://package.elm-lang.org/packages/elm/parser/latest/Parser#getChompedString) : Parser a -> Parser String | Returnes the string that got chomped                                                         |

## Loops

{% hint style="info" %}
This subject is explained at [14:10 in the Video](https://youtu.be/M9ulswr1z0E?t=440).
{% endhint %}

| Function                                                                                                                                                                                                   | Description                                                         |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------- |
| [loop](https://package.elm-lang.org/packages/elm/parser/latest/Parser#loop) : state -> (state -> Parser ([Step](https://package.elm-lang.org/packages/elm/parser/latest/Parser#Step) state a)) -> Parser a | Takes an initial state and a parser step and returns a parser.      |
| type [Step](https://package.elm-lang.org/packages/elm/parser/latest/Parser#Step) state a = Loop state \| Done a                                                                                            | A parser step just specifies if it should continue looping or stop. |

## Error messages

{% hint style="info" %}
This subject is explained at [17:04 in the Video](https://youtu.be/M9ulswr1z0E?t=1024).
{% endhint %}

| Function                                                                                               | Description                                                                          |
| ------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ |
| [problem](https://package.elm-lang.org/packages/elm/parser/latest/Parser#problem) : String -> Parser a | Returns the error message and ends the parsing. The argument is the actuall message. |

## Further Reading

* 🎥**Video:** [Demystifying Parsers](https://www.youtube.com/watch?v=M9ulswr1z0E) by Tereza Sokol
* 📄**Thread:** [Elm parser question](https://www.reddit.com/r/elm/comments/bcb2fi/elm_parser_question/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://orasund.gitbook.io/elm-cookbook/frameworks-1/elm-parser.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
