about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Blum <bblum@andrew.cmu.edu>2013-08-22 17:00:02 -0400
committerBen Blum <bblum@andrew.cmu.edu>2013-08-23 17:24:14 -0400
commite9f6f3f2ccf563b39f7484025c8272da60c24dfa (patch)
tree42e2f6b59b1972cbfe75ec69d14aa97fbbb478a9
parent2c0f9bd35493def5e23f0f43ddeba54da9d788b4 (diff)
downloadrust-e9f6f3f2ccf563b39f7484025c8272da60c24dfa.tar.gz
rust-e9f6f3f2ccf563b39f7484025c8272da60c24dfa.zip
Parse and reserve typeof keyword. #3228
-rw-r--r--src/etc/vim/syntax/rust.vim2
-rw-r--r--src/librustc/middle/typeck/astconv.rs3
-rw-r--r--src/libsyntax/ast.rs1
-rw-r--r--src/libsyntax/fold.rs1
-rw-r--r--src/libsyntax/oldvisit.rs3
-rw-r--r--src/libsyntax/parse/parser.rs9
-rw-r--r--src/libsyntax/parse/token.rs7
-rw-r--r--src/libsyntax/print/pprust.rs5
-rw-r--r--src/libsyntax/visit.rs3
-rw-r--r--src/test/compile-fail/keyword-typeof.rs13
10 files changed, 43 insertions, 4 deletions
diff --git a/src/etc/vim/syntax/rust.vim b/src/etc/vim/syntax/rust.vim
index 260b23cb70b..b5e52939635 100644
--- a/src/etc/vim/syntax/rust.vim
+++ b/src/etc/vim/syntax/rust.vim
@@ -33,7 +33,7 @@ syn match     rustIdentifier  contains=rustIdentifierPrime "\%([^[:cntrl:][:spac
 syn match     rustFuncName    "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained
 
 " reserved
-syn keyword   rustKeyword     be
+syn keyword   rustKeyword     be yield typeof
 
 syn keyword   rustType        int uint float char bool u8 u16 u32 u64 f32
 syn keyword   rustType        f64 i8 i16 i32 i64 str Self
diff --git a/src/librustc/middle/typeck/astconv.rs b/src/librustc/middle/typeck/astconv.rs
index db76d75a5a5..62e46bdd23c 100644
--- a/src/librustc/middle/typeck/astconv.rs
+++ b/src/librustc/middle/typeck/astconv.rs
@@ -504,6 +504,9 @@ pub fn ast_ty_to_ty<AC:AstConv, RS:region_scope + Clone + 'static>(
           }
         }
       }
+      ast::ty_typeof(_e) => {
+          tcx.sess.span_bug(ast_ty.span, "typeof is reserved but unimplemented");
+      }
       ast::ty_infer => {
         // ty_infer should only appear as the type of arguments or return
         // values in a fn_expr, or as the type of local variables.  Both of
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs
index b01c19a59c1..b0c32401292 100644
--- a/src/libsyntax/ast.rs
+++ b/src/libsyntax/ast.rs
@@ -775,6 +775,7 @@ pub enum ty_ {
     ty_tup(~[Ty]),
     ty_path(Path, Option<OptVec<TyParamBound>>, NodeId), // for #7264; see above
     ty_mac(mac),
+    ty_typeof(@expr),
     // ty_infer means the type should be inferred instead of it having been
     // specified. This should only appear at the "top level" of a type and not
     // nested in one.
diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs
index 65694f013f7..2b483c2c2a9 100644
--- a/src/libsyntax/fold.rs
+++ b/src/libsyntax/fold.rs
@@ -697,6 +697,7 @@ pub fn noop_fold_ty(t: &ty_, fld: @ast_fold) -> ty_ {
                 fld.fold_expr(e)
             )
         }
+        ty_typeof(e) => ty_typeof(fld.fold_expr(e)),
         ty_mac(ref mac) => ty_mac(fold_mac(mac))
     }
 }
diff --git a/src/libsyntax/oldvisit.rs b/src/libsyntax/oldvisit.rs
index 295003c6ef5..8087efd5a8b 100644
--- a/src/libsyntax/oldvisit.rs
+++ b/src/libsyntax/oldvisit.rs
@@ -279,6 +279,9 @@ pub fn visit_ty<E:Clone>(t: &Ty, (e, v): (E, vt<E>)) {
             (v.visit_ty)(mt.ty, (e.clone(), v));
             (v.visit_expr)(ex, (e.clone(), v));
         },
