Skip to content

Commit e313692

Browse files
committed
docs
1 parent 4b35749 commit e313692

12 files changed

Lines changed: 403 additions & 1 deletion

.gitignore

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
1-
target/
1+
# Build artifacts
2+
/target/
3+
4+
# Coverage and profiling
5+
/coverage/
6+
*.profraw
7+
*.profdata
8+
9+
# Logs and temp files
10+
*.log
11+
*.tmp
12+
*.temp
13+
14+
# Editor and OS files
15+
/.idea/
16+
/.vscode/
17+
*.swp
18+
*.swo
19+
.DS_Store
20+
Thumbs.db

docs/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# dustfmt Documentation
2+
3+
This directory contains complete Markdown documentation for `dustfmt`.
4+
5+
## Documentation Index
6+
7+
- `getting_started.md`: build, install, and first formatting runs.
8+
- `cli_reference.md`: command-line contract and exit behavior.
9+
- `architecture.md`: formatter pipeline and module boundaries.
10+
- `formatting_rules.md`: canonical formatting behavior implemented today.
11+
- `frontend_validation.md`: parser/lexer validation path used before formatting.
12+
- `formatter_lexer.md`: comment-preserving formatter lexer behavior.
13+
- `library_api.md`: public Rust API (`format_source`).
14+
- `testing.md`: current test coverage and test caveats.
15+
- `known_limitations.md`: implementation limits and behavior mismatches to track.
16+
- `developer_guide.md`: local workflow for contributors.
17+
18+
## Scope
19+
20+
`dustfmt` is the Rust formatter for Dust source files. It validates input through an embedded frontend parser, then re-lexes source with a formatter-specific lexer to preserve comments while normalizing layout.
21+
22+
This documentation describes current behavior from source in `crates/dustfmt/src`.

docs/architecture.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Architecture
2+
3+
## Crate Layout
4+
5+
- `src/main.rs`: CLI and file/stdin orchestration.
6+
- `src/lib.rs`: public formatting API (`format_source`).
7+
- `src/format.rs`: token-to-text canonical formatter.
8+
- `src/lexer.rs`: formatter lexer that preserves comments.
9+
- `src/frontend/*`: embedded frontend lexer/parser/AST used for validation.
10+
11+
## Pipeline
12+
13+
`format_source(src)` executes two phases:
14+
15+
1. **Validation phase**
16+
- run `frontend::lexer::Lexer::lex_all`
17+
- run `frontend::parser::Parser::parse_file`
18+
- fail early on lex/parse errors
19+
20+
2. **Formatting phase**
21+
- run formatter lexer (`src/lexer.rs`) to retain comments
22+
- run `format::format_tokens`
23+
- emit canonical formatted text with trailing newline
24+
25+
## Design Intent
26+
27+
- avoid semantic changes by requiring syntactically valid input
28+
- preserve comments while normalizing whitespace/layout
29+
- deterministic, idempotent formatting output
30+
31+
## Implementation Note
32+
33+
The embedded frontend module is a copied/adjusted parser stack inside this crate rather than an external dependency on the full `dust` compiler crate.

docs/cli_reference.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# CLI Reference
2+
3+
Source: `crates/dustfmt/src/main.rs`
4+
5+
## Invocation
6+
7+
```text
8+
dustfmt [--check|-c] [files...]
9+
```
10+
11+
## Flags
12+
13+
- `--check`: check-only mode
14+
- `-c`: short alias for check-only mode
15+
16+
No other special flags are handled in current implementation.
17+
18+
## Modes
19+
20+
## File Mode
21+
22+
When one or more files are provided:
23+
24+
- normal mode: each file is formatted and rewritten in place
25+
- check mode: formatting differences produce an error and exit code `1`
26+
27+
## STDIN Mode
28+
29+
When no files are provided:
30+
31+
- reads entire stdin
32+
- normal mode: writes formatted text to stdout
33+
- check mode: emits message and exits `1` when formatted output differs
34+
35+
## Exit Behavior
36+
37+
- `0`: successful formatting/check with no differences (in check mode)
38+
- `1`: check failed, parse/lex/format error, file read/write error, or explicit process error path
39+
40+
## Diagnostics
41+
42+
Representative stderr output patterns:
43+
44+
- `format error: <details>`
45+
- `<file>: format error: <details>`
46+
- `check failed: input differs from formatted output`
47+
- `check failed: <file> differs from formatted output`

