rlsp-yaml 0.6.1

A fast, lightweight YAML language server
Documentation
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// SPDX-License-Identifier: MIT

use tower_lsp::lsp_types::{
    SemanticToken, SemanticTokenModifier, SemanticTokenType, SemanticTokensLegend,
};

// Token type indices — order must match legend().
const TOKEN_PROPERTY: u32 = 0;
const TOKEN_STRING: u32 = 1;
const TOKEN_NUMBER: u32 = 2;
const TOKEN_KEYWORD: u32 = 3;
const TOKEN_VARIABLE: u32 = 4;
const TOKEN_TYPE: u32 = 5;
const TOKEN_COMMENT: u32 = 6;
const TOKEN_OPERATOR: u32 = 7;

// Token modifier bitmasks — order must match legend().
const MOD_DECLARATION: u32 = 1; // bit 0

/// Returns the semantic tokens legend (token types and modifiers).
#[must_use]
pub fn legend() -> SemanticTokensLegend {
    SemanticTokensLegend {
        token_types: vec![
            SemanticTokenType::PROPERTY, // 0
            SemanticTokenType::STRING,   // 1
            SemanticTokenType::NUMBER,   // 2
            SemanticTokenType::KEYWORD,  // 3
            SemanticTokenType::VARIABLE, // 4
            SemanticTokenType::TYPE,     // 5
            SemanticTokenType::COMMENT,  // 6
            SemanticTokenType::OPERATOR, // 7
        ],
        token_modifiers: vec![
            SemanticTokenModifier::DECLARATION, // bit 0
        ],
    }
}

/// A token with absolute position, before delta encoding.
struct RawToken {
    line: u32,
    start: u32,
    length: u32,
    token_type: u32,
    token_modifiers_bitset: u32,
}

/// Scans `text` line by line and returns semantic tokens in LSP delta-encoded format.
#[must_use]
pub fn semantic_tokens(text: &str) -> Vec<SemanticToken> {
    let mut raw: Vec<RawToken> = Vec::new();

    for (line_idx, line) in text.lines().enumerate() {
        // LSP line numbers fit in u32 for any realistic document.
        #[allow(clippy::cast_possible_truncation)]
        let line_no = line_idx as u32;
        let trimmed = line.trim_start();
        let indent = to_u32(line.len() - trimmed.len());

        if trimmed.starts_with('#') {
            raw.push(RawToken {
                line: line_no,
                start: indent,
                length: to_u32(trimmed.trim_end().chars().count()),
                token_type: TOKEN_COMMENT,
                token_modifiers_bitset: 0,
            });
            continue;
        }

        // Scan for inline markers (tags, anchors, aliases).
        collect_inline_markers(line, line_no, &mut raw);

        // Split on mapping colon.
        if let Some(colon_pos) = find_mapping_colon(line) {
            // Key: text before the colon.
            let key_raw = &line[..colon_pos];
            let key_text = strip_sequence_prefix(key_raw).trim();
            if !key_text.is_empty() && !starts_with_inline_marker(key_text) {
                // key_text is a subslice of line — pointer arithmetic gives the
                // exact byte offset without substring search.
                let key_byte = key_text.as_ptr() as usize - line.as_ptr() as usize;
                raw.push(RawToken {
                    line: line_no,
                    start: to_u32(char_col_of(line, key_byte)),
                    length: to_u32(key_text.chars().count()),
                    token_type: TOKEN_PROPERTY,
                    token_modifiers_bitset: 0,
                });
            }

            // Value: text after `: `.
            let after_colon = &line[colon_pos + 1..];
            let stripped = after_colon.trim_start_matches([' ', '\t']);
            if !stripped.is_empty() && !starts_with_inline_marker(stripped) {
                let value_byte =
                    line.len() - after_colon.len() + (after_colon.len() - stripped.len());
                let value_start = to_u32(char_col_of(line, value_byte));
                if let Some(rt) = classify_scalar(stripped, line_no, value_start) {
                    raw.push(rt);
                }
            }
        } else {
            // No mapping colon — bare scalar or sequence item.
            let content = strip_sequence_prefix(trimmed).trim();
            if !content.is_empty() && !starts_with_inline_marker(content) {
                // content is a subslice of line — pointer arithmetic gives the exact offset.
                let byte = content.as_ptr() as usize - line.as_ptr() as usize;
                let start = to_u32(char_col_of(line, byte));
                if let Some(rt) = classify_scalar(content, line_no, start) {
                    raw.push(rt);
                }
            }
        }
    }

    // Sort by (line, start) so delta encoding is correct regardless of the
    // order in which key / value / inline-marker tokens were collected.
    raw.sort_by_key(|t| (t.line, t.start));

    // Delta-encode.
    let mut tokens = Vec::with_capacity(raw.len());
    let mut prev_line = 0u32;
    let mut prev_start = 0u32;
    for rt in raw {
        let delta_line = rt.line - prev_line;
        let delta_start = if delta_line == 0 {
            rt.start - prev_start
        } else {
            rt.start
        };
        tokens.push(SemanticToken {
            delta_line,
            delta_start,
            length: rt.length,
            token_type: rt.token_type,
            token_modifiers_bitset: rt.token_modifiers_bitset,
        });
        prev_line = rt.line;
        prev_start = rt.start;
    }
    tokens
}

