Skip to content

Commit

Permalink
Auto merge of #25337 - bleibig:update-grammar, r=nikomatsakis
Browse files Browse the repository at this point in the history
* Correctly lex CRLF in string literals
* Update `extern CRATE as NAME` syntax
* Allow leading `::` in view paths
* Allow TySums in type ascriptions and impls
* Allow macros to have visibility and attributes
* Update syntax for qualified path types and expressions
* Allow block expressions to be called () and indexed []
  • Loading branch information
bors committed May 18, 2015
2 parents 4daa62a + 568d7f1 commit 2e7d7bc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/grammar/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ r/# {
<str>\x22 { BEGIN(suffix); return LIT_STR; }

<str><<EOF>> { return -1; }
<str>\\[n\nrt\\\x27\x220] { yymore(); }
<str>\\[n\nr\rt\\\x27\x220] { yymore(); }
<str>\\x[0-9a-fA-F]{2} { yymore(); }
<str>\\u\{[0-9a-fA-F]?{6}\} { yymore(); }
<str>\\[^n\nrt\\\x27\x220] { return -1; }
Expand Down
55 changes: 33 additions & 22 deletions src/grammar/parser-lalr.y
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,16 @@ item_const
;

item_macro
: path_expr '!' maybe_ident parens_delimited_token_trees ';'
| path_expr '!' maybe_ident braces_delimited_token_trees
| path_expr '!' maybe_ident brackets_delimited_token_trees ';'
: path_expr '!' maybe_ident parens_delimited_token_trees ';' { $$ = mk_node("ItemMacro", 3, $1, $3, $4); }
| path_expr '!' maybe_ident braces_delimited_token_trees { $$ = mk_node("ItemMacro", 3, $1, $3, $4); }
| path_expr '!' maybe_ident brackets_delimited_token_trees ';'{ $$ = mk_node("ItemMacro", 3, $1, $3, $4); }
;

view_item
: use_item
| extern_fn_item
| EXTERN CRATE ident ';' { $$ = mk_node("ViewItemExternCrate", 1, $3); }
| EXTERN CRATE str AS ident ';' { $$ = mk_node("ViewItemExternCrate", 2, $3, $5); }
| EXTERN CRATE ident AS ident ';' { $$ = mk_node("ViewItemExternCrate", 2, $3, $5); }
;

extern_fn_item
Expand All @@ -312,8 +312,11 @@ use_item
view_path
: path_no_types_allowed { $$ = mk_node("ViewPathSimple", 1, $1); }
| path_no_types_allowed MOD_SEP '{' '}' { $$ = mk_node("ViewPathList", 2, $1, mk_atom("ViewPathListEmpty")); }
| MOD_SEP '{' '}' { $$ = mk_node("ViewPathList", 1, mk_atom("ViewPathListEmpty")); }
| path_no_types_allowed MOD_SEP '{' idents_or_self '}' { $$ = mk_node("ViewPathList", 2, $1, $4); }
| MOD_SEP '{' idents_or_self '}' { $$ = mk_node("ViewPathList", 1, $3); }
| path_no_types_allowed MOD_SEP '{' idents_or_self ',' '}' { $$ = mk_node("ViewPathList", 2, $1, $4); }
| MOD_SEP '{' idents_or_self ',' '}' { $$ = mk_node("ViewPathList", 1, $3); }
| path_no_types_allowed MOD_SEP '*' { $$ = mk_node("ViewPathGlob", 1, $1); }
| '{' '}' { $$ = mk_atom("ViewPathListEmpty"); }
| '{' idents_or_self '}' { $$ = mk_node("ViewPathList", 1, $2); }
Expand All @@ -333,7 +336,7 @@ block_item
;

maybe_ty_ascription
: ':' ty { $$ = $2; }
: ':' ty_sum { $$ = $2; }
| %empty { $$ = mk_none(); }
;

Expand Down Expand Up @@ -511,7 +514,7 @@ trait_item
;

trait_const
: maybe_outer_attrs CONST ident maybe_const_default ';' { $$ = mk_node("ConstTraitItem", 3, $1, $3, $4); }
: maybe_outer_attrs CONST ident maybe_ty_ascription maybe_const_default ';' { $$ = mk_node("ConstTraitItem", 4, $1, $3, $4, $5); }
;

maybe_const_default
Expand Down Expand Up @@ -590,11 +593,11 @@ item_impl
{
$$ = mk_node("ItemImpl", 6, $1, $3, 5, $6, $9, $10);
}
| maybe_unsafe IMPL generic_params trait_ref FOR ty maybe_where_clause '{' maybe_inner_attrs maybe_impl_items '}'
| maybe_unsafe IMPL generic_params trait_ref FOR ty_sum maybe_where_clause '{' maybe_inner_attrs maybe_impl_items '}'
{
$$ = mk_node("ItemImpl", 6, $3, $4, $6, $7, $9, $10);
}
| maybe_unsafe IMPL generic_params '!' trait_ref FOR ty maybe_where_clause '{' maybe_inner_attrs maybe_impl_items '}'
| maybe_unsafe IMPL generic_params '!' trait_ref FOR ty_sum maybe_where_clause '{' maybe_inner_attrs maybe_impl_items '}'
{
$$ = mk_node("ItemImplNeg", 7, $1, $3, $5, $7, $8, $10, $11);
}
Expand All @@ -620,7 +623,7 @@ impl_items

