https://github.com/anuragsoni/angstrom-examples
Sample parsers using Angstrom
https://github.com/anuragsoni/angstrom-examples
angstrom bencode color ocaml parser
Last synced: about 1 year ago
JSON representation
Sample parsers using Angstrom
- Host: GitHub
- URL: https://github.com/anuragsoni/angstrom-examples
- Owner: anuragsoni
- Created: 2019-01-05T04:59:02.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-04-28T02:24:41.000Z (about 7 years ago)
- Last Synced: 2025-03-17T14:52:43.218Z (about 1 year ago)
- Topics: angstrom, bencode, color, ocaml, parser
- Language: OCaml
- Size: 7.81 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Angstrom Examples
Examples of parsers written using [Angstrom](https://github.com/inhabitedtype/angstrom).
## Build
1. Install `angstrom` (`opam install angstrom`)
2. `dune build`
## Explore via repl
1. `dune utop src`
## Example list
1. Hex color parser. [Link](https://github.com/anuragsoni/angstrom-examples/blob/master/src/colors.ml).
2. Bencode (`.torrent` file format) parser. [Link](https://github.com/anuragsoni/angstrom-examples/blob/master/src/bencode.ml).
We use the following type for representing bencode:
```ocaml
type t =
| Integer of int
| String of string
| List of t list
| Dict of (string * t) list
```
`List` and `Dict` contain values that are themselves bencode values, so we need
a parser that will accept bencode list and dictionary but we don't yet have access
to a parser for bencode values as a whole. We get around the issue by using `fix` from
`Angstrom`. We define our parsers for `List` and `Dict` within the function passed to `fix`,
which gives us access to a parser we can use to parse bencode values as a whole.