fhp-tokenizer 0.1.2

SIMD-accelerated HTML tokenizer with structural indexing
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
//! Branchless state machine for HTML token extraction.
//!
//! The core idea: a 2D lookup table `STATE_TABLE[state][byte_class]` maps
//! every (state, input-byte-class) pair to a `(new_state, action)` without
//! any conditional branches. This eliminates branch misprediction costs
//! that dominate traditional HTML tokenizers.

/// Tokenizer states — models the HTML5 tokenizer states relevant for
/// our structural-index-driven approach.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum State {
    /// Outside any tag — consuming text content.
    Data = 0,
    /// Saw `<` — deciding if open tag, close tag, comment, or doctype.
    TagOpen,
    /// Inside an open tag name (e.g. reading `div` in `<div`).
    TagName,
    /// Saw `</` — expecting a close tag name.
    EndTagOpen,
    /// Inside a close tag name.
    EndTagName,
    /// After tag name, before attribute name or `>`.
    BeforeAttrName,
    /// Inside an attribute name.
    AttrName,
    /// After attribute name, before `=` or next attribute.
    AfterAttrName,
    /// Saw `=` after attribute name — expecting value.
    BeforeAttrValue,
    /// Inside a quoted attribute value.
    AttrValueQuoted,
    /// Inside an unquoted attribute value.
    AttrValueUnquoted,
    /// Saw `/` inside a tag — expecting `>` for self-closing.
    SelfClosingStartTag,
    /// Inside `<!` — detecting comment vs doctype vs CDATA.
    MarkupDecl,
    /// Inside `<!--` comment body.
    Comment,
    /// Saw first `-` at end of comment (`-`).
    CommentEndDash,
    /// Saw `--` at end of comment.
    CommentEnd,
    /// Inside `<!DOCTYPE` content.
    Doctype,
    /// Inside `<![CDATA[` content.
    CData,
    /// Inside raw text elements (`<script>`, `<style>`).
    RawText,
}

/// Number of states — used for table dimensions.
pub const STATE_COUNT: usize = 19;

/// Byte classification — maps raw bytes to a small enum for table indexing.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum ByteClass {
    /// `<`
    Lt = 0,
    /// `>`
    Gt,
    /// `/`
    Slash,
    /// `=`
    Eq,
    /// `"` or `'`
    Quot,
    /// `&`
    Amp,
    /// `!`
    Bang,
    /// `-`
    Dash,
    /// `a-z`, `A-Z`
    Alpha,
    /// Space, tab, newline, carriage return
    Whitespace,
    /// Everything else
    Other,
}

/// Number of byte classes — used for table dimensions.
pub const BYTE_CLASS_COUNT: usize = 11;

impl ByteClass {
    /// Classify a raw byte into its [`ByteClass`].
    #[inline(always)]
    pub fn from_byte(b: u8) -> Self {
        match b {
            b'<' => ByteClass::Lt,
            b'>' => ByteClass::Gt,
            b'/' => ByteClass::Slash,
            b'=' => ByteClass::Eq,
            b'"' | b'\'' => ByteClass::Quot,
            b'&' => ByteClass::Amp,
            b'!' => ByteClass::Bang,
            b'-' => ByteClass::Dash,
            b'a'..=b'z' | b'A'..=b'Z' => ByteClass::Alpha,
            b' ' | b'\t' | b'\n' | b'\r' => ByteClass::Whitespace,
            _ => ByteClass::Other,
        }
    }
}

/// Actions to perform during state transitions.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum Action {
    /// Do nothing.
    None = 0,
    /// Flush accumulated text as a Text token.
    FlushText,
    /// Begin recording an open tag name.
    StartTag,
    /// Begin recording a close tag name.
    StartEndTag,
    /// Emit the open tag (tag name is complete).
    EmitTagName,
    /// Emit the close tag name.
    EmitEndTagName,
    /// Begin recording an attribute name.
    StartAttrName,
    /// Attribute name is complete.
    EmitAttrName,
    /// Begin recording attribute value.
    StartAttrValue,
    /// Emit attribute value (quoted attribute complete).
    EmitAttrValue,
    /// Emit self-closing tag.
    EmitSelfClose,
    /// Begin comment recording.
    StartComment,
    /// Emit comment token.
    EmitComment,
    /// Begin doctype recording.
    StartDoctype,
    /// Emit doctype token.
    EmitDoctype,
    /// Begin CDATA recording.
    StartCData,
    /// Emit CDATA token.
    EmitCData,
    /// Enter raw text mode (script/style).
    EnterRawText,
    /// Emit open tag and close it (for `>`).
    EmitOpenTagClose,
}

