Skip to content

Commit 74d9a44

Browse files
authored
Merge pull request rust-lang#87 from brauliobz/grammar_crates
Added crates grammar
2 parents 6d29d00 + 1b25010 commit 74d9a44

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/crates-and-source-files.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Crates and source files
22

3+
> **<sup>Syntax</sup>**
4+
> _Crate_ :
5+
> &nbsp;&nbsp; UTF8BOM<sup>?</sup>
6+
> &nbsp;&nbsp; SHEBANG<sup>?</sup>
7+
> &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>
8+
> &nbsp;&nbsp; [_Item_]<sup>\*</sup>
9+
10+
> **<sup>Lexer</sup>**
11+
> UTF8BOM : `\uFEFF`
12+
> SHEBANG : `#!` ~[`[` `\n`] ~`\n`<sup>*</sup>
13+
314
Although Rust, like any other language, can be implemented by an interpreter as
415
well as a compiler, the only existing implementation is a compiler,
516
and the language has
@@ -59,6 +70,24 @@ A crate that contains a `main` function can be compiled to an executable. If a
5970
`main` function is present, its return type must be `()`
6071
("[unit]") and it must take no arguments.
6172

73+
The optional [_UTF8 byte order mark_] (UTF8BOM production) indicates that the
74+
file is encoded in UTF8. It can only occur at the beginning of the file and
75+
is ignored by the compiler.
76+
77+
A source file can have a [_shebang_] (SHEBANG production), which indicates
78+
to the operating system what program to use to execute this file. It serves
79+
essentially to treat the source file as an executable script. The shebang
80+
can only occur at the beginning of the file (but after the optional
81+
_UTF8BOM_). It is ignored by the compiler. For example:
82+
83+
```text,ignore
84+
#!/usr/bin/env rustx
85+
86+
fn main() {
87+
println!("Hello!");
88+
}
89+
```
90+
6291
[^phase-distinction]: This distinction would also exist in an interpreter.
6392
Static checks like syntactic analysis, type checking, and lints should
6493
happen before the program is executed regardless of when it is executed.
@@ -70,4 +99,8 @@ A crate that contains a `main` function can be compiled to an executable. If a
7099
[module]: items.html#modules
71100
[module path]: paths.html
72101
[attributes]: items-and-attributes.html
73-
[unit]: types.html#tuple-types
102+
[unit]: types.html#tuple-types
103+
[_InnerAttribute_]: attributes.html
104+
[_Item_]: items.html
105+
[_shebang_]: https://en.wikipedia.org/wiki/Shebang_(Unix)
106+
[_utf8 byte order mark_]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8

0 commit comments

Comments
 (0)