docs/developer_guide.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Developer Guide
2+
3+
## Build
4+
5+
```bash
6+
cargo build --workspace --verbose
7+
```
8+
9+
## Test
10+
11+
```bash
12+
cargo test --workspace --verbose
13+
```
14+
15+
## Quick Manual Checks
16+
17+
```bash
18+
cargo run -p dustfmt -- --check crates/dustfmt/tests/fixtures/sample.ds
19+
cargo run -p dustfmt -- crates/dustfmt/tests/fixtures/sample.ds
20+
```
21+
22+
(Adjust file paths to local fixtures that exist.)
23+
24+
## Suggested Workflow for Formatter Changes
25+
26+
1. update `src/lexer.rs` or `src/format.rs`
27+
2. run tests
28+
3. add/update tests in `crates/dustfmt/tests/format.rs`
29+
4. update docs in `dustfmt/docs`
30+
31+
## Priority Fix Areas
32+
33+
- align line-comment behavior with intended preservation (`//` fidelity)
34+
- expand parser coverage to match formatter keyword surface
35+
- improve CLI diagnostics for file-system failures

docs/formatter_lexer.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Formatter Lexer
2+
3+
Source: `crates/dustfmt/src/lexer.rs`
4+
5+
## Token Model
6+
7+
Formatter lexer tokens include:
8+
9+
- structural tokens: delimiters/operators/newlines
10+
- lexical atoms: identifiers, keywords, regimes, numbers, bools, strings
11+
- comments: line and block comments preserved as token payload
12+
- fallback tokens: unknown characters
13+
14+
## Comment Handling
15+
16+
- line comments and block comments are tokenized for preservation
17+
- unlike the validation lexer, comments are not discarded
18+
19+
## Keyword Coverage
20+
21+
Keyword matching includes a broad set (core DPL plus several v0.2-oriented terms), independent of whether parser/formatter fully supports all constructs semantically.
22+
23+
## String and Number Reading
24+
25+
- strings are read until closing `"` without escape processing parity with compiler lexer
26+
- number scanning allows digits and `.`
27+
28+
## Operational Role
29+
30+
The formatter lexer is a layout-oriented tokenizer. It is not authoritative for syntax validity; syntax validity is delegated to the embedded frontend validation pass.

docs/formatting_rules.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Formatting Rules (Current Implementation)
2+
3+
Source: `crates/dustfmt/src/format.rs`
4+
5+
## Indentation
6+
7+
- indentation level is tracked by `{` and `}` tokens
8+
- each level uses 4 spaces
9+
10+
## Braces
11+
12+
- `{` is emitted on current line (with a preceding space if needed)
13+
- `}` is emitted on its own line at decremented indentation
14+
15+
## Statement Breaks
16+
17+
- `;` forces line flush
18+
- comments force current line flush before comment emission
19+
20+
## Commas and Colons
21+
22+
- `,` and `:` are followed by one space
23+
24+
## Line Normalization
25+
26+
- trailing whitespace is trimmed per emitted line
27+
- final newline is always ensured (`result` ends with an empty final line)
28+
29+
## Newline Tokens
30+
31+
- lexer newline tokens are ignored by formatter logic; output newlines are structural
32+
33+
## Unknown Tokens
34+
35+
- unknown characters are emitted as standalone indented lines
36+
37+
## Spacing Heuristic
38+
39+
Spacing between tokens uses a local previous-token heuristic (for example around operators, delimiters, and scope operators). This is rule-based and not AST-aware.

