Add Decodal formatter
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
# Formatting
|
||||
|
||||
Decodal includes an initial source formatter for parsed Decodal code.
|
||||
The formatter is available from the Rust crate, the CLI, the WebAssembly package, and the playground.
|
||||
|
||||
## CLI
|
||||
|
||||
Print formatted source to stdout:
|
||||
|
||||
```sh
|
||||
decodal fmt config.dcdl
|
||||
```
|
||||
|
||||
Format in place:
|
||||
|
||||
```sh
|
||||
decodal fmt --write config.dcdl
|
||||
```
|
||||
|
||||
Read from stdin:
|
||||
|
||||
```sh
|
||||
cat config.dcdl | decodal fmt -
|
||||
```
|
||||
|
||||
## Rust
|
||||
|
||||
```rust
|
||||
let formatted = decodal::format_source("Server={port=Int default 8080;};")?;
|
||||
```
|
||||
|
||||
## WebAssembly
|
||||
|
||||
```js
|
||||
import init, { formatSource } from 'decodal-wasm';
|
||||
|
||||
await init();
|
||||
const result = JSON.parse(formatSource('Server={port=Int default 8080;};'));
|
||||
|
||||
if (result.ok) {
|
||||
console.log(result.source);
|
||||
} else {
|
||||
console.error(result.error);
|
||||
}
|
||||
```
|
||||
|
||||
## Current limitation
|
||||
|
||||
The formatter currently rejects source containing line comments rather than silently dropping them.
|
||||
Comment-preserving formatting requires attaching comments to syntax nodes or formatting from a concrete syntax tree, and should be implemented before comment-heavy source is formatted automatically.
|
||||
Reference in New Issue
Block a user