2015-02-14 20:50:08 -05:00
|
|
|
% Grammar
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
# Introduction
|
|
|
|
|
|
|
|
This document is the primary reference for the Rust programming language grammar. It
|
|
|
|
provides only one kind of material:
|
|
|
|
|
2015-04-17 19:22:19 -07:00
|
|
|
- Chapters that formally define the language grammar.
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
This document does not serve as an introduction to the language. Background
|
|
|
|
familiarity with the language is assumed. A separate [guide] is available to
|
2015-09-24 08:04:55 +02:00
|
|
|
help acquire such background.
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
This document also does not serve as a reference to the [standard] library
|
|
|
|
included in the language distribution. Those libraries are documented
|
|
|
|
separately by extracting documentation attributes from their source code. Many
|
|
|
|
of the features that one might expect to be language features are library
|
|
|
|
features in Rust, so what you're looking for may be there, not here.
|
|
|
|
|
|
|
|
[guide]: guide.html
|
|
|
|
[standard]: std/index.html
|
|
|
|
|
|
|
|
# Notation
|
|
|
|
|
|
|
|
Rust's grammar is defined over Unicode codepoints, each conventionally denoted
|
|
|
|
`U+XXXX`, for 4 or more hexadecimal digits `X`. _Most_ of Rust's grammar is
|
|
|
|
confined to the ASCII range of Unicode, and is described in this document by a
|
|
|
|
dialect of Extended Backus-Naur Form (EBNF), specifically a dialect of EBNF
|
|
|
|
supported by common automated LL(k) parsing tools such as `llgen`, rather than
|
|
|
|
the dialect given in ISO 14977. The dialect can be defined self-referentially
|
|
|
|
as follows:
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
grammar : rule + ;
|
|
|
|
rule : nonterminal ':' productionrule ';' ;
|
|
|
|
productionrule : production [ '|' production ] * ;
|
|
|
|
production : term * ;
|
|
|
|
term : element repeats ;
|
|
|
|
element : LITERAL | IDENTIFIER | '[' productionrule ']' ;
|
|
|
|
repeats : [ '*' | '+' ] NUMBER ? | NUMBER ? | '?' ;
|
|
|
|
```
|
|
|
|
|
|
|
|
Where:
|
|
|
|
|
|
|
|
- Whitespace in the grammar is ignored.
|
|
|
|
- Square brackets are used to group rules.
|
|
|
|
- `LITERAL` is a single printable ASCII character, or an escaped hexadecimal
|
|
|
|
ASCII code of the form `\xQQ`, in single quotes, denoting the corresponding
|
|
|
|
Unicode codepoint `U+00QQ`.
|
|
|
|
- `IDENTIFIER` is a nonempty string of ASCII letters and underscores.
|
|
|
|
- The `repeat` forms apply to the adjacent `element`, and are as follows:
|
|
|
|
- `?` means zero or one repetition
|
|
|
|
- `*` means zero or more repetitions
|
|
|
|
- `+` means one or more repetitions
|
|
|
|
- NUMBER trailing a repeat symbol gives a maximum repetition count
|
|
|
|
- NUMBER on its own gives an exact repetition count
|
|
|
|
|
|
|
|
This EBNF dialect should hopefully be familiar to many readers.
|
|
|
|
|
|
|
|
## Unicode productions
|
|
|
|
|
|
|
|
A few productions in Rust's grammar permit Unicode codepoints outside the ASCII
|
|
|
|
range. We define these productions in terms of character properties specified
|
|
|
|
in the Unicode standard, rather than in terms of ASCII-range codepoints. The
|
|
|
|
section [Special Unicode Productions](#special-unicode-productions) lists these
|
|
|
|
productions.
|
|
|
|
|
|
|
|
## String table productions
|
|
|
|
|
|
|
|
Some rules in the grammar — notably [unary
|
|
|
|
operators](#unary-operator-expressions), [binary
|
|
|
|
operators](#binary-operator-expressions), and [keywords](#keywords) — are
|
|
|
|
given in a simplified form: as a listing of a table of unquoted, printable
|
|
|
|
whitespace-separated strings. These cases form a subset of the rules regarding
|
|
|
|
the [token](#tokens) rule, and are assumed to be the result of a
|
|
|
|
lexical-analysis phase feeding the parser, driven by a DFA, operating over the
|
|
|
|
disjunction of all such string table entries.
|
|
|
|
|
|
|
|
When such a string enclosed in double-quotes (`"`) occurs inside the grammar,
|
|
|
|
it is an implicit reference to a single member of such a string table
|
|
|
|
production. See [tokens](#tokens) for more information.
|
|
|
|
|
|
|
|
# Lexical structure
|
|
|
|
|
|
|
|
## Input format
|
|
|
|
|
|
|
|
Rust input is interpreted as a sequence of Unicode codepoints encoded in UTF-8.
|
|
|
|
Most Rust grammar rules are defined in terms of printable ASCII-range
|
|
|
|
codepoints, but a small number are defined in terms of Unicode properties or
|
|
|
|
explicit codepoint lists. [^inputformat]
|
|
|
|
|
|
|
|
[^inputformat]: Substitute definitions for the special Unicode productions are
|
|
|
|
provided to the grammar verifier, restricted to ASCII range, when verifying the
|
|
|
|
grammar in this document.
|
|
|
|
|
|
|
|
## Special Unicode Productions
|
|
|
|
|
|
|
|
The following productions in the Rust grammar are defined in terms of Unicode
|
2015-04-25 22:56:00 +02:00
|
|
|
properties: `ident`, `non_null`, `non_eol`, `non_single_quote` and
|
|
|
|
`non_double_quote`.
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Identifiers
|
|
|
|
|
2018-05-16 00:56:56 +00:00
|
|
|
The `ident` production is any nonempty Unicode string of
|
2015-04-25 22:56:00 +02:00
|
|
|
the following form:
|
|
|
|
|
2018-05-16 00:56:56 +00:00
|
|
|
- The first character is in one of the following ranges `U+0041` to `U+005A`
|
|
|
|
("A" to "Z"), `U+0061` to `U+007A` ("a" to "z"), or `U+005F` ("\_").
|
|
|
|
- The remaining characters are in the range `U+0030` to `U+0039` ("0" to "9"),
|
|
|
|
or any of the prior valid initial characters.
|
2014-11-26 16:40:56 -08:00
|
|
|
|
2018-05-16 00:56:56 +00:00
|
|
|
as long as the identifier does _not_ occur in the set of [keywords](#keywords).
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Delimiter-restricted productions
|
|
|
|
|
|
|
|
Some productions are defined by exclusion of particular Unicode characters:
|
|
|
|
|
|
|
|
- `non_null` is any single Unicode character aside from `U+0000` (null)
|
2018-05-16 01:16:01 +00:00
|
|
|
- `non_eol` is any single Unicode character aside from `U+000A` (`'\n'`)
|
|
|
|
- `non_single_quote` is any single Unicode character aside from `U+0027` (`'`)
|
|
|
|
- `non_double_quote` is any single Unicode character aside from `U+0022` (`"`)
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
## Comments
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
comment : block_comment | line_comment ;
|
|
|
|
block_comment : "/*" block_comment_body * "*/" ;
|
|
|
|
block_comment_body : [block_comment | character] * ;
|
|
|
|
line_comment : "//" non_eol * ;
|
|
|
|
```
|
|
|
|
|
|
|
|
**FIXME:** add doc grammar?
|
|
|
|
|
|
|
|
## Whitespace
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
whitespace_char : '\x20' | '\x09' | '\x0a' | '\x0d' ;
|
|
|
|
whitespace : [ whitespace_char | comment ] + ;
|
|
|
|
```
|
|
|
|
|
|
|
|
## Tokens
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
simple_token : keyword | unop | binop ;
|
|
|
|
token : simple_token | ident | literal | symbol | whitespace token ;
|
|
|
|
```
|
|
|
|
|
|
|
|
### Keywords
|
|
|
|
|
|
|
|
<p id="keyword-table-marker"></p>
|
|
|
|
|
2017-06-17 13:14:26 -07:00
|
|
|
| | | | | |
|
|
|
|
|----------|----------|----------|----------|----------|
|
|
|
|
| _ | abstract | alignof | as | become |
|
|
|
|
| box | break | const | continue | crate |
|
|
|
|
| do | else | enum | extern | false |
|
|
|
|
| final | fn | for | if | impl |
|
|
|
|
| in | let | loop | macro | match |
|
|
|
|
| mod | move | mut | offsetof | override |
|
|
|
|
| priv | proc | pub | pure | ref |
|
|
|
|
| return | Self | self | sizeof | static |
|
|
|
|
| struct | super | trait | true | type |
|
|
|
|
| typeof | unsafe | unsized | use | virtual |
|
|
|
|
| where | while | yield | | |
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
|
|
|
|
Each of these keywords has special meaning in its grammar, and all of them are
|
|
|
|
excluded from the `ident` rule.
|
|
|
|
|
2016-06-19 16:27:37 -04:00
|
|
|
Not all of these keywords are used by the language. Some of them were used
|
|
|
|
before Rust 1.0, and were left reserved once their implementations were
|
|
|
|
removed. Some of them were reserved before 1.0 to make space for possible
|
|
|
|
future features.
|
|
|
|
|
2014-11-26 16:40:56 -08:00
|
|
|
### Literals
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
lit_suffix : ident;
|
2015-04-28 21:09:28 -04:00
|
|
|
literal : [ string_lit | char_lit | byte_string_lit | byte_lit | num_lit | bool_lit ] lit_suffix ?;
|
2014-11-26 16:40:56 -08:00
|
|
|
```
|
|
|
|
|
2015-04-28 22:02:53 -04:00
|
|
|
The optional `lit_suffix` production is only used for certain numeric literals,
|
|
|
|
but is reserved for future extension. That is, the above gives the lexical
|
|
|
|
grammar, but a Rust parser will reject everything but the 12 special cases
|
2017-02-21 01:15:29 -05:00
|
|
|
mentioned in [Number literals](reference/tokens.html#number-literals) in the
|
2015-04-28 22:02:53 -04:00
|
|
|
reference.
|
|
|
|
|
2014-11-26 16:40:56 -08:00
|
|
|
#### Character and string literals
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
char_lit : '\x27' char_body '\x27' ;
|
|
|
|
string_lit : '"' string_body * '"' | 'r' raw_string ;
|
|
|
|
|
|
|
|
char_body : non_single_quote
|
|
|
|
| '\x5c' [ '\x27' | common_escape | unicode_escape ] ;
|
|
|
|
|
|
|
|
string_body : non_double_quote
|
|
|
|
| '\x5c' [ '\x22' | common_escape | unicode_escape ] ;
|
|
|
|
raw_string : '"' raw_string_body '"' | '#' raw_string '#' ;
|
|
|
|
|
|
|
|
common_escape : '\x5c'
|
|
|
|
| 'n' | 'r' | 't' | '0'
|
|
|
|
| 'x' hex_digit 2
|
2015-02-01 16:42:22 +13:00
|
|
|
unicode_escape : 'u' '{' hex_digit+ 6 '}';
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
hex_digit : 'a' | 'b' | 'c' | 'd' | 'e' | 'f'
|
|
|
|
| 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
|
|
|
|
| dec_digit ;
|
|
|
|
oct_digit : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' ;
|
|
|
|
dec_digit : '0' | nonzero_dec ;
|
|
|
|
nonzero_dec: '1' | '2' | '3' | '4'
|
|
|
|
| '5' | '6' | '7' | '8' | '9' ;
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Byte and byte string literals
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
byte_lit : "b\x27" byte_body '\x27' ;
|
|
|
|
byte_string_lit : "b\x22" string_body * '\x22' | "br" raw_byte_string ;
|
|
|
|
|
|
|
|
byte_body : ascii_non_single_quote
|
|
|
|
| '\x5c' [ '\x27' | common_escape ] ;
|
|
|
|
|
|
|
|
byte_string_body : ascii_non_double_quote
|
|
|
|
| '\x5c' [ '\x22' | common_escape ] ;
|
|
|
|
raw_byte_string : '"' raw_byte_string_body '"' | '#' raw_byte_string '#' ;
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Number literals
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
num_lit : nonzero_dec [ dec_digit | '_' ] * float_suffix ?
|
|
|
|
| '0' [ [ dec_digit | '_' ] * float_suffix ?
|
|
|
|
| 'b' [ '1' | '0' | '_' ] +
|
|
|
|
| 'o' [ oct_digit | '_' ] +
|
|
|
|
| 'x' [ hex_digit | '_' ] + ] ;
|
|
|
|
|
|
|
|
float_suffix : [ exponent | '.' dec_lit exponent ? ] ? ;
|
|
|
|
|
|
|
|
exponent : ['E' | 'e'] ['-' | '+' ] ? dec_lit ;
|
|
|
|
dec_lit : [ dec_digit | '_' ] + ;
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Boolean literals
|
|
|
|
|
2015-04-28 21:09:28 -04:00
|
|
|
```antlr
|
|
|
|
bool_lit : [ "true" | "false" ] ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
The two values of the boolean type are written `true` and `false`.
|
|
|
|
|
|
|
|
### Symbols
|
|
|
|
|
|
|
|
```antlr
|
2015-05-10 22:17:33 -04:00
|
|
|
symbol : "::" | "->"
|
2014-11-26 16:40:56 -08:00
|
|
|
| '#' | '[' | ']' | '(' | ')' | '{' | '}'
|
|
|
|
| ',' | ';' ;
|
|
|
|
```
|
|
|
|
|
2015-10-08 07:24:00 +02:00
|
|
|
Symbols are a general class of printable [tokens](#tokens) that play structural
|
2015-10-10 09:15:55 -07:00
|
|
|
roles in a variety of grammar productions. They are cataloged here for
|
2014-11-26 16:40:56 -08:00
|
|
|
completeness as the set of remaining miscellaneous printable tokens that do not
|
|
|
|
otherwise appear as [unary operators](#unary-operator-expressions), [binary
|
|
|
|
operators](#binary-operator-expressions), or [keywords](#keywords).
|
|
|
|
|
|
|
|
## Paths
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
expr_path : [ "::" ] ident [ "::" expr_path_tail ] + ;
|
|
|
|
expr_path_tail : '<' type_expr [ ',' type_expr ] + '>'
|
|
|
|
| expr_path ;
|
|
|
|
|
|
|
|
type_path : ident [ type_path_tail ] + ;
|
|
|
|
type_path_tail : '<' type_expr [ ',' type_expr ] + '>'
|
|
|
|
| "::" type_path ;
|
|
|
|
```
|
|
|
|
|
|
|
|
# Syntax extensions
|
|
|
|
|
|
|
|
## Macros
|
|
|
|
|
|
|
|
```antlr
|
2015-09-03 23:42:33 +03:00
|
|
|
expr_macro_rules : "macro_rules" '!' ident '(' macro_rule * ')' ';'
|
2015-06-09 15:26:51 -04:00
|
|
|
| "macro_rules" '!' ident '{' macro_rule * '}' ;
|
2014-11-26 16:40:56 -08:00
|
|
|
macro_rule : '(' matcher * ')' "=>" '(' transcriber * ')' ';' ;
|
|
|
|
matcher : '(' matcher * ')' | '[' matcher * ']'
|
|
|
|
| '{' matcher * '}' | '$' ident ':' ident
|
|
|
|
| '$' '(' matcher * ')' sep_token? [ '*' | '+' ]
|
|
|
|
| non_special_token ;
|
|
|
|
transcriber : '(' transcriber * ')' | '[' transcriber * ']'
|
|
|
|
| '{' transcriber * '}' | '$' ident
|
|
|
|
| '$' '(' transcriber * ')' sep_token? [ '*' | '+' ]
|
|
|
|
| non_special_token ;
|
|
|
|
```
|
|
|
|
|
|
|
|
# Crates and source files
|
|
|
|
|
|
|
|
**FIXME:** grammar? What production covers #![crate_id = "foo"] ?
|
|
|
|
|
|
|
|
# Items and attributes
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
## Items
|
|
|
|
|
|
|
|
```antlr
|
2015-05-05 20:52:34 -04:00
|
|
|
item : vis ? mod_item | fn_item | type_item | struct_item | enum_item
|
2015-09-04 00:29:47 +03:00
|
|
|
| const_item | static_item | trait_item | impl_item | extern_block_item ;
|
2014-11-26 16:40:56 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
### Type Parameters
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Modules
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
mod_item : "mod" ident ( ';' | '{' mod '}' );
|
|
|
|
mod : [ view_item | item ] * ;
|
|
|
|
```
|
|
|
|
|
|
|
|
#### View items
|
|
|
|
|
|
|
|
```antlr
|
2015-05-10 20:48:02 -04:00
|
|
|
view_item : extern_crate_decl | use_decl ';' ;
|
2014-11-26 16:40:56 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
##### Extern crate declarations
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
extern_crate_decl : "extern" "crate" crate_name
|
2015-05-08 20:48:54 -04:00
|
|
|
crate_name: ident | ( ident "as" ident )
|
2014-11-26 16:40:56 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
##### Use declarations
|
|
|
|
|
|
|
|
```antlr
|
2015-05-05 20:52:34 -04:00
|
|
|
use_decl : vis ? "use" [ path "as" ident
|
|
|
|
| path_glob ] ;
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
path_glob : ident [ "::" [ path_glob
|
|
|
|
| '*' ] ] ?
|
|
|
|
| '{' path_item [ ',' path_item ] * '}' ;
|
|
|
|
|
2015-05-10 22:17:33 -04:00
|
|
|
path_item : ident | "self" ;
|
2014-11-26 16:40:56 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
### Functions
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Generic functions
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Unsafety
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
##### Unsafe functions
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
##### Unsafe blocks
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Diverging functions
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Type definitions
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Structures
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
2015-04-27 22:02:16 -04:00
|
|
|
### Enumerations
|
|
|
|
|
|
|
|
**FIXME:** grammar?
|
|
|
|
|
2014-11-26 16:40:56 -08:00
|
|
|
### Constant items
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
const_item : "const" ident ':' type '=' expr ';' ;
|
|
|
|
```
|
|
|
|
|
|
|
|
### Static items
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
static_item : "static" ident ':' type '=' expr ';' ;
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Mutable statics
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Traits
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Implementations
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### External blocks
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
extern_block_item : "extern" '{' extern_block '}' ;
|
|
|
|
extern_block : [ foreign_fn ] * ;
|
|
|
|
```
|
|
|
|
|
|
|
|
## Visibility and Privacy
|
|
|
|
|
2015-05-05 20:52:34 -04:00
|
|
|
```antlr
|
|
|
|
vis : "pub" ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
### Re-exporting and Visibility
|
|
|
|
|
2015-05-05 21:06:17 -04:00
|
|
|
See [Use declarations](#use-declarations).
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
## Attributes
|
|
|
|
|
|
|
|
```antlr
|
2015-05-10 22:17:33 -04:00
|
|
|
attribute : '#' '!' ? '[' meta_item ']' ;
|
2014-11-26 16:40:56 -08:00
|
|
|
meta_item : ident [ '=' literal
|
|
|
|
| '(' meta_seq ')' ] ? ;
|
|
|
|
meta_seq : meta_item [ ',' meta_seq ] ? ;
|
|
|
|
```
|
|
|
|
|
|
|
|
# Statements and expressions
|
|
|
|
|
|
|
|
## Statements
|
|
|
|
|
2015-05-05 21:52:21 -04:00
|
|
|
```antlr
|
2015-10-24 00:45:18 +02:00
|
|
|
stmt : decl_stmt | expr_stmt | ';' ;
|
2015-05-05 21:52:21 -04:00
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Declaration statements
|
|
|
|
|
2015-05-05 21:52:21 -04:00
|
|
|
```antlr
|
|
|
|
decl_stmt : item | let_decl ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Item declarations
|
|
|
|
|
2015-05-05 21:52:21 -04:00
|
|
|
See [Items](#items).
|
2014-11-26 16:40:56 -08:00
|
|
|
|
2015-04-17 19:52:29 -07:00
|
|
|
#### Variable declarations
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
```antlr
|
|
|
|
let_decl : "let" pat [':' type ] ? [ init ] ? ';' ;
|
|
|
|
init : [ '=' ] expr ;
|
|
|
|
```
|
|
|
|
|
|
|
|
### Expression statements
|
|
|
|
|
2015-05-05 21:52:21 -04:00
|
|
|
```antlr
|
|
|
|
expr_stmt : expr ';' ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
## Expressions
|
|
|
|
|
2015-05-05 22:32:20 -04:00
|
|
|
```antlr
|
|
|
|
expr : literal | path | tuple_expr | unit_expr | struct_expr
|
|
|
|
| block_expr | method_call_expr | field_expr | array_expr
|
|
|
|
| idx_expr | range_expr | unop_expr | binop_expr
|
|
|
|
| paren_expr | call_expr | lambda_expr | while_expr
|
|
|
|
| loop_expr | break_expr | continue_expr | for_expr
|
|
|
|
| if_expr | match_expr | if_let_expr | while_let_expr
|
|
|
|
| return_expr ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Lvalues, rvalues and temporaries
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Moved and copied types
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** Do we want to capture this in the grammar as different productions?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Literal expressions
|
|
|
|
|
2015-05-05 22:32:20 -04:00
|
|
|
See [Literals](#literals).
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Path expressions
|
|
|
|
|
2015-05-05 22:32:20 -04:00
|
|
|
See [Paths](#paths).
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Tuple expressions
|
|
|
|
|
2015-05-05 22:32:20 -04:00
|
|
|
```antlr
|
|
|
|
tuple_expr : '(' [ expr [ ',' expr ] * | expr ',' ] ? ')' ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Unit expressions
|
|
|
|
|
2015-05-05 22:32:20 -04:00
|
|
|
```antlr
|
|
|
|
unit_expr : "()" ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Structure expressions
|
|
|
|
|
|
|
|
```antlr
|
2017-01-10 12:35:04 +01:00
|
|
|
struct_expr_field_init : ident | ident ':' expr ;
|
|
|
|
struct_expr : expr_path '{' struct_expr_field_init
|
|
|
|
[ ',' struct_expr_field_init ] *
|
2014-11-26 16:40:56 -08:00
|
|
|
[ ".." expr ] '}' |
|
|
|
|
expr_path '(' expr
|
|
|
|
[ ',' expr ] * ')' |
|
|
|
|
expr_path ;
|
|
|
|
```
|
|
|
|
|
|
|
|
### Block expressions
|
|
|
|
|
|
|
|
```antlr
|
2016-02-03 13:42:23 -08:00
|
|
|
block_expr : '{' [ stmt | item ] *
|
2014-11-26 16:40:56 -08:00
|
|
|
[ expr ] '}' ;
|
|
|
|
```
|
|
|
|
|
|
|
|
### Method-call expressions
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
method_call_expr : expr '.' ident paren_expr_list ;
|
|
|
|
```
|
|
|
|
|
|
|
|
### Field expressions
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
field_expr : expr '.' ident ;
|
|
|
|
```
|
|
|
|
|
|
|
|
### Array expressions
|
|
|
|
|
|
|
|
```antlr
|
2015-03-01 15:37:40 +11:00
|
|
|
array_expr : '[' "mut" ? array_elems? ']' ;
|
2014-11-26 16:40:56 -08:00
|
|
|
|
2015-05-10 22:17:33 -04:00
|
|
|
array_elems : [expr [',' expr]*] | [expr ';' expr] ;
|
2014-11-26 16:40:56 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
### Index expressions
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
idx_expr : expr '[' expr ']' ;
|
|
|
|
```
|
|
|
|
|
2015-04-24 13:57:37 -04:00
|
|
|
### Range expressions
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
range_expr : expr ".." expr |
|
|
|
|
expr ".." |
|
|
|
|
".." expr |
|
|
|
|
".." ;
|
|
|
|
```
|
|
|
|
|
2014-11-26 16:40:56 -08:00
|
|
|
### Unary operator expressions
|
|
|
|
|
2015-05-05 22:32:20 -04:00
|
|
|
```antlr
|
|
|
|
unop_expr : unop expr ;
|
|
|
|
unop : '-' | '*' | '!' ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Binary operator expressions
|
|
|
|
|
|
|
|
```antlr
|
2015-05-05 22:32:20 -04:00
|
|
|
binop_expr : expr binop expr | type_cast_expr
|
|
|
|
| assignment_expr | compound_assignment_expr ;
|
|
|
|
binop : arith_op | bitwise_op | lazy_bool_op | comp_op
|
2014-11-26 16:40:56 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
#### Arithmetic operators
|
|
|
|
|
2015-05-05 22:32:20 -04:00
|
|
|
```antlr
|
|
|
|
arith_op : '+' | '-' | '*' | '/' | '%' ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Bitwise operators
|
|
|
|
|
2015-05-05 22:32:20 -04:00
|
|
|
```antlr
|
|
|
|
bitwise_op : '&' | '|' | '^' | "<<" | ">>" ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Lazy boolean operators
|
|
|
|
|
2015-05-05 22:32:20 -04:00
|
|
|
```antlr
|
|
|
|
lazy_bool_op : "&&" | "||" ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Comparison operators
|
|
|
|
|
2015-05-05 22:32:20 -04:00
|
|
|
```antlr
|
|
|
|
comp_op : "==" | "!=" | '<' | '>' | "<=" | ">=" ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Type cast expressions
|
|
|
|
|
2015-05-05 22:32:20 -04:00
|
|
|
```antlr
|
|
|
|
type_cast_expr : value "as" type ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Assignment expressions
|
|
|
|
|
2015-05-05 22:32:20 -04:00
|
|
|
```antlr
|
|
|
|
assignment_expr : expr '=' expr ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Compound assignment expressions
|
|
|
|
|
2015-05-05 22:32:20 -04:00
|
|
|
```antlr
|
|
|
|
compound_assignment_expr : expr [ arith_op | bitwise_op ] '=' expr ;
|
|
|
|
```
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Grouped expressions
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
paren_expr : '(' expr ')' ;
|
|
|
|
```
|
|
|
|
|
|
|
|
### Call expressions
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
expr_list : [ expr [ ',' expr ]* ] ? ;
|
|
|
|
paren_expr_list : '(' expr_list ')' ;
|
|
|
|
call_expr : expr paren_expr_list ;
|
|
|
|
```
|
|
|
|
|
|
|
|
### Lambda expressions
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
ident_list : [ ident [ ',' ident ]* ] ? ;
|
|
|
|
lambda_expr : '|' ident_list '|' expr ;
|
|
|
|
```
|
|
|
|
|
|
|
|
### While loops
|
|
|
|
|
|
|
|
```antlr
|
2015-09-03 22:13:50 +03:00
|
|
|
while_expr : [ lifetime ':' ] ? "while" no_struct_literal_expr '{' block '}' ;
|
2014-11-26 16:40:56 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
### Infinite loops
|
|
|
|
|
|
|
|
```antlr
|
2015-09-03 22:13:50 +03:00
|
|
|
loop_expr : [ lifetime ':' ] ? "loop" '{' block '}';
|
2014-11-26 16:40:56 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
### Break expressions
|
|
|
|
|
|
|
|
```antlr
|
2015-09-03 22:13:50 +03:00
|
|
|
break_expr : "break" [ lifetime ] ?;
|
2014-11-26 16:40:56 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
### Continue expressions
|
|
|
|
|
|
|
|
```antlr
|
2015-09-03 22:13:50 +03:00
|
|
|
continue_expr : "continue" [ lifetime ] ?;
|
2014-11-26 16:40:56 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
### For expressions
|
|
|
|
|
|
|
|
```antlr
|
2015-09-03 22:13:50 +03:00
|
|
|
for_expr : [ lifetime ':' ] ? "for" pat "in" no_struct_literal_expr '{' block '}' ;
|
2014-11-26 16:40:56 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
### If expressions
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
if_expr : "if" no_struct_literal_expr '{' block '}'
|
|
|
|
else_tail ? ;
|
|
|
|
|
|
|
|
else_tail : "else" [ if_expr | if_let_expr
|
|
|
|
| '{' block '}' ] ;
|
|
|
|
```
|
|
|
|
|
|
|
|
### Match expressions
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
match_expr : "match" no_struct_literal_expr '{' match_arm * '}' ;
|
|
|
|
|
|
|
|
match_arm : attribute * match_pat "=>" [ expr "," | '{' block '}' ] ;
|
|
|
|
|
|
|
|
match_pat : pat [ '|' pat ] * [ "if" expr ] ? ;
|
|
|
|
```
|
|
|
|
|
|
|
|
### If let expressions
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
if_let_expr : "if" "let" pat '=' expr '{' block '}'
|
|
|
|
else_tail ? ;
|
|
|
|
```
|
|
|
|
|
|
|
|
### While let loops
|
|
|
|
|
|
|
|
```antlr
|
2015-09-03 22:13:50 +03:00
|
|
|
while_let_expr : [ lifetime ':' ] ? "while" "let" pat '=' expr '{' block '}' ;
|
2014-11-26 16:40:56 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
### Return expressions
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
return_expr : "return" expr ? ;
|
|
|
|
```
|
|
|
|
|
|
|
|
# Type system
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** is this entire chapter relevant here? Or should it all have been covered by some production already?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
2014-11-26 16:52:36 -08:00
|
|
|
## Types
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Primitive types
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Machine types
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
#### Machine-dependent integer types
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Textual types
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Tuple types
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Array, and Slice types
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Structure types
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Enumerated types
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Pointer types
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Function types
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Closure types
|
|
|
|
|
2014-11-26 16:52:36 -08:00
|
|
|
```antlr
|
2014-11-26 16:40:56 -08:00
|
|
|
closure_type := [ 'unsafe' ] [ '<' lifetime-list '>' ] '|' arg-list '|'
|
|
|
|
[ ':' bound-list ] [ '->' type ]
|
|
|
|
lifetime-list := lifetime | lifetime ',' lifetime-list
|
|
|
|
arg-list := ident ':' type | ident ':' type ',' arg-list
|
|
|
|
```
|
|
|
|
|
2016-08-20 21:54:29 +02:00
|
|
|
### Never type
|
|
|
|
An empty type
|
|
|
|
|
|
|
|
```antlr
|
|
|
|
never_type : "!" ;
|
|
|
|
```
|
|
|
|
|
2014-11-26 16:40:56 -08:00
|
|
|
### Object types
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Type parameters
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
2017-04-12 20:46:44 -04:00
|
|
|
### Type parameter bounds
|
|
|
|
|
|
|
|
```antlr
|
2017-04-21 00:19:06 +03:00
|
|
|
bound-list := bound | bound '+' bound-list '+' ?
|
2017-04-12 20:46:44 -04:00
|
|
|
bound := ty_bound | lt_bound
|
|
|
|
lt_bound := lifetime
|
2017-04-21 00:19:06 +03:00
|
|
|
ty_bound := ty_bound_noparen | (ty_bound_noparen)
|
|
|
|
ty_bound_noparen := [?] [ for<lt_param_defs> ] simple_path
|
2017-04-12 20:46:44 -04:00
|
|
|
```
|
|
|
|
|
2014-11-26 16:40:56 -08:00
|
|
|
### Self types
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** grammar?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
## Type kinds
|
|
|
|
|
2015-10-07 23:11:25 +01:00
|
|
|
**FIXME:** this is probably not relevant to the grammar...
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
# Memory and concurrency models
|
|
|
|
|
2015-03-14 16:09:26 -07:00
|
|
|
**FIXME:** is this entire chapter relevant here? Or should it all have been covered by some production already?
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
## Memory model
|
|
|
|
|
|
|
|
### Memory allocation and lifetime
|
|
|
|
|
|
|
|
### Memory ownership
|
|
|
|
|
2015-04-17 19:52:29 -07:00
|
|
|
### Variables
|
2014-11-26 16:40:56 -08:00
|
|
|
|
|
|
|
### Boxes
|
|
|
|
|
2015-05-09 00:12:29 +09:00
|
|
|
## Threads
|
2014-11-26 16:40:56 -08:00
|
|
|
|
2015-05-09 00:12:29 +09:00
|
|
|
### Communication between threads
|
2014-11-26 16:40:56 -08:00
|
|
|
|
2015-05-09 00:12:29 +09:00
|
|
|
### Thread lifecycle
|