// ─────────────────────────────────────────────────────────────────────────────
// Internal helpers
// ─────────────────────────────────────────────────────────────────────────────

/// Classifies a scalar string and returns a `RawToken`, or `None` if empty.
fn classify_scalar(value: &str, line: u32, start: u32) -> Option<RawToken> {
    let trimmed = value.trim_end();
    if trimmed.is_empty() {
        return None;
    }

    // Block scalar indicators.
    if matches!(trimmed, "|" | ">" | "|-" | ">-" | "|+" | ">+") {
        return Some(RawToken {
            line,
            start,
            length: to_u32(trimmed.chars().count()),
            token_type: TOKEN_OPERATOR,
            token_modifiers_bitset: 0,
        });
    }

    // Quoted strings.
    if (trimmed.starts_with('"') && trimmed.ends_with('"') && trimmed.len() >= 2)
        || (trimmed.starts_with('\'') && trimmed.ends_with('\'') && trimmed.len() >= 2)
    {
        return Some(RawToken {
            line,
            start,
            length: to_u32(trimmed.chars().count()),
            token_type: TOKEN_STRING,
            token_modifiers_bitset: 0,
        });
    }

    // Keywords: booleans and null.
    let lower = trimmed.to_ascii_lowercase();
    if matches!(
        lower.as_str(),
        "true" | "false" | "yes" | "no" | "on" | "off" | "null" | "~"
    ) {
        return Some(RawToken {
            line,
            start,
            length: to_u32(trimmed.chars().count()),
            token_type: TOKEN_KEYWORD,
            token_modifiers_bitset: 0,
        });
    }

    // Numbers.
    if is_number(trimmed) {
        return Some(RawToken {
            line,
            start,
            length: to_u32(trimmed.chars().count()),
            token_type: TOKEN_NUMBER,
            token_modifiers_bitset: 0,
        });
    }

    // Fallback: unquoted scalar → STRING.
    Some(RawToken {
        line,
        start,
        length: to_u32(trimmed.chars().count()),
        token_type: TOKEN_STRING,
        token_modifiers_bitset: 0,
    })
}

/// Returns `true` if `s` looks like a YAML number.
fn is_number(s: &str) -> bool {
    let s = s.trim();
    if s.is_empty() {
        return false;
    }
    let s = s.strip_prefix('-').unwrap_or(s);
    if s.is_empty() {
        return false;
    }
    let (int_part, rest) = split_digits(s);
    if int_part.is_empty() {
        return false;
    }
    let rest = if let Some(r) = rest.strip_prefix('.') {
        let (frac, r2) = split_digits(r);
        if frac.is_empty() {
            return false;
        }
        r2
    } else {
        rest
    };
    if rest.is_empty() {
        return true;
    }
    let rest = if rest.starts_with(['e', 'E']) {
        let r = &rest[1..];
        r.strip_prefix(['+', '-']).unwrap_or(r)
    } else {
        return false;
    };
    let (exp_digits, leftover) = split_digits(rest);
    !exp_digits.is_empty() && leftover.is_empty()
}

