Skip to content

Commit

Permalink
feat(parse): Strip frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 17, 2025
1 parent 6d178d5 commit 060c012
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 80 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ pub(crate) fn lex_token_trees<'psess, 'src>(
start_pos = start_pos + BytePos::from_usize(shebang_len);
}

// Skip frontmatter, if present.
if let Some(frontmatter_len) = rustc_lexer::strip_frontmatter(src) {
src = &src[frontmatter_len..];
start_pos = start_pos + BytePos::from_usize(frontmatter_len);
}

let cursor = Cursor::new(src);
let mut lexer = Lexer {
psess,
Expand Down
30 changes: 30 additions & 0 deletions src/doc/unstable-book/src/language-features/frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,33 @@ The tracking issue for this feature is: [#136889]

The `frontmatter` feature adds support for a specialized and simplified attribute syntax
intended for external tools to consume.

For example, when used with Cargo:
```rust
#!/usr/bin/env -S cargo -Zscript

---
[dependencies]
clap = "4"
---

use clap::Parser;

#[derive(Parser)]
struct Cli {
}

fn main () {
Cli::parse();
}
```

A frontmatter may come after a shebang and must come before any other syntax except whitespace.
The open delimiter is three or more dashes (`-`) at the start of a new line.
The open delimiter may be followed by whitespace and / or an identifier to mark the interpretation of the frontmatter within an external tool.
It is then concluded at a newline.
The close delimiter is a series of dashes that matches the open delimiter, at the start of a line.
The close delimiter may be followed by whitespace.
Any other trailing content, including more dashes than the open delimiter, is an error.
It is then concluded at a newline.
All content between the open and close delimiter lines is ignored.
3 changes: 2 additions & 1 deletion tests/ui/feature-gates/feature-gate-frontmatter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
//~^ ERROR expected item, found `-`
---

//@ check-pass

pub fn main() {
}
10 changes: 0 additions & 10 deletions tests/ui/feature-gates/feature-gate-frontmatter.stderr

This file was deleted.

3 changes: 2 additions & 1 deletion tests/ui/frontmatter/frontmatter-escaped.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
-----
//~^ ERROR expected item, found `-`

---
---

-----

//@ check-pass

#![feature(frontmatter)]

fn main() {
Expand Down
10 changes: 0 additions & 10 deletions tests/ui/frontmatter/frontmatter-escaped.stderr

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ui/frontmatter/frontmatter-ignored-space.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@


---
//~^ ERROR expected item, found `-`

---

//@ check-pass
#![feature(frontmatter)]
// ignore-tidy-end-whitespace
// ignore-tidy-leading-newlines
Expand Down
10 changes: 0 additions & 10 deletions tests/ui/frontmatter/frontmatter-ignored-space.stderr

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ui/frontmatter/frontmatter-invalid-multiple.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---cargo
//~^ ERROR expected item, found `-`
---

---buck
//~^ ERROR expected item, found `-`
---

#![feature(frontmatter)]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/frontmatter/frontmatter-invalid-multiple.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: expected item, found `-`
--> $DIR/frontmatter-invalid-multiple.rs:1:1
--> $DIR/frontmatter-invalid-multiple.rs:4:1
|
LL | ---cargo
LL | ---buck
| ^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/frontmatter/frontmatter-with-body.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
//~^ ERROR expected item, found `-`
[dependencies]
---

//@ check-pass

#![feature(frontmatter)]

fn main() {
Expand Down
10 changes: 0 additions & 10 deletions tests/ui/frontmatter/frontmatter-with-body.stderr

This file was deleted.

3 changes: 2 additions & 1 deletion tests/ui/frontmatter/frontmatter-with-infostring-spaces.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
--- cargo
//~^ ERROR expected item, found `-`
---

//@ check-pass

#![feature(frontmatter)]

fn main() {
Expand Down
10 changes: 0 additions & 10 deletions tests/ui/frontmatter/frontmatter-with-infostring-spaces.stderr

This file was deleted.

3 changes: 2 additions & 1 deletion tests/ui/frontmatter/frontmatter-with-infostring.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---cargo
//~^ ERROR expected item, found `-`
---

//@ check-pass

#![feature(frontmatter)]

fn main() {
Expand Down
10 changes: 0 additions & 10 deletions tests/ui/frontmatter/frontmatter-with-infostring.stderr

This file was deleted.

3 changes: 2 additions & 1 deletion tests/ui/frontmatter/frontmatter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
//~^ ERROR expected item, found `-`
---

//@ check-pass

#![feature(frontmatter)]

fn main() {
Expand Down
10 changes: 0 additions & 10 deletions tests/ui/frontmatter/frontmatter.stderr

This file was deleted.

0 comments on commit 060c012

Please sign in to comment.