1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
use {Token, SyntaxKind};
use syntax_kinds::*;
mod ptr;
use self::ptr::Ptr;
mod classes;
use self::classes::*;
mod numbers;
use self::numbers::scan_number;
mod strings;
use self::strings::{is_string_literal_start, scan_char, scan_byte_char_or_string, scan_string, scan_raw_string};
mod comments;
use self::comments::{scan_shebang, scan_comment};
pub fn tokenize(text: &str) -> Vec<Token> {
let mut text = text;
let mut acc = Vec::new();
while !text.is_empty() {
let token = next_token(text);
acc.push(token);
let len: u32 = token.len.into();
text = &text[len as usize..];
}
acc
}
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 {
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.next(), ptr.nnext());
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.
match c {
';' => return SEMI,
',' => return COMMA,
'(' => return L_PAREN,
')' => return R_PAREN,
'{' => return L_CURLY,
'}' => return R_CURLY,
'[' => return L_BRACK,
']' => return R_BRACK,
'<' => return L_ANGLE,
'>' => return R_ANGLE,
'@' => return AT,
'#' => return POUND,
'~' => return TILDE,
'?' => return QUESTION,
'$' => return DOLLAR,
'&' => return AMPERSAND,
'|' => return PIPE,
'+' => return PLUS,
'*' => return STAR,
'/' => return SLASH,
'^' => return CARET,
'%' => return PERCENT,
// Multi-byte tokens.
'.' => 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
},
'=' => return match ptr.next() {
Some('=') => {
ptr.bump();
EQEQ
}
Some('>') => {
ptr.bump();
FAT_ARROW
}
_ => EQ,
},
'!' => return match ptr.next() {
Some('=') => {
ptr.bump();
NEQ
}
_ => EXCL,
},
'-' => return if ptr.next_is('>') {
ptr.bump();
THIN_ARROW
} else {
MINUS
},
// If the character is an ident start not followed by another single
// quote, then this is a lifetime name:
'\'' => return if ptr.next_is_p(is_ident_start) && !ptr.nnext_is('\'') {
ptr.bump();
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; // TODO: error reporting
}
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_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);
if let Some(kind) = ident_to_keyword(ptr.current_token_text()) {
return kind;
}
IDENT
}
fn scan_literal_suffix(ptr: &mut Ptr) {
if ptr.next_is_p(is_ident_start) {
ptr.bump();
}
ptr.bump_while(is_ident_continue);
}
|