https://github.com/fgdorais/lean4-parser
Parser Combinator Library for Lean 4
https://github.com/fgdorais/lean4-parser
lean lean4
Last synced: 4 months ago
JSON representation
Parser Combinator Library for Lean 4
- Host: GitHub
- URL: https://github.com/fgdorais/lean4-parser
- Owner: fgdorais
- License: apache-2.0
- Created: 2023-01-08T21:16:12.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-12-19T00:02:06.000Z (6 months ago)
- Last Synced: 2025-12-21T23:07:07.508Z (5 months ago)
- Topics: lean, lean4
- Language: Lean
- Homepage:
- Size: 12.9 MB
- Stars: 66
- Watchers: 1
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Lean 4 / Parser
A parser combinator library for [Lean 4](https://leanprover.github.io/).
Source documentation is available at [www.dorais.org/lean4-parser/doc/](https://www.dorais.org/lean4-parser/doc).
## Usage
Add this dependency to your project's `lakefile.toml`:
```toml
[[require]]
name = "Parser"
git = "https://github.com/fgdorais/lean4-parser"
rev = "main"
```
Then add `import Parser` at the top of any Lean file where you plan to use this library.
For example:
```lean
import Parser
open Parser Char
/--
Parses a list of sign-separated integers (no spaces) from an input string and returns the sum.
-/
def parseSum : SimpleParser Substring Char Int := do
let mut sum : Int := 0
-- parse until all input is consumed
while ! (← test endOfInput) do
-- parse an integer (decimal only) and add to sum
sum := sum + (← ASCII.parseInt)
return sum
-- returns 42
#eval match parseSum.run "11-1+2-3+33" with
| .ok _ sum => sum
| .error _ e => panic! (toString e)
```
The `examples` directory contains more elaborate sample parsers.
## Acknowledgements
Original work for the Lean 4 Parser library was done by [François G. Dorais](https://github.com/fgdorais), [Kyrill Serdyuk](https://github.com/kyserd), and [Emma Shroyer](https://github.com/emma-shroyer).
This work was partly supported by the CEMS REU program at The University of Vermont.
-----
* The Parser library is copyright © 2022-2025 François G. Dorais, Kyrill Serdyuk, Emma Shroyer. The library is released under the [Apache 2.0 license](http://www.apache.org/licenses/LICENSE-2.0). See the file LICENSE for additional details.