baml_fmt: basically a formatter#3111
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds a new baml_fmt formatter crate (AST, printer, trivia classifier, Salsa entry), integrates formatter tests and tools, updates parser to handle pending '>'/ '>>' in type arguments, and adds SyntaxKind::is_keyword. Changes
Sequence Diagram(s)sequenceDiagram
participant Source as Source
participant Lexer as Lexer
participant Parser as Parser/CST
participant Trivia as TriviaClassifier
participant AST as AST Builder
participant Printer as Formatter
Source->>Lexer: lex -> tokens
Lexer->>Parser: token stream
Parser->>Parser: build CST (handle pending '>' / '>>')
Parser->>Trivia: provide CST root
Trivia->>AST: provide EmittableTrivia for tokens
Parser->>AST: provide SOURCE_FILE CST
AST->>Printer: Strong AST + FormatOptions + TriviaInfo
Printer->>Printer: Printable.print with Shape decisions
Printer->>Source: formatted output
Estimated Code Review Effort🎯 5 (Critical) | ⏱️ ~120 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
Merging this PR will not alter performance
|
3f4939b to
7817412
Compare
Binary size checks passed✅ 7 passed
Generated by |
There was a problem hiding this comment.
Actionable comments posted: 11
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
baml_language/crates/baml_compiler_parser/src/parser.rs (1)
606-616:⚠️ Potential issue | 🟡 MinorUpdate stale comment in pending
>handling.The comment says nothing is emitted, but the code now emits a
GREATERtoken.✏️ Suggested doc fix
- // First check if we have a pending '>' from a previous '>>' split. - // Don't emit anything - the '>>' token is already in the tree. + // First check if we have a pending '>' from a previous '>>' split. + // Emit that pending token now to keep the CST balanced.
7817412 to
77169fb
Compare
77169fb to
4ff8afe
Compare
|
Caution Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted. Error details |
There was a problem hiding this comment.
Actionable comments posted: 11
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
baml_language/crates/baml_compiler_parser/src/parser.rs (1)
605-640:⚠️ Potential issue | 🟡 MinorFix the stale comment about pending
>emission.
The code now emits aGREATERtoken whenpending_greaters > 0, but the comment still says “Don’t emit anything,” which is misleading.📝 Proposed comment tweak
- // First check if we have a pending '>' from a previous '>>' split. - // Don't emit anything - the '>>' token is already in the tree. + // First check if we have a pending '>' from a previous '>>' split. + // Emit a virtual '>' token for the pending split.
| pub struct ClientField { | ||
| pub keyword: t::Client, | ||
| // not currently allowed | ||
| // pub colon: Option<t::Colon>, |
There was a problem hiding this comment.
what do you mean -- are colons not allowed inside of clients? It should be possible.
| pub struct ConfigItem { | ||
| pub key: ConfigItemKey, | ||
| // /// Colons are currently invalid, it seems | ||
| // pub colon: Option<t::Colon>, |
There was a problem hiding this comment.
oh yeah this should definitely also be possible. Like either whitespace or colon
6bfeef9 to
74ec0ff
Compare
3a175a1 to
be93f0b
Compare
be93f0b to
23c06ee
Compare
There was a problem hiding this comment.
Actionable comments posted: 8
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
baml_language/crates/baml_compiler_parser/src/parser.rs (1)
3074-3084:⚠️ Potential issue | 🟠 MajorEmit a parse error for trailing commas in generic args.
Lines 3079-3083 currently treat
, >/, >>as a silent break, which can allow invalid trailing commas to pass parsing and later fail during formatting. Consider emitting an unexpected-token error when the comma is trailing.🛠️ Suggested change
while p.eat(TokenKind::Comma) { - if p.at(TokenKind::Greater) || p.at(TokenKind::GreaterGreater) { - break; // Trailing comma - } + if p.at(TokenKind::Greater) || p.at(TokenKind::GreaterGreater) { + p.error_unexpected_token("type argument after comma".to_string()); + break; // Trailing comma + } p.parse_type(); }Based on learnings: In BAML, when formatting generic type parameter lists (angle-bracket syntax), do not use a trailing comma after the last type parameter.
Core formatter (crates/baml_fmt/, ~7,900 lines):
lib.rs— Public API:format(source, options) -> Result<String, FormatterError>. Refuses to format files with parse errors.trivia_classifier.rs— Single forward pass over the CST that tags each comment and blank line as leading, trailing, or before-EOF. Keeps the printer free of lookahead.ast/— Strong AST layer withFromCSTtrait. Converts the lossless Rowan CST into typed structs that are easy for the printer to consume, similar tosynfor rust.printer.rs— Output engine withPrintable/PrintMultiLinetraits andShape-based width tracking. Uses the "try single-line, fallback to multi-line" strategy throughout.Key formatting behaviors:
a.b.c().d) flatten and break at field access pointsParser fixes (prerequisite changes):
>>token splitting when expecting>(e.g. forMap<string, Map<string, int>>)Test infrastructure (crates/baml_tests/):
format(format(x)) == format(x)for all valid filesSummary by CodeRabbit
New Features
Refactors
Tests
Bug Fixes