docs/frontend_validation.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Frontend Validation
2+
3+
Sources:
4+
5+
- `crates/dustfmt/src/frontend/lexer.rs`
6+
- `crates/dustfmt/src/frontend/parser.rs`
7+
- `crates/dustfmt/src/frontend/ast.rs`
8+
9+
## Purpose
10+
11+
Before formatting, `dustfmt` validates that source can be lexed and parsed by the embedded frontend parser.
12+
13+
## Validation Surface
14+
15+
`format_source` rejects input when:
16+
17+
- frontend lexing fails (`LexError`)
18+
- frontend parsing fails (`ParseError`)
19+
20+
Error strings are normalized as:
21+
22+
- `lex error: <kind> at <start>..<end>`
23+
- `parse error: <message> at <start>..<end>`
24+
25+
## Parser Coverage in This Crate
26+
27+
The embedded parser is intentionally limited in current state:
28+
29+
- supports forge parsing and top-level shorthand procs
30+
- supports `proc` declarations and shorthand `K/Q/F name { ... }`
31+
- statement support is narrow (`emit` statement path in parser)
32+
- many keywords/features return explicit unsupported errors
33+
34+
Because validation runs before formatting, unsupported-but-lexable syntax can still fail format with parser errors.

docs/getting_started.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Getting Started
2+
3+
## Prerequisites
4+
5+
- Rust toolchain with `cargo`.
6+
- Dust source files (`.ds`) to format.
7+
8+
## Build
9+
10+
From `dustfmt/`:
11+
12+
```bash
13+
cargo build --release
14+
```
15+
16+
Binary path (Windows):
17+
18+
```text
19+
target\release\dustfmt.exe
20+
```
21+
22+
## Format Files In Place
23+
24+
```bash
25+
cargo run -p dustfmt -- file1.ds file2.ds
26+
```
27+
28+
Behavior:
29+
30+
- each listed file is read
31+
- formatted output overwrites the same file
32+
- stops on first formatting/parse/lex error
33+
34+
## Check Mode
35+
36+
```bash
37+
cargo run -p dustfmt -- --check file.ds
38+
```
39+
40+
or:
41+
42+
```bash
43+
cargo run -p dustfmt -- -c file.ds
44+
```
45+
46+
Behavior:
47+
48+
- no writes
49+
- exits `1` if any file differs from canonical formatted output
50+
51+
## STDIN Mode
52+
53+
If no files are supplied, input is read from stdin.
54+
55+
```bash
56+
cat file.ds | cargo run -p dustfmt --
57+
```
58+
59+
- normal mode: prints formatted source to stdout
60+
- check mode: compares stdin text to formatted result and exits `1` on mismatch

docs/known_limitations.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Known Limitations
2+
3+
## Parser Coverage vs Keyword Surface
4+
5+
`src/lexer.rs` recognizes many keywords, but embedded parser coverage is narrower. Formatting can fail on valid-looking inputs when parser support is incomplete.
6+
7+
## Comment Tokenization Quirk
8+
9+
Current formatter lexer comment path consumes one `/` before line-comment capture, and existing tests encode this behavior. This can produce non-ideal comment rendering.
10+
11+
## Formatting Is Token-Heuristic, Not Full AST Pretty-Print
12+
13+
`format_tokens` applies local token spacing/newline rules; it does not perform deep AST-aware layout decisions.
14+
15+
## Limited CLI Feature Set
16+
17+
Only `--check`/`-c` is implemented. There are no configuration files, width options, or selective rule toggles in current code.
18+
19+
## Error Handling Style
20+
21+
Several file I/O paths use `expect(...)`, resulting in immediate process termination on file read/write failures rather than structured recoverable diagnostics.

0 commit comments

Comments
 (0)