Skip to content

Commit 236bd42

Browse files
committed
feat(parse): Strip frontmatter
1 parent 471846b commit 236bd42

18 files changed

+52
-80
lines changed

Diff for: compiler/rustc_parse/src/lexer/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ pub(crate) fn lex_token_trees<'psess, 'src>(
5555
start_pos = start_pos + BytePos::from_usize(shebang_len);
5656
}
5757

58+
// Skip frontmatter, if present.
59+
if let Some(frontmatter_len) = rustc_lexer::strip_frontmatter(src) {
60+
src = &src[frontmatter_len..];
61+
start_pos = start_pos + BytePos::from_usize(frontmatter_len);
62+
}
63+
5864
let cursor = Cursor::new(src);
5965
let mut lexer = Lexer {
6066
psess,

Diff for: src/doc/unstable-book/src/language-features/frontmatter.md

+30
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,33 @@ The tracking issue for this feature is: [#136889]
88

99
The `frontmatter` feature adds support for a specialized and simplified attribute syntax
1010
intended for external tools to consume.
11+
12+
For example, when used with Cargo:
13+
```rust
14+
#!/usr/bin/env -S cargo -Zscript
15+
16+
---
17+
[dependencies]
18+
clap = "4"
19+
---
20+
21+
use clap::Parser;
22+
23+
#[derive(Parser)]
24+
struct Cli {
25+
}
26+
27+
fn main () {
28+
Cli::parse();
29+
}
30+
```
31+
32+
A frontmatter may come after a shebang and must come before any other syntax except whitespace.
33+
The open delimiter is three or more dashes (`-`) at the start of a new line.
34+
The open delimiter may be followed by whitespace and / or an identifier to mark the interpretation of the frontmatter within an external tool.
35+
It is then concluded at a newline.
36+
The close delimiter is a series of dashes that matches the open delimiter, at the start of a line.
37+
The close delimiter may be followed by whitespace.
38+
Any other trailing content, including more dashes than the open delimiter, is an error.
39+
It is then concluded at a newline.
40+
All content between the open and close delimiter lines is ignored.

Diff for: tests/ui/feature-gates/feature-gate-frontmatter.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
2-
//~^ ERROR expected item, found `-`
32
---
43

4+
//@ check-pass
5+
56
pub fn main() {
67
}

Diff for: tests/ui/feature-gates/feature-gate-frontmatter.stderr

-10
This file was deleted.

Diff for: tests/ui/frontmatter/frontmatter-escaped.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
-----
2-
//~^ ERROR expected item, found `-`
32

43
---
54
---
65

76
-----
87

8+
//@ check-pass
9+
910
#![feature(frontmatter)]
1011

1112
fn main() {

Diff for: tests/ui/frontmatter/frontmatter-escaped.stderr

-10
This file was deleted.

Diff for: tests/ui/frontmatter/frontmatter-ignored-space.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

22

33
---
4-
//~^ ERROR expected item, found `-`
54

65
---
76

7+
//@ check-pass
88
#![feature(frontmatter)]
99
// ignore-tidy-end-whitespace
1010
// ignore-tidy-leading-newlines

Diff for: tests/ui/frontmatter/frontmatter-ignored-space.stderr

-10
This file was deleted.

Diff for: tests/ui/frontmatter/frontmatter-invalid-multiple.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---cargo
2-
//~^ ERROR expected item, found `-`
32
---
43

54
---buck
5+
//~^ ERROR expected item, found `-`
66
---
77

88
#![feature(frontmatter)]

Diff for: tests/ui/frontmatter/frontmatter-invalid-multiple.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: expected item, found `-`
2-
--> $DIR/frontmatter-invalid-multiple.rs:1:1
2+
--> $DIR/frontmatter-invalid-multiple.rs:4:1
33
|
4-
LL | ---cargo
4+
LL | ---buck
55
| ^ expected item
66
|
77
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>

Diff for: tests/ui/frontmatter/frontmatter-with-body.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
---
2-
//~^ ERROR expected item, found `-`
32
[dependencies]
43
---
54

5+
//@ check-pass
6+
67
#![feature(frontmatter)]
78

89
fn main() {

Diff for: tests/ui/frontmatter/frontmatter-with-body.stderr

-10
This file was deleted.

Diff for: tests/ui/frontmatter/frontmatter-with-infostring-spaces.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
--- cargo
2-
//~^ ERROR expected item, found `-`
32
---
43

4+
//@ check-pass
5+
56
#![feature(frontmatter)]
67

78
fn main() {

Diff for: tests/ui/frontmatter/frontmatter-with-infostring-spaces.stderr

-10
This file was deleted.

Diff for: tests/ui/frontmatter/frontmatter-with-infostring.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---cargo
2-
//~^ ERROR expected item, found `-`
32
---
43

4+
//@ check-pass
5+
56
#![feature(frontmatter)]
67

78
fn main() {

Diff for: tests/ui/frontmatter/frontmatter-with-infostring.stderr

-10
This file was deleted.

Diff for: tests/ui/frontmatter/frontmatter.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
2-
//~^ ERROR expected item, found `-`
32
---
43

4+
//@ check-pass
5+
56
#![feature(frontmatter)]
67

78
fn main() {

Diff for: tests/ui/frontmatter/frontmatter.stderr

-10
This file was deleted.

0 commit comments

Comments
 (0)