From 775ccadd255a034745e2b741434bd6a159a10869 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 12 Dec 2013 10:42:03 -0800 Subject: [PATCH] libsyntax: Implement the new `box` syntax for unique pointers. --- src/libsyntax/parse/parser.rs | 16 ++++++++++++++++ src/libsyntax/parse/token.rs | 16 +++++++++------- src/test/run-pass/new-box-syntax.rs | 8 ++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 src/test/run-pass/new-box-syntax.rs diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index aa37d859d797..33e3bae99a75 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2326,6 +2326,22 @@ impl Parser { _ => self.mk_unary(UnUniq, e) }; } + token::IDENT(_, _) if self.is_keyword(keywords::Box) => { + self.bump(); + + let subexpression = self.parse_prefix_expr(); + hi = subexpression.span.hi; + // HACK: turn `box [...]` into a boxed-evec + ex = match subexpression.node { + ExprVec(..) | + ExprLit(@codemap::Spanned { + node: lit_str(..), + span: _ + }) | + ExprRepeat(..) => ExprVstore(subexpression, ExprVstoreUniq), + _ => self.mk_unary(UnUniq, subexpression) + }; + } _ => return self.parse_dot_or_call_expr() } return self.mk_expr(lo, hi, ex); diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index a49f423c4087..9e1eec19b2c4 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -465,15 +465,17 @@ declare_special_idents_and_keywords! { (45, While, "while"); (46, Continue, "continue"); (47, Proc, "proc"); + (48, Box, "box"); 'reserved: - (48, Alignof, "alignof"); - (49, Be, "be"); - (50, Offsetof, "offsetof"); - (51, Pure, "pure"); - (52, Sizeof, "sizeof"); - (53, Typeof, "typeof"); - (54, Yield, "yield"); + (49, Alignof, "alignof"); + (50, Be, "be"); + (51, Offsetof, "offsetof"); + (52, Pure, "pure"); + (53, Sizeof, "sizeof"); + (54, Typeof, "typeof"); + (55, Unsized, "unsized"); + (56, Yield, "yield"); } } diff --git a/src/test/run-pass/new-box-syntax.rs b/src/test/run-pass/new-box-syntax.rs new file mode 100644 index 000000000000..d5fd333411b8 --- /dev/null +++ b/src/test/run-pass/new-box-syntax.rs @@ -0,0 +1,8 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +fn main() { + let x: ~int = box 3; + println!("{}", *x); +} +