51 lines
1.2 KiB
Markdown
51 lines
1.2 KiB
Markdown
# Haskell Parser Combinators
|
|
|
|
This note covers `12-haskell-parser-combinators/`, which parses a tiny deploy-command language with Megaparsec.
|
|
|
|
---
|
|
|
|
## 1. Why Parser Combinators Matter
|
|
|
|
Parser combinators let you build a parser out of smaller parsers:
|
|
|
|
- parse one token,
|
|
- combine it with another parser,
|
|
- choose between alternatives, and
|
|
- repeat parts that can appear many times.
|
|
|
|
That makes them a very Haskell-shaped tool: you write small functions, compose them, and end up with a parser for a whole language.
|
|
|
|
---
|
|
|
|
## 2. What This Example Shows
|
|
|
|
The example defines parsers for:
|
|
|
|
- the environment,
|
|
- identifiers,
|
|
- tag lists, and
|
|
- the full deploy command.
|
|
|
|
The full parser then composes those pieces with sequencing, `choice`, `many`, and `sepBy1`.
|
|
|
|
That is the main intermediate idea: treat a parser as a reusable value, not a one-off block of string handling code.
|
|
|
|
---
|
|
|
|
## 3. Commands to Try
|
|
|
|
```bash
|
|
cd 12-haskell-parser-combinators
|
|
|
|
nix develop
|
|
cabal run
|
|
cabal run -- deploy api production tags=blue,stable
|
|
cabal test
|
|
|
|
nix build
|
|
./result/bin/mini-parser deploy api production tags=blue,stable
|
|
|
|
nix run . -- deploy api production tags=blue,stable
|
|
nix flake check
|
|
```
|