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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::scanner::*;
use crate::token::{Token, TokenKind};
pub struct YamlScanner;
fn at(b: &[u8], i: usize) -> u8 {
if i < b.len() { b[i] } else { 0 }
}
impl Scanner for YamlScanner {
fn scan(&self, code: &str) -> Vec<Token> {
let b = code.as_bytes();
let mut tokens = Vec::new();
let mut i = 0;
let mut line_start = true;
while i < b.len() {
let c = b[i];
if c == b'\n' {
line_start = true;
i += 1;
continue;
}
if c == b' ' || c == b'\t' || c == b'\r' {
i += 1;
continue;
}
// Document markers
if line_start && (b[i..].starts_with(b"---") || b[i..].starts_with(b"...")) {
let start = i;
i += 3;
tokens.push(Token {
kind: TokenKind::Keyword,
start,
end: i,
});
line_start = false;
continue;
}
// Comments
if c == b'#'
&& let Some(end) = scan_hash_comment(b, i)
{
tokens.push(Token {
kind: TokenKind::Comment,
start: i,
end,
});
i = end;
continue;
}
// Strings
if c == b'"'
&& let Some(end) = scan_double_string(b, i)
{
tokens.push(Token {
kind: TokenKind::String,
start: i,
end,
});
i = end;
line_start = false;
continue;
}
if c == b'\''
&& let Some(end) = scan_single_string(b, i)
{
tokens.push(Token {
kind: TokenKind::String,
start: i,
end,
});
i = end;
line_start = false;
continue;
}
// Key: value pattern — scan identifier then check for :
if c.is_ascii_alphanumeric() || c == b'_' || c == b'-' {
let start = i;
while i < b.len()
&& (b[i].is_ascii_alphanumeric()
|| b[i] == b'_'
|| b[i] == b'-'
|| b[i] == b'.')
{
i += 1;
}
let ident = &b[start..i];
let after = skip_whitespace_no_newline(b, i);
if at(b, after) == b':'
&& (at(b, after + 1) == b' '
|| at(b, after + 1) == b'\n'
|| after + 1 >= b.len())
{
tokens.push(Token {
kind: TokenKind::Property,
start,
end: i,
});
} else {
// Value keywords
let kind = match ident {
b"true" | b"false" | b"yes" | b"no" | b"on" | b"off" | b"True"
| b"False" | b"Yes" | b"No" => TokenKind::Keyword,
b"null" | b"Null" | b"~" => TokenKind::Keyword,
_ => {
// Try number: must start with digit or sign followed by digit
if !ident.is_empty()
&& (ident[0].is_ascii_digit()
|| ((ident[0] == b'-' || ident[0] == b'+')
&& ident.len() > 1
&& ident[1].is_ascii_digit()))
&& ident[1..].iter().all(|&c| {
c.is_ascii_digit()
|| c == b'.'
|| c == b'e'
|| c == b'E'
|| c == b'_'
|| c == b'+'
|| c == b'-'
})
{
TokenKind::Number
} else {
TokenKind::Plain
}
}
};
tokens.push(Token {
kind,
start,
end: i,
});
}
line_start = false;
continue;
}
// Punctuation: : - [ ] { } ,
if matches!(c, b':' | b'-' | b'[' | b']' | b'{' | b'}' | b',') {
tokens.push(Token {
kind: TokenKind::Punctuation,
start: i,
end: i + 1,
});
i += 1;
line_start = false;
continue;
}
// Anchors & aliases
if c == b'&' || c == b'*' {
let start = i;
i += 1;
while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'_' || b[i] == b'-')
{
i += 1;
}
tokens.push(Token {
kind: TokenKind::Variable,
start,
end: i,
});
line_start = false;
continue;
}
// Tags !tag
if c == b'!' {
let start = i;
i += 1;
while i < b.len() && !b[i].is_ascii_whitespace() {
i += 1;
}
tokens.push(Token {
kind: TokenKind::Attr,
start,
end: i,
});
line_start = false;
continue;
}
line_start = false;
i += 1;
}
tokens
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::token::render;
fn hl(code: &str) -> String {
let tokens = YamlScanner.scan(code);
render(code, &tokens)
}
#[test]
fn key_value() {
let out = hl("name: test");
assert!(out.contains("tok-property"));
}
#[test]
fn boolean() {
let out = hl("enabled: true");
assert!(out.contains("tok-keyword"));
}
#[test]
fn comment() {
let out = hl("# comment");
assert!(out.contains("tok-comment"));
}
#[test]
fn document_marker() {
let out = hl("---");
assert!(out.contains("tok-keyword"));
}
#[test]
fn string() {
let out = hl(r#"name: "hello""#);
assert!(out.contains("tok-string"));
}
}