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 packagearrow-up-right)

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 Parsersarrow-up-right by Tereza Sokol.

Basics

Function

Description

runarrow-up-right : Parser a -> String -> Result (List DeadEnd) a

Runs the Parser on a String

intarrow-up-right : Parser Int

Parser for Int

floatarrow-up-right : Parser Float

Parser for Float

oneOfarrow-up-right : List (Parser a) -> Parser a

Trys different Parsers. Uses the first that succeeds.

Once the parser matches the first element, there is no going back!

andThenarrow-up-right : (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

circle-info

This subject is explained at 4:48 in the Videoarrow-up-right.

Function

Description

succeedarrow-up-right : a -> Parser a

Starts the Pipeline. a -> Parser a is the constructor.

For a record Point use the structur called Point .

(|=)arrow-up-right : Parser (a -> b) -> Parser a -> Parser b

Keeps an element.

(|.)arrow-up-right : Parser keep -> Parser ignore -> Parser keep

Eats a character and throws it away.

Represents whitespace

Strings

circle-info

This subject is explained at 7:20 in the Videoarrow-up-right.

Function

Description

chompWhilearrow-up-right : (Char -> Bool) -> Parser ()

Looks for zero or more characters that succeeds the check. Stops as soon as the check fails.

chompIfarrow-up-right : (Char -> Bool) -> Parser ()

Reads one symbols.

getChompedStringarrow-up-right : Parser a -> Parser String

Returnes the string that got chomped

Loops

circle-info

This subject is explained at 14:10 in the Videoarrow-up-right.

Function

Description

looparrow-up-right : state -> (state -> Parser (Steparrow-up-right state a)) -> Parser a

Takes an initial state and a parser step and returns a parser.

type Steparrow-up-right state a = Loop state | Done a

A parser step just specifies if it should continue looping or stop.

Error messages

circle-info

This subject is explained at 17:04 in the Videoarrow-up-right.

Function

Description

problemarrow-up-right : String -> Parser a

Returns the error message and ends the parsing. The argument is the actuall message.

Further Reading

Last updated