schoolmarm 0.1.1

GBNF grammar-constrained decoding for LLM inference, ported from llama.cpp
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
use crate::error::GrammarError;
use crate::parse::{self, ParsedGrammar};
use crate::types::*;

const MAX_RECURSION_DEPTH: usize = 512;

/// A compiled grammar ready for constrained decoding.
#[derive(Debug, Clone)]
pub struct Grammar {
    pub(crate) rules: Rules,
    pub(crate) root_index: usize,
    pub(crate) symbol_names: Vec<String>,
}

/// Runtime grammar state for constrained decoding.
/// Created from a Grammar, advanced token-by-token.
#[derive(Debug, Clone)]
pub struct GrammarState {
    grammar: Grammar,
    stacks: Stacks,
}

impl Grammar {
    /// Parse a GBNF grammar string and compile it.
    pub fn new(grammar_text: &str) -> Result<Self, GrammarError> {
        Self::with_root(grammar_text, "root")
    }

    /// Parse with a custom root rule name.
    pub fn with_root(grammar_text: &str, root_name: &str) -> Result<Self, GrammarError> {
        let parsed = parse::parse(grammar_text)?;
        Self::from_parsed(parsed, root_name)
    }

    fn from_parsed(parsed: ParsedGrammar, root_name: &str) -> Result<Self, GrammarError> {
        let root_index = *parsed
            .symbol_ids
            .get(root_name)
            .ok_or(GrammarError::NoRootRule)? as usize;

        if root_index >= parsed.rules.len() {
            return Err(GrammarError::InvalidStartRule(root_index));
        }

        // Build name lookup (index → name) for diagnostics
        let mut symbol_names = vec![String::new(); parsed.rules.len()];
        for (name, &id) in &parsed.symbol_ids {
            let idx = id as usize;
            if idx < symbol_names.len() {
                symbol_names[idx] = name.clone();
            }
        }

        // Check for left recursion
        detect_left_recursion(&parsed.rules)?;

        Ok(Grammar {
            rules: parsed.rules,
            root_index,
            symbol_names,
        })
    }

    /// Number of rules.
    pub fn num_rules(&self) -> usize {
        self.rules.len()
    }
}

impl GrammarState {
    /// Initialize from a compiled grammar. Expands the root rule into initial stacks.
    pub fn new(grammar: Grammar) -> Result<Self, GrammarError> {
        let stacks = init_stacks(&grammar.rules, grammar.root_index)?;
        Ok(GrammarState { grammar, stacks })
    }

    /// Check if the grammar is in an accepting state (at least one empty stack).
    pub fn is_accepting(&self) -> bool {
        self.stacks.iter().any(|s| s.is_empty())
    }

    /// Check if the grammar has any valid continuation.
    pub fn is_valid(&self) -> bool {
        !self.stacks.is_empty()
    }

    /// Get a bitmask of which tokens are allowed at this position.
    /// `vocab[i]` is the string representation of token ID `i`.
    /// Returns a Vec<bool> of length vocab.len().
    pub fn allowed_tokens(&self, vocab: &[&str]) -> Vec<bool> {
        let mut allowed = vec![false; vocab.len()];

        // If any stack is empty, the grammar can accept EOG
        let allow_eog = self.is_accepting();

        for (i, token_str) in vocab.iter().enumerate() {
            if token_str.is_empty() {
                // Empty tokens are allowed only if we're in accepting state
                allowed[i] = allow_eog;
                continue;
            }

            let codepoints = crate::parse::decode_utf8_string(token_str);
            // codepoints has trailing 0; actual chars are [0..len-1]
            let char_count = codepoints.len() - 1;
            if char_count == 0 {
                allowed[i] = allow_eog;
                continue;
            }

            // Check if this token can be consumed by any current stack
            allowed[i] = self.can_accept_token(&codepoints[..char_count]);
        }

        allowed
    }

    /// Check if a specific token string can be consumed from the current state.
    fn can_accept_token(&self, codepoints: &[u32]) -> bool {
        // Try every current stack
        for stack in &self.stacks {
            if self.can_accept_codepoints_from_stack(stack, codepoints) {
                return true;
            }
        }
        false
    }

