Skip to content

Commit dd042ef

Browse files
committed
Separate strict/reserved keywords, derive bounds.
It's twenty lines longer, but makes for clearer separation of strict and reserved keywords (probably a good thing) and removes another moving part (the definitions of `(STRICT|RESERVED)_KEYWORD_(START|FINAL)`).
1 parent d4f5ae0 commit dd042ef

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

src/libsyntax/parse/token.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,17 @@ pub fn is_bar(t: &Token) -> bool {
316316
match *t { BINOP(OR) | OROR => true, _ => false }
317317
}
318318

319+
// Get the first "argument"
320+
macro_rules! first {
321+
( $first:expr, $( $remainder:expr, )* ) => ( $first )
322+
}
323+
324+
// Get the last "argument" (has to be done recursively to avoid phoney local ambiguity error)
325+
macro_rules! last {
326+
( $first:expr, $( $remainder:expr, )+ ) => ( last!( $( $remainder, )+ ) );
327+
( $first:expr, ) => ( $first )
328+
}
329+
319330
// In this macro, there is the requirement that the name (the number) must be monotonically
320331
// increasing by one in the special identifiers, starting at 0; the same holds for the keywords,
321332
// except starting from the next number instead of zero, and with the additional exception that
@@ -329,9 +340,17 @@ macro_rules! declare_special_idents_and_keywords {(
329340
}
330341

331342
pub mod keywords {
332-
$( ($k_name:expr, $k_variant:ident, $k_str:expr); )*
343+
'strict:
344+
$( ($sk_name:expr, $sk_variant:ident, $sk_str:expr); )*
345+
'reserved:
346+
$( ($rk_name:expr, $rk_variant:ident, $rk_str:expr); )*
333347
}
334348
) => {
349+
static STRICT_KEYWORD_START: Name = first!($( $sk_name, )*);
350+
static STRICT_KEYWORD_FINAL: Name = last!($( $sk_name, )*);
351+
static RESERVED_KEYWORD_START: Name = first!($( $rk_name, )*);
352+
static RESERVED_KEYWORD_FINAL: Name = last!($( $rk_name, )*);
353+
335354
pub mod special_idents {
336355
use ast::Ident;
337356
$( pub static $si_static: Ident = Ident { name: $si_name, ctxt: 0 }; )*
@@ -348,13 +367,15 @@ macro_rules! declare_special_idents_and_keywords {(
348367
use ast::Ident;
349368

350369
pub enum Keyword {
351-
$( $k_variant, )*
370+
$( $sk_variant, )*
371+
$( $rk_variant, )*
352372
}
353373

354374
impl Keyword {
355375
pub fn to_ident(&self) -> Ident {
356376
match *self {
357-
$( $k_variant => Ident { name: $k_name, ctxt: 0 }, )*
377+
$( $sk_variant => Ident { name: $sk_name, ctxt: 0 }, )*
378+
$( $rk_variant => Ident { name: $rk_name, ctxt: 0 }, )*
358379
}
359380
}
360381
}
@@ -366,20 +387,17 @@ macro_rules! declare_special_idents_and_keywords {(
366387
// constants below.
367388
let init_vec = ~[
368389
$( $si_str, )*
369-
$( $k_str, )*
390+
$( $sk_str, )*
391+
$( $rk_str, )*
370392
];
371393

372394
@interner::StrInterner::prefill(init_vec)
373395
}
374396
}}
375397

376-
// If modifying the numbers below, remember to modify these as appropriate
398+
// If the special idents get renumbered, remember to modify these two as appropriate
377399
static SELF_KEYWORD_NAME: Name = 3;
378400
static STATIC_KEYWORD_NAME: Name = 10;
379-
static STRICT_KEYWORD_START: Name = 14;
380-
static STRICT_KEYWORD_FINAL: Name = 47;
381-
static RESERVED_KEYWORD_START: Name = 48;
382-
static RESERVED_KEYWORD_FINAL: Name = 54;
383401

384402
declare_special_idents_and_keywords! {
385403
pub mod special_idents {
@@ -409,7 +427,7 @@ declare_special_idents_and_keywords! {
409427
pub mod keywords {
410428
// These ones are variants of the Keyword enum
411429

412-
// Strict keywords
430+
'strict:
413431
(14, As, "as");
414432
(15, Break, "break");
415433
(16, Const, "const");
@@ -448,7 +466,7 @@ declare_special_idents_and_keywords! {
448466
(46, Continue, "continue");
449467
(47, Proc, "proc");
450468

451-
// Reserved keywords
469+
'reserved:
452470
(48, Alignof, "alignof");
453471
(49, Be, "be");
454472
(50, Offsetof, "offsetof");

0 commit comments

Comments
 (0)