From c9c8ff1762b6761b738380ba57c939d515eab50c Mon Sep 17 00:00:00 2001 From: Carol Nichols Date: Tue, 5 May 2015 20:52:34 -0400 Subject: [PATCH 1/7] Add visibility section of the grammar Namely an optional "pub" before any item. The "pub" in the Use declaration section should use this too. --- src/doc/grammar.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/doc/grammar.md b/src/doc/grammar.md index 4897b27dec35d..e14300dcd5dd7 100644 --- a/src/doc/grammar.md +++ b/src/doc/grammar.md @@ -304,7 +304,7 @@ transcriber : '(' transcriber * ')' | '[' transcriber * ']' ## Items ```antlr -item : mod_item | fn_item | type_item | struct_item | enum_item +item : vis ? mod_item | fn_item | type_item | struct_item | enum_item | const_item | static_item | trait_item | impl_item | extern_block ; ``` @@ -335,8 +335,8 @@ crate_name: ident | ( ident "as" ident ) ##### Use declarations ```antlr -use_decl : "pub" ? "use" [ path "as" ident - | path_glob ] ; +use_decl : vis ? "use" [ path "as" ident + | path_glob ] ; path_glob : ident [ "::" [ path_glob | '*' ] ] ? @@ -414,8 +414,9 @@ extern_block : [ foreign_fn ] * ; ## Visibility and Privacy -**FIXME:** grammar? - +```antlr +vis : "pub" ; +``` ### Re-exporting and Visibility **FIXME:** grammar? From a5c651cfdfe78d464653d63cc5b6496f50c2fab0 Mon Sep 17 00:00:00 2001 From: Carol Nichols Date: Tue, 5 May 2015 21:06:17 -0400 Subject: [PATCH 2/7] Point to the use declaration section from the re-exporting section The syntax for re-exporting privacy is covered in the use declaration item. --- src/doc/grammar.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/grammar.md b/src/doc/grammar.md index e14300dcd5dd7..6a2ad874513f1 100644 --- a/src/doc/grammar.md +++ b/src/doc/grammar.md @@ -419,7 +419,7 @@ vis : "pub" ; ``` ### Re-exporting and Visibility -**FIXME:** grammar? +See [Use declarations](#use-declarations). ## Attributes From ab913c881cfd559591fcde3ca1c82c784c5aab87 Mon Sep 17 00:00:00 2001 From: Carol Nichols Date: Tue, 5 May 2015 21:52:21 -0400 Subject: [PATCH 3/7] Fill in grammar for Statements Some of this text is duplicated in the reference (and belongs there) so remove it. It says item grammar is the same, so point to that grammar section. --- src/doc/grammar.md | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/doc/grammar.md b/src/doc/grammar.md index 6a2ad874513f1..e606b282f5c21 100644 --- a/src/doc/grammar.md +++ b/src/doc/grammar.md @@ -434,26 +434,19 @@ meta_seq : meta_item [ ',' meta_seq ] ? ; ## Statements -**FIXME:** grammar? +```antlr +stmt : decl_stmt | expr_stmt ; +``` ### Declaration statements -**FIXME:** grammar? - -A _declaration statement_ is one that introduces one or more *names* into the -enclosing statement block. The declared names may denote new variables or new -items. +```antlr +decl_stmt : item | let_decl ; +``` #### Item declarations -**FIXME:** grammar? - -An _item declaration statement_ has a syntactic form identical to an -[item](#items) declaration within a module. Declaring an item — a -function, enumeration, structure, type, static, trait, implementation or module -— locally within a statement block is simply a way of restricting its -scope to a narrow region containing all of its uses; it is otherwise identical -in meaning to declaring the item outside the statement block. +See [Items](#items). #### Variable declarations @@ -464,7 +457,9 @@ init : [ '=' ] expr ; ### Expression statements -**FIXME:** grammar? +```antlr +expr_stmt : expr ';' ; +``` ## Expressions From e607d76564cf01247b247e0e83c7abdc4bb1fbee Mon Sep 17 00:00:00 2001 From: Carol Nichols Date: Tue, 5 May 2015 22:32:20 -0400 Subject: [PATCH 4/7] Fill in more parts of the grammar for Expressions --- src/doc/grammar.md | 59 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/src/doc/grammar.md b/src/doc/grammar.md index e606b282f5c21..a752ce47b1ba8 100644 --- a/src/doc/grammar.md +++ b/src/doc/grammar.md @@ -463,7 +463,15 @@ expr_stmt : expr ';' ; ## Expressions -**FIXME:** grammar? +```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 ; +``` #### Lvalues, rvalues and temporaries @@ -475,19 +483,23 @@ expr_stmt : expr ';' ; ### Literal expressions -**FIXME:** grammar? +See [Literals](#literals). ### Path expressions -**FIXME:** grammar? +See [Paths](#paths). ### Tuple expressions -**FIXME:** grammar? +```antlr +tuple_expr : '(' [ expr [ ',' expr ] * | expr ',' ] ? ')' ; +``` ### Unit expressions -**FIXME:** grammar? +```antlr +unit_expr : "()" ; +``` ### Structure expressions @@ -545,41 +557,60 @@ range_expr : expr ".." expr | ### Unary operator expressions -**FIXME:** grammar? +```antlr +unop_expr : unop expr ; +unop : '-' | '*' | '!' ; +``` ### Binary operator expressions ```antlr -binop_expr : expr binop expr ; +binop_expr : expr binop expr | type_cast_expr + | assignment_expr | compound_assignment_expr ; +binop : arith_op | bitwise_op | lazy_bool_op | comp_op ``` #### Arithmetic operators -**FIXME:** grammar? +```antlr +arith_op : '+' | '-' | '*' | '/' | '%' ; +``` #### Bitwise operators -**FIXME:** grammar? +```antlr +bitwise_op : '&' | '|' | '^' | "<<" | ">>" ; +``` #### Lazy boolean operators -**FIXME:** grammar? +```antlr +lazy_bool_op : "&&" | "||" ; +``` #### Comparison operators -**FIXME:** grammar? +```antlr +comp_op : "==" | "!=" | '<' | '>' | "<=" | ">=" ; +``` #### Type cast expressions -**FIXME:** grammar? +```antlr +type_cast_expr : value "as" type ; +``` #### Assignment expressions -**FIXME:** grammar? +```antlr +assignment_expr : expr '=' expr ; +``` #### Compound assignment expressions -**FIXME:** grammar? +```antlr +compound_assignment_expr : expr [ arith_op | bitwise_op ] '=' expr ; +``` #### Operator precedence From c3156a8ff434060403a1743cff780d8e80a53db2 Mon Sep 17 00:00:00 2001 From: Carol Nichols Date: Tue, 5 May 2015 22:32:38 -0400 Subject: [PATCH 5/7] Remove operator precedence section covered in the reference --- src/doc/grammar.md | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/src/doc/grammar.md b/src/doc/grammar.md index a752ce47b1ba8..3cecc28b80360 100644 --- a/src/doc/grammar.md +++ b/src/doc/grammar.md @@ -612,30 +612,6 @@ assignment_expr : expr '=' expr ; compound_assignment_expr : expr [ arith_op | bitwise_op ] '=' expr ; ``` -#### Operator precedence - -The precedence of Rust binary operators is ordered as follows, going from -strong to weak: - -```text -* / % -as -+ - -<< >> -& -^ -| -< > <= >= -== != -&& -|| -= -``` - -Operators at the same precedence level are evaluated left-to-right. [Unary -operators](#unary-operator-expressions) have the same precedence level and it -is stronger than any of the binary operators'. - ### Grouped expressions ```antlr From 53cd0bc772c5b73d8059a02f828ceecf9ed2287c Mon Sep 17 00:00:00 2001 From: Carol Nichols Date: Sun, 10 May 2015 20:48:02 -0400 Subject: [PATCH 6/7] Add literal semicolon to the grammar of view_item Both external crate declarations and use declarations need to end with a semicolon. --- src/doc/grammar.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/grammar.md b/src/doc/grammar.md index 3cecc28b80360..a3325b82a9dab 100644 --- a/src/doc/grammar.md +++ b/src/doc/grammar.md @@ -322,7 +322,7 @@ mod : [ view_item | item ] * ; #### View items ```antlr -view_item : extern_crate_decl | use_decl ; +view_item : extern_crate_decl | use_decl ';' ; ``` ##### Extern crate declarations From 218d38fb94379feca89eb858b309890d513c35da Mon Sep 17 00:00:00 2001 From: Carol Nichols Date: Sun, 10 May 2015 22:17:33 -0400 Subject: [PATCH 7/7] Overwrite grammar sections with what was removed from the reference Between ffc5f1c, when grammar.md was created by copying parts of the reference, and 8cf2552, when all EBNF was removed from reference.md, there were parts of the grammar that were updated in reference.md but not grammar.md, and then they weren't copied over because they existed already, but they were slightly out of date. Example: the `path_item : ident | "self" ;` rule in Use declarations was changed from "mod" to "self" in the reference in 195fd9a but wasn't updated in the grammar. --- src/doc/grammar.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/doc/grammar.md b/src/doc/grammar.md index a3325b82a9dab..fb7562e7bdf8b 100644 --- a/src/doc/grammar.md +++ b/src/doc/grammar.md @@ -253,7 +253,7 @@ The two values of the boolean type are written `true` and `false`. ### Symbols ```antlr -symbol : "::" "->" +symbol : "::" | "->" | '#' | '[' | ']' | '(' | ')' | '{' | '}' | ',' | ';' ; ``` @@ -342,7 +342,7 @@ path_glob : ident [ "::" [ path_glob | '*' ] ] ? | '{' path_item [ ',' path_item ] * '}' ; -path_item : ident | "mod" ; +path_item : ident | "self" ; ``` ### Functions @@ -424,7 +424,7 @@ See [Use declarations](#use-declarations). ## Attributes ```antlr -attribute : "#!" ? '[' meta_item ']' ; +attribute : '#' '!' ? '[' meta_item ']' ; meta_item : ident [ '=' literal | '(' meta_seq ')' ] ? ; meta_seq : meta_item [ ',' meta_seq ] ? ; @@ -515,8 +515,7 @@ struct_expr : expr_path '{' ident ':' expr ### Block expressions ```antlr -block_expr : '{' [ view_item ] * - [ stmt ';' | item ] * +block_expr : '{' [ stmt ';' | item ] * [ expr ] '}' ; ``` @@ -537,7 +536,7 @@ field_expr : expr '.' ident ; ```antlr array_expr : '[' "mut" ? array_elems? ']' ; -array_elems : [expr [',' expr]*] | [expr ',' ".." expr] ; +array_elems : [expr [',' expr]*] | [expr ';' expr] ; ``` ### Index expressions