From ca05cced489d2aedf5e698e3c61d08810321fb92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Br=C3=A1ulio=20Bezerra?= Date: Sat, 22 Jul 2017 09:29:37 -0300 Subject: [PATCH 1/2] Added crates grammar --- src/crates-and-source-files.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/crates-and-source-files.md b/src/crates-and-source-files.md index d41a8dc96..71a43cef0 100644 --- a/src/crates-and-source-files.md +++ b/src/crates-and-source-files.md @@ -18,6 +18,17 @@ processes a single crate in source form, and if successful, produces a single crate in binary form: either an executable or some sort of library.[^cratesourcefile] +> **Syntax** +> _Crate_ : +>    UTF8BOM? +>    SHEBANG? +>    [_InnerAttribute_]\* +>    [_Item_]\* + +> **Lexer** +> UTF8BOM : `\xFE\xFF` +> SHEBANG : `#!` ~[`[` `\n`] ~`\n`* + A _crate_ is a unit of compilation and linking, as well as versioning, distribution and runtime loading. A crate contains a _tree_ of nested [module] scopes. The top level of this tree is a module that is @@ -70,4 +81,6 @@ A crate that contains a `main` function can be compiled to an executable. If a [module]: items.html#modules [module path]: paths.html [attributes]: items-and-attributes.html -[unit]: types.html#tuple-types \ No newline at end of file +[unit]: types.html#tuple-types +[_InnerAttribute_]: attributes.html +[_Item_]: items.html From 1b2501069e373cbaaecb56de5e25e67586f8a5b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Br=C3=A1ulio=20Bezerra?= Date: Sat, 22 Jul 2017 10:28:58 -0300 Subject: [PATCH 2/2] Crate grammar: moved the syntax up and explained UTF8BOM and SHEBANG --- src/crates-and-source-files.md | 42 +++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/src/crates-and-source-files.md b/src/crates-and-source-files.md index 71a43cef0..62f33343b 100644 --- a/src/crates-and-source-files.md +++ b/src/crates-and-source-files.md @@ -1,5 +1,16 @@ # Crates and source files +> **Syntax** +> _Crate_ : +>    UTF8BOM? +>    SHEBANG? +>    [_InnerAttribute_]\* +>    [_Item_]\* + +> **Lexer** +> UTF8BOM : `\uFEFF` +> SHEBANG : `#!` ~[`[` `\n`] ~`\n`* + Although Rust, like any other language, can be implemented by an interpreter as well as a compiler, the only existing implementation is a compiler, and the language has @@ -18,17 +29,6 @@ processes a single crate in source form, and if successful, produces a single crate in binary form: either an executable or some sort of library.[^cratesourcefile] -> **Syntax** -> _Crate_ : ->    UTF8BOM? ->    SHEBANG? ->    [_InnerAttribute_]\* ->    [_Item_]\* - -> **Lexer** -> UTF8BOM : `\xFE\xFF` -> SHEBANG : `#!` ~[`[` `\n`] ~`\n`* - A _crate_ is a unit of compilation and linking, as well as versioning, distribution and runtime loading. A crate contains a _tree_ of nested [module] scopes. The top level of this tree is a module that is @@ -70,6 +70,24 @@ A crate that contains a `main` function can be compiled to an executable. If a `main` function is present, its return type must be `()` ("[unit]") and it must take no arguments. +The optional [_UTF8 byte order mark_] (UTF8BOM production) indicates that the +file is encoded in UTF8. It can only occur at the beginning of the file and +is ignored by the compiler. + +A source file can have a [_shebang_] (SHEBANG production), which indicates +to the operating system what program to use to execute this file. It serves +essentially to treat the source file as an executable script. The shebang +can only occur at the beginning of the file (but after the optional +_UTF8BOM_). It is ignored by the compiler. For example: + +```text,ignore +#!/usr/bin/env rustx + +fn main() { + println!("Hello!"); +} +``` + [^phase-distinction]: This distinction would also exist in an interpreter. Static checks like syntactic analysis, type checking, and lints should happen before the program is executed regardless of when it is executed. @@ -84,3 +102,5 @@ A crate that contains a `main` function can be compiled to an executable. If a [unit]: types.html#tuple-types [_InnerAttribute_]: attributes.html [_Item_]: items.html +[_shebang_]: https://en.wikipedia.org/wiki/Shebang_(Unix) +[_utf8 byte order mark_]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8