    /// Check if a sequence of codepoints can be consumed starting from a single stack.
    fn can_accept_codepoints_from_stack(&self, stack: &Stack, codepoints: &[u32]) -> bool {
        if codepoints.is_empty() {
            return true;
        }

        if stack.is_empty() {
            // Stack is completed but we still have chars to consume
            return false;
        }

        let pos = *stack.last().unwrap();
        let elem = self.grammar.rules[pos.0][pos.1];

        // Must be at a char element
        if !matches!(
            elem.etype,
            ElementType::Char | ElementType::CharNot | ElementType::CharAny
        ) {
            return false;
        }

        let (matched, after_pos) = match_char(&self.grammar.rules, pos, codepoints[0]);
        if !matched {
            return false;
        }

        // Build new stack after consuming this character
        let mut new_stack: Stack = stack[..stack.len() - 1].to_vec();
        if !self.grammar.rules[after_pos.0][after_pos.1].is_end_of_sequence() {
            new_stack.push(after_pos);
        }

        // Expand the new stack (resolve rule refs)
        let mut expanded_stacks = Stacks::new();
        advance_stack(&self.grammar.rules, &new_stack, &mut expanded_stacks, 0);

        // Recurse for remaining codepoints
        let remaining = &codepoints[1..];
        if remaining.is_empty() {
            return true; // consumed all chars
        }
        for es in &expanded_stacks {
            if self.can_accept_codepoints_from_stack(es, remaining) {
                return true;
            }
        }
        false
    }

    /// Accept a token's text and advance the grammar state.
    /// Call after sampling, with the chosen token's string.
    pub fn accept_token(&mut self, token_text: &str) -> Result<(), GrammarError> {
        let codepoints = crate::parse::decode_utf8_string(token_text);
        // chars are [0..len-1], last is terminating 0
        let chars = &codepoints[..codepoints.len() - 1];

        let mut current_stacks = self.stacks.clone();

        for &cp in chars {
            let mut next_stacks = Stacks::new();
            for stack in &current_stacks {
                accept_char(&self.grammar.rules, stack, cp, &mut next_stacks);
            }
            current_stacks = next_stacks;
            if current_stacks.is_empty() {
                return Err(GrammarError::InvalidState(format!(
                    "no valid stacks after accepting character U+{:04X} in '{}'",
                    cp, token_text
                )));
            }
        }

        self.stacks = current_stacks;
        Ok(())
    }

    /// Reset to initial state.
    pub fn reset(&mut self) -> Result<(), GrammarError> {
        self.stacks = init_stacks(&self.grammar.rules, self.grammar.root_index)?;
        Ok(())
    }

    /// Current number of active stacks (for diagnostics).
    pub fn num_stacks(&self) -> usize {
        self.stacks.len()
    }

    // Expose stacks for testing
    #[doc(hidden)]
    pub fn stacks(&self) -> &Stacks {
        &self.stacks
    }
}

// ── Core algorithms ─────────────────────────────────────────────────

/// Initialize stacks from the root rule.
fn init_stacks(rules: &Rules, start_rule_index: usize) -> Result<Stacks, GrammarError> {
    if start_rule_index >= rules.len() {
        return Err(GrammarError::InvalidStartRule(start_rule_index));
    }

    let rule = &rules[start_rule_index];
    let mut stacks = Stacks::new();
    let mut ei = 0;

    loop {
        let mut stack = Stack::new();
        if !rule[ei].is_end_of_sequence() {
            stack.push((start_rule_index, ei));
        }
        advance_stack(rules, &stack, &mut stacks, 0);
        // Scan to end of this alternate
        while !rule[ei].is_end_of_sequence() {
            ei += 1;
        }
        if rule[ei].etype == ElementType::Alt {
            ei += 1; // next alternate
        } else {
            break;
        }
    }

    Ok(stacks)
}

/// Expand a stack until all tops are at terminal elements (char ranges).
/// This is the direct port of llama_grammar_advance_stack.
fn advance_stack(rules: &Rules, stack: &Stack, new_stacks: &mut Stacks, depth: usize) {
    if depth > MAX_RECURSION_DEPTH {
        return; // Fix for C++ bug #18988: prevent stack overflow
    }

    if stack.is_empty() {
        if !new_stacks.contains(stack) {
            new_stacks.push(stack.clone());
        }
        return;
    }

    let pos = *stack.last().unwrap();
    let elem = rules[pos.0][pos.1];

    match elem.etype {
        ElementType::RuleRef => {
            let ref_rule_id = elem.value as usize;
            let ref_rule = &rules[ref_rule_id];
            let mut subpos = 0;

            loop {
                let mut new_stack: Stack = stack[..stack.len() - 1].to_vec();

                // If this rule ref is followed by another element, push continuation
                let next_pos = (pos.0, pos.1 + 1);
                if !rules[next_pos.0][next_pos.1].is_end_of_sequence() {
                    new_stack.push(next_pos);
                }

                // If this alternate of the referenced rule is non-empty, push it
                if !ref_rule[subpos].is_end_of_sequence() {
                    new_stack.push((ref_rule_id, subpos));
                }

                advance_stack(rules, &new_stack, new_stacks, depth + 1);

                // Scan to end of this alternate in the referenced rule
                while !ref_rule[subpos].is_end_of_sequence() {
                    subpos += 1;
                }
                if ref_rule[subpos].etype == ElementType::Alt {
                    subpos += 1;
                } else {
                    break;
                }
            }
        }
        ElementType::Char | ElementType::CharNot | ElementType::CharAny => {
            if !new_stacks.contains(stack) {
                new_stacks.push(stack.clone());
            }
        }
        _ => {
            // End/Alt/CharRngUpper/CharAlt should never be on top of stack
            // (CharRngUpper and CharAlt are always part of a char sequence
            // and the stack should point to the start of the sequence)
        }
    }
}

