about summary refs log tree commit diff
path: root/src/libsyntax/parse/token.rs
blob: fe7bd5b3bc17db3790e954d396dc11f805b840c6 (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
// Copyright 2012 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 ast;
use ast_util;
use parse::token;
use util::interner::Interner;
use util::interner;

use core::cmp::Equiv;
use core::hashmap::HashSet;
use core::to_bytes;

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

#[auto_encode]
#[auto_decode]
#[deriving(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,
}

#[auto_encode]
#[auto_decode]
#[deriving(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(s, t) => {
        let mut body = copy *in.get(s);
        if body.ends_with(~".") {
            body = body + ~"0";  // `10.f` is not a float literal
        }
        body + ast_util::float_ty_to_str(t)
      }
      LIT_FLOAT_UNSUFFIXED(s) => {
        let mut body = copy *in.get(s);
        if body.ends_with(~".") {
            body = body + ~"0";  // `10.f` is not a float literal
        }
        body
      }
      LIT_STR(s) => { ~"\"" + str::escape_default(*in.get(s)) + ~"\"" }

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

      /* Other */
      DOC_COMMENT(s) => copy *in.get(s),
      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 { repr: 0, ctxt: 0};
    pub static anon : ident = ident { repr: 1, ctxt: 0};
    pub static invalid : ident = ident { repr: 2, ctxt: 0}; // ''
    pub static unary : ident = ident { repr: 3, ctxt: 0};
    pub static not_fn : ident = ident { repr: 4, ctxt: 0};
    pub static idx_fn : ident = ident { repr: 5, ctxt: 0};
    pub static unary_minus_fn : ident = ident { repr: 6, ctxt: 0};
    pub static clownshoes_extensions : ident = ident { repr: 7, ctxt: 0};

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

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

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

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

pub struct StringRef<'self>(&'self str);

impl<'self> Equiv<@~str> for StringRef<'self> {
    #[inline(always)]
    fn equiv(&self, other: &@~str) -> bool { str::eq_slice(**self, **other) }
}

impl<'self> to_bytes::IterBytes for StringRef<'self> {
    fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) {
        (**self).iter_bytes(lsb0, f);
    }
}

/**
 * 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: Interner<@~str>,
}

pub impl ident_interner {
    fn intern(&self, val: @~str) -> ast::ident {
        ast::ident { repr: self.interner.intern(val), ctxt: 0 }
    }
    fn gensym(&self, val: @~str) -> ast::ident {
        ast::ident { repr: self.interner.gensym(val), ctxt: 0 }
    }
    fn get(&self, idx: ast::ident) -> @~str {
        self.interner.get(idx.repr)
    }
    fn len(&self) -> uint {
        self.interner.len()
    }
    fn find_equiv<Q:Hash + IterBytes + Equiv<@~str>>(&self, val: &Q)
                                                     -> Option<ast::ident> {
        match self.interner.find_equiv(val) {
            Some(v) => Some(ast::ident { repr: v, ctxt: 0 }),
            None => None,
        }
    }
}

// return a fresh interner, preloaded with special identifiers.
// EFFECT: stores this interner in TLS
pub 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
    ];

    let rv = @ident_interner {
        interner: interner::Interner::prefill(init_vec)
    };
    unsafe {
        task::local_data::local_data_set(interner_key!(), @rv);
    }
    rv
}

// if an interner exists in TLS, return it. Otherwise, prepare a
// fresh one.
pub fn mk_ident_interner() -> @ident_interner {
    unsafe {
        match task::local_data::local_data_get(interner_key!()) {
            Some(interner) => *interner,
            None => {
                mk_fresh_ident_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::Interner::new() }
}

/**
 * All the valid words that have meaning in the Rust language.
 *
 * Rust keywords are either 'temporary', 'strict' or 'reserved'.  Temporary
 * keywords are contextual and may be used as identifiers anywhere.  They are
 * expected to disappear from the grammar soon.  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 fn keyword_table() -> HashSet<~str> {
    let mut keywords = HashSet::new();
    let mut tmp = temporary_keyword_table();
    let mut strict = strict_keyword_table();
    let mut reserved = reserved_keyword_table();

    do tmp.consume |word|      { keywords.insert(word); }
    do strict.consume |word|   { keywords.insert(word); }
    do reserved.consume |word| { keywords.insert(word); }
    return keywords;
}

/// Keywords that may be used as identifiers
pub fn temporary_keyword_table() -> HashSet<~str> {
    let mut words = HashSet::new();
    let keys = ~[
        ~"self", ~"static",
    ];
    do vec::consume(keys) |_, s| {
        words.insert(s);
    }
    return words;
}

/// Full keywords. May not appear anywhere else.
pub fn strict_keyword_table() -> HashSet<~str> {
    let mut words = HashSet::new();
    let keys = ~[
        ~"as",
        ~"break",
        ~"const", ~"copy",
        ~"do", ~"drop",
        ~"else", ~"enum", ~"extern",
        ~"false", ~"fn", ~"for",
        ~"if", ~"impl",
        ~"let", ~"__log", ~"loop",
        ~"match", ~"mod", ~"mut",
        ~"once",
        ~"priv", ~"pub", ~"pure",
        ~"ref", ~"return",
        ~"struct", ~"super",
        ~"true", ~"trait", ~"type",
        ~"unsafe", ~"use",
        ~"while"
    ];
    do vec::consume(keys) |_, w| {
        words.insert(w);
    }
    return words;
}

pub fn reserved_keyword_table() -> HashSet<~str> {
    let mut words = HashSet::new();
    let keys = ~[
        ~"be"
    ];
    do vec::consume(keys) |_, s| {
        words.insert(s);
    }
    return words;
}