rustc: Max/min classes: Add struct literal syntax

This commit is contained in:
Patrick Walton 2012-07-23 16:39:18 -07:00
parent ee2abc1cae
commit df4db83ed8
9 changed files with 98 additions and 43 deletions

View file

@ -30,33 +30,31 @@ import ast::{_mod, add, alt_check, alt_exhaustive, arg, arm, attribute,
expr_fail, expr_field, expr_fn, expr_fn_block, expr_if,
expr_index, expr_lit, expr_log, expr_loop,
expr_loop_body, expr_mac, expr_move, expr_new, expr_path,
expr_rec, expr_ret, expr_swap, expr_tup, expr_unary, expr_vec,
expr_vstore, expr_while, extern_fn, field, fn_decl, foreign_item,
foreign_item_fn, foreign_mod, ident, impure_fn, infer,
init_assign, init_move, initializer, instance_var, item, item_,
item_class, item_const, item_enum, item_fn, item_foreign_mod,
item_impl, item_mac, item_mod, item_trait, item_ty, lit, lit_,
lit_bool, lit_float, lit_int, lit_int_unsuffixed, lit_nil,
lit_str, lit_uint, local, m_const, m_imm, m_mutbl, mac_, mac_aq,
mac_ellipsis, mac_embed_block, mac_embed_type, mac_invoc,
mac_invoc_tt, mac_var, matcher, method, mode, mt, mtc_bb,
mtc_rep, mtc_tok, mul, mutability, neg, noreturn, not, pat,
pat_box, pat_enum, pat_ident, pat_lit, pat_range, pat_rec,
pat_tup, pat_uniq, pat_wild, path, private, proto, proto_any,
proto_bare, proto_block, proto_box, proto_uniq, provided, public,
pure_fn, purity, re_anon, re_named, region, rem, required,
ret_style, return_val, shl, shr, stmt, stmt_decl, stmt_expr,
stmt_semi, subtract, token_tree, trait_method, trait_ref,
tt_delim, tt_dotdotdot, tt_flat, tt_interpolate, ty, ty_, ty_bot,
ty_box, ty_field, ty_fn,
ty_infer, ty_mac, ty_method, ty_nil, ty_param, ty_path, ty_ptr,
ty_rec, ty_rptr, ty_tup, ty_u32, ty_uniq, ty_vec,
ty_fixed_length,
unchecked_blk, uniq, unsafe_blk, unsafe_fn, variant, view_item,
view_item_, view_item_export, view_item_import, view_item_use,
view_path, view_path_glob, view_path_list, view_path_simple,
visibility, vstore, vstore_box, vstore_fixed, vstore_slice,
vstore_uniq};
expr_rec, expr_ret, expr_swap, expr_struct, expr_tup, expr_unary,
expr_vec, expr_vstore, expr_while, extern_fn, field, fn_decl,
foreign_item, foreign_item_fn, foreign_mod, ident, impure_fn,
infer, init_assign, init_move, initializer, instance_var, item,
item_, item_class, item_const, item_enum, item_fn,
item_foreign_mod, item_impl, item_mac, item_mod, item_trait,
item_ty, lit, lit_, lit_bool, lit_float, lit_int,
lit_int_unsuffixed, lit_nil, lit_str, lit_uint, local, m_const,
m_imm, m_mutbl, mac_, mac_aq, mac_ellipsis, mac_embed_block,
mac_embed_type, mac_invoc, mac_invoc_tt, mac_var, matcher,
method, mode, mt, mtc_bb, mtc_rep, mtc_tok, mul, mutability, neg,
noreturn, not, pat, pat_box, pat_enum, pat_ident, pat_lit,
pat_range, pat_rec, pat_tup, pat_uniq, pat_wild, path, private,
proto, proto_any, proto_bare, proto_block, proto_box, proto_uniq,
provided, public, pure_fn, purity, re_anon, re_named, region,
rem, required, ret_style, return_val, shl, shr, stmt, stmt_decl,
stmt_expr, stmt_semi, subtract, token_tree, trait_method,
trait_ref, tt_delim, tt_dotdotdot, tt_flat, tt_interpolate, ty,
ty_, ty_bot, ty_box, ty_field, ty_fn, ty_infer, ty_mac,
ty_method, ty_nil, ty_param, ty_path, ty_ptr, ty_rec, ty_rptr,
ty_tup, ty_u32, ty_uniq, ty_vec, ty_fixed_length, unchecked_blk,
uniq, unsafe_blk, unsafe_fn, variant, view_item, view_item_,
view_item_export, view_item_import, view_item_use, view_path,
view_path_glob, view_path_list, view_path_simple, visibility,
vstore, vstore_box, vstore_fixed, vstore_slice, vstore_uniq};
export file_type;
export parser;
@ -912,10 +910,37 @@ class parser {
let hi = self.span.hi;
ret pexpr(self.mk_mac_expr(lo, hi, mac_invoc_tt(pth, tts)));
} else {
hi = pth.span.hi;
ex = expr_path(pth);
} else if self.token == token::LBRACE {
// This might be a struct literal.
let lookahead = self.look_ahead(1);
if self.token_is_keyword(~"mut", lookahead) ||
(is_plain_ident(lookahead) &&
self.look_ahead(2) == token::COLON) {
// It's a struct literal.
self.bump();
let mut fields = ~[];
if self.is_keyword(~"mut") || is_plain_ident(self.token)
&& self.look_ahead(1) == token::COLON {
vec::push(fields, self.parse_field(token::COLON));
while self.token != token::RBRACE {
self.expect(token::COMMA);
if self.token == token::RBRACE {
// Accept an optional trailing comma.
break;
}
vec::push(fields, self.parse_field(token::COLON));
}
}
hi = pth.span.hi;
ex = expr_struct(pth, fields);
ret self.mk_pexpr(lo, hi, ex);
}
}
hi = pth.span.hi;
ex = expr_path(pth);
} else {
let lit = self.parse_lit();
hi = lit.span.hi;