about summary refs log tree commit diff
path: root/src/libsyntax/parse/token.rs
blob: fc6f07288f27cc4a308b17ccfbd00ea1da6a6b01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
// Copyright 2012-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.

use core::prelude::*;

use ast;
use ast::Name;
use ast_util;
use parse::token;
use util::interner::StrInterner;
use util::interner;

use core::cast;
use core::char;
use core::cmp::Equiv;
use core::local_data;
use core::rand;
use core::rand::RngUtil;

#[deriving(Encodable, Decodable, Eq)]
pub enum binop {
    PLUS,
    MINUS,
    STAR,
    SLASH,
    PERCENT,
    CARET,
    AND,
    OR,
    SHL,
    SHR,
}

#[deriving(Encodable, Decodable, Eq)]
pub enum Token {
    /* Expression-operator symbols. */
    EQ,
    LT,
    LE,
    EQEQ,
    NE,
    GE,
    GT,
    ANDAND,
    OROR,
    NOT,
    TILDE,
    BINOP(binop),
    BINOPEQ(binop),

    /* Structural symbols */
    AT,
    DOT,
    DOTDOT,
    COMMA,
    SEMI,
    COLON,
    MOD_SEP,
    RARROW,
    LARROW,
    DARROW,
    FAT_ARROW,
    LPAREN,
    RPAREN,
    LBRACKET,
    RBRACKET,
    LBRACE,
    RBRACE,
    POUND,
    DOLLAR,

    /* Literals */
    LIT_INT(i64, ast::int_ty),
    LIT_UINT(u64, ast::uint_ty),
    LIT_INT_UNSUFFIXED(i64),
    LIT_FLOAT(ast::ident, ast::float_ty),
    LIT_FLOAT_UNSUFFIXED(ast::ident),
    LIT_STR(ast::ident),

    /* Name components */
    // an identifier contains an "is_mod_name" boolean,
    // indicating whether :: follows this token with no
    // whitespace in between.
    IDENT(ast::ident, bool),
    UNDERSCORE,
    LIFETIME(ast::ident),

    /* For interpolation */
    INTERPOLATED(nonterminal),

    DOC_COMMENT(ast::ident),
    EOF,
}

#[deriving(Encodable, Decodable, Eq)]
/// For interpolation during macro expansion.
pub enum nonterminal {
    nt_item(@ast::item),
    nt_block(ast::blk),
    nt_stmt(@ast::stmt),
    nt_pat( @ast::pat),
    nt_expr(@ast::expr),
    nt_ty(  @ast::Ty),
    nt_ident(ast::ident, bool),
    nt_path(@ast::Path),
    nt_tt(  @ast::token_tree), //needs @ed to break a circularity
    nt_matchers(~[ast::matcher])
}

pub fn binop_to_str(o: binop) -> ~str {
    match o {
      PLUS => ~"+",
      MINUS => ~"-",
      STAR => ~"*",
      SLASH => ~"/",
      PERCENT => ~"%",
      CARET => ~"^",
      AND => ~"&",
      OR => ~"|",
      SHL => ~"<<",
      SHR => ~">>"
    }
}

