]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/token.rs
auto merge of #12639 : luqmana/rust/struct-variant-pat, r=pcwalton
[rust.git] / src / libsyntax / parse / token.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use ast;
12 use ast::{P, Ident, Name, Mrk};
13 use ast_util;
14 use parse::token;
15 use util::interner::{RcStr, StrInterner};
16 use util::interner;
17
18 use serialize::{Decodable, Decoder, Encodable, Encoder};
19 use std::cast;
20 use std::char;
21 use std::fmt;
22 use std::local_data;
23 use std::path::BytesContainer;
24
25 #[allow(non_camel_case_types)]
26 #[deriving(Clone, Encodable, Decodable, Eq, Hash, Show)]
27 pub enum BinOp {
28     PLUS,
29     MINUS,
30     STAR,
31     SLASH,
32     PERCENT,
33     CARET,
34     AND,
35     OR,
36     SHL,
37     SHR,
38 }
39
40 #[allow(non_camel_case_types)]
41 #[deriving(Clone, Encodable, Decodable, Eq, Hash, Show)]
42 pub enum Token {
43     /* Expression-operator symbols. */
44     EQ,
45     LT,
46     LE,
47     EQEQ,
48     NE,
49     GE,
50     GT,
51     ANDAND,
52     OROR,
53     NOT,
54     TILDE,
55     BINOP(BinOp),
56     BINOPEQ(BinOp),
57
58     /* Structural symbols */
59     AT,
60     DOT,
61     DOTDOT,
62     DOTDOTDOT,
63     COMMA,
64     SEMI,
65     COLON,
66     MOD_SEP,
67     RARROW,
68     LARROW,
69     DARROW,
70     FAT_ARROW,
71     LPAREN,
72     RPAREN,
73     LBRACKET,
74     RBRACKET,
75     LBRACE,
76     RBRACE,
77     POUND,
78     DOLLAR,
79
80     /* Literals */
81     LIT_CHAR(u32),
82     LIT_INT(i64, ast::IntTy),
83     LIT_UINT(u64, ast::UintTy),
84     LIT_INT_UNSUFFIXED(i64),
85     LIT_FLOAT(ast::Ident, ast::FloatTy),
86     LIT_FLOAT_UNSUFFIXED(ast::Ident),
87     LIT_STR(ast::Ident),
88     LIT_STR_RAW(ast::Ident, uint), /* raw str delimited by n hash symbols */
89
90     /* Name components */
91     // an identifier contains an "is_mod_name" boolean,
92     // indicating whether :: follows this token with no
93     // whitespace in between.
94     IDENT(ast::Ident, bool),
95     UNDERSCORE,
96     LIFETIME(ast::Ident),
97
98     /* For interpolation */
99     INTERPOLATED(Nonterminal),
100
101     DOC_COMMENT(ast::Ident),
102     EOF,
103 }
104
105 #[deriving(Clone, Encodable, Decodable, Eq, Hash)]
106 /// For interpolation during macro expansion.
107 pub enum Nonterminal {
108     NtItem(@ast::Item),
109     NtBlock(P<ast::Block>),
110     NtStmt(@ast::Stmt),
111     NtPat( @ast::Pat),
112     NtExpr(@ast::Expr),
113     NtTy(  P<ast::Ty>),
114     NtIdent(~ast::Ident, bool),
115     NtAttr(@ast::Attribute), // #[foo]
116     NtPath(~ast::Path),
117     NtTT(  @ast::TokenTree), // needs @ed to break a circularity
118     NtMatchers(~[ast::Matcher])
119 }
120
121 impl fmt::Show for Nonterminal {
122     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
123         match *self {
124             NtItem(..) => f.pad("NtItem(..)"),
125             NtBlock(..) => f.pad("NtBlock(..)"),
126             NtStmt(..) => f.pad("NtStmt(..)"),
127             NtPat(..) => f.pad("NtPat(..)"),
128             NtExpr(..) => f.pad("NtExpr(..)"),
129             NtTy(..) => f.pad("NtTy(..)"),
130             NtIdent(..) => f.pad("NtIdent(..)"),
131             NtAttr(..) => f.pad("NtAttr(..)"),
132             NtPath(..) => f.pad("NtPath(..)"),
133             NtTT(..) => f.pad("NtTT(..)"),
134             NtMatchers(..) => f.pad("NtMatchers(..)"),
135         }
136     }
137 }
138
139 pub fn binop_to_str(o: BinOp) -> ~str {
140     match o {
141       PLUS => ~"+",
142       MINUS => ~"-",
143       STAR => ~"*",
144       SLASH => ~"/",
145       PERCENT => ~"%",
146       CARET => ~"^",
147       AND => ~"&",
148       OR => ~"|",
149       SHL => ~"<<",
150       SHR => ~">>"
151     }
152 }
153
154 pub fn to_str(t: &Token) -> ~str {
155     match *t {
156       EQ => ~"=",
157       LT => ~"<",
158       LE => ~"<=",
159       EQEQ => ~"==",
160       NE => ~"!=",
161       GE => ~">=",
162       GT => ~">",
163       NOT => ~"!",
164       TILDE => ~"~",
165       OROR => ~"||",
166       ANDAND => ~"&&",
167       BINOP(op) => binop_to_str(op),
168       BINOPEQ(op) => binop_to_str(op) + "=",
169
170       /* Structural symbols */
171       AT => ~"@",
172       DOT => ~".",
173       DOTDOT => ~"..",
174       DOTDOTDOT => ~"...",
175       COMMA => ~",",
176       SEMI => ~";",
177       COLON => ~":",
178       MOD_SEP => ~"::",
179       RARROW => ~"->",
180       LARROW => ~"<-",
181       DARROW => ~"<->",
182       FAT_ARROW => ~"=>",
183       LPAREN => ~"(",
184       RPAREN => ~")",
185       LBRACKET => ~"[",
186       RBRACKET => ~"]",
187       LBRACE => ~"{",
188       RBRACE => ~"}",
189       POUND => ~"#",
190       DOLLAR => ~"$",
191
192       /* Literals */
193       LIT_CHAR(c) => {
194           let mut res = ~"'";
195           char::from_u32(c).unwrap().escape_default(|c| {
196               res.push_char(c);
197           });
198           res.push_char('\'');
199           res
200       }
201       LIT_INT(i, t) => {
202           i.to_str() + ast_util::int_ty_to_str(t)
203       }
204       LIT_UINT(u, t) => {
205           u.to_str() + ast_util::uint_ty_to_str(t)
206       }
207       LIT_INT_UNSUFFIXED(i) => { i.to_str() }
208       LIT_FLOAT(s, t) => {
209         let mut body = get_ident(s).get().to_str();
210         if body.ends_with(".") {
211             body.push_char('0');  // `10.f` is not a float literal
212         }
213         body + ast_util::float_ty_to_str(t)
214       }
215       LIT_FLOAT_UNSUFFIXED(s) => {
216         let mut body = get_ident(s).get().to_str();
217         if body.ends_with(".") {
218             body.push_char('0');  // `10.f` is not a float literal
219         }
220         body
221       }
222       LIT_STR(s) => {
223           format!("\"{}\"", get_ident(s).get().escape_default())
224       }
225       LIT_STR_RAW(s, n) => {
226           format!("r{delim}\"{string}\"{delim}",
227                   delim="#".repeat(n), string=get_ident(s))
228       }
229
230       /* Name components */
231       IDENT(s, _) => get_ident(s).get().to_str(),
232       LIFETIME(s) => {
233           format!("'{}", get_ident(s))
234       }
235       UNDERSCORE => ~"_",
236
237       /* Other */
238       DOC_COMMENT(s) => get_ident(s).get().to_str(),
239       EOF => ~"<eof>",
240       INTERPOLATED(ref nt) => {
241         match nt {
242             &NtExpr(e) => ::print::pprust::expr_to_str(e),
243             &NtAttr(e) => ::print::pprust::attribute_to_str(e),
244             _ => {
245                 ~"an interpolated " +
246                     match *nt {
247                         NtItem(..) => ~"item",
248                         NtBlock(..) => ~"block",
249                         NtStmt(..) => ~"statement",
250                         NtPat(..) => ~"pattern",
251                         NtAttr(..) => fail!("should have been handled"),
252                         NtExpr(..) => fail!("should have been handled above"),
253                         NtTy(..) => ~"type",
254                         NtIdent(..) => ~"identifier",
255                         NtPath(..) => ~"path",
256                         NtTT(..) => ~"tt",
257                         NtMatchers(..) => ~"matcher sequence"
258                     }
259             }
260         }
261       }
262     }
263 }
264
265 pub fn can_begin_expr(t: &Token) -> bool {
266     match *t {
267       LPAREN => true,
268       LBRACE => true,
269       LBRACKET => true,
270       IDENT(_, _) => true,
271       UNDERSCORE => true,
272       TILDE => true,
273       LIT_CHAR(_) => true,
274       LIT_INT(_, _) => true,
275       LIT_UINT(_, _) => true,
276       LIT_INT_UNSUFFIXED(_) => true,
277       LIT_FLOAT(_, _) => true,
278       LIT_FLOAT_UNSUFFIXED(_) => true,
279       LIT_STR(_) => true,
280       LIT_STR_RAW(_, _) => true,
281       POUND => true,
282       AT => true,
283       NOT => true,
284       BINOP(MINUS) => true,
285       BINOP(STAR) => true,
286       BINOP(AND) => true,
287       BINOP(OR) => true, // in lambda syntax
288       OROR => true, // in lambda syntax
289       MOD_SEP => true,
290       INTERPOLATED(NtExpr(..))
291       | INTERPOLATED(NtIdent(..))
292       | INTERPOLATED(NtBlock(..))
293       | INTERPOLATED(NtPath(..)) => true,
294       _ => false
295     }
296 }
297
298 /// what's the opposite delimiter?
299 pub fn flip_delimiter(t: &token::Token) -> token::Token {
300     match *t {
301       LPAREN => RPAREN,
302       LBRACE => RBRACE,
303       LBRACKET => RBRACKET,
304       RPAREN => LPAREN,
305       RBRACE => LBRACE,
306       RBRACKET => LBRACKET,
307       _ => fail!()
308     }
309 }
310
311
312
313 pub fn is_lit(t: &Token) -> bool {
314     match *t {
315       LIT_CHAR(_) => true,
316       LIT_INT(_, _) => true,
317       LIT_UINT(_, _) => true,
318       LIT_INT_UNSUFFIXED(_) => true,
319       LIT_FLOAT(_, _) => true,
320       LIT_FLOAT_UNSUFFIXED(_) => true,
321       LIT_STR(_) => true,
322       LIT_STR_RAW(_, _) => true,
323       _ => false
324     }
325 }
326
327 pub fn is_ident(t: &Token) -> bool {
328     match *t { IDENT(_, _) => true, _ => false }
329 }
330
331 pub fn is_ident_or_path(t: &Token) -> bool {
332     match *t {
333       IDENT(_, _) | INTERPOLATED(NtPath(..)) => true,
334       _ => false
335     }
336 }
337
338 pub fn is_plain_ident(t: &Token) -> bool {
339     match *t { IDENT(_, false) => true, _ => false }
340 }
341
342 pub fn is_bar(t: &Token) -> bool {
343     match *t { BINOP(OR) | OROR => true, _ => false }
344 }
345
346 // Get the first "argument"
347 macro_rules! first {
348     ( $first:expr, $( $remainder:expr, )* ) => ( $first )
349 }
350
351 // Get the last "argument" (has to be done recursively to avoid phoney local ambiguity error)
352 macro_rules! last {
353     ( $first:expr, $( $remainder:expr, )+ ) => ( last!( $( $remainder, )+ ) );
354     ( $first:expr, ) => ( $first )
355 }
356
357 // In this macro, there is the requirement that the name (the number) must be monotonically
358 // increasing by one in the special identifiers, starting at 0; the same holds for the keywords,
359 // except starting from the next number instead of zero, and with the additional exception that
360 // special identifiers are *also* allowed (they are deduplicated in the important place, the
361 // interner), an exception which is demonstrated by "static" and "self".
362 macro_rules! declare_special_idents_and_keywords {(
363     // So now, in these rules, why is each definition parenthesised?
364     // Answer: otherwise we get a spurious local ambiguity bug on the "}"
365     pub mod special_idents {
366         $( ($si_name:expr, $si_static:ident, $si_str:expr); )*
367     }
368
369     pub mod keywords {
370         'strict:
371         $( ($sk_name:expr, $sk_variant:ident, $sk_str:expr); )*
372         'reserved:
373         $( ($rk_name:expr, $rk_variant:ident, $rk_str:expr); )*
374     }
375 ) => {
376     static STRICT_KEYWORD_START: Name = first!($( $sk_name, )*);
377     static STRICT_KEYWORD_FINAL: Name = last!($( $sk_name, )*);
378     static RESERVED_KEYWORD_START: Name = first!($( $rk_name, )*);
379     static RESERVED_KEYWORD_FINAL: Name = last!($( $rk_name, )*);
380
381     pub mod special_idents {
382         use ast::Ident;
383         $( pub static $si_static: Ident = Ident { name: $si_name, ctxt: 0 }; )*
384     }
385
386     /**
387      * All the valid words that have meaning in the Rust language.
388      *
389      * Rust keywords are either 'strict' or 'reserved'.  Strict keywords may not
390      * appear as identifiers at all. Reserved keywords are not used anywhere in
391      * the language and may not appear as identifiers.
392      */
393     pub mod keywords {
394         use ast::Ident;
395
396         pub enum Keyword {
397             $( $sk_variant, )*
398             $( $rk_variant, )*
399         }
400
401         impl Keyword {
402             pub fn to_ident(&self) -> Ident {
403                 match *self {
404                     $( $sk_variant => Ident { name: $sk_name, ctxt: 0 }, )*
405                     $( $rk_variant => Ident { name: $rk_name, ctxt: 0 }, )*
406                 }
407             }
408         }
409     }
410
411     fn mk_fresh_ident_interner() -> IdentInterner {
412         // The indices here must correspond to the numbers in
413         // special_idents, in Keyword to_ident(), and in static
414         // constants below.
415         let init_vec = ~[
416             $( $si_str, )*
417             $( $sk_str, )*
418             $( $rk_str, )*
419         ];
420
421         interner::StrInterner::prefill(init_vec)
422     }
423 }}
424
425 // If the special idents get renumbered, remember to modify these two as appropriate
426 static SELF_KEYWORD_NAME: Name = 1;
427 static STATIC_KEYWORD_NAME: Name = 2;
428
429 declare_special_idents_and_keywords! {
430     pub mod special_idents {
431         // These ones are statics
432         (0,                          invalid,                "");
433         (super::SELF_KEYWORD_NAME,   self_,                  "self");
434         (super::STATIC_KEYWORD_NAME, statik,                 "static");
435
436         // for matcher NTs
437         (3,                          tt,                     "tt");
438         (4,                          matchers,               "matchers");
439
440         // outside of libsyntax
441         (5,                          clownshoe_abi,          "__rust_abi");
442         (6,                          opaque,                 "<opaque>");
443         (7,                          unnamed_field,          "<unnamed_field>");
444         (8,                          type_self,              "Self");
445     }
446
447     pub mod keywords {
448         // These ones are variants of the Keyword enum
449
450         'strict:
451         (9,                          As,         "as");
452         (10,                         Break,      "break");
453         (11,                         Const,      "const");
454         (12,                         Crate,      "crate");
455         (13,                         Else,       "else");
456         (14,                         Enum,       "enum");
457         (15,                         Extern,     "extern");
458         (16,                         False,      "false");
459         (17,                         Fn,         "fn");
460         (18,                         For,        "for");
461         (19,                         If,         "if");
462         (20,                         Impl,       "impl");
463         (21,                         In,         "in");
464         (22,                         Let,        "let");
465         (23,                         __LogLevel, "__log_level");
466         (24,                         Loop,       "loop");
467         (25,                         Match,      "match");
468         (26,                         Mod,        "mod");
469         (27,                         Mut,        "mut");
470         (28,                         Once,       "once");
471         (29,                         Priv,       "priv");
472         (30,                         Pub,        "pub");
473         (31,                         Ref,        "ref");
474         (32,                         Return,     "return");
475         // Static and Self are also special idents (prefill de-dupes)
476         (super::STATIC_KEYWORD_NAME, Static,     "static");
477         (super::SELF_KEYWORD_NAME,   Self,       "self");
478         (33,                         Struct,     "struct");
479         (34,                         Super,      "super");
480         (35,                         True,       "true");
481         (36,                         Trait,      "trait");
482         (37,                         Type,       "type");
483         (38,                         Unsafe,     "unsafe");
484         (39,                         Use,        "use");
485         (40,                         While,      "while");
486         (41,                         Continue,   "continue");
487         (42,                         Proc,       "proc");
488         (43,                         Box,        "box");
489
490         'reserved:
491         (44,                         Alignof,    "alignof");
492         (45,                         Be,         "be");
493         (46,                         Offsetof,   "offsetof");
494         (47,                         Pure,       "pure");
495         (48,                         Sizeof,     "sizeof");
496         (49,                         Typeof,     "typeof");
497         (50,                         Unsized,    "unsized");
498         (51,                         Yield,      "yield");
499         (52,                         Do,         "do");
500     }
501 }
502
503 /**
504  * Maps a token to a record specifying the corresponding binary
505  * operator
506  */
507 pub fn token_to_binop(tok: &Token) -> Option<ast::BinOp> {
508   match *tok {
509       BINOP(STAR)    => Some(ast::BiMul),
510       BINOP(SLASH)   => Some(ast::BiDiv),
511       BINOP(PERCENT) => Some(ast::BiRem),
512       BINOP(PLUS)    => Some(ast::BiAdd),
513       BINOP(MINUS)   => Some(ast::BiSub),
514       BINOP(SHL)     => Some(ast::BiShl),
515       BINOP(SHR)     => Some(ast::BiShr),
516       BINOP(AND)     => Some(ast::BiBitAnd),
517       BINOP(CARET)   => Some(ast::BiBitXor),
518       BINOP(OR)      => Some(ast::BiBitOr),
519       LT             => Some(ast::BiLt),
520       LE             => Some(ast::BiLe),
521       GE             => Some(ast::BiGe),
522       GT             => Some(ast::BiGt),
523       EQEQ           => Some(ast::BiEq),
524       NE             => Some(ast::BiNe),
525       ANDAND         => Some(ast::BiAnd),
526       OROR           => Some(ast::BiOr),
527       _              => None
528   }
529 }
530
531 // looks like we can get rid of this completely...
532 pub type IdentInterner = StrInterner;
533
534 // if an interner exists in TLS, return it. Otherwise, prepare a
535 // fresh one.
536 pub fn get_ident_interner() -> @IdentInterner {
537     local_data_key!(key: @::parse::token::IdentInterner)
538     match local_data::get(key, |k| k.map(|k| *k)) {
539         Some(interner) => interner,
540         None => {
541             let interner = @mk_fresh_ident_interner();
542             local_data::set(key, interner);
543             interner
544         }
545     }
546 }
547
548 /// Represents a string stored in the task-local interner. Because the
549 /// interner lives for the life of the task, this can be safely treated as an
550 /// immortal string, as long as it never crosses between tasks.
551 ///
552 /// FIXME(pcwalton): You must be careful about what you do in the destructors
553 /// of objects stored in TLS, because they may run after the interner is
554 /// destroyed. In particular, they must not access string contents. This can
555 /// be fixed in the future by just leaking all strings until task death
556 /// somehow.
557 #[deriving(Clone, Eq, Hash, Ord, TotalEq, TotalOrd)]
558 pub struct InternedString {
559     priv string: RcStr,
560 }
561
562 impl InternedString {
563     #[inline]
564     pub fn new(string: &'static str) -> InternedString {
565         InternedString {
566             string: RcStr::new(string),
567         }
568     }
569
570     #[inline]
571     fn new_from_rc_str(string: RcStr) -> InternedString {
572         InternedString {
573             string: string,
574         }
575     }
576
577     #[inline]
578     pub fn get<'a>(&'a self) -> &'a str {
579         self.string.as_slice()
580     }
581 }
582
583 impl BytesContainer for InternedString {
584     fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
585         // FIXME(pcwalton): This is a workaround for the incorrect signature
586         // of `BytesContainer`, which is itself a workaround for the lack of
587         // DST.
588         unsafe {
589             let this = self.get();
590             cast::transmute(this.container_as_bytes())
591         }
592     }
593 }
594
595 impl fmt::Show for InternedString {
596     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
597         write!(f.buf, "{}", self.string.as_slice())
598     }
599 }
600
601 impl<'a> Equiv<&'a str> for InternedString {
602     fn equiv(&self, other: & &'a str) -> bool {
603         (*other) == self.string.as_slice()
604     }
605 }
606
607 impl<D:Decoder> Decodable<D> for InternedString {
608     fn decode(d: &mut D) -> InternedString {
609         get_name(get_ident_interner().intern(d.read_str()))
610     }
611 }
612
613 impl<E:Encoder> Encodable<E> for InternedString {
614     fn encode(&self, e: &mut E) {
615         e.emit_str(self.string.as_slice())
616     }
617 }
618
619 /// Returns the string contents of a name, using the task-local interner.
620 #[inline]
621 pub fn get_name(name: Name) -> InternedString {
622     let interner = get_ident_interner();
623     InternedString::new_from_rc_str(interner.get(name))
624 }
625
626 /// Returns the string contents of an identifier, using the task-local
627 /// interner.
628 #[inline]
629 pub fn get_ident(ident: Ident) -> InternedString {
630     get_name(ident.name)
631 }
632
633 /// Interns and returns the string contents of an identifier, using the
634 /// task-local interner.
635 #[inline]
636 pub fn intern_and_get_ident(s: &str) -> InternedString {
637     get_name(intern(s))
638 }
639
640 /// Maps a string to its interned representation.
641 #[inline]
642 pub fn intern(s: &str) -> Name {
643     get_ident_interner().intern(s)
644 }
645
646 /// gensym's a new uint, using the current interner.
647 #[inline]
648 pub fn gensym(s: &str) -> Name {
649     get_ident_interner().gensym(s)
650 }
651
652 /// Maps a string to an identifier with an empty syntax context.
653 #[inline]
654 pub fn str_to_ident(s: &str) -> ast::Ident {
655     ast::Ident::new(intern(s))
656 }
657
658 /// Maps a string to a gensym'ed identifier.
659 #[inline]
660 pub fn gensym_ident(s: &str) -> ast::Ident {
661     ast::Ident::new(gensym(s))
662 }
663
664 // create a fresh name that maps to the same string as the old one.
665 // note that this guarantees that str_ptr_eq(ident_to_str(src),interner_get(fresh_name(src)));
666 // that is, that the new name and the old one are connected to ptr_eq strings.
667 pub fn fresh_name(src: &ast::Ident) -> Name {
668     let interner = get_ident_interner();
669     interner.gensym_copy(src.name)
670     // following: debug version. Could work in final except that it's incompatible with
671     // good error messages and uses of struct names in ambiguous could-be-binding
672     // locations. Also definitely destroys the guarantee given above about ptr_eq.
673     /*let num = rand::rng().gen_uint_range(0,0xffff);
674     gensym(format!("{}_{}",ident_to_str(src),num))*/
675 }
676
677 // create a fresh mark.
678 pub fn fresh_mark() -> Mrk {
679     gensym("mark")
680 }
681
682 // See the macro above about the types of keywords
683
684 pub fn is_keyword(kw: keywords::Keyword, tok: &Token) -> bool {
685     match *tok {
686         token::IDENT(sid, false) => { kw.to_ident().name == sid.name }
687         _ => { false }
688     }
689 }
690
691 pub fn is_any_keyword(tok: &Token) -> bool {
692     match *tok {
693         token::IDENT(sid, false) => match sid.name {
694             SELF_KEYWORD_NAME | STATIC_KEYWORD_NAME |
695             STRICT_KEYWORD_START .. RESERVED_KEYWORD_FINAL => true,
696             _ => false,
697         },
698         _ => false
699     }
700 }
701
702 pub fn is_strict_keyword(tok: &Token) -> bool {
703     match *tok {
704         token::IDENT(sid, false) => match sid.name {
705             SELF_KEYWORD_NAME | STATIC_KEYWORD_NAME |
706             STRICT_KEYWORD_START .. STRICT_KEYWORD_FINAL => true,
707             _ => false,
708         },
709         _ => false,
710     }
711 }
712
713 pub fn is_reserved_keyword(tok: &Token) -> bool {
714     match *tok {
715         token::IDENT(sid, false) => match sid.name {
716             RESERVED_KEYWORD_START .. RESERVED_KEYWORD_FINAL => true,
717             _ => false,
718         },
719         _ => false,
720     }
721 }
722
723 pub fn mtwt_token_eq(t1 : &Token, t2 : &Token) -> bool {
724     match (t1,t2) {
725         (&IDENT(id1,_),&IDENT(id2,_)) | (&LIFETIME(id1),&LIFETIME(id2)) =>
726             ast_util::mtwt_resolve(id1) == ast_util::mtwt_resolve(id2),
727         _ => *t1 == *t2
728     }
729 }
730
731
732 #[cfg(test)]
733 mod test {
734     use super::*;
735     use ast;
736     use ast_util;
737
738     fn mark_ident(id : ast::Ident, m : ast::Mrk) -> ast::Ident {
739         ast::Ident{name:id.name,ctxt:ast_util::new_mark(m,id.ctxt)}
740     }
741
742     #[test] fn mtwt_token_eq_test() {
743         assert!(mtwt_token_eq(&GT,&GT));
744         let a = str_to_ident("bac");
745         let a1 = mark_ident(a,92);
746         assert!(mtwt_token_eq(&IDENT(a,true),&IDENT(a1,false)));
747     }
748 }