Skip to content

Commit

Permalink
Separate strict/reserved keywords, derive bounds.
Browse files Browse the repository at this point in the history
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)`).
  • Loading branch information
chris-morgan committed Dec 11, 2013
1 parent d4f5ae0 commit dd042ef
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/libsyntax/parse/token.rs
Expand Up @@ -316,6 +316,17 @@ pub fn is_bar(t: &Token) -> bool {
match *t { BINOP(OR) | OROR => true, _ => false }
}

// Get the first "argument"
macro_rules! first {
( $first:expr, $( $remainder:expr, )* ) => ( $first )
}

// Get the last "argument" (has to be done recursively to avoid phoney local ambiguity error)
macro_rules! last {
( $first:expr, $( $remainder:expr, )+ ) => ( last!( $( $remainder, )+ ) );
( $first:expr, ) => ( $first )
}

// In this macro, there is the requirement that the name (the number) must be monotonically
// increasing by one in the special identifiers, starting at 0; the same holds for the keywords,
// except starting from the next number instead of zero, and with the additional exception that
Expand All @@ -329,9 +340,17 @@ macro_rules! declare_special_idents_and_keywords {(
}

pub mod keywords {
$( ($k_name:expr, $k_variant:ident, $k_str:expr); )*
'strict:
$( ($sk_name:expr, $sk_variant:ident, $sk_str:expr); )*
'reserved:
$( ($rk_name:expr, $rk_variant:ident, $rk_str:expr); )*
}
) => {
static STRICT_KEYWORD_START: Name = first!($( $sk_name, )*);
static STRICT_KEYWORD_FINAL: Name = last!($( $sk_name, )*);
static RESERVED_KEYWORD_START: Name = first!($( $rk_name, )*);
static RESERVED_KEYWORD_FINAL: Name = last!($( $rk_name, )*);

pub mod special_idents {
use ast::Ident;
$( pub static $si_static: Ident = Ident { name: $si_name, ctxt: 0 }; )*
Expand All @@ -348,13 +367,15 @@ macro_rules! declare_special_idents_and_keywords {(
use ast::Ident;

pub enum Keyword {
$( $k_variant, )*
$( $sk_variant, )*
$( $rk_variant, )*
}

impl Keyword {
pub fn to_ident(&self) -> Ident {
match *self {
$( $k_variant => Ident { name: $k_name, ctxt: 0 }, )*
$( $sk_variant => Ident { name: $sk_name, ctxt: 0 }, )*
$( $rk_variant => Ident { name: $rk_name, ctxt: 0 }, )*
}
}
}
Expand All @@ -366,20 +387,17 @@ macro_rules! declare_special_idents_and_keywords {(
// constants below.
let init_vec = ~[
$( $si_str, )*
$( $k_str, )*
$( $sk_str, )*
$( $rk_str, )*
];

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

// If modifying the numbers below, remember to modify these as appropriate
// If the special idents get renumbered, remember to modify these two as appropriate
static SELF_KEYWORD_NAME: Name = 3;
static STATIC_KEYWORD_NAME: Name = 10;
static STRICT_KEYWORD_START: Name = 14;
static STRICT_KEYWORD_FINAL: Name = 47;
static RESERVED_KEYWORD_START: Name = 48;
static RESERVED_KEYWORD_FINAL: Name = 54;

declare_special_idents_and_keywords! {
pub mod special_idents {
Expand Down Expand Up @@ -409,7 +427,7 @@ declare_special_idents_and_keywords! {
pub mod keywords {
// These ones are variants of the Keyword enum

// Strict keywords
'strict:
(14, As, "as");
(15, Break, "break");
(16, Const, "const");
Expand Down Expand Up @@ -448,7 +466,7 @@ declare_special_idents_and_keywords! {
(46, Continue, "continue");
(47, Proc, "proc");

// Reserved keywords
'reserved:
(48, Alignof, "alignof");
(49, Be, "be");
(50, Offsetof, "offsetof");
Expand Down

5 comments on commit dd042ef

@bors
Copy link
Contributor

@bors bors commented on dd042ef Dec 11, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from huonw
at chris-morgan@dd042ef

@bors
Copy link
Contributor

@bors bors commented on dd042ef Dec 11, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging chris-morgan/rust/macroize-(or-should-that-be-macroify)-syntax--parse--token-so-that-we-don't-make-mistakes-and-to-reduce-the-maintenance-burden = dd042ef into auto

@bors
Copy link
Contributor

@bors bors commented on dd042ef Dec 11, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chris-morgan/rust/macroize-(or-should-that-be-macroify)-syntax--parse--token-so-that-we-don't-make-mistakes-and-to-reduce-the-maintenance-burden = dd042ef merged ok, testing candidate = 47d10c7

@bors
Copy link
Contributor

@bors bors commented on dd042ef Dec 11, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on dd042ef Dec 11, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 47d10c7

Please sign in to comment.