pub fn to_str(in: @ident_interner, t: &Token) -> ~str {
    match *t {
      EQ => ~"=",
      LT => ~"<",
      LE => ~"<=",
      EQEQ => ~"==",
      NE => ~"!=",
      GE => ~">=",
      GT => ~">",
      NOT => ~"!",
      TILDE => ~"~",
      OROR => ~"||",
      ANDAND => ~"&&",
      BINOP(op) => binop_to_str(op),
      BINOPEQ(op) => binop_to_str(op) + "=",

      /* Structural symbols */
      AT => ~"@",
      DOT => ~".",
      DOTDOT => ~"..",
      COMMA => ~",",
      SEMI => ~";",
      COLON => ~":",
      MOD_SEP => ~"::",
      RARROW => ~"->",
      LARROW => ~"<-",
      DARROW => ~"<->",
      FAT_ARROW => ~"=>",
      LPAREN => ~"(",
      RPAREN => ~")",
      LBRACKET => ~"[",
      RBRACKET => ~"]",
      LBRACE => ~"{",
      RBRACE => ~"}",
      POUND => ~"#",
      DOLLAR => ~"$",

      /* Literals */
      LIT_INT(c, ast::ty_char) => {
        ~"'" + char::escape_default(c as char) + "'"
      }
      LIT_INT(i, t) => {
          i.to_str() + ast_util::int_ty_to_str(t)
      }
      LIT_UINT(u, t) => {
          u.to_str() + ast_util::uint_ty_to_str(t)
      }
      LIT_INT_UNSUFFIXED(i) => { i.to_str() }
      LIT_FLOAT(ref s, t) => {
        let mut body = ident_to_str(s).to_owned();
        if body.ends_with(".") {
            body += "0";  // `10.f` is not a float literal
        }
        body + ast_util::float_ty_to_str(t)
      }
      LIT_FLOAT_UNSUFFIXED(ref s) => {
        let mut body = ident_to_str(s).to_owned();
        if body.ends_with(".") {
            body += "0";  // `10.f` is not a float literal
        }
        body
      }
      LIT_STR(ref s) => { fmt!("\"%s\"", ident_to_str(s).escape_default()) }

      /* Name components */
      IDENT(s, _) => in.get(s.name).to_owned(),
      LIFETIME(s) => fmt!("'%s", in.get(s.name)),
      UNDERSCORE => ~"_",

      /* Other */
      DOC_COMMENT(ref s) => ident_to_str(s).to_owned(),
      EOF => ~"<eof>",
      INTERPOLATED(ref nt) => {
        match nt {
            &nt_expr(e) => ::print::pprust::expr_to_str(e, in),
            _ => {
                ~"an interpolated " +
                    match (*nt) {
                      nt_item(*) => ~"item",
                      nt_block(*) => ~"block",
                      nt_stmt(*) => ~"statement",
                      nt_pat(*) => ~"pattern",
                      nt_expr(*) => fail!("should have been handled above"),
                      nt_ty(*) => ~"type",
                      nt_ident(*) => ~"identifier",
                      nt_path(*) => ~"path",
                      nt_tt(*) => ~"tt",
                      nt_matchers(*) => ~"matcher sequence"
                    }
            }
        }
      }
    }
}

pub fn can_begin_expr(t: &Token) -> bool {
    match *t {
      LPAREN => true,
      LBRACE => true,
      LBRACKET => true,
      IDENT(_, _) => true,
      UNDERSCORE => true,
      TILDE => true,
      LIT_INT(_, _) => true,
      LIT_UINT(_, _) => true,
      LIT_INT_UNSUFFIXED(_) => true,
      LIT_FLOAT(_, _) => true,
      LIT_FLOAT_UNSUFFIXED(_) => true,
      LIT_STR(_) => true,
      POUND => true,
      AT => true,
      NOT => true,
      BINOP(MINUS) => true,
      BINOP(STAR) => true,
      BINOP(AND) => true,
      BINOP(OR) => true, // in lambda syntax
      OROR => true, // in lambda syntax
      MOD_SEP => true,
      INTERPOLATED(nt_expr(*))
      | INTERPOLATED(nt_ident(*))
      | INTERPOLATED(nt_block(*))
      | INTERPOLATED(nt_path(*)) => true,
      _ => false
    }
}

/// what's the opposite delimiter?
pub fn flip_delimiter(t: &token::Token) -> token::Token {
    match *t {
      LPAREN => RPAREN,
      LBRACE => RBRACE,
      LBRACKET => RBRACKET,
      RPAREN => LPAREN,
      RBRACE => LBRACE,
      RBRACKET => LBRACKET,
      _ => fail!()
    }
}



pub fn is_lit(t: &Token) -> bool {
    match *t {
      LIT_INT(_, _) => true,
      LIT_UINT(_, _) => true,
      LIT_INT_UNSUFFIXED(_) => true,
      LIT_FLOAT(_, _) => true,
      LIT_FLOAT_UNSUFFIXED(_) => true,
      LIT_STR(_) => true,
      _ => false
    }
}

pub fn is_ident(t: &Token) -> bool {
    match *t { IDENT(_, _) => true, _ => false }
}

pub fn is_ident_or_path(t: &Token) -> bool {
    match *t {
      IDENT(_, _) | INTERPOLATED(nt_path(*)) => true,
      _ => false
    }
}

pub fn is_plain_ident(t: &Token) -> bool {
    match *t { IDENT(_, false) => true, _ => false }
}

pub fn is_bar(t: &Token) -> bool {
    match *t { BINOP(OR) | OROR => true, _ => false }
}


pub mod special_idents {
    use ast::ident;

