about summary refs log tree commit diff
path: root/src/libsyntax_pos
diff options
context:
space:
mode:
authorAgustin Chiappe Berrini <jnieve@gmail.com>2017-12-06 04:28:01 -0500
committerAgustin Chiappe Berrini <jnieve@gmail.com>2017-12-06 04:28:01 -0500
commit65ccf24ce8e51b199d60d06ad41ea35f4cdee15c (patch)
tree733ff2b5a3998cc91517bb2aa1fae3ea5f42ad7c /src/libsyntax_pos
parenta2899408dd162bfe349e7ff8bceaee60b34e77cc (diff)
downloadrust-65ccf24ce8e51b199d60d06ad41ea35f4cdee15c.tar.gz
rust-65ccf24ce8e51b199d60d06ad41ea35f4cdee15c.zip
and refactor to just move the checking
Diffstat (limited to 'src/libsyntax_pos')
-rw-r--r--src/libsyntax_pos/symbol.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs
index 69ddd560213..c904e93e63a 100644
--- a/src/libsyntax_pos/symbol.rs
+++ b/src/libsyntax_pos/symbol.rs
@@ -35,9 +35,17 @@ impl Ident {
         Ident::with_empty_ctxt(Symbol::intern(string))
     }
 
+    pub fn without_first_quote(&self) -> Ident {
+        Ident { name: self.name.without_first_quote(), ctxt: self.ctxt }
+    }
+
     pub fn modern(self) -> Ident {
         Ident { name: self.name, ctxt: self.ctxt.modern() }
     }
+
+    pub fn is_valid(&self) -> bool {
+        !self.name.is_used_keyword() && !self.name.is_unused_keyword()
+    }
 }
 
 impl fmt::Debug for Ident {
@@ -113,6 +121,24 @@ impl Symbol {
     pub fn as_u32(self) -> u32 {
         self.0
     }
+
+    /// Returns `true` if the token is a keyword used in the language.
+    pub fn is_used_keyword(&self) -> bool {
+        self >= &keywords::As.name() && self <= &keywords::While.name()
+    }
+
+    /// Returns `true` if the token is a keyword reserved for possible future use.
+    pub fn is_unused_keyword(&self) -> bool {
+        self >= &keywords::Abstract.name() && self <= &keywords::Yield.name()
+    }
+
+    pub fn is_static_keyword(&self) -> bool {
+        self == &keywords::StaticLifetime.name()
+    }
+
+    pub fn without_first_quote(&self) -> Symbol {
+        Symbol::from(self.as_str().trim_left_matches('\''))
+    }
 }
 
 impl<'a> From<&'a str> for Symbol {
@@ -428,4 +454,30 @@ mod tests {
         // gensym of *existing* string gets new number:
         assert_eq!(i.gensym("dog"), Symbol(4294967293));
     }
+
+    #[test]
+    fn is_used_keyword_test() {
+        let s = Symbol(5);
+        assert_eq!(s.is_used_keyword(), true);
+    }
+
+    #[test]
+    fn is_unused_keyword_test() {
+        let s = Symbol(40);
+        assert_eq!(s.is_unused_keyword(), true);
+    }
+
+    #[test]
+    fn is_valid_test() {
+        let i = Ident { name: Symbol(40), ctxt: SyntaxContext(0) };
+        assert_eq!(i.is_valid(), false);
+        let i = Ident { name: Symbol(61), ctxt: SyntaxContext(0) };
+        assert_eq!(i.is_valid(), true);
+    }
+
+    #[test]
+    fn without_first_quote_test() {
+        let i = Ident::from_str("'break");
+        assert_eq!(i.without_first_quote().name, keywords::Break.name());
+    }
 }