about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2021-12-18 15:36:21 +0300
committerAleksey Kladov <aleksey.kladov@gmail.com>2021-12-18 15:36:21 +0300
commit78926027e33573a348c17d9bc4c5d4ca09718f96 (patch)
treebe5e4276434e6c79e434c6e546d9d958fce11050
parent8b9d145dea17dc28d83fae23b5be63233483ec6d (diff)
downloadrust-78926027e33573a348c17d9bc4c5d4ca09718f96.tar.gz
rust-78926027e33573a348c17d9bc4c5d4ca09718f96.zip
converting lexed str to tokens
-rw-r--r--crates/parser/src/lexed_str.rs29
1 files changed, 28 insertions, 1 deletions
diff --git a/crates/parser/src/lexed_str.rs b/crates/parser/src/lexed_str.rs
index 595b6072293..9c5d27f51d0 100644
--- a/crates/parser/src/lexed_str.rs
+++ b/crates/parser/src/lexed_str.rs
@@ -6,7 +6,7 @@
 //! convenient to include a text-based lexer here!
 //!
 //! Note that these tokens, unlike the tokens we feed into the parser, do
-//! include info about comments and whitespace. 
+//! include info about comments and whitespace.
 
 use crate::{
     SyntaxKind::{self, *},
@@ -82,18 +82,45 @@ impl<'a> LexedStr<'a> {
         assert!(i < self.len());
         self.kind[i]
     }
+
     pub fn text(&self, i: usize) -> &str {
         assert!(i < self.len());
         let lo = self.start[i] as usize;
         let hi = self.start[i + 1] as usize;
         &self.text[lo..hi]
     }
+
     pub fn error(&self, i: usize) -> Option<&str> {
         assert!(i < self.len());
         let err = self.error.binary_search_by_key(&(i as u32), |i| i.token).ok()?;
         Some(self.error[err].msg.as_str())
     }
 
+    pub fn to_tokens(&self) -> crate::Tokens {
+        let mut res = crate::Tokens::default();
+        let mut was_joint = false;
+        for i in 0..self.len() {
+            let kind = self.kind(i);
+            if kind.is_trivia() {
+                was_joint = false
+            } else {
+                if kind == SyntaxKind::IDENT {
+                    let token_text = self.text(i);
+                    let contextual_kw = SyntaxKind::from_contextual_keyword(token_text)
+                        .unwrap_or(SyntaxKind::IDENT);
+                    res.push_ident(contextual_kw);
+                } else {
+                    if was_joint {
+                        res.was_joint();
+                    }
+                    res.push(kind);
+                }
+                was_joint = true;
+            }
+        }
+        res
+    }
+
     fn push(&mut self, kind: SyntaxKind, offset: usize) {
         self.kind.push(kind);
         self.start.push(offset as u32);