    pub static underscore : ident = ident { name: 0, ctxt: 0};
    pub static anon : ident = ident { name: 1, ctxt: 0};
    pub static invalid : ident = ident { name: 2, ctxt: 0}; // ''
    pub static unary : ident = ident { name: 3, ctxt: 0};
    pub static not_fn : ident = ident { name: 4, ctxt: 0};
    pub static idx_fn : ident = ident { name: 5, ctxt: 0};
    pub static unary_minus_fn : ident = ident { name: 6, ctxt: 0};
    pub static clownshoes_extensions : ident = ident { name: 7, ctxt: 0};

    pub static self_ : ident = ident { name: 8, ctxt: 0}; // 'self'

    /* for matcher NTs */
    pub static item : ident = ident { name: 9, ctxt: 0};
    pub static block : ident = ident { name: 10, ctxt: 0};
    pub static stmt : ident = ident { name: 11, ctxt: 0};
    pub static pat : ident = ident { name: 12, ctxt: 0};
    pub static expr : ident = ident { name: 13, ctxt: 0};
    pub static ty : ident = ident { name: 14, ctxt: 0};
    pub static ident : ident = ident { name: 15, ctxt: 0};
    pub static path : ident = ident { name: 16, ctxt: 0};
    pub static tt : ident = ident { name: 17, ctxt: 0};
    pub static matchers : ident = ident { name: 18, ctxt: 0};

    pub static str : ident = ident { name: 19, ctxt: 0}; // for the type

    /* outside of libsyntax */
    pub static ty_visitor : ident = ident { name: 20, ctxt: 0};
    pub static arg : ident = ident { name: 21, ctxt: 0};
    pub static descrim : ident = ident { name: 22, ctxt: 0};
    pub static clownshoe_abi : ident = ident { name: 23, ctxt: 0};
    pub static clownshoe_stack_shim : ident = ident { name: 24, ctxt: 0};
    pub static tydesc : ident = ident { name: 25, ctxt: 0};
    pub static main : ident = ident { name: 26, ctxt: 0};
    pub static opaque : ident = ident { name: 27, ctxt: 0};
    pub static blk : ident = ident { name: 28, ctxt: 0};
    pub static statik : ident = ident { name: 29, ctxt: 0};
    pub static intrinsic : ident = ident { name: 30, ctxt: 0};
    pub static clownshoes_foreign_mod: ident = ident { name: 31, ctxt: 0};
    pub static unnamed_field: ident = ident { name: 32, ctxt: 0};
    pub static c_abi: ident = ident { name: 33, ctxt: 0};
    pub static type_self: ident = ident { name: 34, ctxt: 0};    // `Self`
}

/**
 * Maps a token to a record specifying the corresponding binary
 * operator
 */
pub fn token_to_binop(tok: Token) -> Option<ast::binop> {
  match tok {
      BINOP(STAR)    => Some(ast::mul),
      BINOP(SLASH)   => Some(ast::div),
      BINOP(PERCENT) => Some(ast::rem),
      BINOP(PLUS)    => Some(ast::add),
      BINOP(MINUS)   => Some(ast::subtract),
      BINOP(SHL)     => Some(ast::shl),
      BINOP(SHR)     => Some(ast::shr),
      BINOP(AND)     => Some(ast::bitand),
      BINOP(CARET)   => Some(ast::bitxor),
      BINOP(OR)      => Some(ast::bitor),
      LT             => Some(ast::lt),
      LE             => Some(ast::le),
      GE             => Some(ast::ge),
      GT             => Some(ast::gt),
      EQEQ           => Some(ast::eq),
      NE             => Some(ast::ne),
      ANDAND         => Some(ast::and),
      OROR           => Some(ast::or),
      _              => None
  }
}

pub struct ident_interner {
    priv interner: StrInterner,
}

impl ident_interner {
    pub fn intern(&self, val: &str) -> Name {
        self.interner.intern(val)
    }
    pub fn gensym(&self, val: &str) -> Name {
        self.interner.gensym(val)
    }
    pub fn get(&self, idx: Name) -> @str {
        self.interner.get(idx)
    }
    // is this really something that should be exposed?
    pub fn len(&self) -> uint {
        self.interner.len()
    }
    pub fn find_equiv<Q:Hash + IterBytes + Equiv<@str>>(&self, val: &Q)
                                                     -> Option<Name> {
        self.interner.find_equiv(val)
    }
}


