← スキル一覧に戻る

parsing
by kim-em
Advent of Code 2025 solutions in Lean 4 with proofs
⭐ 0🍴 0📅 2025年12月16日
SKILL.md
name: parsing description: Use the lean4-parser library for parsing structured input. Invoke when implementing parsers for AoC puzzles or other text processing tasks in Lean. allowed-tools: Read, Edit, Write
Parsing Skill: lean4-parser Library
When parsing structured input (especially for AoC puzzles), use the lean4-parser library instead of manual string splitting.
Installation
Already configured in lakefile.toml:
[[require]]
name = "Parser"
git = "https://github.com/fgdorais/lean4-parser"
rev = "main"
Import
import Parser
open Parser Char
Quick Reference
Parser Types
-- Use SimpleParser for good error messages
abbrev MyParser := SimpleParser Substring Char
Running Parsers
match myParser.run inputString with
| .ok _ result => -- use result
| .error _ e => -- handle error
Common Combinators
| Combinator | Description |
|---|---|
ASCII.parseNat | Parse natural number |
ASCII.parseInt | Parse signed integer |
char c | Match exact character |
string s | Match exact string |
space, whitespace | Whitespace matching |
eol | End of line (LF or CRLF) |
endOfInput | Assert at end of input |
takeMany p | Zero or more, collect results |
dropMany p | Zero or more, discard |
sepBy p sep | Items separated by delimiter |
first [p1, p2] | Try alternatives |
test p | Check without consuming (returns Bool) |
optional p | Zero or one |
Example: Parse Numbers from Line
def parseLine : SimpleParser Substring Char (List Nat) := do
sepBy ASCII.parseNat (dropMany1 (char ' '))
Example: Parse Coordinates
def parseCoord : SimpleParser Substring Char (Int × Int) := do
let _ ← char '('
let x ← ASCII.parseInt
let _ ← char ','
dropMany space
let y ← ASCII.parseInt
let _ ← char ')'
return (x, y)
Example: Parse Key-Value Pairs
def parseKeyValue : SimpleParser Substring Char (String × Int) := do
let key ← takeMany1 alpha
let _ ← char ':'
dropMany space
let value ← ASCII.parseInt
return (⟨key⟩, value)
Example: Parse with Alternatives
def parseDirection : SimpleParser Substring Char Int := first [
string "up" *> pure 1,
string "down" *> pure (-1),
string "left" *> pure 0,
string "right" *> pure 0
]
Example: Looping Until End
def parseAll : SimpleParser Substring Char (List Int) := do
let mut results := []
while !(← test endOfInput) do
let n ← ASCII.parseInt
results := results ++ [n]
dropMany (char ' ' <|> char '\n')
return results
Documentation
- Full docs: https://www.dorais.org/lean4-parser/doc/
- Repository: https://github.com/fgdorais/lean4-parser
スコア
総合スコア
50/100
リポジトリの品質指標に基づく評価
✓SKILL.md
SKILL.mdファイルが含まれている
+20
○LICENSE
ライセンスが設定されている
0/10
○説明文
100文字以上の説明がある
0/10
○人気
GitHub Stars 100以上
0/15
○最近の活動
3ヶ月以内に更新がある
0/10
○フォーク
10回以上フォークされている
0/5
✓Issue管理
オープンIssueが50未満
+5
✓言語
プログラミング言語が設定されている
+5
○タグ
1つ以上のタグが設定されている
0/5
レビュー
💬
レビュー機能は近日公開予定です