20 lines
853 B
Markdown
20 lines
853 B
Markdown
---
|
|
date: 2025-07-22 17:00
|
|
---
|
|
#programming-language
|
|
|
|
一般的なコードフォーマッターの実装について。
|
|
|
|
[How to write a code formatter](https://yorickpeterse.com/articles/how-to-write-a-code-formatter/)
|
|
|
|
1行当たり最大文字数がこのくらい、としたときに、どういう戦略で折り返すかは結構難しい問題。
|
|
|
|
文字列→トークン→ASTという順番で変換されるので、この逆順が良いのかとも思ったが、
|
|
|
|
AST→フォーマッタ用の専用の木構造みたいな中間表現を一度挟んだほうが賢いのかもしれない
|
|
|
|
[[Rust]]の[pretty](https://docs.rs/pretty/latest/pretty/)クレートがコンビネーターとして定義してあるやつ
|
|
|
|
[[Tree-sitter]]を使った汎用フォーマッター[Topiary](https://topiary.tweag.io/)とかいうのもある
|
|
|