// return a fresh interner, preloaded with special identifiers.
#[cfg(stage0)]
fn mk_fresh_ident_interner() -> @ident_interner {
    // the indices here must correspond to the numbers in
    // special_idents.
    let init_vec = ~[
        "_",                  // 0
        "anon",               // 1
        "",                   // 2
        "unary",              // 3
        "!",                  // 4
        "[]",                 // 5
        "unary-",             // 6
        "__extensions__",     // 7
        "self",               // 8
        "item",               // 9
        "block",              // 10
        "stmt",               // 11
        "pat",                // 12
        "expr",               // 13
        "ty",                 // 14
        "ident",              // 15
        "path",               // 16
        "tt",                 // 17
        "matchers",           // 18
        "str",                // 19
        "TyVisitor",          // 20
        "arg",                // 21
        "descrim",            // 22
        "__rust_abi",         // 23
        "__rust_stack_shim",  // 24
        "TyDesc",             // 25
        "main",               // 26
        "<opaque>",           // 27
        "blk",                // 28
        "static",             // 29
        "intrinsic",          // 30
        "__foreign_mod__",    // 31
        "__field__",          // 32
        "C",                  // 33
        "Self",               // 34

        "as",                 // 35
        "break",              // 36
        "const",              // 37
        "copy",               // 38
        "do",                 // 39
        "drop",               // 40
        "else",               // 41
        "enum",               // 42
        "extern",             // 43
        "false",              // 44
        "fn",                 // 45
        "for",                // 46
        "if",                 // 47
        "impl",               // 48
        "let",                // 49
        "__log",              // 50
        "loop",               // 51
        "match",              // 52
        "mod",                // 53
        "mut",                // 54
        "once",               // 55
        "priv",               // 56
        "pub",                // 57
        "pure",               // 58
        "ref",                // 59
        "return",             // 60
        "static",             // 29 -- also a special ident
        "self",               //  8 -- also a special ident
        "struct",             // 61
        "super",              // 62
        "true",               // 63
        "trait",              // 64
        "type",               // 65
        "unsafe",             // 66
        "use",                // 67
        "while",              // 68

        "be",                 // 69
    ];

    @ident_interner {
        interner: interner::StrInterner::prefill(init_vec)
    }
}

// return a fresh interner, preloaded with special identifiers.
#[cfg(not(stage0))]
fn mk_fresh_ident_interner() -> @ident_interner {
    // the indices here must correspond to the numbers in
    // special_idents.
    let init_vec = ~[
        "_",                  // 0
        "anon",               // 1
        "",                   // 2
        "unary",              // 3
        "!",                  // 4
        "[]",                 // 5
        "unary-",             // 6
        "__extensions__",     // 7
        "self",               // 8
        "item",               // 9
        "block",              // 10
        "stmt",               // 11
        "pat",                // 12
        "expr",               // 13
        "ty",                 // 14
        "ident",              // 15
        "path",               // 16
        "tt",                 // 17
        "matchers",           // 18
        "str",                // 19
        "TyVisitor",          // 20
        "arg",                // 21
        "descrim",            // 22
        "__rust_abi",         // 23
        "__rust_stack_shim",  // 24
        "TyDesc",             // 25
        "main",               // 26
        "<opaque>",           // 27
        "blk",                // 28
        "static",             // 29
        "intrinsic",          // 30
        "__foreign_mod__",    // 31
        "__field__",          // 32
        "C",                  // 33
        "Self",               // 34

        "as",                 // 35
        "break",              // 36
        "const",              // 37
        "copy",               // 38
        "do",                 // 39
        "else",               // 40
        "enum",               // 41
        "extern",             // 42
        "false",              // 43
        "fn",                 // 44
        "for",                // 45
        "if",                 // 46
        "impl",               // 47
        "let",                // 48
        "__log",              // 49
        "loop",               // 50
        "match",              // 51
        "mod",                // 52
        "mut",                // 53
        "once",               // 54
        "priv",               // 55
        "pub",                // 56
        "pure",               // 57
        "ref",                // 58
        "return",             // 59
        "static",             // 29 -- also a special ident
        "self",               //  8 -- also a special ident
        "struct",             // 60
        "super",              // 61
        "true",               // 62
        "trait",              // 63
        "type",               // 64
        "unsafe",             // 65
        "use",                // 66
        "while",              // 67

        "be",                 // 68
    ];

    @ident_interner {
        interner: interner::StrInterner::prefill(init_vec)
    }
}

// if an interner exists in TLS, return it. Otherwise, prepare a
// fresh one.
pub fn get_ident_interner() -> @ident_interner {
    unsafe {
        let key =
            (cast::transmute::<(uint, uint),
             &fn(v: @@::parse::token::ident_interner)>(
                 (-3 as uint, 0u)));
        match local_data::local_data_get(key) {
            Some(interner) => *interner,
            None => {
                let interner = mk_fresh_ident_interner();
                unsafe {
                    local_data::local_data_set(key, @interner);
                }
                interner
            }
        }
    }
}

