Code Monkey home page Code Monkey logo

elm-parser's Introduction

Elm Parser

General purpose parser library for Elm

elm-package install jaredramirez/elm-parser

Usage

module Sample exposing (..)

import Parser exposing (Parser, (|=), (|*))
import Parser.Char as Char
import Parser.Number as Number


tuple : Parser ( Int, Int )
tuple =
    Parser.parse (\x y -> ( x, y ))
        |* Char.char '('
        |= Number.naturalNumber
        |* Char.char ','
        |= Number.naturalNumber
        |* Char.char ')'

run1 =
  Parser.run tuple "(3,3)"

run2 =
  Parser.run tuple "(35)"

run1 will produce the value:

Ok (3,3)

run2 will produce the value:

Err
    ( { source = "(35)"
      , offset = 3
      , row = 1
      , col = 4
      }
    , ExpectedSymbol ","
    )

In the run2 example, you are givin the state of the parser at the moment of failure and the Problem with the parse operation.

You can look all the possible values of Problem's here.

How it works

Take the parser from the above example.

transform : Int -> Int -> ( Int, Int )
transform x y =
    ( x, y )


tuple : Parser ( Int, Int )
tuple =
    Parser.succeed transform
        |* Char.char '('
        |= Number.naturalNumber
        |* Char.char ','
        |= Number.naturalNumber
        |* Char.char ')'

First, we create the function transform that takes two Ints, and puts them in a tuple. Then, in the first line of the tuple function, we take the function transform and lifting it into a "parser". In the subsequent parts of the pipline, we are applying parsers to the function transform. If the parser in the pipline begins with

  • |* it means "run is parser and make sure it is successful, then throw away the result".
  • |= it means "run is parser and make sure it is successful, then apply the result to the function".

This relies on partial function application. In this case, transform has two arguements, so in our pipeline we must have two |= to get a result, otherwise transform won't have all of it's arguements applied!

For more on the pipeline parser concept, see this.

You can also use this library to chain parsers with andThen. This is generally not as readable and can be more confuisng than pipeline-style parsing. It can be helpeful to have though. Say you only want tuples where the values are equal. That is, (1,1) would succeed but (1,2) would not. You can refactor tuple it include that functionality easily with andThen:

tuple : Parser ( Int, Int )
tuple =
    let
        tupleParser =
            Parser.succeed transform
                |* Char.char '('
                |= Number.naturalNumber
                |* Char.char ','
                |= Number.naturalNumber
                |* Char.char ')'
    in
        tupleParser
            |> Parser.andThen
                (\( x, y ) ->
                    if x == y then
                        Parser.succeed ( x, y )
                    else
                        Parser.fail <|
                            Parser.Bad "I expected the values to match"
                )

Thanks

While I wrote all of the code in this package, most of it was heavily influenced/inspired by others (with the exception of Parser.Html). This package was written for the purpose of my learning, and I figured I'd publish it for kicks. So, a big thanks to the following as they taught me a lot about parsers in a functional language, and are great resources.

elm-parser's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.