rust/src/lexer/mod.rs

177 lines
4.4 KiB
Rust
Raw Normal View History

2017-12-29 14:33:04 -06:00
use {Token, SyntaxKind};
use syntax_kinds::*;
mod ptr;
use self::ptr::Ptr;
2017-12-29 15:48:47 -06:00
mod classes;
use self::classes::*;
2017-12-30 06:56:52 -06:00
mod numbers;
use self::numbers::scan_number;
2017-12-29 14:33:04 -06: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 15:48:47 -06:00
let ident_start = is_ident_start(c) && !string_literal_start(c, ptr.next(), ptr.nnext());
2017-12-29 14:33:04 -06:00
if ident_start {
2017-12-30 06:22:40 -06:00
return scan_ident(c, ptr);
2017-12-29 14:33:04 -06:00
}
2017-12-29 15:48:47 -06:00
if is_whitespace(c) {
ptr.bump_while(is_whitespace);
return WHITESPACE;
}
2017-12-29 14:33:04 -06:00
2017-12-30 06:22:40 -06:00
if is_dec_digit(c) {
2017-12-31 01:41:42 -06:00
let kind = scan_number(c, ptr);
scan_literal_suffix(ptr);
return kind;
2017-12-30 06:22:40 -06:00
}
2017-12-30 07:30:37 -06:00
// One-byte tokens.
match c {
';' => return SEMI,
',' => return COMMA,
'(' => return L_PAREN,
')' => return R_PAREN,
'{' => return L_CURLY,
'}' => return R_CURLY,
'[' => return L_BRACK,
']' => return R_BRACK,
2017-12-30 11:56:54 -06:00
'<' => return L_ANGLE,
'>' => return R_ANGLE,
2017-12-30 07:30:37 -06:00
'@' => return AT,
'#' => return POUND,
'~' => return TILDE,
'?' => return QUESTION,
'$' => return DOLLAR,
2017-12-30 09:25:37 -06:00
// Multi-byte tokens.
2017-12-30 07:30:37 -06:00
'.' => return match (ptr.next(), ptr.nnext()) {
(Some('.'), Some('.')) => {
ptr.bump();
ptr.bump();
DOTDOTDOT
},
(Some('.'), Some('=')) => {
ptr.bump();
ptr.bump();
DOTDOTEQ
},
(Some('.'), _) => {
ptr.bump();
DOTDOT
},
_ => DOT
},
':' => return match ptr.next() {
Some(':') => {
ptr.bump();
COLONCOLON
}
_ => COLON
},
2017-12-30 09:25:37 -06:00
'=' => return match ptr.next() {
Some('=') => {
ptr.bump();
EQEQ
}
Some('>') => {
ptr.bump();
FAT_ARROW
}
_ => EQ,
},
'!' => return match ptr.next() {
Some('=') => {
ptr.bump();
NEQ
}
_ => NOT,
},
2017-12-31 01:41:42 -06:00
'\'' => return scan_char_or_lifetime(ptr),
2017-12-30 07:30:37 -06:00
_ => (),
}
2017-12-29 15:53:06 -06:00
ERROR
2017-12-29 14:33:04 -06:00
}
2017-12-30 06:22:40 -06:00
fn scan_ident(c: char, ptr: &mut Ptr) -> SyntaxKind {
let is_single_letter = match ptr.next() {
None => true,
Some(c) if !is_ident_continue(c) => true,
_ => false,
};
if is_single_letter {
return if c == '_' { UNDERSCORE } else { IDENT };
}
ptr.bump_while(is_ident_continue);
IDENT
}
2017-12-31 01:41:42 -06:00
fn scan_char_or_lifetime(ptr: &mut Ptr) -> SyntaxKind {
// Either a character constant 'a' OR a lifetime name 'abc
let c = match ptr.bump() {
Some(c) => c,
None => return CHAR, // TODO: error reporting is upper in the stack
};
// If the character is an ident start not followed by another single
// quote, then this is a lifetime name:
if is_ident_start(c) && !ptr.next_is('\'') {
while ptr.next_is_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.next_is('\'') {
ptr.bump();
return CHAR;
}
return LIFETIME;
}
scan_char_or_byte(ptr);
if !ptr.next_is('\'') {
return CHAR; // TODO: error reporting
}
ptr.bump();
scan_literal_suffix(ptr);
CHAR
}
fn scan_literal_suffix(ptr: &mut Ptr) {
2017-12-31 02:15:27 -06:00
if ptr.next_is_p(is_ident_start) {
ptr.bump();
}
ptr.bump_while(is_ident_continue);
2017-12-31 01:41:42 -06:00
}
fn scan_char_or_byte(ptr: &mut Ptr) {
//FIXME: deal with escape sequencies
2017-12-31 01:41:42 -06:00
ptr.bump();
}
2017-12-29 14:33:04 -06: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
}
}