Skip to content

Commit 7892602

Browse files
committed
converting lexed str to tokens
1 parent 8b9d145 commit 7892602

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

crates/parser/src/lexed_str.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! convenient to include a text-based lexer here!
77
//!
88
//! Note that these tokens, unlike the tokens we feed into the parser, do
9-
//! include info about comments and whitespace.
9+
//! include info about comments and whitespace.
1010
1111
use crate::{
1212
SyntaxKind::{self, *},
@@ -82,18 +82,45 @@ impl<'a> LexedStr<'a> {
8282
assert!(i < self.len());
8383
self.kind[i]
8484
}
85+
8586
pub fn text(&self, i: usize) -> &str {
8687
assert!(i < self.len());
8788
let lo = self.start[i] as usize;
8889
let hi = self.start[i + 1] as usize;
8990
&self.text[lo..hi]
9091
}
92+
9193
pub fn error(&self, i: usize) -> Option<&str> {
9294
assert!(i < self.len());
9395
let err = self.error.binary_search_by_key(&(i as u32), |i| i.token).ok()?;
9496
Some(self.error[err].msg.as_str())
9597
}
9698

99+
pub fn to_tokens(&self) -> crate::Tokens {
100+
let mut res = crate::Tokens::default();
101+
let mut was_joint = false;
102+
for i in 0..self.len() {
103+
let kind = self.kind(i);
104+
if kind.is_trivia() {
105+
was_joint = false
106+
} else {
107+
if kind == SyntaxKind::IDENT {
108+
let token_text = self.text(i);
109+
let contextual_kw = SyntaxKind::from_contextual_keyword(token_text)
110+
.unwrap_or(SyntaxKind::IDENT);
111+
res.push_ident(contextual_kw);
112+
} else {
113+
if was_joint {
114+
res.was_joint();
115+
}
116+
res.push(kind);
117+
}
118+
was_joint = true;
119+
}
120+
}
121+
res
122+
}
123+
97124
fn push(&mut self, kind: SyntaxKind, offset: usize) {
98125
self.kind.push(kind);
99126
self.start.push(offset as u32);

0 commit comments

Comments
 (0)