Skip to content

Commit 77f4eea

Browse files
ehusstraviscross
authored andcommitted
Update shebang
This updates the shebang description, primarily to add a grammar for it. It also reworks the rules so the introduction explains what this section is talking about, and placing specific behaviors in individual rules.
1 parent 0a1928e commit 77f4eea

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

src/input-format.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,33 @@ r[input.shebang]
4242
## Shebang removal
4343

4444
r[input.shebang.intro]
45-
If the remaining sequence begins with the characters `#!`, the characters up to and including the first `U+000A` (LF) are removed from the sequence.
45+
A *shebang* is an optional line that is typically used in Unix-like systems to specify an interpreter for executing the file.
4646

47-
For example, the first line of the following file would be ignored:
47+
> [!EXAMPLE]
48+
> <!-- ignore: tests don't like shebang -->
49+
> ```rust,ignore
50+
> #!/usr/bin/env rustx
51+
>
52+
> fn main() {
53+
> println!("Hello!");
54+
> }
55+
> ```
4856
49-
<!-- ignore: tests don't like shebang -->
50-
```rust,ignore
51-
#!/usr/bin/env rustx
57+
r[input.shebang.syntax]
5258
53-
fn main() {
54-
println!("Hello!");
55-
}
59+
```grammar,lexer
60+
@root SHEBANG ->
61+
`#!` !((WHITESPACE | LINE_COMMENT | BLOCK_COMMENT)* `[`)
62+
~LF* (LF | EOF)
5663
```
5764
58-
r[input.shebang.inner-attribute]
59-
As an exception, if the `#!` characters are followed (ignoring intervening [comments] or [whitespace]) by a `[` token, nothing is removed. This prevents an [inner attribute] at the start of a source file being removed.
65+
The shebang starts with the characters `#!`. However, if these characters are followed by `[` (ignoring any intervening [comments] or [whitespace]), the line is not considered a shebang to avoid ambiguity with an [inner attribute]. The shebang continues to and including the first `U+000A` (LF), or to EOF if there is no line ending.
66+
67+
r[input.shebang.position]
68+
The shebang may appear immediately at the start of the file or after the optional [byte order mark].
69+
70+
r[input.shebang.removal]
71+
The shebang is removed from the input sequence and is ignored.
6072

6173
r[input.tokenization]
6274
## Tokenization

src/whitespace.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ r[lex.whitespace]
33

44
r[whitespace.syntax]
55
```grammar,lexer
6-
@root WHITESPACE ->
6+
WHITESPACE ->
77
U+0009 // Horizontal tab, `'\t'`
88
| U+000A // Line feed, `'\n'`
99
| U+000B // Vertical tab

0 commit comments

Comments
 (0)