Skip to content

Commit b849612

Browse files
committed
docs(ref): Specify frontmatter
1 parent c564c4c commit b849612

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- [Lexical structure](lexical-structure.md)
88
- [Input format](input-format.md)
9+
- [Frontmatter](frontmatter.md)
910
- [Keywords](keywords.md)
1011
- [Identifiers](identifiers.md)
1112
- [Comments](comments.md)

src/frontmatter.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
r[frontmatter]
2+
# Frontmatter
3+
4+
r[frontmatter.syntax]
5+
```grammar,lexer
6+
@root FRONTMATTER ->
7+
FRONTMATTER_FENCE HORIZONTAL_WHITESPACE* INFOSTRING? HORIZONTAL_WHITESPACE* LF
8+
(FRONTMATTER_LINE LF )*
9+
FRONTMATTER_FENCE[^matched-fence] HORIZONTAL_WHITESPACE* LF
10+
11+
FRONTMATTER_FENCE -> `---` `-`{..=255}
12+
13+
INFOSTRING -> (XID_Start | `_`) ( XID_Continue | `-` | `.` )*
14+
15+
FRONTMATTER_LINE -> (~INVALID_FRONTMATTER_LINE_START (~INVALID_FRONTMATTER_LINE_CONTINUE)*)?
16+
17+
INVALID_FRONTMATTER_LINE_START -> (FRONTMATTER_FENCE[^escaped-fence] | LF)
18+
19+
INVALID_FRONTMATTER_LINE_CONTINUE -> LF
20+
```
21+
22+
[^matched-fence]: The closing fence must have the same number of `-` as the opening fence
23+
[^escaped-fence]: A `FRONTMATTER_FENCE` at the beginning of a `FRONTMATTER_LINE` is only invalid if it has the same or more `-` as the `FRONTMATTER_FENCE`
24+
25+
r[frontmatter.intro]
26+
Frontmatter is an optional section for content intended for external tools without requiring these tools to have full knowledge of the Rust grammar.
27+
28+
```rust
29+
#!/usr/bin/env cargo
30+
---
31+
[dependencies]
32+
fastrand = "2"
33+
---
34+
35+
fn main() {
36+
let num = fastrand::i32(..);
37+
println!("{num}");
38+
}
39+
```
40+
41+
r[frontmatter.document]
42+
Frontmatter may only be preceded by a [shebang] and whitespace.
43+
44+
r[frontmatter.fence]
45+
The delimiters are referred to as a *fence*. The opening and closing fences must be at the start of a line. They must be a matching pair of three or more hyphens (`-`). A fence may be followed by horizontal whitespace.
46+
47+
r[frontmatter.infostring]
48+
Following the opening fence may be an infostring for identifying the intention of the contained content. An infostring may be followed by horizontal whitespace.
49+
50+
r[frontmatter.body]
51+
The body of the frontmatter may contain any content except for a line starting with as many or more hyphens (`-`) than in the fences.
52+
53+
[shebang]: input-format.md#shebang-removal

src/input-format.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ This prevents an [inner attribute] at the start of a source file being removed.
5959
> [!NOTE]
6060
> The standard library [`include!`] macro applies byte order mark removal, CRLF normalization, and shebang removal to the file it reads. The [`include_str!`] and [`include_bytes!`] macros do not.
6161
62+
r[input.frontmatter]
63+
## Frontmatter removal
64+
65+
After some whitespace, [frontmatter] may next appear in the input.
66+
6267
r[input.tokenization]
6368
## Tokenization
6469

@@ -69,4 +74,5 @@ The resulting sequence of characters is then converted into tokens as described
6974
[comments]: comments.md
7075
[Crates and source files]: crates-and-source-files.md
7176
[_shebang_]: https://en.wikipedia.org/wiki/Shebang_(Unix)
77+
[frontmatter]: frontmatter.md
7278
[whitespace]: whitespace.md

src/items/modules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ r[items.mod.attributes]
152152
r[items.mod.attributes.intro]
153153
Modules, like all items, accept outer attributes. They also accept inner
154154
attributes: either after `{` for a module with a body, or at the beginning of the
155-
source file, after the optional BOM and shebang.
155+
source file, after the optional BOM, shebang, and frontmatter.
156156

157157
r[items.mod.attributes.supported]
158158
The built-in attributes that have meaning on a module are [`cfg`],

0 commit comments

Comments
 (0)