/// Returns `(digit_prefix, remainder)`.
fn split_digits(s: &str) -> (&str, &str) {
    let end = s.find(|c: char| !c.is_ascii_digit()).unwrap_or(s.len());
    s.split_at(end)
}

/// Scans `line` for `!tag`, `&anchor`, and `*alias` outside quotes and appends
/// `RawToken`s to `out`.
fn collect_inline_markers(line: &str, line_no: u32, out: &mut Vec<RawToken>) {
    let mut in_single = false;
    let mut in_double = false;

    let mut iter = line.char_indices().peekable();
    while let Some((byte_pos, ch)) = iter.next() {
        match ch {
            '\'' if !in_double => in_single = !in_single,
            '"' if !in_single => in_double = !in_double,
            '#' if !in_single && !in_double => break,
            '!' | '&' | '*' if !in_single && !in_double => {
                let rest = &line[byte_pos..];
                let len_bytes = rest
                    .find(|c: char| c.is_whitespace() || c == ':' || c == ',')
                    .unwrap_or(rest.len());
                let token_str = &rest[..len_bytes];
                if token_str.len() > 1 {
                    let (token_type, modifiers) = match ch {
                        '!' => (TOKEN_TYPE, 0),
                        '&' => (TOKEN_VARIABLE, MOD_DECLARATION),
                        '*' => (TOKEN_VARIABLE, 0),
                        _ => unreachable!(),
                    };
                    out.push(RawToken {
                        line: line_no,
                        start: to_u32(char_col_of(line, byte_pos)),
                        length: to_u32(token_str.chars().count()),
                        token_type,
                        token_modifiers_bitset: modifiers,
                    });
                    // Advance the iterator past the rest of the token.
                    // We already consumed `ch`; skip `token_str.len() - 1` more bytes.
                    let end_byte = byte_pos + len_bytes;
                    // Drain characters until we reach end_byte.
                    while iter.peek().is_some_and(|(b, _)| *b < end_byte) {
                        iter.next();
                    }
                }
            }
            _ => {}
        }
    }
}

/// Returns `true` if `s` starts with a tag, anchor, or alias sigil.
fn starts_with_inline_marker(s: &str) -> bool {
    s.starts_with('!') || s.starts_with('&') || s.starts_with('*')
}

/// Strips a leading `- ` sequence prefix.
fn strip_sequence_prefix(s: &str) -> &str {
    s.strip_prefix("- ")
        .unwrap_or_else(|| if s == "-" { "" } else { s })
}

/// Returns the character-column offset of `byte_pos` in `line`.
fn char_col_of(line: &str, byte_pos: usize) -> usize {
    line[..byte_pos].chars().count()
}

/// Find the byte position of the mapping colon in a YAML line.
fn find_mapping_colon(line: &str) -> Option<usize> {
    let mut in_single_quote = false;
    let mut in_double_quote = false;

    for (i, ch) in line.char_indices() {
        match ch {
            '\'' if !in_double_quote => in_single_quote = !in_single_quote,
            '"' if !in_single_quote => in_double_quote = !in_double_quote,
            ':' if !in_single_quote && !in_double_quote => {
                let rest = &line[i + 1..];
                if rest.is_empty() || rest.starts_with(' ') || rest.starts_with('\t') {
                    return Some(i);
                }
            }
            _ => {}
        }
    }
    None
}

/// Converts a `usize` to `u32`, clamping at `u32::MAX`.
/// LSP positions are u32; documents exceeding 4 GB or 4 billion lines are
/// not supported in practice.
fn to_u32(v: usize) -> u32 {
    v.try_into().unwrap_or(u32::MAX)
}