/// A state transition entry: new state + action to perform.
#[derive(Clone, Copy, Debug)]
pub struct Transition {
    /// The next state.
    pub state: State,
    /// The action to perform.
    pub action: Action,
}

impl Transition {
    /// No-op transition: stay in current state, do nothing.
    const fn noop(state: State) -> Self {
        Self {
            state,
            action: Action::None,
        }
    }

    /// Transition to a new state with an action.
    const fn new(state: State, action: Action) -> Self {
        Self { state, action }
    }
}

/// The master state transition table.
///
/// `STATE_TABLE[state][byte_class]` yields the `Transition` to apply.
/// Const-initialized at compile time — no runtime cost.
pub static STATE_TABLE: [[Transition; BYTE_CLASS_COUNT]; STATE_COUNT] = build_state_table();

/// Build the state transition table at compile time.
const fn build_state_table() -> [[Transition; BYTE_CLASS_COUNT]; STATE_COUNT] {
    // Default: stay in same state, do nothing. We'll fill per-state below.
    // Can't use Default in const, so manually init.
    let noop = Transition::noop(State::Data);
    let mut table = [[noop; BYTE_CLASS_COUNT]; STATE_COUNT];

    // ----- Data state -----
    // Default: stay in Data
    table[State::Data as usize][ByteClass::Lt as usize] =
        Transition::new(State::TagOpen, Action::FlushText);
    table[State::Data as usize][ByteClass::Gt as usize] = Transition::noop(State::Data);
    table[State::Data as usize][ByteClass::Slash as usize] = Transition::noop(State::Data);
    table[State::Data as usize][ByteClass::Eq as usize] = Transition::noop(State::Data);
    table[State::Data as usize][ByteClass::Quot as usize] = Transition::noop(State::Data);
    table[State::Data as usize][ByteClass::Amp as usize] = Transition::noop(State::Data);
    table[State::Data as usize][ByteClass::Bang as usize] = Transition::noop(State::Data);
    table[State::Data as usize][ByteClass::Dash as usize] = Transition::noop(State::Data);
    table[State::Data as usize][ByteClass::Alpha as usize] = Transition::noop(State::Data);
    table[State::Data as usize][ByteClass::Whitespace as usize] = Transition::noop(State::Data);
    table[State::Data as usize][ByteClass::Other as usize] = Transition::noop(State::Data);

    // ----- TagOpen state (saw '<') -----
    table[State::TagOpen as usize][ByteClass::Alpha as usize] =
        Transition::new(State::TagName, Action::StartTag);
    table[State::TagOpen as usize][ByteClass::Slash as usize] =
        Transition::new(State::EndTagOpen, Action::None);
    table[State::TagOpen as usize][ByteClass::Bang as usize] =
        Transition::new(State::MarkupDecl, Action::None);
    // Malformed: '<' followed by non-alpha — treat as text
    table[State::TagOpen as usize][ByteClass::Lt as usize] =
        Transition::new(State::TagOpen, Action::FlushText);
    table[State::TagOpen as usize][ByteClass::Gt as usize] = Transition::noop(State::Data);
    table[State::TagOpen as usize][ByteClass::Other as usize] = Transition::noop(State::Data);
    table[State::TagOpen as usize][ByteClass::Whitespace as usize] = Transition::noop(State::Data);
    table[State::TagOpen as usize][ByteClass::Eq as usize] = Transition::noop(State::Data);
    table[State::TagOpen as usize][ByteClass::Quot as usize] = Transition::noop(State::Data);
    table[State::TagOpen as usize][ByteClass::Amp as usize] = Transition::noop(State::Data);
    table[State::TagOpen as usize][ByteClass::Dash as usize] = Transition::noop(State::Data);

    // ----- TagName state -----
    table[State::TagName as usize][ByteClass::Alpha as usize] = Transition::noop(State::TagName);
    table[State::TagName as usize][ByteClass::Other as usize] = Transition::noop(State::TagName);
    table[State::TagName as usize][ByteClass::Dash as usize] = Transition::noop(State::TagName);
    table[State::TagName as usize][ByteClass::Whitespace as usize] =
        Transition::new(State::BeforeAttrName, Action::EmitTagName);
    table[State::TagName as usize][ByteClass::Gt as usize] =
        Transition::new(State::Data, Action::EmitOpenTagClose);
    table[State::TagName as usize][ByteClass::Slash as usize] =
        Transition::new(State::SelfClosingStartTag, Action::EmitTagName);
    table[State::TagName as usize][ByteClass::Lt as usize] =
        Transition::new(State::TagOpen, Action::EmitOpenTagClose);
    table[State::TagName as usize][ByteClass::Eq as usize] = Transition::noop(State::TagName);
    table[State::TagName as usize][ByteClass::Quot as usize] = Transition::noop(State::TagName);
    table[State::TagName as usize][ByteClass::Amp as usize] = Transition::noop(State::TagName);
    table[State::TagName as usize][ByteClass::Bang as usize] = Transition::noop(State::TagName);

    // ----- EndTagOpen state (saw '</') -----
    table[State::EndTagOpen as usize][ByteClass::Alpha as usize] =
        Transition::new(State::EndTagName, Action::StartEndTag);
    table[State::EndTagOpen as usize][ByteClass::Gt as usize] = Transition::noop(State::Data); // </> — malformed, ignore
    table[State::EndTagOpen as usize][ByteClass::Lt as usize] =
        Transition::new(State::TagOpen, Action::FlushText);
    table[State::EndTagOpen as usize][ByteClass::Other as usize] = Transition::noop(State::Data);
    table[State::EndTagOpen as usize][ByteClass::Slash as usize] = Transition::noop(State::Data);
    table[State::EndTagOpen as usize][ByteClass::Eq as usize] = Transition::noop(State::Data);
    table[State::EndTagOpen as usize][ByteClass::Quot as usize] = Transition::noop(State::Data);
    table[State::EndTagOpen as usize][ByteClass::Amp as usize] = Transition::noop(State::Data);
    table[State::EndTagOpen as usize][ByteClass::Bang as usize] = Transition::noop(State::Data);
    table[State::EndTagOpen as usize][ByteClass::Dash as usize] = Transition::noop(State::Data);
    table[State::EndTagOpen as usize][ByteClass::Whitespace as usize] =
        Transition::noop(State::Data);

    // ----- EndTagName state -----
    table[State::EndTagName as usize][ByteClass::Alpha as usize] =
        Transition::noop(State::EndTagName);
    table[State::EndTagName as usize][ByteClass::Other as usize] =
        Transition::noop(State::EndTagName);
    table[State::EndTagName as usize][ByteClass::Dash as usize] =
        Transition::noop(State::EndTagName);
    table[State::EndTagName as usize][ByteClass::Gt as usize] =
        Transition::new(State::Data, Action::EmitEndTagName);
    table[State::EndTagName as usize][ByteClass::Whitespace as usize] =
        Transition::new(State::EndTagName, Action::EmitEndTagName);
    table[State::EndTagName as usize][ByteClass::Lt as usize] =
        Transition::new(State::TagOpen, Action::EmitEndTagName);
    table[State::EndTagName as usize][ByteClass::Slash as usize] =
        Transition::noop(State::EndTagName);
    table[State::EndTagName as usize][ByteClass::Eq as usize] = Transition::noop(State::EndTagName);
    table[State::EndTagName as usize][ByteClass::Quot as usize] =
        Transition::noop(State::EndTagName);
    table[State::EndTagName as usize][ByteClass::Amp as usize] =
        Transition::noop(State::EndTagName);
    table[State::EndTagName as usize][ByteClass::Bang as usize] =
        Transition::noop(State::EndTagName);

    // ----- BeforeAttrName state (after tag name whitespace) -----
    table[State::BeforeAttrName as usize][ByteClass::Alpha as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);
    table[State::BeforeAttrName as usize][ByteClass::Gt as usize] =
        Transition::new(State::Data, Action::EmitOpenTagClose);
    table[State::BeforeAttrName as usize][ByteClass::Slash as usize] =
        Transition::new(State::SelfClosingStartTag, Action::None);
    table[State::BeforeAttrName as usize][ByteClass::Whitespace as usize] =
        Transition::noop(State::BeforeAttrName);
    table[State::BeforeAttrName as usize][ByteClass::Other as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);
    table[State::BeforeAttrName as usize][ByteClass::Dash as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);
    table[State::BeforeAttrName as usize][ByteClass::Lt as usize] =
        Transition::new(State::TagOpen, Action::EmitOpenTagClose);
    table[State::BeforeAttrName as usize][ByteClass::Eq as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);
    table[State::BeforeAttrName as usize][ByteClass::Quot as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);
    table[State::BeforeAttrName as usize][ByteClass::Amp as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);
    table[State::BeforeAttrName as usize][ByteClass::Bang as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);

    // ----- AttrName state -----
    table[State::AttrName as usize][ByteClass::Alpha as usize] = Transition::noop(State::AttrName);
    table[State::AttrName as usize][ByteClass::Other as usize] = Transition::noop(State::AttrName);
    table[State::AttrName as usize][ByteClass::Dash as usize] = Transition::noop(State::AttrName);
    table[State::AttrName as usize][ByteClass::Eq as usize] =
        Transition::new(State::BeforeAttrValue, Action::EmitAttrName);
    table[State::AttrName as usize][ByteClass::Whitespace as usize] =
        Transition::new(State::AfterAttrName, Action::EmitAttrName);
    table[State::AttrName as usize][ByteClass::Gt as usize] =
        Transition::new(State::Data, Action::EmitOpenTagClose);
    table[State::AttrName as usize][ByteClass::Slash as usize] =
        Transition::new(State::SelfClosingStartTag, Action::EmitAttrName);
    table[State::AttrName as usize][ByteClass::Lt as usize] =
        Transition::new(State::TagOpen, Action::EmitOpenTagClose);
    table[State::AttrName as usize][ByteClass::Quot as usize] = Transition::noop(State::AttrName);
    table[State::AttrName as usize][ByteClass::Amp as usize] = Transition::noop(State::AttrName);
    table[State::AttrName as usize][ByteClass::Bang as usize] = Transition::noop(State::AttrName);

    // ----- AfterAttrName state (after attr name, looking for = or next attr) -----
    table[State::AfterAttrName as usize][ByteClass::Eq as usize] =
        Transition::new(State::BeforeAttrValue, Action::None);
    table[State::AfterAttrName as usize][ByteClass::Whitespace as usize] =
        Transition::noop(State::AfterAttrName);
    table[State::AfterAttrName as usize][ByteClass::Alpha as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);
    table[State::AfterAttrName as usize][ByteClass::Gt as usize] =
        Transition::new(State::Data, Action::EmitOpenTagClose);
    table[State::AfterAttrName as usize][ByteClass::Slash as usize] =
        Transition::new(State::SelfClosingStartTag, Action::None);
    table[State::AfterAttrName as usize][ByteClass::Lt as usize] =
        Transition::new(State::TagOpen, Action::EmitOpenTagClose);
    table[State::AfterAttrName as usize][ByteClass::Other as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);
    table[State::AfterAttrName as usize][ByteClass::Dash as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);
    table[State::AfterAttrName as usize][ByteClass::Quot as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);
    table[State::AfterAttrName as usize][ByteClass::Amp as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);
    table[State::AfterAttrName as usize][ByteClass::Bang as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);

    // ----- BeforeAttrValue state (saw '=', expecting quote or unquoted) -----
    table[State::BeforeAttrValue as usize][ByteClass::Quot as usize] =
        Transition::new(State::AttrValueQuoted, Action::StartAttrValue);
    table[State::BeforeAttrValue as usize][ByteClass::Whitespace as usize] =
        Transition::noop(State::BeforeAttrValue);
    table[State::BeforeAttrValue as usize][ByteClass::Alpha as usize] =
        Transition::new(State::AttrValueUnquoted, Action::StartAttrValue);
    table[State::BeforeAttrValue as usize][ByteClass::Other as usize] =
        Transition::new(State::AttrValueUnquoted, Action::StartAttrValue);
    table[State::BeforeAttrValue as usize][ByteClass::Gt as usize] =
        Transition::new(State::Data, Action::EmitOpenTagClose);
    table[State::BeforeAttrValue as usize][ByteClass::Dash as usize] =
        Transition::new(State::AttrValueUnquoted, Action::StartAttrValue);
    table[State::BeforeAttrValue as usize][ByteClass::Lt as usize] =
        Transition::new(State::TagOpen, Action::EmitOpenTagClose);
    table[State::BeforeAttrValue as usize][ByteClass::Slash as usize] =
        Transition::new(State::AttrValueUnquoted, Action::StartAttrValue);
    table[State::BeforeAttrValue as usize][ByteClass::Eq as usize] =
        Transition::new(State::AttrValueUnquoted, Action::StartAttrValue);
    table[State::BeforeAttrValue as usize][ByteClass::Amp as usize] =
        Transition::new(State::AttrValueUnquoted, Action::StartAttrValue);
    table[State::BeforeAttrValue as usize][ByteClass::Bang as usize] =
        Transition::new(State::AttrValueUnquoted, Action::StartAttrValue);

    // ----- AttrValueQuoted state -----
    // Quotes end the value; everything else stays in quoted value.
    table[State::AttrValueQuoted as usize][ByteClass::Quot as usize] =
        Transition::new(State::BeforeAttrName, Action::EmitAttrValue);
    table[State::AttrValueQuoted as usize][ByteClass::Lt as usize] =
        Transition::noop(State::AttrValueQuoted);
    table[State::AttrValueQuoted as usize][ByteClass::Gt as usize] =
        Transition::noop(State::AttrValueQuoted);
    table[State::AttrValueQuoted as usize][ByteClass::Slash as usize] =
        Transition::noop(State::AttrValueQuoted);
    table[State::AttrValueQuoted as usize][ByteClass::Eq as usize] =
        Transition::noop(State::AttrValueQuoted);
    table[State::AttrValueQuoted as usize][ByteClass::Amp as usize] =
        Transition::noop(State::AttrValueQuoted);
    table[State::AttrValueQuoted as usize][ByteClass::Bang as usize] =
        Transition::noop(State::AttrValueQuoted);
    table[State::AttrValueQuoted as usize][ByteClass::Dash as usize] =
        Transition::noop(State::AttrValueQuoted);
    table[State::AttrValueQuoted as usize][ByteClass::Alpha as usize] =
        Transition::noop(State::AttrValueQuoted);
    table[State::AttrValueQuoted as usize][ByteClass::Whitespace as usize] =
        Transition::noop(State::AttrValueQuoted);
    table[State::AttrValueQuoted as usize][ByteClass::Other as usize] =
        Transition::noop(State::AttrValueQuoted);

    // ----- AttrValueUnquoted state -----
    table[State::AttrValueUnquoted as usize][ByteClass::Whitespace as usize] =
        Transition::new(State::BeforeAttrName, Action::EmitAttrValue);
    table[State::AttrValueUnquoted as usize][ByteClass::Gt as usize] =
        Transition::new(State::Data, Action::EmitOpenTagClose);
    table[State::AttrValueUnquoted as usize][ByteClass::Lt as usize] =
        Transition::new(State::TagOpen, Action::EmitOpenTagClose);
    table[State::AttrValueUnquoted as usize][ByteClass::Alpha as usize] =
        Transition::noop(State::AttrValueUnquoted);
    table[State::AttrValueUnquoted as usize][ByteClass::Other as usize] =
        Transition::noop(State::AttrValueUnquoted);
    table[State::AttrValueUnquoted as usize][ByteClass::Dash as usize] =
        Transition::noop(State::AttrValueUnquoted);
    table[State::AttrValueUnquoted as usize][ByteClass::Slash as usize] =
        Transition::noop(State::AttrValueUnquoted);
    table[State::AttrValueUnquoted as usize][ByteClass::Eq as usize] =
        Transition::noop(State::AttrValueUnquoted);
    table[State::AttrValueUnquoted as usize][ByteClass::Quot as usize] =
        Transition::noop(State::AttrValueUnquoted);
    table[State::AttrValueUnquoted as usize][ByteClass::Amp as usize] =
        Transition::noop(State::AttrValueUnquoted);
    table[State::AttrValueUnquoted as usize][ByteClass::Bang as usize] =
        Transition::noop(State::AttrValueUnquoted);

    // ----- SelfClosingStartTag state (saw '/' inside tag) -----
    table[State::SelfClosingStartTag as usize][ByteClass::Gt as usize] =
        Transition::new(State::Data, Action::EmitSelfClose);
    // Not a self-close — treat '/' as ignored, go back to before-attr
    table[State::SelfClosingStartTag as usize][ByteClass::Alpha as usize] =
        Transition::new(State::AttrName, Action::StartAttrName);
    table[State::SelfClosingStartTag as usize][ByteClass::Whitespace as usize] =
        Transition::noop(State::BeforeAttrName);
    table[State::SelfClosingStartTag as usize][ByteClass::Lt as usize] =
        Transition::new(State::TagOpen, Action::EmitSelfClose);
    table[State::SelfClosingStartTag as usize][ByteClass::Other as usize] =
        Transition::noop(State::BeforeAttrName);
    table[State::SelfClosingStartTag as usize][ByteClass::Slash as usize] =
        Transition::noop(State::SelfClosingStartTag);
    table[State::SelfClosingStartTag as usize][ByteClass::Eq as usize] =
        Transition::noop(State::BeforeAttrName);
    table[State::SelfClosingStartTag as usize][ByteClass::Quot as usize] =
        Transition::noop(State::BeforeAttrName);
    table[State::SelfClosingStartTag as usize][ByteClass::Amp as usize] =
        Transition::noop(State::BeforeAttrName);
    table[State::SelfClosingStartTag as usize][ByteClass::Bang as usize] =
        Transition::noop(State::BeforeAttrName);
    table[State::SelfClosingStartTag as usize][ByteClass::Dash as usize] =
        Transition::noop(State::BeforeAttrName);

    // ----- MarkupDecl state (saw '<!') -----
    table[State::MarkupDecl as usize][ByteClass::Dash as usize] =
        Transition::new(State::Comment, Action::StartComment);
    table[State::MarkupDecl as usize][ByteClass::Alpha as usize] =
        Transition::new(State::Doctype, Action::StartDoctype);
    // '[' for CDATA — classified as Other
    table[State::MarkupDecl as usize][ByteClass::Other as usize] =
        Transition::new(State::CData, Action::StartCData);
    table[State::MarkupDecl as usize][ByteClass::Gt as usize] = Transition::noop(State::Data);
    table[State::MarkupDecl as usize][ByteClass::Lt as usize] =
        Transition::new(State::TagOpen, Action::FlushText);
    table[State::MarkupDecl as usize][ByteClass::Slash as usize] = Transition::noop(State::Data);
    table[State::MarkupDecl as usize][ByteClass::Eq as usize] = Transition::noop(State::Data);
    table[State::MarkupDecl as usize][ByteClass::Quot as usize] = Transition::noop(State::Data);
    table[State::MarkupDecl as usize][ByteClass::Amp as usize] = Transition::noop(State::Data);
    table[State::MarkupDecl as usize][ByteClass::Bang as usize] = Transition::noop(State::Data);
    table[State::MarkupDecl as usize][ByteClass::Whitespace as usize] =
        Transition::noop(State::Data);

    // ----- Comment state -----
    table[State::Comment as usize][ByteClass::Dash as usize] =
        Transition::noop(State::CommentEndDash);
    table[State::Comment as usize][ByteClass::Lt as usize] = Transition::noop(State::Comment);
    table[State::Comment as usize][ByteClass::Gt as usize] = Transition::noop(State::Comment);
    table[State::Comment as usize][ByteClass::Slash as usize] = Transition::noop(State::Comment);
    table[State::Comment as usize][ByteClass::Eq as usize] = Transition::noop(State::Comment);
    table[State::Comment as usize][ByteClass::Quot as usize] = Transition::noop(State::Comment);
    table[State::Comment as usize][ByteClass::Amp as usize] = Transition::noop(State::Comment);
    table[State::Comment as usize][ByteClass::Bang as usize] = Transition::noop(State::Comment);
    table[State::Comment as usize][ByteClass::Alpha as usize] = Transition::noop(State::Comment);
    table[State::Comment as usize][ByteClass::Whitespace as usize] =
        Transition::noop(State::Comment);
    table[State::Comment as usize][ByteClass::Other as usize] = Transition::noop(State::Comment);

    // ----- CommentEndDash state (saw '-' in comment) -----
    table[State::CommentEndDash as usize][ByteClass::Dash as usize] =
        Transition::noop(State::CommentEnd);
    // Not end of comment — back to Comment
    table[State::CommentEndDash as usize][ByteClass::Lt as usize] =
        Transition::noop(State::Comment);
    table[State::CommentEndDash as usize][ByteClass::Gt as usize] =
        Transition::noop(State::Comment);
    table[State::CommentEndDash as usize][ByteClass::Slash as usize] =
        Transition::noop(State::Comment);
    table[State::CommentEndDash as usize][ByteClass::Eq as usize] =
        Transition::noop(State::Comment);
    table[State::CommentEndDash as usize][ByteClass::Quot as usize] =
        Transition::noop(State::Comment);
    table[State::CommentEndDash as usize][ByteClass::Amp as usize] =
        Transition::noop(State::Comment);
    table[State::CommentEndDash as usize][ByteClass::Bang as usize] =
        Transition::noop(State::Comment);
    table[State::CommentEndDash as usize][ByteClass::Alpha as usize] =
        Transition::noop(State::Comment);
    table[State::CommentEndDash as usize][ByteClass::Whitespace as usize] =
        Transition::noop(State::Comment);
    table[State::CommentEndDash as usize][ByteClass::Other as usize] =
        Transition::noop(State::Comment);

    // ----- CommentEnd state (saw '--' in comment) -----
    table[State::CommentEnd as usize][ByteClass::Gt as usize] =
        Transition::new(State::Data, Action::EmitComment);
    // Not closing yet — back to Comment
    table[State::CommentEnd as usize][ByteClass::Dash as usize] =
        Transition::noop(State::CommentEnd);
    table[State::CommentEnd as usize][ByteClass::Lt as usize] = Transition::noop(State::Comment);
    table[State::CommentEnd as usize][ByteClass::Slash as usize] = Transition::noop(State::Comment);
    table[State::CommentEnd as usize][ByteClass::Eq as usize] = Transition::noop(State::Comment);
    table[State::CommentEnd as usize][ByteClass::Quot as usize] = Transition::noop(State::Comment);
    table[State::CommentEnd as usize][ByteClass::Amp as usize] = Transition::noop(State::Comment);
    table[State::CommentEnd as usize][ByteClass::Bang as usize] = Transition::noop(State::Comment);
    table[State::CommentEnd as usize][ByteClass::Alpha as usize] = Transition::noop(State::Comment);
    table[State::CommentEnd as usize][ByteClass::Whitespace as usize] =
        Transition::noop(State::Comment);
    table[State::CommentEnd as usize][ByteClass::Other as usize] = Transition::noop(State::Comment);

    // ----- Doctype state -----
    table[State::Doctype as usize][ByteClass::Gt as usize] =
        Transition::new(State::Data, Action::EmitDoctype);
    table[State::Doctype as usize][ByteClass::Lt as usize] = Transition::noop(State::Doctype);
    table[State::Doctype as usize][ByteClass::Slash as usize] = Transition::noop(State::Doctype);
    table[State::Doctype as usize][ByteClass::Eq as usize] = Transition::noop(State::Doctype);
    table[State::Doctype as usize][ByteClass::Quot as usize] = Transition::noop(State::Doctype);
    table[State::Doctype as usize][ByteClass::Amp as usize] = Transition::noop(State::Doctype);
    table[State::Doctype as usize][ByteClass::Bang as usize] = Transition::noop(State::Doctype);
    table[State::Doctype as usize][ByteClass::Dash as usize] = Transition::noop(State::Doctype);
    table[State::Doctype as usize][ByteClass::Alpha as usize] = Transition::noop(State::Doctype);
    table[State::Doctype as usize][ByteClass::Whitespace as usize] =
        Transition::noop(State::Doctype);
    table[State::Doctype as usize][ByteClass::Other as usize] = Transition::noop(State::Doctype);

    // ----- CData state -----
    // CDATA ends with `]]>` — we detect `>` and check context in extraction.
    table[State::CData as usize][ByteClass::Gt as usize] =
        Transition::new(State::Data, Action::EmitCData);
    table[State::CData as usize][ByteClass::Lt as usize] = Transition::noop(State::CData);
    table[State::CData as usize][ByteClass::Slash as usize] = Transition::noop(State::CData);
    table[State::CData as usize][ByteClass::Eq as usize] = Transition::noop(State::CData);
    table[State::CData as usize][ByteClass::Quot as usize] = Transition::noop(State::CData);
    table[State::CData as usize][ByteClass::Amp as usize] = Transition::noop(State::CData);
    table[State::CData as usize][ByteClass::Bang as usize] = Transition::noop(State::CData);
    table[State::CData as usize][ByteClass::Dash as usize] = Transition::noop(State::CData);
    table[State::CData as usize][ByteClass::Alpha as usize] = Transition::noop(State::CData);
    table[State::CData as usize][ByteClass::Whitespace as usize] = Transition::noop(State::CData);
    table[State::CData as usize][ByteClass::Other as usize] = Transition::noop(State::CData);

    // ----- RawText state (script/style content) -----
    // Everything stays in RawText until we see '<' (potential end tag).
    table[State::RawText as usize][ByteClass::Lt as usize] =
        Transition::new(State::TagOpen, Action::FlushText);
    table[State::RawText as usize][ByteClass::Gt as usize] = Transition::noop(State::RawText);
    table[State::RawText as usize][ByteClass::Slash as usize] = Transition::noop(State::RawText);
    table[State::RawText as usize][ByteClass::Eq as usize] = Transition::noop(State::RawText);
    table[State::RawText as usize][ByteClass::Quot as usize] = Transition::noop(State::RawText);
    table[State::RawText as usize][ByteClass::Amp as usize] = Transition::noop(State::RawText);
    table[State::RawText as usize][ByteClass::Bang as usize] = Transition::noop(State::RawText);
    table[State::RawText as usize][ByteClass::Dash as usize] = Transition::noop(State::RawText);
    table[State::RawText as usize][ByteClass::Alpha as usize] = Transition::noop(State::RawText);
    table[State::RawText as usize][ByteClass::Whitespace as usize] =
        Transition::noop(State::RawText);
    table[State::RawText as usize][ByteClass::Other as usize] = Transition::noop(State::RawText);

    table
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn byte_class_delimiters() {
        assert_eq!(ByteClass::from_byte(b'<'), ByteClass::Lt);
        assert_eq!(ByteClass::from_byte(b'>'), ByteClass::Gt);
        assert_eq!(ByteClass::from_byte(b'/'), ByteClass::Slash);
        assert_eq!(ByteClass::from_byte(b'='), ByteClass::Eq);
        assert_eq!(ByteClass::from_byte(b'"'), ByteClass::Quot);
        assert_eq!(ByteClass::from_byte(b'\''), ByteClass::Quot);
        assert_eq!(ByteClass::from_byte(b'&'), ByteClass::Amp);
        assert_eq!(ByteClass::from_byte(b'!'), ByteClass::Bang);
        assert_eq!(ByteClass::from_byte(b'-'), ByteClass::Dash);
    }

    #[test]
    fn byte_class_alpha() {
        assert_eq!(ByteClass::from_byte(b'a'), ByteClass::Alpha);
        assert_eq!(ByteClass::from_byte(b'z'), ByteClass::Alpha);
        assert_eq!(ByteClass::from_byte(b'A'), ByteClass::Alpha);
        assert_eq!(ByteClass::from_byte(b'Z'), ByteClass::Alpha);
    }

    #[test]
    fn byte_class_whitespace() {
        assert_eq!(ByteClass::from_byte(b' '), ByteClass::Whitespace);
        assert_eq!(ByteClass::from_byte(b'\t'), ByteClass::Whitespace);
        assert_eq!(ByteClass::from_byte(b'\n'), ByteClass::Whitespace);
        assert_eq!(ByteClass::from_byte(b'\r'), ByteClass::Whitespace);
    }

    #[test]
    fn byte_class_other() {
        assert_eq!(ByteClass::from_byte(b'0'), ByteClass::Other);
        assert_eq!(ByteClass::from_byte(b'['), ByteClass::Other);
        assert_eq!(ByteClass::from_byte(0xFF), ByteClass::Other);
    }

    #[test]
    fn data_lt_transitions_to_tag_open() {
        let t = STATE_TABLE[State::Data as usize][ByteClass::Lt as usize];
        assert_eq!(t.state, State::TagOpen);
        assert_eq!(t.action, Action::FlushText);
    }

    #[test]
    fn tag_open_alpha_starts_tag() {
        let t = STATE_TABLE[State::TagOpen as usize][ByteClass::Alpha as usize];
        assert_eq!(t.state, State::TagName);
        assert_eq!(t.action, Action::StartTag);
    }

    #[test]
    fn tag_open_slash_to_end_tag() {
        let t = STATE_TABLE[State::TagOpen as usize][ByteClass::Slash as usize];
        assert_eq!(t.state, State::EndTagOpen);
    }

    #[test]
    fn tag_name_gt_emits_tag() {
        let t = STATE_TABLE[State::TagName as usize][ByteClass::Gt as usize];
        assert_eq!(t.state, State::Data);
        assert_eq!(t.action, Action::EmitOpenTagClose);
    }

    #[test]
    fn self_closing_gt_emits() {
        let t = STATE_TABLE[State::SelfClosingStartTag as usize][ByteClass::Gt as usize];
        assert_eq!(t.state, State::Data);
        assert_eq!(t.action, Action::EmitSelfClose);
    }

    #[test]
    fn comment_dash_dash_gt_emits() {
        // First dash
        let t1 = STATE_TABLE[State::Comment as usize][ByteClass::Dash as usize];
        assert_eq!(t1.state, State::CommentEndDash);
        // Second dash
        let t2 = STATE_TABLE[State::CommentEndDash as usize][ByteClass::Dash as usize];
        assert_eq!(t2.state, State::CommentEnd);
        // >
        let t3 = STATE_TABLE[State::CommentEnd as usize][ByteClass::Gt as usize];
        assert_eq!(t3.state, State::Data);
        assert_eq!(t3.action, Action::EmitComment);
    }
}