/// Check if a character matches at a position. Returns (matched, pos_after_char_elements).
fn match_char(rules: &Rules, pos: Pos, chr: u32) -> (bool, Pos) {
    let rule = &rules[pos.0];
    let mut ei = pos.1;
    let elem = rule[ei];

    let is_positive = elem.etype == ElementType::Char || elem.etype == ElementType::CharAny;
    debug_assert!(
        is_positive || elem.etype == ElementType::CharNot,
        "match_char called on non-char element: {:?}",
        elem.etype
    );

    let mut found = false;

    loop {
        if ei + 1 < rule.len() && rule[ei + 1].etype == ElementType::CharRngUpper {
            // Inclusive range
            found = found || (rule[ei].value <= chr && chr <= rule[ei + 1].value);
            ei += 2;
        } else if rule[ei].etype == ElementType::CharAny {
            found = true;
            ei += 1;
        } else {
            // Exact match
            found = found || rule[ei].value == chr;
            ei += 1;
        }

        if ei >= rule.len() || rule[ei].etype != ElementType::CharAlt {
            break;
        }
    }

    (found == is_positive, (pos.0, ei))
}

/// Accept a character on a single stack, producing new expanded stacks.
fn accept_char(rules: &Rules, stack: &Stack, chr: u32, new_stacks: &mut Stacks) {
    if stack.is_empty() {
        return;
    }

    let pos = *stack.last().unwrap();
    let elem = rules[pos.0][pos.1];

    if !matches!(
        elem.etype,
        ElementType::Char | ElementType::CharNot | ElementType::CharAny
    ) {
        return;
    }

    let (matched, after_pos) = match_char(rules, pos, chr);
    if matched {
        let mut new_stack: Stack = stack[..stack.len() - 1].to_vec();
        if !rules[after_pos.0][after_pos.1].is_end_of_sequence() {
            new_stack.push(after_pos);
        }
        advance_stack(rules, &new_stack, new_stacks, 0);
    }
}

// ── Left recursion detection ────────────────────────────────────────

fn detect_left_recursion(rules: &Rules) -> Result<(), GrammarError> {
    let n = rules.len();
    let mut visited = vec![false; n];
    let mut in_progress = vec![false; n];
    let mut may_be_empty = vec![false; n];

    for i in 0..n {
        if visited[i] {
            continue;
        }
        if detect_lr_recursive(rules, i, &mut visited, &mut in_progress, &mut may_be_empty) {
            return Err(GrammarError::LeftRecursion(i));
        }
    }
    Ok(())
}

