Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dandandan/parser
Parser combinator library for Elm
https://github.com/dandandan/parser
Last synced: 2 months ago
JSON representation
Parser combinator library for Elm
- Host: GitHub
- URL: https://github.com/dandandan/parser
- Owner: Dandandan
- Created: 2014-06-19T14:20:27.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-11-29T10:03:20.000Z (about 8 years ago)
- Last Synced: 2024-06-19T01:58:09.302Z (7 months ago)
- Language: Elm
- Homepage:
- Size: 60.5 KB
- Stars: 36
- Watchers: 9
- Forks: 13
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
**Parser**
======Parsers convert text sequences into a data structure, for example
for handling user input and compiling programming languages.With this parser combinator you can build parsers in a modular way, building
parsers by combining parser functions. Parser functions can either succeed or
fail.Examples
======```elm
{-| Parse a optional sign, succeeds with a -1 if it matches a minus `Char`, otherwise it returns 1 -}
sign : Parser Int
sign =
let
minus =
map (always -1) (symbol '-')
plus =
map (always 1) (symbol '+')
in
optional (or plus minus) 1{-| Parse a digit -}
digit : Parser Int
digit =
let
charToInt c = Char.toCode c - Char.toCode '0'
in
map charToInt (satisfy Char.isDigit){-| Parse a natural number -}
natural : Parser Int
natural =
some digit
|> map (List.foldl (\b a -> a * 10 + b) 0){-| Parse an integer with optional sign -}
integer : Parser Int
integer =
map (*) sign
|> andMap natural
```