]> git.lizzy.rs Git - rust.git/blob - src/doc/grammar.md
ee9135b6578f6071e7f43fbbe6f2214f1253dbf6
[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 string of
105 the following form:
106
107 - The first character is in one of the following ranges `U+0041` to `U+005A`
108 ("A" to "Z"), `U+0061` to `U+007A` ("a" to "z"), or `U+005F` ("\_").
109 - The remaining characters are in the range `U+0030` to `U+0039` ("0" to "9"),
110 or any of the prior valid initial characters.
111
112 as long as the identifier does _not_ occur in the set of [keywords](#keywords).
113
114 ### Delimiter-restricted productions
115
116 Some productions are defined by exclusion of particular Unicode characters:
117
118 - `non_null` is any single Unicode character aside from `U+0000` (null)
119 - `non_eol` is any single Unicode character aside from `U+000A` (`'\n'`)
120 - `non_single_quote` is any single Unicode character aside from `U+0027`  (`'`)
121 - `non_double_quote` is any single Unicode character aside from `U+0022` (`"`)
122
123 ## Comments
124
125 ```antlr
126 comment : block_comment | line_comment ;
127 block_comment : "/*" block_comment_body * "*/" ;
128 block_comment_body : [block_comment | character] * ;
129 line_comment : "//" non_eol * ;
130 ```
131
132 **FIXME:** add doc grammar?
133
134 ## Whitespace
135
136 ```antlr
137 whitespace_char : '\x20' | '\x09' | '\x0a' | '\x0d' ;
138 whitespace : [ whitespace_char | comment ] + ;
139 ```
140
141 ## Tokens
142
143 ```antlr
144 simple_token : keyword | unop | binop ;
145 token : simple_token | ident | literal | symbol | whitespace token ;
146 ```
147
148 ### Keywords
149
150 <p id="keyword-table-marker"></p>
151
152 |          |          |          |          |          |
153 |----------|----------|----------|----------|----------|
154 | _        | abstract | alignof  | as       | become   |
155 | box      | break    | const    | continue | crate    |
156 | do       | else     | enum     | extern   | false    |
157 | final    | fn       | for      | if       | impl     |
158 | in       | let      | loop     | macro    | match    |
159 | mod      | move     | mut      | offsetof | override |
160 | priv     | proc     | pub      | pure     | ref      |
161 | return   | Self     | self     | sizeof   | static   |
162 | struct   | super    | trait    | true     | type     |
163 | typeof   | unsafe   | unsized  | use      | virtual  |
164 | where    | while    | yield    |          |          |
165
166
167 Each of these keywords has special meaning in its grammar, and all of them are
168 excluded from the `ident` rule.
169
170 Not all of these keywords are used by the language. Some of them were used
171 before Rust 1.0, and were left reserved once their implementations were
172 removed. Some of them were reserved before 1.0 to make space for possible
173 future features.
174
175 ### Literals
176
177 ```antlr
178 lit_suffix : ident;
179 literal : [ string_lit | char_lit | byte_string_lit | byte_lit | num_lit | bool_lit ] lit_suffix ?;
180 ```
181
182 The optional `lit_suffix` production is only used for certain numeric literals,
183 but is reserved for future extension. That is, the above gives the lexical
184 grammar, but a Rust parser will reject everything but the 12 special cases
185 mentioned in [Number literals](reference/tokens.html#number-literals) in the
186 reference.
187
188 #### Character and string literals
189
190 ```antlr
191 char_lit : '\x27' char_body '\x27' ;
192 string_lit : '"' string_body * '"' | 'r' raw_string ;
193
194 char_body : non_single_quote
195           | '\x5c' [ '\x27' | common_escape | unicode_escape ] ;
196
197 string_body : non_double_quote
198             | '\x5c' [ '\x22' | common_escape | unicode_escape ] ;
199 raw_string : '"' raw_string_body '"' | '#' raw_string '#' ;
200
201 common_escape : '\x5c'
202               | 'n' | 'r' | 't' | '0'
203               | 'x' hex_digit 2
204 unicode_escape : 'u' '{' hex_digit+ 6 '}';
205
206 hex_digit : 'a' | 'b' | 'c' | 'd' | 'e' | 'f'
207           | 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
208           | dec_digit ;
209 oct_digit : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' ;
210 dec_digit : '0' | nonzero_dec ;
211 nonzero_dec: '1' | '2' | '3' | '4'
212            | '5' | '6' | '7' | '8' | '9' ;
213 ```
214
215 #### Byte and byte string literals
216
217 ```antlr
218 byte_lit : "b\x27" byte_body '\x27' ;
219 byte_string_lit : "b\x22" string_body * '\x22' | "br" raw_byte_string ;
220
221 byte_body : ascii_non_single_quote
222           | '\x5c' [ '\x27' | common_escape ] ;
223
224 byte_string_body : ascii_non_double_quote
225             | '\x5c' [ '\x22' | common_escape ] ;
226 raw_byte_string : '"' raw_byte_string_body '"' | '#' raw_byte_string '#' ;
227
228 ```
229
230 #### Number literals
231
232 ```antlr
233 num_lit : nonzero_dec [ dec_digit | '_' ] * float_suffix ?
234         | '0' [       [ dec_digit | '_' ] * float_suffix ?
235               | 'b'   [ '1' | '0' | '_' ] +
236               | 'o'   [ oct_digit | '_' ] +
237               | 'x'   [ hex_digit | '_' ] +  ] ;
238
239 float_suffix : [ exponent | '.' dec_lit exponent ? ] ? ;
240
241 exponent : ['E' | 'e'] ['-' | '+' ] ? dec_lit ;
242 dec_lit : [ dec_digit | '_' ] + ;
243 ```
244
245 #### Boolean literals
246
247 ```antlr
248 bool_lit : [ "true" | "false" ] ;
249 ```
250
251 The two values of the boolean type are written `true` and `false`.
252
253 ### Symbols
254
255 ```antlr
256 symbol : "::" | "->"
257        | '#' | '[' | ']' | '(' | ')' | '{' | '}'
258        | ',' | ';' ;
259 ```
260
261 Symbols are a general class of printable [tokens](#tokens) that play structural
262 roles in a variety of grammar productions. They are cataloged here for
263 completeness as the set of remaining miscellaneous printable tokens that do not
264 otherwise appear as [unary operators](#unary-operator-expressions), [binary
265 operators](#binary-operator-expressions), or [keywords](#keywords).
266
267 ## Paths
268
269 ```antlr
270 expr_path : [ "::" ] ident [ "::" expr_path_tail ] + ;
271 expr_path_tail : '<' type_expr [ ',' type_expr ] + '>'
272                | expr_path ;
273
274 type_path : ident [ type_path_tail ] + ;
275 type_path_tail : '<' type_expr [ ',' type_expr ] + '>'
276                | "::" type_path ;
277 ```
278
279 # Syntax extensions
280
281 ## Macros
282
283 ```antlr
284 expr_macro_rules : "macro_rules" '!' ident '(' macro_rule * ')' ';'
285                  | "macro_rules" '!' ident '{' macro_rule * '}' ;
286 macro_rule : '(' matcher * ')' "=>" '(' transcriber * ')' ';' ;
287 matcher : '(' matcher * ')' | '[' matcher * ']'
288         | '{' matcher * '}' | '$' ident ':' ident
289         | '$' '(' matcher * ')' sep_token? [ '*' | '+' ]
290         | non_special_token ;
291 transcriber : '(' transcriber * ')' | '[' transcriber * ']'
292             | '{' transcriber * '}' | '$' ident
293             | '$' '(' transcriber * ')' sep_token? [ '*' | '+' ]
294             | non_special_token ;
295 ```
296
297 # Crates and source files
298
299 **FIXME:** grammar? What production covers #![crate_id = "foo"] ?
300
301 # Items and attributes
302
303 **FIXME:** grammar?
304
305 ## Items
306
307 ```antlr
308 item : vis ? mod_item | fn_item | type_item | struct_item | enum_item
309      | const_item | static_item | trait_item | impl_item | extern_block_item ;
310 ```
311
312 ### Type Parameters
313
314 **FIXME:** grammar?
315
316 ### Modules
317
318 ```antlr
319 mod_item : "mod" ident ( ';' | '{' mod '}' );
320 mod : [ view_item | item ] * ;
321 ```
322
323 #### View items
324
325 ```antlr
326 view_item : extern_crate_decl | use_decl ';' ;
327 ```
328
329 ##### Extern crate declarations
330
331 ```antlr
332 extern_crate_decl : "extern" "crate" crate_name
333 crate_name: ident | ( ident "as" ident )
334 ```
335
336 ##### Use declarations
337
338 ```antlr
339 use_decl : vis ? "use" [ path "as" ident
340                         | path_glob ] ;
341
342 path_glob : ident [ "::" [ path_glob
343                           | '*' ] ] ?
344           | '{' path_item [ ',' path_item ] * '}' ;
345
346 path_item : ident | "self" ;
347 ```
348
349 ### Functions
350
351 **FIXME:** grammar?
352
353 #### Generic functions
354
355 **FIXME:** grammar?
356
357 #### Unsafety
358
359 **FIXME:** grammar?
360
361 ##### Unsafe functions
362
363 **FIXME:** grammar?
364
365 ##### Unsafe blocks
366
367 **FIXME:** grammar?
368
369 #### Diverging functions
370
371 **FIXME:** grammar?
372
373 ### Type definitions
374
375 **FIXME:** grammar?
376
377 ### Structures
378
379 **FIXME:** grammar?
380
381 ### Enumerations
382
383 **FIXME:** grammar?
384
385 ### Constant items
386
387 ```antlr
388 const_item : "const" ident ':' type '=' expr ';' ;
389 ```
390
391 ### Static items
392
393 ```antlr
394 static_item : "static" ident ':' type '=' expr ';' ;
395 ```
396
397 #### Mutable statics
398
399 **FIXME:** grammar?
400
401 ### Traits
402
403 **FIXME:** grammar?
404
405 ### Implementations
406
407 **FIXME:** grammar?
408
409 ### External blocks
410
411 ```antlr
412 extern_block_item : "extern" '{' extern_block '}' ;
413 extern_block : [ foreign_fn ] * ;
414 ```
415
416 ## Visibility and Privacy
417
418 ```antlr
419 vis : "pub" ;
420 ```
421 ### Re-exporting and Visibility
422
423 See [Use declarations](#use-declarations).
424
425 ## Attributes
426
427 ```antlr
428 attribute : '#' '!' ? '[' meta_item ']' ;
429 meta_item : ident [ '=' literal
430                   | '(' meta_seq ')' ] ? ;
431 meta_seq : meta_item [ ',' meta_seq ] ? ;
432 ```
433
434 # Statements and expressions
435
436 ## Statements
437
438 ```antlr
439 stmt : decl_stmt | expr_stmt | ';' ;
440 ```
441
442 ### Declaration statements
443
444 ```antlr
445 decl_stmt : item | let_decl ;
446 ```
447
448 #### Item declarations
449
450 See [Items](#items).
451
452 #### Variable declarations
453
454 ```antlr
455 let_decl : "let" pat [':' type ] ? [ init ] ? ';' ;
456 init : [ '=' ] expr ;
457 ```
458
459 ### Expression statements
460
461 ```antlr
462 expr_stmt : expr ';' ;
463 ```
464
465 ## Expressions
466
467 ```antlr
468 expr : literal | path | tuple_expr | unit_expr | struct_expr
469      | block_expr | method_call_expr | field_expr | array_expr
470      | idx_expr | range_expr | unop_expr | binop_expr
471      | paren_expr | call_expr | lambda_expr | while_expr
472      | loop_expr | break_expr | continue_expr | for_expr
473      | if_expr | match_expr | if_let_expr | while_let_expr
474      | return_expr ;
475 ```
476
477 #### Lvalues, rvalues and temporaries
478
479 **FIXME:** grammar?
480
481 #### Moved and copied types
482
483 **FIXME:** Do we want to capture this in the grammar as different productions?
484
485 ### Literal expressions
486
487 See [Literals](#literals).
488
489 ### Path expressions
490
491 See [Paths](#paths).
492
493 ### Tuple expressions
494
495 ```antlr
496 tuple_expr : '(' [ expr [ ',' expr ] * | expr ',' ] ? ')' ;
497 ```
498
499 ### Unit expressions
500
501 ```antlr
502 unit_expr : "()" ;
503 ```
504
505 ### Structure expressions
506
507 ```antlr
508 struct_expr_field_init : ident | ident ':' expr ;
509 struct_expr : expr_path '{' struct_expr_field_init
510                       [ ',' struct_expr_field_init ] *
511                       [ ".." expr ] '}' |
512               expr_path '(' expr
513                       [ ',' expr ] * ')' |
514               expr_path ;
515 ```
516
517 ### Block expressions
518
519 ```antlr
520 block_expr : '{' [ stmt | item ] *
521                  [ expr ] '}' ;
522 ```
523
524 ### Method-call expressions
525
526 ```antlr
527 method_call_expr : expr '.' ident paren_expr_list ;
528 ```
529
530 ### Field expressions
531
532 ```antlr
533 field_expr : expr '.' ident ;
534 ```
535
536 ### Array expressions
537
538 ```antlr
539 array_expr : '[' "mut" ? array_elems? ']' ;
540
541 array_elems : [expr [',' expr]*] | [expr ';' expr] ;
542 ```
543
544 ### Index expressions
545
546 ```antlr
547 idx_expr : expr '[' expr ']' ;
548 ```
549
550 ### Range expressions
551
552 ```antlr
553 range_expr : expr ".." expr |
554              expr ".." |
555              ".." expr |
556              ".." ;
557 ```
558
559 ### Unary operator expressions
560
561 ```antlr
562 unop_expr : unop expr ;
563 unop : '-' | '*' | '!' ;
564 ```
565
566 ### Binary operator expressions
567
568 ```antlr
569 binop_expr : expr binop expr | type_cast_expr
570            | assignment_expr | compound_assignment_expr ;
571 binop : arith_op | bitwise_op | lazy_bool_op | comp_op
572 ```
573
574 #### Arithmetic operators
575
576 ```antlr
577 arith_op : '+' | '-' | '*' | '/' | '%' ;
578 ```
579
580 #### Bitwise operators
581
582 ```antlr
583 bitwise_op : '&' | '|' | '^' | "<<" | ">>" ;
584 ```
585
586 #### Lazy boolean operators
587
588 ```antlr
589 lazy_bool_op : "&&" | "||" ;
590 ```
591
592 #### Comparison operators
593
594 ```antlr
595 comp_op : "==" | "!=" | '<' | '>' | "<=" | ">=" ;
596 ```
597
598 #### Type cast expressions
599
600 ```antlr
601 type_cast_expr : value "as" type ;
602 ```
603
604 #### Assignment expressions
605
606 ```antlr
607 assignment_expr : expr '=' expr ;
608 ```
609
610 #### Compound assignment expressions
611
612 ```antlr
613 compound_assignment_expr : expr [ arith_op | bitwise_op ] '=' expr ;
614 ```
615
616 ### Grouped expressions
617
618 ```antlr
619 paren_expr : '(' expr ')' ;
620 ```
621
622 ### Call expressions
623
624 ```antlr
625 expr_list : [ expr [ ',' expr ]* ] ? ;
626 paren_expr_list : '(' expr_list ')' ;
627 call_expr : expr paren_expr_list ;
628 ```
629
630 ### Lambda expressions
631
632 ```antlr
633 ident_list : [ ident [ ',' ident ]* ] ? ;
634 lambda_expr : '|' ident_list '|' expr ;
635 ```
636
637 ### While loops
638
639 ```antlr
640 while_expr : [ lifetime ':' ] ? "while" no_struct_literal_expr '{' block '}' ;
641 ```
642
643 ### Infinite loops
644
645 ```antlr
646 loop_expr : [ lifetime ':' ] ? "loop" '{' block '}';
647 ```
648
649 ### Break expressions
650
651 ```antlr
652 break_expr : "break" [ lifetime ] ?;
653 ```
654
655 ### Continue expressions
656
657 ```antlr
658 continue_expr : "continue" [ lifetime ] ?;
659 ```
660
661 ### For expressions
662
663 ```antlr
664 for_expr : [ lifetime ':' ] ? "for" pat "in" no_struct_literal_expr '{' block '}' ;
665 ```
666
667 ### If expressions
668
669 ```antlr
670 if_expr : "if" no_struct_literal_expr '{' block '}'
671           else_tail ? ;
672
673 else_tail : "else" [ if_expr | if_let_expr
674                    | '{' block '}' ] ;
675 ```
676
677 ### Match expressions
678
679 ```antlr
680 match_expr : "match" no_struct_literal_expr '{' match_arm * '}' ;
681
682 match_arm : attribute * match_pat "=>" [ expr "," | '{' block '}' ] ;
683
684 match_pat : pat [ '|' pat ] * [ "if" expr ] ? ;
685 ```
686
687 ### If let expressions
688
689 ```antlr
690 if_let_expr : "if" "let" pat '=' expr '{' block '}'
691                else_tail ? ;
692 ```
693
694 ### While let loops
695
696 ```antlr
697 while_let_expr : [ lifetime ':' ] ? "while" "let" pat '=' expr '{' block '}' ;
698 ```
699
700 ### Return expressions
701
702 ```antlr
703 return_expr : "return" expr ? ;
704 ```
705
706 # Type system
707
708 **FIXME:** is this entire chapter relevant here? Or should it all have been covered by some production already?
709
710 ## Types
711
712 ### Primitive types
713
714 **FIXME:** grammar?
715
716 #### Machine types
717
718 **FIXME:** grammar?
719
720 #### Machine-dependent integer types
721
722 **FIXME:** grammar?
723
724 ### Textual types
725
726 **FIXME:** grammar?
727
728 ### Tuple types
729
730 **FIXME:** grammar?
731
732 ### Array, and Slice types
733
734 **FIXME:** grammar?
735
736 ### Structure types
737
738 **FIXME:** grammar?
739
740 ### Enumerated types
741
742 **FIXME:** grammar?
743
744 ### Pointer types
745
746 **FIXME:** grammar?
747
748 ### Function types
749
750 **FIXME:** grammar?
751
752 ### Closure types
753
754 ```antlr
755 closure_type := [ 'unsafe' ] [ '<' lifetime-list '>' ] '|' arg-list '|'
756                 [ ':' bound-list ] [ '->' type ]
757 lifetime-list := lifetime | lifetime ',' lifetime-list
758 arg-list := ident ':' type | ident ':' type ',' arg-list
759 ```
760
761 ### Never type
762 An empty type
763
764 ```antlr
765 never_type : "!" ;
766 ```
767
768 ### Object types
769
770 **FIXME:** grammar?
771
772 ### Type parameters
773
774 **FIXME:** grammar?
775
776 ### Type parameter bounds
777
778 ```antlr
779 bound-list := bound | bound '+' bound-list '+' ?
780 bound := ty_bound | lt_bound
781 lt_bound := lifetime
782 ty_bound := ty_bound_noparen | (ty_bound_noparen)
783 ty_bound_noparen := [?] [ for<lt_param_defs> ] simple_path
784 ```
785
786 ### Self types
787
788 **FIXME:** grammar?
789
790 ## Type kinds
791
792 **FIXME:** this is probably not relevant to the grammar...
793
794 # Memory and concurrency models
795
796 **FIXME:** is this entire chapter relevant here? Or should it all have been covered by some production already?
797
798 ## Memory model
799
800 ### Memory allocation and lifetime
801
802 ### Memory ownership
803
804 ### Variables
805
806 ### Boxes
807
808 ## Threads
809
810 ### Communication between threads
811
812 ### Thread lifecycle