fn detect_lr_recursive(
    rules: &Rules,
    rule_index: usize,
    visited: &mut [bool],
    in_progress: &mut [bool],
    may_be_empty: &mut [bool],
) -> bool {
    if in_progress[rule_index] {
        return true;
    }
    in_progress[rule_index] = true;

    let rule = &rules[rule_index];

    // Check if rule might produce empty string
    let mut at_rule_start = true;
    for elem in rule {
        if elem.is_end_of_sequence() {
            if at_rule_start {
                may_be_empty[rule_index] = true;
                break;
            }
            at_rule_start = true;
        } else {
            at_rule_start = false;
        }
    }

    // Recurse into leftmost nonterminals
    let mut recurse_into_nonterminal = true;
    for elem in rule {
        if elem.etype == ElementType::RuleRef && recurse_into_nonterminal {
            let ref_idx = elem.value as usize;
            if detect_lr_recursive(rules, ref_idx, visited, in_progress, may_be_empty) {
                return true;
            }
            if !may_be_empty[ref_idx] {
                recurse_into_nonterminal = false;
            }
        } else if elem.is_end_of_sequence() {
            recurse_into_nonterminal = true;
        } else {
            recurse_into_nonterminal = false;
        }
    }

    in_progress[rule_index] = false;
    visited[rule_index] = true;
    false
}

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

    #[test]
    fn test_init_simple() {
        let g = Grammar::new(r#"root ::= "a""#).unwrap();
        let state = GrammarState::new(g).unwrap();
        assert!(state.is_valid());
        assert!(!state.is_accepting()); // haven't consumed 'a' yet
    }

    #[test]
    fn test_accept_simple() {
        let g = Grammar::new(r#"root ::= "a""#).unwrap();
        let mut state = GrammarState::new(g).unwrap();
        state.accept_token("a").unwrap();
        assert!(state.is_accepting());
    }

    #[test]
    fn test_accept_reject() {
        let g = Grammar::new(r#"root ::= "a""#).unwrap();
        let mut state = GrammarState::new(g).unwrap();
        let result = state.accept_token("b");
        assert!(result.is_err());
    }

    #[test]
    fn test_accept_sequence() {
        let g = Grammar::new(r#"root ::= "abc""#).unwrap();
        let mut state = GrammarState::new(g).unwrap();
        // Accept as one token
        state.accept_token("abc").unwrap();
        assert!(state.is_accepting());
    }

    #[test]
    fn test_accept_sequence_char_by_char() {
        let g = Grammar::new(r#"root ::= "abc""#).unwrap();
        let mut state = GrammarState::new(g).unwrap();
        state.accept_token("a").unwrap();
        assert!(!state.is_accepting());
        state.accept_token("b").unwrap();
        assert!(!state.is_accepting());
        state.accept_token("c").unwrap();
        assert!(state.is_accepting());
    }

    #[test]
    fn test_alternates() {
        let g = Grammar::new(r#"root ::= "a" | "b" | "c""#).unwrap();
        let mut state = GrammarState::new(g).unwrap();
        state.accept_token("b").unwrap();
        assert!(state.is_accepting());
    }

    #[test]
    fn test_char_range() {
        let g = Grammar::new(r#"root ::= [a-z]+"#).unwrap();
        let mut state = GrammarState::new(g).unwrap();
        state.accept_token("h").unwrap();
        state.accept_token("e").unwrap();
        state.accept_token("l").unwrap();
        state.accept_token("l").unwrap();
        state.accept_token("o").unwrap();
        assert!(state.is_accepting());
    }

    #[test]
    fn test_star_empty() {
        let g = Grammar::new(r#"root ::= "a"*"#).unwrap();
        let state = GrammarState::new(g).unwrap();
        // * allows empty match
        assert!(state.is_accepting());
    }

    #[test]
    fn test_star_multiple() {
        let g = Grammar::new(r#"root ::= "a"*"#).unwrap();
        let mut state = GrammarState::new(g).unwrap();
        state.accept_token("a").unwrap();
        state.accept_token("a").unwrap();
        state.accept_token("a").unwrap();
        assert!(state.is_accepting());
    }

    #[test]
    fn test_plus_nonempty() {
        let g = Grammar::new(r#"root ::= "a"+"#).unwrap();
        let state = GrammarState::new(g).unwrap();
        assert!(!state.is_accepting()); // + requires at least one
    }

    #[test]
    fn test_allowed_tokens_simple() {
        let g = Grammar::new(r#"root ::= "a" | "b""#).unwrap();
        let state = GrammarState::new(g).unwrap();
        let vocab = vec!["a", "b", "c", "ab"];
        let allowed = state.allowed_tokens(&vocab);
        assert!(allowed[0]); // "a"
        assert!(allowed[1]); // "b"
        assert!(!allowed[2]); // "c"
        assert!(!allowed[3]); // "ab" — would need to match exactly, but grammar only accepts single char
    }

    #[test]
    fn test_allowed_tokens_sequence() {
        let g = Grammar::new(r#"root ::= "ab""#).unwrap();
        let state = GrammarState::new(g).unwrap();
        let vocab = vec!["a", "b", "ab", "abc", "ba"];
        let allowed = state.allowed_tokens(&vocab);
        assert!(allowed[0]); // "a" — partial match is ok
        assert!(!allowed[1]); // "b" — doesn't match start
        assert!(allowed[2]); // "ab" — full match
        assert!(!allowed[3]); // "abc" — too long
        assert!(!allowed[4]); // "ba" — wrong order
    }

    #[test]
    fn test_left_recursion_detected() {
        let result = Grammar::new(r#"root ::= "a" | root "a""#);
        assert!(result.is_err());
    }

    #[test]
    fn test_left_recursion_indirect() {
        let result = Grammar::new("root ::= asdf\nasdf ::= \"a\" | asdf \"a\"");
        assert!(result.is_err());
    }

    #[test]
    fn test_left_recursion_via_empty() {
        let result = Grammar::new(
            "root ::= asdf\nasdf ::= \"a\" | foo \"b\"\nfoo ::= \"c\" | empty asdf \"d\" | \"e\"\nempty ::= \"blah\" | ",
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_dot_any_char() {
        let g = Grammar::new(r#"root ::= ... "abc" ..."#).unwrap();
        let mut state = GrammarState::new(g).unwrap();
        // 3 any + "abc" + 3 any = 9 chars
        state.accept_token("x").unwrap();
        state.accept_token("y").unwrap();
        state.accept_token("z").unwrap();
        state.accept_token("a").unwrap();
        state.accept_token("b").unwrap();
        state.accept_token("c").unwrap();
        state.accept_token("1").unwrap();
        state.accept_token("2").unwrap();
        state.accept_token("3").unwrap();
        assert!(state.is_accepting());
    }

    #[test]
    fn test_negated_range() {
        let g = Grammar::new(r#"root ::= [^0-9]+"#).unwrap();
        let mut state = GrammarState::new(g).unwrap();
        state.accept_token("a").unwrap();
        state.accept_token("b").unwrap();
        assert!(state.is_accepting());
    }

    #[test]
    fn test_negated_range_fails() {
        let g = Grammar::new(r#"root ::= [^0-9]+"#).unwrap();
        let mut state = GrammarState::new(g).unwrap();
        let result = state.accept_token("5");
        assert!(result.is_err());
    }

    #[test]
    fn test_expression_grammar() {
        let g = Grammar::new(
            r#"root ::= expr
expr ::= term ("+" term)*
term ::= number
number ::= [0-9]+"#,
        )
        .unwrap();
        let mut state = GrammarState::new(g).unwrap();
        // "42"
        state.accept_token("4").unwrap();
        state.accept_token("2").unwrap();
        assert!(state.is_accepting());
    }

    #[test]
    fn test_expression_grammar_complex() {
        let g = Grammar::new(
            r#"root ::= expr
expr ::= term ("+" term)*
term ::= number
number ::= [0-9]+"#,
        )
        .unwrap();
        let mut state = GrammarState::new(g).unwrap();
        // "1+2+3"
        for c in "1+2+3".chars() {
            state.accept_token(&c.to_string()).unwrap();
        }
        assert!(state.is_accepting());
    }

    #[test]
    fn test_expression_grammar_trailing_plus_fails() {
        let g = Grammar::new(
            r#"root ::= expr
expr ::= term ("+" term)*
term ::= number
number ::= [0-9]+"#,
        )
        .unwrap();
        let mut state = GrammarState::new(g).unwrap();
        // "42+" — should not accept (trailing operator)
        for c in "42+".chars() {
            let _ = state.accept_token(&c.to_string());
        }
        assert!(!state.is_accepting());
    }

    #[test]
    fn test_quantifier_exact() {
        let g = Grammar::new(r#"root ::= [ab]{4}"#).unwrap();
        let mut state = GrammarState::new(g).unwrap();
        for c in "abab".chars() {
            state.accept_token(&c.to_string()).unwrap();
        }
        assert!(state.is_accepting());
    }

    #[test]
    fn test_quantifier_min() {
        let g = Grammar::new(r#"root ::= [ab]{4,}"#).unwrap();
        let mut state = GrammarState::new(g).unwrap();
        for c in "ababab".chars() {
            state.accept_token(&c.to_string()).unwrap();
        }
        assert!(state.is_accepting());
    }

    #[test]
    fn test_quantifier_range() {
        let g = Grammar::new(r#"root ::= [ab]{0,4}"#).unwrap();
        let state = GrammarState::new(g).unwrap();
        // 0 should be fine
        assert!(state.is_accepting());
    }

    #[test]
    fn test_reset() {
        let g = Grammar::new(r#"root ::= "a""#).unwrap();
        let mut state = GrammarState::new(g).unwrap();
        state.accept_token("a").unwrap();
        assert!(state.is_accepting());
        state.reset().unwrap();
        assert!(!state.is_accepting());
    }
}