kill old lexer

This commit is contained in:
Aleksey Kladov 2019-07-22 17:56:19 +03:00
parent 75761c0e47
commit 700669bbd0
6 changed files with 17 additions and 571 deletions

View file

@ -1,22 +1,6 @@
mod classes;
mod comments;
mod numbers;
mod ptr;
mod strings;
use crate::{
SyntaxKind::{self, *},
TextUnit, T,
};
use self::{
classes::*,
comments::{scan_comment, scan_shebang},
numbers::scan_number,
ptr::Ptr,
strings::{
is_string_literal_start, scan_byte_char_or_string, scan_char, scan_raw_string, scan_string,
},
TextUnit,
};
/// A token of Rust source.
@ -141,138 +125,23 @@ pub fn tokenize(text: &str) -> Vec<Token> {
acc
}
/// Get the next token from a string
fn next_token(text: &str) -> Token {
assert!(!text.is_empty());
let mut ptr = Ptr::new(text);
let c = ptr.bump().unwrap();
let kind = next_token_inner(c, &mut ptr);
let len = ptr.into_len();
Token { kind, len }
}
fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
if is_whitespace(c) {
ptr.bump_while(is_whitespace);
return WHITESPACE;
}
match c {
'#' => {
if scan_shebang(ptr) {
return SHEBANG;
}
}
'/' => {
if let Some(kind) = scan_comment(ptr) {
return kind;
}
}
_ => (),
}
let ident_start = is_ident_start(c) && !is_string_literal_start(c, ptr.current(), ptr.nth(1));
if ident_start {
return scan_ident(c, ptr);
}
if is_dec_digit(c) {
let kind = scan_number(c, ptr);
scan_literal_suffix(ptr);
return kind;
}
// One-byte tokens.
if let Some(kind) = SyntaxKind::from_char(c) {
return kind;
}
match c {
// Possiblily multi-byte tokens,
// but we only produce single byte token now
// T![...], T![..], T![..=], T![.]
'.' => return T![.],
// T![::] T![:]
':' => return T![:],
// T![==] FATARROW T![=]
'=' => return T![=],
// T![!=] T![!]
'!' => return T![!],
// T![->] T![-]
'-' => return T![-],
// If the character is an ident start not followed by another single
// quote, then this is a lifetime name:
'\'' => {
return if ptr.at_p(is_ident_start) && !ptr.at_str("''") {
ptr.bump();
while ptr.at_p(is_ident_continue) {
ptr.bump();
}
// lifetimes shouldn't end with a single quote
// if we find one, then this is an invalid character literal
if ptr.at('\'') {
ptr.bump();
return CHAR;
}
LIFETIME
} else {
scan_char(ptr);
scan_literal_suffix(ptr);
CHAR
};
}
'b' => {
let kind = scan_byte_char_or_string(ptr);
scan_literal_suffix(ptr);
return kind;
}
'"' => {
scan_string(ptr);
scan_literal_suffix(ptr);
return STRING;
}
'r' => {
scan_raw_string(ptr);
scan_literal_suffix(ptr);
return RAW_STRING;
}
_ => (),
}
ERROR
}
fn scan_ident(c: char, ptr: &mut Ptr) -> SyntaxKind {
let is_raw = match (c, ptr.current()) {
('r', Some('#')) => {
ptr.bump();
true
}
('_', None) => return T![_],
('_', Some(c)) if !is_ident_continue(c) => return T![_],
_ => false,
};
ptr.bump_while(is_ident_continue);
if !is_raw {
if let Some(kind) = SyntaxKind::from_keyword(ptr.current_token_text()) {
return kind;
}
}
IDENT
}
fn scan_literal_suffix(ptr: &mut Ptr) {
if ptr.at_p(is_ident_start) {
ptr.bump();
}
ptr.bump_while(is_ident_continue);
}
pub fn classify_literal(text: &str) -> Option<Token> {
let tkn = next_token(text);
if !tkn.kind.is_literal() || tkn.len.to_usize() != text.len() {
let t = ra_rustc_lexer::first_token(text);
if t.len != text.len() {
return None;
}
Some(tkn)
let kind = match t.kind {
ra_rustc_lexer::TokenKind::Literal { kind, .. } => match kind {
ra_rustc_lexer::LiteralKind::Int { .. } => INT_NUMBER,
ra_rustc_lexer::LiteralKind::Float { .. } => FLOAT_NUMBER,
ra_rustc_lexer::LiteralKind::Char { .. } => CHAR,
ra_rustc_lexer::LiteralKind::Byte { .. } => BYTE,
ra_rustc_lexer::LiteralKind::Str { .. } => STRING,
ra_rustc_lexer::LiteralKind::ByteStr { .. } => BYTE_STRING,
ra_rustc_lexer::LiteralKind::RawStr { .. } => RAW_STRING,
ra_rustc_lexer::LiteralKind::RawByteStr { .. } => RAW_BYTE_STRING,
},
_ => return None,
};
Some(Token { kind, len: TextUnit::from_usize(t.len) })
}