https://github.com/lukad/pears
A parser combinator library for gleam
https://github.com/lukad/pears
Last synced: 2 months ago
JSON representation
A parser combinator library for gleam
- Host: GitHub
- URL: https://github.com/lukad/pears
- Owner: lukad
- Created: 2024-03-13T21:58:19.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-16T09:13:38.000Z (9 months ago)
- Last Synced: 2025-02-08T03:11:19.397Z (3 months ago)
- Language: Gleam
- Size: 32.2 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Pears - A parser combinator library for Gleam
[](https://hex.pm/packages/pears)
[](https://hexdocs.pm/pears/)🚧️ **This library is in early development. Expect breaking changes.** 🚧
## Installation
```sh
gleam add pears
```## Usage
```gleam
import pears.{type Parser, Parsed}
import pears/chars.{type Char, number}
import pears/combinators.{alt, between, just, lazy, map, sep_by0}pub type Tree(a) {
Leaf(a)
Node(List(Tree(a)))
}fn tree_parser(p: Parser(Char, a)) -> Parser(Char, Tree(a)) {
let tree = lazy(fn() { tree_parser(p) })
let leaf = map(p, Leaf)
let node =
tree
|> sep_by0(just(","))
|> between(just("["), just("]"))
|> map(Node)
alt(leaf, node)
}pub fn main() {
let parse_result =
"[1,[2,3],4]"
|> chars.input()
|> tree_parser(number())let assert Ok(Parsed([], Node([Leaf(1), Node([Leaf(2), Leaf(3)]), Leaf(4)]))) =
parse_result
}
```Further documentation can be found at [https://hexdocs.pm/pears](https://hexdocs.pm/pears/pears.html).
See the [test](./test) directory for more examples.
## What's missing?
- Proper error handling
- Test helpers
- ...## Development
```sh
gleam test # Run the tests
gleam shell # Run an Erlang shell
```