+        ty_typeof(ex) => {
+            (v.visit_expr)(ex, (e.clone(), v));
+        }
         ty_nil | ty_bot | ty_mac(_) | ty_infer => ()
     }
 }
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 9aa2bf3f063..51213512a55 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -51,7 +51,7 @@ use ast::{struct_variant_kind, subtract};
 use ast::{sty_box, sty_region, sty_static, sty_uniq, sty_value};
 use ast::{token_tree, trait_method, trait_ref, tt_delim, tt_seq, tt_tok};
 use ast::{tt_nonterminal, tuple_variant_kind, Ty, ty_, ty_bot, ty_box};
-use ast::{TypeField, ty_fixed_length_vec, ty_closure, ty_bare_fn};
+use ast::{TypeField, ty_fixed_length_vec, ty_closure, ty_bare_fn, ty_typeof};
 use ast::{ty_infer, TypeMethod};
 use ast::{ty_nil, TyParam, TyParamBound, ty_path, ty_ptr, ty_rptr};
 use ast::{ty_tup, ty_u32, ty_uniq, ty_vec, uniq};
@@ -1105,6 +1105,13 @@ impl Parser {
             let result = self.parse_ty_closure(ast::BorrowedSigil, None);
             self.obsolete(*self.last_span, ObsoleteBareFnType);
             result
+        } else if self.eat_keyword(keywords::Typeof) {
+            // TYPEOF
+            // In order to not be ambiguous, the type must be surrounded by parens.
+            self.expect(&token::LPAREN);
+            let e = self.parse_expr();
+            self.expect(&token::RPAREN);
+            ty_typeof(e)
         } else if *self.token == token::MOD_SEP
             || is_ident_or_path(self.token) {
             // NAMED TYPE
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs
index 0d7def84003..8128a4e905c 100644
--- a/src/libsyntax/parse/token.rs
+++ b/src/libsyntax/parse/token.rs
@@ -478,6 +478,7 @@ fn mk_fresh_ident_interner() -> @ident_interner {
         "be",                 // 64
         "pure",               // 65
         "yield",              // 66
+        "typeof",             // 67
     ];
 
     @ident_interner {
@@ -595,6 +596,7 @@ pub mod keywords {
         True,
         Trait,
         Type,
+        Typeof,
         Unsafe,
         Use,
         While,
@@ -639,6 +641,7 @@ pub mod keywords {
                 True => ident { name: 57, ctxt: 0 },
                 Trait => ident { name: 58, ctxt: 0 },
                 Type => ident { name: 59, ctxt: 0 },
+                Typeof => ident { name: 67, ctxt: 0 },
                 Unsafe => ident { name: 60, ctxt: 0 },
                 Use => ident { name: 61, ctxt: 0 },
                 While => ident { name: 62, ctxt: 0 },
@@ -660,7 +663,7 @@ pub fn is_keyword(kw: keywords::Keyword, tok: &Token) -> bool {
 pub fn is_any_keyword(tok: &Token) -> bool {
     match *tok {
         token::IDENT(sid, false) => match sid.name {
-            8 | 27 | 32 .. 66 => true,
+            8 | 27 | 32 .. 67 => true,
             _ => false,
         },
         _ => false
@@ -680,7 +683,7 @@ pub fn is_strict_keyword(tok: &Token) -> bool {
 pub fn is_reserved_keyword(tok: &Token) -> bool {
     match *tok {
         token::IDENT(sid, false) => match sid.name {
-            64 .. 66 => true,
+            64 .. 67 => true,
             _ => false,
         },
         _ => false,
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index 3bad1ed9384..f7714d4e836 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -437,6 +437,11 @@ pub fn print_type(s: @ps, ty: &ast::Ty) {
         print_expr(s, v);
         word(s.s, "]");
       }
+      ast::ty_typeof(e) => {
+          word(s.s, "typeof(");
+          print_expr(s, e);
+          word(s.s, ")");
+      }
       ast::ty_mac(_) => {
           fail!("print_type doesn't know how to print a ty_mac");
       }
diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs
index 8178e7f3760..b78bf41bc30 100644
--- a/src/libsyntax/visit.rs
+++ b/src/libsyntax/visit.rs
@@ -314,6 +314,9 @@ pub fn walk_ty<E:Clone, V:Visitor<E>>(visitor: &mut V, typ: &Ty, env: E) {
             visitor.visit_ty(mutable_type.ty, env.clone());
             visitor.visit_expr(expression, env)
         }
+        ty_typeof(expression) => {
+            visitor.visit_expr(expression, env)
+        }
         ty_nil | ty_bot | ty_mac(_) | ty_infer => ()
     }
 }
diff --git a/src/test/compile-fail/keyword-typeof.rs b/src/test/compile-fail/keyword-typeof.rs
new file mode 100644
index 00000000000..c42875382b9
--- /dev/null
+++ b/src/test/compile-fail/keyword-typeof.rs
@@ -0,0 +1,13 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() {
+    let typeof = (); //~ ERROR `typeof` is a reserved keyword
+}