#[cfg(test)]
#[allow(clippy::indexing_slicing, clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;

    /// Decode delta-encoded tokens into `(abs_line, abs_start, length, token_type, modifiers)`.
    fn absolute(tokens: &[SemanticToken]) -> Vec<(u32, u32, u32, u32, u32)> {
        let mut line = 0u32;
        let mut start = 0u32;
        tokens
            .iter()
            .map(|t| {
                line += t.delta_line;
                start = if t.delta_line == 0 {
                    start + t.delta_start
                } else {
                    t.delta_start
                };
                (
                    line,
                    start,
                    t.length,
                    t.token_type,
                    t.token_modifiers_bitset,
                )
            })
            .collect()
    }

    #[test]
    fn legend_has_correct_token_type_count() {
        assert_eq!(legend().token_types.len(), 8);
    }

    #[test]
    fn legend_has_correct_modifier_count() {
        assert_eq!(legend().token_modifiers.len(), 1);
    }

    #[test]
    fn empty_document_produces_no_tokens() {
        assert!(semantic_tokens("").is_empty());
    }

    #[test]
    fn comment_line_produces_comment_token() {
        let abs = absolute(&semantic_tokens("# comment"));
        let comments: Vec<_> = abs.iter().filter(|t| t.3 == TOKEN_COMMENT).collect();
        assert_eq!(comments.len(), 1);
        assert_eq!(comments[0].0, 0); // line 0
        assert_eq!(comments[0].1, 0); // col 0
    }

    #[test]
    fn comment_line_with_indent_starts_at_hash() {
        let abs = absolute(&semantic_tokens("  # indented comment"));
        let comments: Vec<_> = abs.iter().filter(|t| t.3 == TOKEN_COMMENT).collect();
        assert_eq!(comments.len(), 1);
        assert_eq!(comments[0].1, 2); // col 2
    }

    #[test]
    fn mapping_key_produces_property_token() {
        let abs = absolute(&semantic_tokens("name: value"));
        let keys: Vec<_> = abs.iter().filter(|t| t.3 == TOKEN_PROPERTY).collect();
        assert_eq!(keys.len(), 1);
        assert_eq!(keys[0].0, 0); // line 0
        assert_eq!(keys[0].1, 0); // col 0
        assert_eq!(keys[0].2, 4); // len("name")
    }

    #[test]
    fn string_value_produces_string_token() {
        let abs = absolute(&semantic_tokens("key: hello"));
        let strings: Vec<_> = abs.iter().filter(|t| t.3 == TOKEN_STRING).collect();
        assert!(!strings.is_empty());
        assert_eq!(strings[0].2, 5); // len("hello")
    }

    #[test]
    fn quoted_string_value_produces_string_token() {
        let abs = absolute(&semantic_tokens(r#"key: "quoted""#));
        assert!(abs.iter().any(|t| t.3 == TOKEN_STRING));
    }

    #[test]
    fn integer_value_produces_number_token() {
        let abs = absolute(&semantic_tokens("count: 42"));
        let nums: Vec<_> = abs.iter().filter(|t| t.3 == TOKEN_NUMBER).collect();
        assert_eq!(nums.len(), 1);
        assert_eq!(nums[0].2, 2); // len("42")
    }

    #[test]
    fn float_value_produces_number_token() {
        let abs = absolute(&semantic_tokens("pi: 3.14"));
        let nums: Vec<_> = abs.iter().filter(|t| t.3 == TOKEN_NUMBER).collect();
        assert_eq!(nums.len(), 1);
        assert_eq!(nums[0].2, 4); // len("3.14")
    }

    #[test]
    fn boolean_true_produces_keyword_token() {
        let abs = absolute(&semantic_tokens("flag: true"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_KEYWORD));
    }

    #[test]
    fn boolean_false_produces_keyword_token() {
        let abs = absolute(&semantic_tokens("flag: false"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_KEYWORD));
    }

    #[test]
    fn boolean_yes_produces_keyword_token() {
        let abs = absolute(&semantic_tokens("flag: yes"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_KEYWORD));
    }

    #[test]
    fn boolean_no_produces_keyword_token() {
        let abs = absolute(&semantic_tokens("flag: no"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_KEYWORD));
    }

    #[test]
    fn null_value_produces_keyword_token() {
        let abs = absolute(&semantic_tokens("x: null"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_KEYWORD));
    }

    #[test]
    fn tilde_null_produces_keyword_token() {
        let abs = absolute(&semantic_tokens("x: ~"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_KEYWORD));
    }

    #[test]
    fn anchor_produces_variable_with_declaration_modifier() {
        let abs = absolute(&semantic_tokens("base: &anchor value"));
        assert!(
            abs.iter()
                .any(|t| t.3 == TOKEN_VARIABLE && t.4 == MOD_DECLARATION)
        );
    }

    #[test]
    fn alias_produces_variable_without_modifier() {
        let abs = absolute(&semantic_tokens("child: *anchor"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_VARIABLE && t.4 == 0));
    }

    #[test]
    fn tag_produces_type_token() {
        let abs = absolute(&semantic_tokens("value: !include file.yaml"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_TYPE));
    }

    #[test]
    fn block_scalar_pipe_produces_operator_token() {
        let abs = absolute(&semantic_tokens("text: |"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_OPERATOR));
    }

    #[test]
    fn block_scalar_gt_produces_operator_token() {
        let abs = absolute(&semantic_tokens("text: >"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_OPERATOR));
    }

    #[test]
    fn delta_encoding_correct_for_multi_line_document() {
        let text = "a: 1\nb: 2\n";
        let abs = absolute(&semantic_tokens(text));
        let keys: Vec<_> = abs.iter().filter(|t| t.3 == TOKEN_PROPERTY).collect();
        assert!(keys.iter().any(|k| k.0 == 0 && k.1 == 0)); // "a" line 0 col 0
        assert!(keys.iter().any(|k| k.0 == 1 && k.1 == 0)); // "b" line 1 col 0
    }

    #[test]
    fn delta_line_is_zero_for_tokens_on_same_line() {
        let tokens = semantic_tokens("key: value");
        for t in tokens.iter().skip(1) {
            assert_eq!(t.delta_line, 0);
        }
    }

    #[test]
    fn delta_start_is_relative_to_previous_token_on_same_line() {
        // "key: value" — property at col 0 (len 3), string at col 5 (len 5).
        let abs = absolute(&semantic_tokens("key: value"));
        let prop = abs.iter().find(|t| t.3 == TOKEN_PROPERTY).unwrap();
        let str_tok = abs.iter().find(|t| t.3 == TOKEN_STRING).unwrap();
        assert_eq!(prop.1, 0);
        assert_eq!(str_tok.1, 5);
    }

    // ---- Additional coverage tests ----

    // strip_sequence_prefix: bare dash returns empty (no token)
    #[test]
    fn bare_dash_sequence_item_produces_no_token() {
        // A bare "-" with no value should produce no scalar token
        let abs = absolute(&semantic_tokens("items:\n  -\n"));
        // Only "items" key token expected; no STRING/NUMBER/KEYWORD for bare "-"
        let non_property: Vec<_> = abs.iter().filter(|t| t.3 != TOKEN_PROPERTY).collect();
        assert!(
            non_property.is_empty(),
            "bare '-' should produce no scalar token, got: {non_property:?}"
        );
    }

    // is_number: negative integer
    #[test]
    fn negative_integer_produces_number_token() {
        let abs = absolute(&semantic_tokens("temp: -42"));
        let nums: Vec<_> = abs.iter().filter(|t| t.3 == TOKEN_NUMBER).collect();
        assert_eq!(nums.len(), 1);
        assert_eq!(nums[0].2, 3); // len("-42")
    }

    // is_number: scientific notation
    #[test]
    fn scientific_notation_produces_number_token() {
        let abs = absolute(&semantic_tokens("val: 1.5e10"));
        assert_eq!(abs.iter().filter(|t| t.3 == TOKEN_NUMBER).count(), 1);
    }

    // is_number: negative scientific notation
    #[test]
    fn negative_float_produces_number_token() {
        let abs = absolute(&semantic_tokens("val: -3.14"));
        let nums: Vec<_> = abs.iter().filter(|t| t.3 == TOKEN_NUMBER).collect();
        assert_eq!(nums.len(), 1);
        assert_eq!(nums[0].2, 5); // len("-3.14")
    }

    // block scalar variants: |- and >-
    #[test]
    fn block_scalar_pipe_minus_produces_operator_token() {
        let abs = absolute(&semantic_tokens("text: |-"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_OPERATOR));
    }

    #[test]
    fn block_scalar_gt_minus_produces_operator_token() {
        let abs = absolute(&semantic_tokens("text: >-"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_OPERATOR));
    }

    #[test]
    fn block_scalar_pipe_plus_produces_operator_token() {
        let abs = absolute(&semantic_tokens("text: |+"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_OPERATOR));
    }

    #[test]
    fn block_scalar_gt_plus_produces_operator_token() {
        let abs = absolute(&semantic_tokens("text: >+"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_OPERATOR));
    }

    // sequence item with string value (no mapping colon)
    #[test]
    fn sequence_item_string_value_produces_string_token() {
        let abs = absolute(&semantic_tokens("- hello"));
        let strings: Vec<_> = abs.iter().filter(|t| t.3 == TOKEN_STRING).collect();
        assert_eq!(strings.len(), 1);
        assert_eq!(strings[0].2, 5); // len("hello")
    }

    // sequence item with number value
    #[test]
    fn sequence_item_number_value_produces_number_token() {
        let abs = absolute(&semantic_tokens("- 42"));
        assert_eq!(abs.iter().filter(|t| t.3 == TOKEN_NUMBER).count(), 1);
    }

    // sequence item with keyword value
    #[test]
    fn sequence_item_keyword_value_produces_keyword_token() {
        let abs = absolute(&semantic_tokens("- true"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_KEYWORD));
    }

    // inline tag on value side (key: !tag value)
    #[test]
    fn tag_on_value_side_of_mapping_produces_type_token() {
        let abs = absolute(&semantic_tokens("key: !str hello"));
        assert!(
            abs.iter().any(|t| t.3 == TOKEN_TYPE),
            "tag on value side should produce type token"
        );
    }

    // anchor on sequence item
    #[test]
    fn anchor_on_sequence_item_produces_variable_with_declaration() {
        let abs = absolute(&semantic_tokens("- &myanchor value"));
        assert!(
            abs.iter()
                .any(|t| t.3 == TOKEN_VARIABLE && t.4 == MOD_DECLARATION),
            "anchor on sequence item should produce variable with declaration modifier"
        );
    }

    // keyword variants: on/off
    #[test]
    fn on_keyword_produces_keyword_token() {
        let abs = absolute(&semantic_tokens("flag: on"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_KEYWORD));
    }

    #[test]
    fn off_keyword_produces_keyword_token() {
        let abs = absolute(&semantic_tokens("flag: off"));
        assert!(abs.iter().any(|t| t.3 == TOKEN_KEYWORD));
    }

    // comment stops inline marker scan (# in middle of line)
    #[test]
    fn inline_comment_stops_marker_scan() {
        // "&anchor" after "#" should NOT be treated as an anchor token
        let abs = absolute(&semantic_tokens("key: value # &notananchor"));
        assert!(
            abs.iter().all(|t| t.3 != TOKEN_VARIABLE),
            "marker inside comment should not produce variable token"
        );
    }

    // delta_line is correct when crossing multiple lines
    #[test]
    fn delta_line_correct_across_multiple_lines() {
        let text = "a: 1\n\nb: 2\n";
        let abs = absolute(&semantic_tokens(text));
        let keys: Vec<_> = abs.iter().filter(|t| t.3 == TOKEN_PROPERTY).collect();
        assert!(keys.iter().any(|k| k.0 == 0)); // "a" on line 0
        assert!(keys.iter().any(|k| k.0 == 2)); // "b" on line 2 (blank line skipped)
    }
}