]> git.lizzy.rs Git - rust.git/blob - src/doc/grammar.md
78432b6a9659370cac1ae373770e795f0b8469b7
[rust.git] / src / doc / grammar.md
1 % Grammar
2
3 # Introduction
4
5 This document is the primary reference for the Rust programming language grammar. It
6 provides only one kind of material:
7
8   - Chapters that formally define the language grammar.
9
10 This document does not serve as an introduction to the language. Background
11 familiarity with the language is assumed. A separate [guide] is available to
12 help acquire such background.
13
14 This document also does not serve as a reference to the [standard] library
15 included in the language distribution. Those libraries are documented
16 separately by extracting documentation attributes from their source code. Many
17 of the features that one might expect to be language features are library
18 features in Rust, so what you're looking for may be there, not here.
19
20 [guide]: guide.html
21 [standard]: std/index.html
22
23 # Notation
24
25 Rust's grammar is defined over Unicode codepoints, each conventionally denoted
26 `U+XXXX`, for 4 or more hexadecimal digits `X`. _Most_ of Rust's grammar is
27 confined to the ASCII range of Unicode, and is described in this document by a
28 dialect of Extended Backus-Naur Form (EBNF), specifically a dialect of EBNF
29 supported by common automated LL(k) parsing tools such as `llgen`, rather than
30 the dialect given in ISO 14977. The dialect can be defined self-referentially
31 as follows:
32
33 ```antlr
34 grammar : rule + ;
35 rule    : nonterminal ':' productionrule ';' ;
36 productionrule : production [ '|' production ] * ;
37 production : term * ;
38 term : element repeats ;
39 element : LITERAL | IDENTIFIER | '[' productionrule ']' ;
40 repeats : [ '*' | '+' ] NUMBER ? | NUMBER ? | '?' ;
41 ```
42
43 Where:
44
45 - Whitespace in the grammar is ignored.
46 - Square brackets are used to group rules.
47 - `LITERAL` is a single printable ASCII character, or an escaped hexadecimal
48   ASCII code of the form `\xQQ`, in single quotes, denoting the corresponding
49   Unicode codepoint `U+00QQ`.
50 - `IDENTIFIER` is a nonempty string of ASCII letters and underscores.
51 - The `repeat` forms apply to the adjacent `element`, and are as follows:
52   - `?` means zero or one repetition
53   - `*` means zero or more repetitions
54   - `+` means one or more repetitions
55   - NUMBER trailing a repeat symbol gives a maximum repetition count
56   - NUMBER on its own gives an exact repetition count
57
58 This EBNF dialect should hopefully be familiar to many readers.
59
60 ## Unicode productions
61
62 A few productions in Rust's grammar permit Unicode codepoints outside the ASCII
63 range. We define these productions in terms of character properties specified
64 in the Unicode standard, rather than in terms of ASCII-range codepoints. The
65 section [Special Unicode Productions](#special-unicode-productions) lists these
66 productions.
67
68 ## String table productions
69
70 Some rules in the grammar — notably [unary
71 operators](#unary-operator-expressions), [binary
72 operators](#binary-operator-expressions), and [keywords](#keywords) — are
73 given in a simplified form: as a listing of a table of unquoted, printable
74 whitespace-separated strings. These cases form a subset of the rules regarding
75 the [token](#tokens) rule, and are assumed to be the result of a
76 lexical-analysis phase feeding the parser, driven by a DFA, operating over the
77 disjunction of all such string table entries.
78
79 When such a string enclosed in double-quotes (`"`) occurs inside the grammar,
80 it is an implicit reference to a single member of such a string table
81 production. See [tokens](#tokens) for more information.
82
83 # Lexical structure
84
85 ## Input format
86
87 Rust input is interpreted as a sequence of Unicode codepoints encoded in UTF-8.
88 Most Rust grammar rules are defined in terms of printable ASCII-range
89 codepoints, but a small number are defined in terms of Unicode properties or
90 explicit codepoint lists. [^inputformat]
91
92 [^inputformat]: Substitute definitions for the special Unicode productions are
93   provided to the grammar verifier, restricted to ASCII range, when verifying the
94   grammar in this document.
95
96 ## Special Unicode Productions
97
98 The following productions in the Rust grammar are defined in terms of Unicode
99 properties: `ident`, `non_null`, `non_eol`, `non_single_quote` and
100 `non_double_quote`.
101
102 ### Identifiers
103
104 The `ident` production is any nonempty Unicode[^non_ascii_idents] string of
105 the following form:
106
107 [^non_ascii_idents]: Non-ASCII characters in identifiers are currently feature
108   gated. This is expected to improve soon.
109
110 - The first character has property `XID_start`
111 - The remaining characters have property `XID_continue`
112
113 that does _not_ occur in the set of [keywords](#keywords).
114
115 > **Note**: `XID_start` and `XID_continue` as character properties cover the
116 > character ranges used to form the more familiar C and Java language-family
117 > identifiers.
118
119 ### Delimiter-restricted productions
120
121 Some productions are defined by exclusion of particular Unicode characters:
122
123 - `non_null` is any single Unicode character aside from `U+0000` (null)
124 - `non_eol` is `non_null` restricted to exclude `U+000A` (`'\n'`)
125 - `non_single_quote` is `non_null` restricted to exclude `U+0027`  (`'`)
126 - `non_double_quote` is `non_null` restricted to exclude `U+0022` (`"`)
127
128 ## Comments
129
130 ```antlr
131 comment : block_comment | line_comment ;
132 block_comment : "/*" block_comment_body * "*/" ;
133 block_comment_body : [block_comment | character] * ;
134 line_comment : "//" non_eol * ;
135 ```
136
137 **FIXME:** add doc grammar?
138
139 ## Whitespace
140
141 ```antlr
142 whitespace_char : '\x20' | '\x09' | '\x0a' | '\x0d' ;
143 whitespace : [ whitespace_char | comment ] + ;
144 ```
145
146 ## Tokens
147
148 ```antlr
149 simple_token : keyword | unop | binop ;
150 token : simple_token | ident | literal | symbol | whitespace token ;
151 ```
152
153 ### Keywords
154
155 <p id="keyword-table-marker"></p>
156
157 |          |          |          |          |          |
158 |----------|----------|----------|----------|----------|
159 | _        | abstract | alignof  | as       | become   |
160 | box      | break    | const    | continue | crate    |
161 | do       | else     | enum     | extern   | false    |
162 | final    | fn       | for      | if       | impl     |
163 | in       | let      | loop     | macro    | match    |
164 | mod      | move     | mut      | offsetof | override |
165 | priv     | proc     | pub      | pure     | ref      |
166 | return   | Self     | self     | sizeof   | static   |
167 | struct   | super    | trait    | true     | type     |
168 | typeof   | unsafe   | unsized  | use      | virtual  |
169 | where    | while    | yield    |          |          |
170
171
172 Each of these keywords has special meaning in its grammar, and all of them are
173 excluded from the `ident` rule.
174
175 Not all of these keywords are used by the language. Some of them were used
176 before Rust 1.0, and were left reserved once their implementations were
177 removed. Some of them were reserved before 1.0 to make space for possible
178 future features.
179
180 ### Literals
181
182 ```antlr
183 lit_suffix : ident;
184 literal : [ string_lit | char_lit | byte_string_lit | byte_lit | num_lit | bool_lit ] lit_suffix ?;
185 ```
186
187 The optional `lit_suffix` production is only used for certain numeric literals,
188 but is reserved for future extension. That is, the above gives the lexical
189 grammar, but a Rust parser will reject everything but the 12 special cases
190 mentioned in [Number literals](reference/tokens.html#number-literals) in the
191 reference.
192
193 #### Character and string literals
194
195 ```antlr
196 char_lit : '\x27' char_body '\x27' ;
197 string_lit : '"' string_body * '"' | 'r' raw_string ;
198
199 char_body : non_single_quote
200           | '\x5c' [ '\x27' | common_escape | unicode_escape ] ;
201
202 string_body : non_double_quote
203             | '\x5c' [ '\x22' | common_escape | unicode_escape ] ;
204 raw_string : '"' raw_string_body '"' | '#' raw_string '#' ;
205
206 common_escape : '\x5c'
207               | 'n' | 'r' | 't' | '0'
208               | 'x' hex_digit 2
209 unicode_escape : 'u' '{' hex_digit+ 6 '}';
210
211 hex_digit : 'a' | 'b' | 'c' | 'd' | 'e' | 'f'
212           | 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
213           | dec_digit ;
214 oct_digit : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' ;
215 dec_digit : '0' | nonzero_dec ;
216 nonzero_dec: '1' | '2' | '3' | '4'
217            | '5' | '6' | '7' | '8' | '9' ;
218 ```
219
220 #### Byte and byte string literals
221
222 ```antlr
223 byte_lit : "b\x27" byte_body '\x27' ;
224 byte_string_lit : "b\x22" string_body * '\x22' | "br" raw_byte_string ;
225
226 byte_body : ascii_non_single_quote
227           | '\x5c' [ '\x27' | common_escape ] ;
228
229 byte_string_body : ascii_non_double_quote
230             | '\x5c' [ '\x22' | common_escape ] ;
231 raw_byte_string : '"' raw_byte_string_body '"' | '#' raw_byte_string '#' ;
232
233 ```
234
235 #### Number literals
236
237 ```antlr
238 num_lit : nonzero_dec [ dec_digit | '_' ] * float_suffix ?
239         | '0' [       [ dec_digit | '_' ] * float_suffix ?
240               | 'b'   [ '1' | '0' | '_' ] +
241               | 'o'   [ oct_digit | '_' ] +
242               | 'x'   [ hex_digit | '_' ] +  ] ;
243
244 float_suffix : [ exponent | '.' dec_lit exponent ? ] ? ;
245
246 exponent : ['E' | 'e'] ['-' | '+' ] ? dec_lit ;
247 dec_lit : [ dec_digit | '_' ] + ;
248 ```
249
250 #### Boolean literals
251
252 ```antlr
253 bool_lit : [ "true" | "false" ] ;
254 ```
255
256 The two values of the boolean type are written `true` and `false`.
257
258 ### Symbols
259
260 ```antlr
261 symbol : "::" | "->"
262        | '#' | '[' | ']' | '(' | ')' | '{' | '}'
263        | ',' | ';' ;
264 ```
265
266 Symbols are a general class of printable [tokens](#tokens) that play structural
267 roles in a variety of grammar productions. They are cataloged here for
268 completeness as the set of remaining miscellaneous printable tokens that do not
269 otherwise appear as [unary operators](#unary-operator-expressions), [binary
270 operators](#binary-operator-expressions), or [keywords](#keywords).
271
272 ## Paths
273
274 ```antlr
275 expr_path : [ "::" ] ident [ "::" expr_path_tail ] + ;
276 expr_path_tail : '<' type_expr [ ',' type_expr ] + '>'
277                | expr_path ;
278
279 type_path : ident [ type_path_tail ] + ;
280 type_path_tail : '<' type_expr [ ',' type_expr ] + '>'
281                | "::" type_path ;
282 ```
283
284 # Syntax extensions
285
286 ## Macros
287
288 ```antlr
289 expr_macro_rules : "macro_rules" '!' ident '(' macro_rule * ')' ';'
290                  | "macro_rules" '!' ident '{' macro_rule * '}' ;
291 macro_rule : '(' matcher * ')' "=>" '(' transcriber * ')' ';' ;
292 matcher : '(' matcher * ')' | '[' matcher * ']'
293         | '{' matcher * '}' | '$' ident ':' ident
294         | '$' '(' matcher * ')' sep_token? [ '*' | '+' ]
295         | non_special_token ;
296 transcriber : '(' transcriber * ')' | '[' transcriber * ']'
297             | '{' transcriber * '}' | '$' ident
298             | '$' '(' transcriber * ')' sep_token? [ '*' | '+' ]
299             | non_special_token ;
300 ```
301
302 # Crates and source files
303
304 **FIXME:** grammar? What production covers #![crate_id = "foo"] ?
305
306 # Items and attributes
307
308 **FIXME:** grammar?
309
310 ## Items
311
312 ```antlr
313 item : vis ? mod_item | fn_item | type_item | struct_item | enum_item
314      | const_item | static_item | trait_item | impl_item | extern_block_item ;
315 ```
316
317 ### Type Parameters
318
319 **FIXME:** grammar?
320
321 ### Modules
322
323 ```antlr
324 mod_item : "mod" ident ( ';' | '{' mod '}' );
325 mod : [ view_item | item ] * ;
326 ```
327
328 #### View items
329
330 ```antlr
331 view_item : extern_crate_decl | use_decl ';' ;
332 ```
333
334 ##### Extern crate declarations
335
336 ```antlr
337 extern_crate_decl : "extern" "crate" crate_name
338 crate_name: ident | ( ident "as" ident )
339 ```
340
341 ##### Use declarations
342
343 ```antlr
344 use_decl : vis ? "use" [ path "as" ident
345                         | path_glob ] ;
346
347 path_glob : ident [ "::" [ path_glob
348                           | '*' ] ] ?
349           | '{' path_item [ ',' path_item ] * '}' ;
350
351 path_item : ident | "self" ;
352 ```
353
354 ### Functions
355
356 **FIXME:** grammar?
357
358 #### Generic functions
359
360 **FIXME:** grammar?
361
362 #### Unsafety
363
364 **FIXME:** grammar?
365
366 ##### Unsafe functions
367
368 **FIXME:** grammar?
369
370 ##### Unsafe blocks
371
372 **FIXME:** grammar?
373
374 #### Diverging functions
375
376 **FIXME:** grammar?
377
378 ### Type definitions
379
380 **FIXME:** grammar?
381
382 ### Structures
383
384 **FIXME:** grammar?
385
386 ### Enumerations
387
388 **FIXME:** grammar?
389
390 ### Constant items
391
392 ```antlr
393 const_item : "const" ident ':' type '=' expr ';' ;
394 ```
395
396 ### Static items
397
398 ```antlr
399 static_item : "static" ident ':' type '=' expr ';' ;
400 ```
401
402 #### Mutable statics
403
404 **FIXME:** grammar?
405
406 ### Traits
407
408 **FIXME:** grammar?
409
410 ### Implementations
411
412 **FIXME:** grammar?
413
414 ### External blocks
415
416 ```antlr
417 extern_block_item : "extern" '{' extern_block '}' ;
418 extern_block : [ foreign_fn ] * ;
419 ```
420
421 ## Visibility and Privacy
422
423 ```antlr
424 vis : "pub" ;
425 ```
426 ### Re-exporting and Visibility
427
428 See [Use declarations](#use-declarations).
429
430 ## Attributes
431
432 ```antlr
433 attribute : '#' '!' ? '[' meta_item ']' ;
434 meta_item : ident [ '=' literal
435                   | '(' meta_seq ')' ] ? ;
436 meta_seq : meta_item [ ',' meta_seq ] ? ;
437 ```
438
439 # Statements and expressions
440
441 ## Statements
442
443 ```antlr
444 stmt : decl_stmt | expr_stmt | ';' ;
445 ```
446
447 ### Declaration statements
448
449 ```antlr
450 decl_stmt : item | let_decl ;
451 ```
452
453 #### Item declarations
454
455 See [Items](#items).
456
457 #### Variable declarations
458
459 ```antlr
460 let_decl : "let" pat [':' type ] ? [ init ] ? ';' ;
461 init : [ '=' ] expr ;
462 ```
463
464 ### Expression statements
465
466 ```antlr
467 expr_stmt : expr ';' ;
468 ```
469
470 ## Expressions
471
472 ```antlr
473 expr : literal | path | tuple_expr | unit_expr | struct_expr
474      | block_expr | method_call_expr | field_expr | array_expr
475      | idx_expr | range_expr | unop_expr | binop_expr
476      | paren_expr | call_expr | lambda_expr | while_expr
477      | loop_expr | break_expr | continue_expr | for_expr
478      | if_expr | match_expr | if_let_expr | while_let_expr
479      | return_expr ;
480 ```
481
482 #### Lvalues, rvalues and temporaries
483
484 **FIXME:** grammar?
485
486 #### Moved and copied types
487
488 **FIXME:** Do we want to capture this in the grammar as different productions?
489
490 ### Literal expressions
491
492 See [Literals](#literals).
493
494 ### Path expressions
495
496 See [Paths](#paths).
497
498 ### Tuple expressions
499
500 ```antlr
501 tuple_expr : '(' [ expr [ ',' expr ] * | expr ',' ] ? ')' ;
502 ```
503
504 ### Unit expressions
505
506 ```antlr
507 unit_expr : "()" ;
508 ```
509
510 ### Structure expressions
511
512 ```antlr
513 struct_expr_field_init : ident | ident ':' expr ;
514 struct_expr : expr_path '{' struct_expr_field_init
515                       [ ',' struct_expr_field_init ] *
516                       [ ".." expr ] '}' |
517               expr_path '(' expr
518                       [ ',' expr ] * ')' |
519               expr_path ;
520 ```
521
522 ### Block expressions
523
524 ```antlr
525 block_expr : '{' [ stmt | item ] *
526                  [ expr ] '}' ;
527 ```
528
529 ### Method-call expressions
530
531 ```antlr
532 method_call_expr : expr '.' ident paren_expr_list ;
533 ```
534
535 ### Field expressions
536
537 ```antlr
538 field_expr : expr '.' ident ;
539 ```
540
541 ### Array expressions
542
543 ```antlr
544 array_expr : '[' "mut" ? array_elems? ']' ;
545
546 array_elems : [expr [',' expr]*] | [expr ';' expr] ;
547 ```
548
549 ### Index expressions
550
551 ```antlr
552 idx_expr : expr '[' expr ']' ;
553 ```
554
555 ### Range expressions
556
557 ```antlr
558 range_expr : expr ".." expr |
559              expr ".." |
560              ".." expr |
561              ".." ;
562 ```
563
564 ### Unary operator expressions
565
566 ```antlr
567 unop_expr : unop expr ;
568 unop : '-' | '*' | '!' ;
569 ```
570
571 ### Binary operator expressions
572
573 ```antlr
574 binop_expr : expr binop expr | type_cast_expr
575            | assignment_expr | compound_assignment_expr ;
576 binop : arith_op | bitwise_op | lazy_bool_op | comp_op
577 ```
578
579 #### Arithmetic operators
580
581 ```antlr
582 arith_op : '+' | '-' | '*' | '/' | '%' ;
583 ```
584
585 #### Bitwise operators
586
587 ```antlr
588 bitwise_op : '&' | '|' | '^' | "<<" | ">>" ;
589 ```
590
591 #### Lazy boolean operators
592
593 ```antlr
594 lazy_bool_op : "&&" | "||" ;
595 ```
596
597 #### Comparison operators
598
599 ```antlr
600 comp_op : "==" | "!=" | '<' | '>' | "<=" | ">=" ;
601 ```
602
603 #### Type cast expressions
604
605 ```antlr
606 type_cast_expr : value "as" type ;
607 ```
608
609 #### Assignment expressions
610
611 ```antlr
612 assignment_expr : expr '=' expr ;
613 ```
614
615 #### Compound assignment expressions
616
617 ```antlr
618 compound_assignment_expr : expr [ arith_op | bitwise_op ] '=' expr ;
619 ```
620
621 ### Grouped expressions
622
623 ```antlr
624 paren_expr : '(' expr ')' ;
625 ```
626
627 ### Call expressions
628
629 ```antlr
630 expr_list : [ expr [ ',' expr ]* ] ? ;
631 paren_expr_list : '(' expr_list ')' ;
632 call_expr : expr paren_expr_list ;
633 ```
634
635 ### Lambda expressions
636
637 ```antlr
638 ident_list : [ ident [ ',' ident ]* ] ? ;
639 lambda_expr : '|' ident_list '|' expr ;
640 ```
641
642 ### While loops
643
644 ```antlr
645 while_expr : [ lifetime ':' ] ? "while" no_struct_literal_expr '{' block '}' ;
646 ```
647
648 ### Infinite loops
649
650 ```antlr
651 loop_expr : [ lifetime ':' ] ? "loop" '{' block '}';
652 ```
653
654 ### Break expressions
655
656 ```antlr
657 break_expr : "break" [ lifetime ] ?;
658 ```
659
660 ### Continue expressions
661
662 ```antlr
663 continue_expr : "continue" [ lifetime ] ?;
664 ```
665
666 ### For expressions
667
668 ```antlr
669 for_expr : [ lifetime ':' ] ? "for" pat "in" no_struct_literal_expr '{' block '}' ;
670 ```
671
672 ### If expressions
673
674 ```antlr
675 if_expr : "if" no_struct_literal_expr '{' block '}'
676           else_tail ? ;
677
678 else_tail : "else" [ if_expr | if_let_expr
679                    | '{' block '}' ] ;
680 ```
681
682 ### Match expressions
683
684 ```antlr
685 match_expr : "match" no_struct_literal_expr '{' match_arm * '}' ;
686
687 match_arm : attribute * match_pat "=>" [ expr "," | '{' block '}' ] ;
688
689 match_pat : pat [ '|' pat ] * [ "if" expr ] ? ;
690 ```
691
692 ### If let expressions
693
694 ```antlr
695 if_let_expr : "if" "let" pat '=' expr '{' block '}'
696                else_tail ? ;
697 ```
698
699 ### While let loops
700
701 ```antlr
702 while_let_expr : [ lifetime ':' ] ? "while" "let" pat '=' expr '{' block '}' ;
703 ```
704
705 ### Return expressions
706
707 ```antlr
708 return_expr : "return" expr ? ;
709 ```
710
711 # Type system
712
713 **FIXME:** is this entire chapter relevant here? Or should it all have been covered by some production already?
714
715 ## Types
716
717 ### Primitive types
718
719 **FIXME:** grammar?
720
721 #### Machine types
722
723 **FIXME:** grammar?
724
725 #### Machine-dependent integer types
726
727 **FIXME:** grammar?
728
729 ### Textual types
730
731 **FIXME:** grammar?
732
733 ### Tuple types
734
735 **FIXME:** grammar?
736
737 ### Array, and Slice types
738
739 **FIXME:** grammar?
740
741 ### Structure types
742
743 **FIXME:** grammar?
744
745 ### Enumerated types
746
747 **FIXME:** grammar?
748
749 ### Pointer types
750
751 **FIXME:** grammar?
752
753 ### Function types
754
755 **FIXME:** grammar?
756
757 ### Closure types
758
759 ```antlr
760 closure_type := [ 'unsafe' ] [ '<' lifetime-list '>' ] '|' arg-list '|'
761                 [ ':' bound-list ] [ '->' type ]
762 lifetime-list := lifetime | lifetime ',' lifetime-list
763 arg-list := ident ':' type | ident ':' type ',' arg-list
764 ```
765
766 ### Never type
767 An empty type
768
769 ```antlr
770 never_type : "!" ;
771 ```
772
773 ### Object types
774
775 **FIXME:** grammar?
776
777 ### Type parameters
778
779 **FIXME:** grammar?
780
781 ### Type parameter bounds
782
783 ```antlr
784 bound-list := bound | bound '+' bound-list '+' ?
785 bound := ty_bound | lt_bound
786 lt_bound := lifetime
787 ty_bound := ty_bound_noparen | (ty_bound_noparen)
788 ty_bound_noparen := [?] [ for<lt_param_defs> ] simple_path
789 ```
790
791 ### Self types
792
793 **FIXME:** grammar?
794
795 ## Type kinds
796
797 **FIXME:** this is probably not relevant to the grammar...
798
799 # Memory and concurrency models
800
801 **FIXME:** is this entire chapter relevant here? Or should it all have been covered by some production already?
802
803 ## Memory model
804
805 ### Memory allocation and lifetime
806
807 ### Memory ownership
808
809 ### Variables
810
811 ### Boxes
812
813 ## Threads
814
815 ### Communication between threads
816
817 ### Thread lifecycle