/* for when we don't care about the contents; doesn't interact with TLD or
   serialization */
pub fn mk_fake_ident_interner() -> @ident_interner {
    @ident_interner { interner: interner::StrInterner::new() }
}

// maps a string to its interned representation
pub fn intern(str : &str) -> Name {
    let interner = get_ident_interner();
    interner.intern(str)
}

// gensyms a new uint, using the current interner
pub fn gensym(str : &str) -> Name {
    let interner = get_ident_interner();
    interner.gensym(str)
}

// map an interned representation back to a string
pub fn interner_get(name : Name) -> @str {
    get_ident_interner().get(name)
}

// maps an identifier to the string that it corresponds to
pub fn ident_to_str(id : &ast::ident) -> @str {
    interner_get(id.name)
}

// maps a string to an identifier with an empty syntax context
pub fn str_to_ident(str : &str) -> ast::ident {
    ast::new_ident(intern(str))
}

// maps a string to a gensym'ed identifier
pub fn gensym_ident(str : &str) -> ast::ident {
    ast::new_ident(gensym(str))
}


// create a fresh name. In principle, this is just a
// gensym, but for debugging purposes, you'd like the
// resulting name to have a suggestive stringify, without
// paying the cost of guaranteeing that the name is
// truly unique.  I'm going to try to strike a balance
// by using a gensym with a name that has a random number
// at the end. So, the gensym guarantees the uniqueness,
// and the int helps to avoid confusion.
pub fn fresh_name(src_name : &str) -> Name {
    let num = rand::rng().gen_uint_range(0,0xffff);
   gensym(fmt!("%s_%u",src_name,num))
}

/**
 * All the valid words that have meaning in the Rust language.
 *
 * Rust keywords are either 'strict' or 'reserved'.  Strict keywords may not
 * appear as identifiers at all. Reserved keywords are not used anywhere in
 * the language and may not appear as identifiers.
 */
pub mod keywords {
    use ast::ident;

    pub enum Keyword {
        // Strict keywords
        As,
        Break,
        Const,
        Copy,
        Do,
        Else,
        Enum,
        Extern,
        False,
        Fn,
        For,
        If,
        Impl,
        Let,
        __Log,
        Loop,
        Match,
        Mod,
        Mut,
        Once,
        Priv,
        Pub,
        Pure,
        Ref,
        Return,
        Static,
        Self,
        Struct,
        Super,
        True,
        Trait,
        Type,
        Unsafe,
        Use,
        While,

        // Reserved keywords
        Be,
    }

