rust/src/lexer/mod.rs

48 lines
1.2 KiB
Rust
Raw Normal View History

2017-12-29 21:33:04 +01:00
use {Token, SyntaxKind};
use syntax_kinds::*;
mod ptr;
use self::ptr::Ptr;
2017-12-29 22:48:47 +01:00
mod classes;
use self::classes::*;
2017-12-29 21:33:04 +01:00
pub 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 {
// Note: r as in r" or r#" is part of a raw string literal,
// b as in b' is part of a byte literal.
// They are not identifiers, and are handled further down.
2017-12-29 22:48:47 +01:00
let ident_start = is_ident_start(c) && !string_literal_start(c, ptr.next(), ptr.nnext());
2017-12-29 21:33:04 +01:00
if ident_start {
2017-12-29 22:48:47 +01:00
ptr.bump_while(is_ident_continue);
return IDENT;
2017-12-29 21:33:04 +01:00
}
2017-12-29 22:48:47 +01:00
if is_whitespace(c) {
ptr.bump_while(is_whitespace);
return WHITESPACE;
}
2017-12-29 21:33:04 +01:00
2017-12-29 22:48:47 +01:00
return ERROR
2017-12-29 21:33:04 +01:00
}
fn string_literal_start(c: char, c1: Option<char>, c2: Option<char>) -> bool {
match (c, c1, c2) {
('r', Some('"'), _) |
('r', Some('#'), _) |
('b', Some('"'), _) |
('b', Some('\''), _) |
('b', Some('r'), Some('"')) |
('b', Some('r'), Some('#')) => true,
_ => false
}
}