Skip to content

Commit fafe84b

Browse files
epagetraviscross
authored andcommitted
docs(ref): Specify frontmatter
Edited-by: TC Fixed conflicts due to #2199. No other changes.
1 parent 91f1444 commit fafe84b

4 files changed

Lines changed: 67 additions & 2 deletions

File tree

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Lexical structure](lexical-structure.md)
88
- [Input format](input-format.md)
99
- [Shebang](shebang.md)
10+
- [Frontmatter](frontmatter.md)
1011
- [Keywords](keywords.md)
1112
- [Identifiers](identifiers.md)
1213
- [Comments](comments.md)

src/frontmatter.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 -> `-`{3..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] | CR | LF)
18+
19+
INVALID_FRONTMATTER_LINE_CONTINUE -> CR | LF
20+
21+
HORIZONTAL_WHITESPACE ->
22+
U+0009 // horizontal tab, `'\t'`
23+
| U+0020 // space, `' '`
24+
```
25+
26+
[^matched-fence]: The closing fence must have the same number of `-` as the opening fence
27+
[^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`
28+
29+
r[frontmatter.intro]
30+
Frontmatter is an optional section for content intended for external tools without requiring these tools to have full knowledge of the Rust grammar.
31+
32+
```rust
33+
#!/usr/bin/env cargo
34+
---
35+
[dependencies]
36+
fastrand = "2"
37+
---
38+
39+
fn main() {
40+
let num = fastrand::i32(..);
41+
println!("{num}");
42+
}
43+
```
44+
45+
r[frontmatter.document]
46+
Frontmatter may only be preceded by a [shebang] and [whitespace].
47+
48+
r[frontmatter.fence]
49+
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 hyphens (`-`), from 3 to 255. A fence may be followed by horizontal whitespace.
50+
51+
r[frontmatter.infostring]
52+
Following the opening fence may be an infostring for identifying the intention of the contained content. An infostring may be followed by horizontal whitespace.
53+
54+
r[frontmatter.body]
55+
The body of the frontmatter may contain any content except for a line starting with as many or more hyphens (`-`) than in the fences or carriage returns.
56+
57+
[shebang]: input-format.md#shebang-removal
58+
[whitespace]: whitespace.md

src/input-format.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ r[input.shebang]
4444
r[input.shebang.removal]
4545
If a [shebang] is present, it is removed from the input sequence (and is therefore ignored).
4646

47+
r[input.frontmatter]
48+
## Frontmatter removal
49+
50+
After some [whitespace], [frontmatter] may next appear in the input.
51+
4752
r[input.tokenization]
4853
## Tokenization
4954

@@ -54,11 +59,12 @@ The resulting sequence of characters is then converted into tokens as described
5459
>
5560
> - Byte order mark removal.
5661
> - CRLF normalization.
57-
> - Shebang removal when invoked in an item context (as opposed to expression or statement contexts).
62+
> - Shebang and frontmatter removal when invoked in an item context (as opposed to expression or statement contexts).
5863
>
5964
> The [`include_str!`] and [`include_bytes!`] macros do not apply these transformations.
6065
6166
[BYTE ORDER MARK]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
6267
[Crates and source files]: crates-and-source-files.md
68+
[frontmatter]: frontmatter.md
6369
[shebang]: shebang.md
6470
[whitespace]: whitespace.md

src/items/modules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ r[items.mod.attributes]
123123
## Attributes on modules
124124

125125
r[items.mod.attributes.intro]
126-
Modules, like all items, accept outer attributes. They also accept inner attributes: either after `{` for a module with a body, or at the beginning of the source file, after the optional BOM and shebang.
126+
Modules, like all items, accept outer attributes. They also accept inner attributes: either after `{` for a module with a body, or at the beginning of the source file, after the optional BOM, shebang, and frontmatter.
127127

128128
r[items.mod.attributes.supported]
129129
The built-in attributes that have meaning on a module are [`cfg`], [`deprecated`], [`doc`], [the lint check attributes], [`path`], and [`no_implicit_prelude`]. Modules also accept macro attributes.

0 commit comments

Comments
 (0)