    impl Keyword {
        #[cfg(stage0)]
        pub fn to_ident(&self) -> ident {
            match *self {
                As => ident { name: 35, ctxt: 0 },
                Break => ident { name: 36, ctxt: 0 },
                Const => ident { name: 37, ctxt: 0 },
                Copy => ident { name: 38, ctxt: 0 },
                Do => ident { name: 39, ctxt: 0 },
                Else => ident { name: 41, ctxt: 0 },
                Enum => ident { name: 42, ctxt: 0 },
                Extern => ident { name: 43, ctxt: 0 },
                False => ident { name: 44, ctxt: 0 },
                Fn => ident { name: 45, ctxt: 0 },
                For => ident { name: 46, ctxt: 0 },
                If => ident { name: 47, ctxt: 0 },
                Impl => ident { name: 48, ctxt: 0 },
                Let => ident { name: 49, ctxt: 0 },
                __Log => ident { name: 50, ctxt: 0 },
                Loop => ident { name: 51, ctxt: 0 },
                Match => ident { name: 52, ctxt: 0 },
                Mod => ident { name: 53, ctxt: 0 },
                Mut => ident { name: 54, ctxt: 0 },
                Once => ident { name: 55, ctxt: 0 },
                Priv => ident { name: 56, ctxt: 0 },
                Pub => ident { name: 57, ctxt: 0 },
                Pure => ident { name: 58, ctxt: 0 },
                Ref => ident { name: 59, ctxt: 0 },
                Return => ident { name: 60, ctxt: 0 },
                Static => ident { name: 29, ctxt: 0 },
                Self => ident { name: 8, ctxt: 0 },
                Struct => ident { name: 61, ctxt: 0 },
                Super => ident { name: 62, ctxt: 0 },
                True => ident { name: 63, ctxt: 0 },
                Trait => ident { name: 64, ctxt: 0 },
                Type => ident { name: 65, ctxt: 0 },
                Unsafe => ident { name: 66, ctxt: 0 },
                Use => ident { name: 67, ctxt: 0 },
                While => ident { name: 68, ctxt: 0 },
                Be => ident { name: 69, ctxt: 0 },
            }
        }
        #[cfg(not(stage0))]
        pub fn to_ident(&self) -> ident {
            match *self {
                As => ident { name: 35, ctxt: 0 },
                Break => ident { name: 36, ctxt: 0 },
                Const => ident { name: 37, ctxt: 0 },
                Copy => ident { name: 38, ctxt: 0 },
                Do => ident { name: 39, ctxt: 0 },
                Else => ident { name: 40, ctxt: 0 },
                Enum => ident { name: 41, ctxt: 0 },
                Extern => ident { name: 42, ctxt: 0 },
                False => ident { name: 43, ctxt: 0 },
                Fn => ident { name: 44, ctxt: 0 },
                For => ident { name: 45, ctxt: 0 },
                If => ident { name: 46, ctxt: 0 },
                Impl => ident { name: 47, ctxt: 0 },
                Let => ident { name: 48, ctxt: 0 },
                __Log => ident { name: 49, ctxt: 0 },
                Loop => ident { name: 50, ctxt: 0 },
                Match => ident { name: 51, ctxt: 0 },
                Mod => ident { name: 52, ctxt: 0 },
                Mut => ident { name: 53, ctxt: 0 },
                Once => ident { name: 54, ctxt: 0 },
                Priv => ident { name: 55, ctxt: 0 },
                Pub => ident { name: 56, ctxt: 0 },
                Pure => ident { name: 57, ctxt: 0 },
                Ref => ident { name: 58, ctxt: 0 },
                Return => ident { name: 59, ctxt: 0 },
                Static => ident { name: 29, ctxt: 0 },
                Self => ident { name: 8, ctxt: 0 },
                Struct => ident { name: 60, ctxt: 0 },
                Super => ident { name: 61, ctxt: 0 },
                True => ident { name: 62, ctxt: 0 },
                Trait => ident { name: 63, ctxt: 0 },
                Type => ident { name: 64, ctxt: 0 },
                Unsafe => ident { name: 65, ctxt: 0 },
                Use => ident { name: 66, ctxt: 0 },
                While => ident { name: 67, ctxt: 0 },
                Be => ident { name: 68, ctxt: 0 },
            }
        }
    }
}

pub fn is_keyword(kw: keywords::Keyword, tok: &Token) -> bool {
    match *tok {
        token::IDENT(sid, false) => { kw.to_ident().name == sid.name }
        _ => { false }
    }
}

#[cfg(stage0)]
pub fn is_any_keyword(tok: &Token) -> bool {
    match *tok {
        token::IDENT(sid, false) => match sid.name {
            8 | 29 | 35 .. 69 => true,
            _ => false,
        },
        _ => false
    }
}

#[cfg(not(stage0))]
pub fn is_any_keyword(tok: &Token) -> bool {
    match *tok {
        token::IDENT(sid, false) => match sid.name {
            8 | 29 | 35 .. 68 => true,
            _ => false,
        },
        _ => false
    }
}

#[cfg(stage0)]
pub fn is_strict_keyword(tok: &Token) -> bool {
    match *tok {
        token::IDENT(sid, false) => match sid.name {
            8 | 29 | 35 .. 68 => true,
            _ => false,
        },
        _ => false,
    }
}

#[cfg(not(stage0))]
pub fn is_strict_keyword(tok: &Token) -> bool {
    match *tok {
        token::IDENT(sid, false) => match sid.name {
            8 | 29 | 35 .. 67 => true,
            _ => false,
        },
        _ => false,
    }
}

#[cfg(stage0)]
pub fn is_reserved_keyword(tok: &Token) -> bool {
    match *tok {
        token::IDENT(sid, false) => match sid.name {
            69 => true,
            _ => false,
        },
        _ => false,
    }
}

#[cfg(not(stage0))]
pub fn is_reserved_keyword(tok: &Token) -> bool {
    match *tok {
        token::IDENT(sid, false) => match sid.name {
            68 => true,
            _ => false,
        },
        _ => false,
    }
}


#[cfg(test)]
mod test {
    use super::*;
    use std::io;
    #[test] fn t1() {
        let a = fresh_name("ghi");
        io::println(fmt!("interned name: %u,\ntextual name: %s\n",
                         a,interner_get(a)));
    }
}