impl_item
: impl_method
| item_macro
| attrs_and_vis item_macro { $$ = mk_node("ImplMacroItem", 2, $1, $2); }
| impl_const
| impl_type
;
Expand Down Expand Up @@ -698,7 +701,7 @@ params
;

param
: pat ':' ty { $$ = mk_node("Arg", 2, $1, $3); }
: pat ':' ty_sum { $$ = mk_node("Arg", 2, $1, $3); }
;

inferrable_params
Expand Down Expand Up @@ -909,6 +912,11 @@ pat
| ident '@' pat { $$ = mk_node("PatIdent", 3, mk_node("BindByValue", 1, mk_atom("MutImmutable")), $1, $3); }
| binding_mode ident '@' pat { $$ = mk_node("PatIdent", 3, $1, $2, $4); }
| BOX pat { $$ = mk_node("PatUniq", 1, $2); }
| '<' ty_sum maybe_as_trait_ref '>' MOD_SEP ident { $$ = mk_node("PatQualifiedPath", 3, $2, $3, $6); }
| SHL ty_sum maybe_as_trait_ref '>' MOD_SEP ident maybe_as_trait_ref '>' MOD_SEP ident
{
$$ = mk_node("PatQualifiedPath", 3, mk_node("PatQualifiedPath", 3, $2, $3, $6), $7, $10);
}
;

pats_or
Expand Down Expand Up @@ -981,11 +989,11 @@ pat_vec_elts
ty
: ty_prim
| ty_closure
| '<' ty_sum AS trait_ref '>' MOD_SEP ident { $$ = mk_node("TyQualifiedPath", 3, $2, $4, $7); }
| SHL ty_sum AS trait_ref '>' MOD_SEP ident AS trait_ref '>' MOD_SEP ident { $$ = mk_node("TyQualifiedPath", 3, mk_node("TyQualifiedPath", 3, $2, $4, $7), $9, $12); }
| '(' ty_sums ')' { $$ = mk_node("TyTup", 1, $2); }
| '(' ty_sums ',' ')' { $$ = mk_node("TyTup", 1, $2); }
| '(' ')' { $$ = mk_atom("TyNil"); }
| '<' ty_sum maybe_as_trait_ref '>' MOD_SEP ident { $$ = mk_node("TyQualifiedPath", 3, $2, $3, $6); }
| SHL ty_sum maybe_as_trait_ref '>' MOD_SEP ident maybe_as_trait_ref '>' MOD_SEP ident { $$ = mk_node("TyQualifiedPath", 3, mk_node("TyQualifiedPath", 3, $2, $3, $6), $7, $10); }
| '(' ty_sums ')' { $$ = mk_node("TyTup", 1, $2); }
| '(' ty_sums ',' ')' { $$ = mk_node("TyTup", 1, $2); }
| '(' ')' { $$ = mk_atom("TyNil"); }
;

ty_prim
Expand Down Expand Up @@ -1551,11 +1559,7 @@ nonblock_prefix_expr
;

expr_qualified_path
: '<' ty_sum maybe_as_trait_ref '>' MOD_SEP ident
{
$$ = mk_node("ExprQualifiedPath", 3, $2, $3, $6);
}
| '<' ty_sum maybe_as_trait_ref '>' MOD_SEP ident generic_args
: '<' ty_sum maybe_as_trait_ref '>' MOD_SEP ident maybe_qpath_params
{
$$ = mk_node("ExprQualifiedPath", 4, $2, $3, $6, $7);
}
Expand All @@ -1576,6 +1580,11 @@ expr_qualified_path
$$ = mk_node("ExprQualifiedPath", 4, mk_node("ExprQualifiedPath", 4, $2, $3, $6, $7), $8, $11, $12);
}

maybe_qpath_params
: MOD_SEP generic_args { $$ = $2; }
| %empty { $$ = mk_none(); }
;

maybe_as_trait_ref
: AS trait_ref { $$ = $2; }
| %empty { $$ = mk_none(); }
Expand Down Expand Up @@ -1666,8 +1675,10 @@ block_expr

full_block_expr
: block_expr
| full_block_expr '.' path_generic_args_with_colons { $$ = mk_node("ExprField", 2, $1, $3); }
| full_block_expr '.' LIT_INTEGER { $$ = mk_node("ExprTupleIndex", 1, $1); }
| full_block_expr '.' path_generic_args_with_colons %prec IDENT { $$ = mk_node("ExprField", 2, $1, $3); }
| full_block_expr '.' path_generic_args_with_colons '[' maybe_expr ']' { $$ = mk_node("ExprIndex", 3, $1, $3, $5); }
| full_block_expr '.' path_generic_args_with_colons '(' maybe_exprs ')' { $$ = mk_node("ExprCall", 3, $1, $3, $5); }
| full_block_expr '.' LIT_INTEGER { $$ = mk_node("ExprTupleIndex", 1, $1); }
;

expr_match
Expand Down

0 comments on commit 2e7d7bc

Please sign in to comment.