kiki 7.1.0

A minimalist parser generator for Rust.
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
---
source: kiki/src/tests/e2e_dry.rs
expression: rust_src
---
// This code was generated by Kiki.
// Kiki is an open-source minimalist parser generator for Rust.
// You can read more at https://crates.io/crates/kiki
//
// This code was generated from a grammar with the following hash:
// @sha256 f6176f7ea7b906a84d040468932bed981a6503efa72c10983e3c44be453ec264

// Since this code is automatically generated,
// some parts may be unidiomatic.
// The linter often complains about these parts.
// However, these warnings are not useful.
// Therefore, we disable certain lints for this file.
#![allow(non_snake_case)]
#![allow(dead_code)]

pub enum Token {
    String(String),
    Number(isize),
}

pub enum Foo {
    Empty,
    Number,
    Pair {
        val: Box<Pair>,
    },
}

pub struct Epsilon;

pub enum Pair {
    StringPair(
        Box<StringPair>,
    ),
    NumberPair(
        Box<NumberPair>,
    ),
}

pub struct StringPair;

pub struct NumberPair;

/// If the parser encounters an unexpected token `t`, it will return `Err(Some(t))`.
/// If the parser encounters an unexpected end of input, it will return `Err(None)`.
pub fn parse<S>(src: S) -> Result<Foo, Option<Token>>
where S: IntoIterator<Item = Token> {
    let mut quasiterminals = src.into_iter()
        .map(Quasiterminal::Terminal)
        .chain(std::iter::once(Quasiterminal::Eof))
        .peekable();
    let mut states = vec![State::S0];
    let mut nodes: Vec<Node> = vec![];
    loop {
        let top_state = *states.last().unwrap();
        let next_quasiterminal_kind = QuasiterminalKind::from_quasiterminal(quasiterminals.peek().unwrap());
        match get_action(top_state, next_quasiterminal_kind) {
            Action::Shift(new_state) => {
                states.push(new_state);
                nodes.push(Node::from_terminal(quasiterminals.next().unwrap().try_into_terminal().unwrap()));
            }

            Action::Reduce(rule_kind) => {
                let (new_node, new_node_kind) = pop_and_reduce(&mut states, &mut nodes, rule_kind);
                nodes.push(new_node);
                let temp_top_state = *states.last().unwrap();
                let Some(new_state) = get_goto(temp_top_state, new_node_kind) else {
                    return Err(quasiterminals.next().unwrap().try_into_terminal().ok());
                };
                states.push(new_state);
            }

            Action::Accept => {
                return Ok(Foo::try_from(nodes.pop().unwrap()).ok().unwrap());
            }

            Action::Err => {
                return Err(quasiterminals.next().unwrap().try_into_terminal().ok());
            }
        }
    }
}

enum Quasiterminal {
    Terminal(Token),
    Eof,
}

#[derive(Clone, Copy, Debug)]
enum QuasiterminalKind {
    String = 0,
    Number = 1,
    Eof = 2,
}

#[derive(Clone, Copy, Debug)]
enum NonterminalKind {
    Foo = 0,
    Epsilon = 1,
    Pair = 2,
    StringPair = 3,
    NumberPair = 4,
}

#[derive(Clone, Copy, Debug)]
enum State {
    S0 = 0,
    S1 = 1,
    S2 = 2,
    S3 = 3,
    S4 = 4,
    S5 = 5,
    S6 = 6,
    S7 = 7,
    S8 = 8,
    S9 = 9,
}

enum Node {
    Foo(Foo),
    Epsilon(Epsilon),
    Pair(Pair),
    StringPair(StringPair),
    NumberPair(NumberPair),
    String(String),
    Number(isize),
}

#[derive(Clone, Copy, Debug)]
enum Action {
    Shift(State),
    Reduce(RuleKind),
    Accept,
    Err,
}

#[derive(Clone, Copy, Debug)]
enum RuleKind {
    R0 = 0,
    R1 = 1,
    R2 = 2,
    R3 = 3,
    R4 = 4,
    R5 = 5,
    R6 = 6,
    R7 = 7,
}

fn pop_and_reduce(states: &mut Vec<State>, nodes: &mut Vec<Node>, rule_kind: RuleKind) -> (Node, NonterminalKind) {
    match rule_kind {
        RuleKind::R0 => reduce_r0(states, nodes),
        RuleKind::R1 => reduce_r1(states, nodes),
        RuleKind::R2 => reduce_r2(states, nodes),
        RuleKind::R3 => reduce_r3(states, nodes),
        RuleKind::R4 => reduce_r4(states, nodes),
        RuleKind::R5 => reduce_r5(states, nodes),
        RuleKind::R6 => reduce_r6(states, nodes),
        RuleKind::R7 => reduce_r7(states, nodes),
    }
}

fn reduce_r0(states: &mut Vec<State>, nodes: &mut Vec<Node>) -> (Node, NonterminalKind) {
    nodes.pop().unwrap();

    states.truncate(states.len() - 1);

    (
        Node::Foo(Foo::Empty),
        NonterminalKind::Foo,
    )
}

fn reduce_r1(states: &mut Vec<State>, nodes: &mut Vec<Node>) -> (Node, NonterminalKind) {
    nodes.pop().unwrap();

    states.truncate(states.len() - 1);

    (
        Node::Foo(Foo::Number),
        NonterminalKind::Foo,
    )
}

fn reduce_r2(states: &mut Vec<State>, nodes: &mut Vec<Node>) -> (Node, NonterminalKind) {
    let val_0 = Box::new(Pair::try_from(nodes.pop().unwrap()).ok().unwrap());

    states.truncate(states.len() - 1);

    (
        Node::Foo(Foo::Pair {
            val: val_0,
        }),
        NonterminalKind::Foo,
    )
}

fn reduce_r3(_states: &mut Vec<State>, _nodes: &mut Vec<Node>) -> (Node, NonterminalKind) {
    (
        Node::Epsilon(Epsilon),
        NonterminalKind::Epsilon,
    )
}

fn reduce_r4(states: &mut Vec<State>, nodes: &mut Vec<Node>) -> (Node, NonterminalKind) {
    let t0 = Box::new(StringPair::try_from(nodes.pop().unwrap()).ok().unwrap());

    states.truncate(states.len() - 1);

    (
        Node::Pair(Pair::StringPair(
            t0,
        )),
        NonterminalKind::Pair,
    )
}

fn reduce_r5(states: &mut Vec<State>, nodes: &mut Vec<Node>) -> (Node, NonterminalKind) {
    let t0 = Box::new(NumberPair::try_from(nodes.pop().unwrap()).ok().unwrap());

    states.truncate(states.len() - 1);

    (
        Node::Pair(Pair::NumberPair(
            t0,
        )),
        NonterminalKind::Pair,
    )
}

fn reduce_r6(states: &mut Vec<State>, nodes: &mut Vec<Node>) -> (Node, NonterminalKind) {
    nodes.pop().unwrap();
    nodes.pop().unwrap();

    states.truncate(states.len() - 2);

    (
        Node::StringPair(StringPair),
        NonterminalKind::StringPair,
    )
}

fn reduce_r7(states: &mut Vec<State>, nodes: &mut Vec<Node>) -> (Node, NonterminalKind) {
    nodes.pop().unwrap();
    nodes.pop().unwrap();

    states.truncate(states.len() - 2);

    (
        Node::NumberPair(NumberPair),
        NonterminalKind::NumberPair,
    )
}

impl QuasiterminalKind {
    fn from_quasiterminal(quasiterminal: &Quasiterminal) -> Self {
        match quasiterminal {
            Quasiterminal::Terminal(terminal) => Self::from_terminal(terminal),
            Quasiterminal::Eof => Self::Eof,
        }
    }

    fn from_terminal(terminal: &Token) -> Self {
        match terminal {
            Token::String(_) => Self::String,
            Token::Number(_) => Self::Number,
        }
    }
}

impl Node {
    fn from_terminal(terminal: Token) -> Self {
        match terminal {
            Token::String(t) => Self::String(t),
            Token::Number(t) => Self::Number(t),
        }
    }
}

impl Quasiterminal {
    fn try_into_terminal(self) -> Result<Token, ()> {
        match self {
            Self::Terminal(terminal) => Ok(terminal),
            Self::Eof => Err(()),
        }
    }
}

static ACTION_TABLE: [[Action; 3]; 10] = [
    [
        Action::Shift(State::S6),
        Action::Shift(State::S2),
        Action::Reduce(RuleKind::R3),
    ],
    [
        Action::Err,
        Action::Err,
        Action::Reduce(RuleKind::R0),
    ],
    [
        Action::Err,
        Action::Shift(State::S8),
        Action::Reduce(RuleKind::R1),
    ],
    [
        Action::Err,
        Action::Err,
        Action::Reduce(RuleKind::R2),
    ],
    [
        Action::Err,
        Action::Err,
        Action::Reduce(RuleKind::R4),
    ],
    [
        Action::Err,
        Action::Err,
        Action::Reduce(RuleKind::R5),
    ],
    [
        Action::Shift(State::S7),
        Action::Err,
        Action::Err,
    ],
    [
        Action::Err,
        Action::Err,
        Action::Reduce(RuleKind::R6),
    ],
    [
        Action::Err,
        Action::Err,
        Action::Reduce(RuleKind::R7),
    ],
    [
        Action::Err,
        Action::Err,
        Action::Accept,
    ],
];

fn get_action(top_state: State, next_quasiterminal_kind: QuasiterminalKind) -> Action {
    ACTION_TABLE[top_state as usize][next_quasiterminal_kind as usize]
}

static GOTO_TABLE: [[Option<State>; 5]; 10] = [
    [
        Some(State::S9),
        Some(State::S1),
        Some(State::S3),
        Some(State::S4),
        Some(State::S5),
    ],
    [
        None,
        None,
        None,
        None,
        None,
    ],
    [
        None,
        None,
        None,
        None,
        None,
    ],
    [
        None,
        None,
        None,
        None,
        None,
    ],
    [
        None,
        None,
        None,
        None,
        None,
    ],
    [
        None,
        None,
        None,
        None,
        None,
    ],
    [
        None,
        None,
        None,
        None,
        None,
    ],
    [
        None,
        None,
        None,
        None,
        None,
    ],
    [
        None,
        None,
        None,
        None,
        None,
    ],
    [
        None,
        None,
        None,
        None,
        None,
    ],
];

fn get_goto(top_state: State, new_node_kind: NonterminalKind) -> Option<State> {
    GOTO_TABLE[top_state as usize][new_node_kind as usize]
}

impl TryFrom<Node> for Foo {
    type Error = Node;

    fn try_from(node: Node) -> Result<Self, Self::Error> {
        match node {
            Node::Foo(n) => Ok(n),
            _ => Err(node),
        }
    }
}

impl TryFrom<Node> for Epsilon {
    type Error = Node;

    fn try_from(node: Node) -> Result<Self, Self::Error> {
        match node {
            Node::Epsilon(n) => Ok(n),
            _ => Err(node),
        }
    }
}

impl TryFrom<Node> for Pair {
    type Error = Node;

    fn try_from(node: Node) -> Result<Self, Self::Error> {
        match node {
            Node::Pair(n) => Ok(n),
            _ => Err(node),
        }
    }
}

impl TryFrom<Node> for StringPair {
    type Error = Node;

    fn try_from(node: Node) -> Result<Self, Self::Error> {
        match node {
            Node::StringPair(n) => Ok(n),
            _ => Err(node),
        }
    }
}

impl TryFrom<Node> for NumberPair {
    type Error = Node;

    fn try_from(node: Node) -> Result<Self, Self::Error> {
        match node {
            Node::NumberPair(n) => Ok(n),
            _ => Err(node),
        }
    }
}

impl Node {
    fn try_into_string_0(self) -> Result<String, Self> {
        match self {
            Self::String(t) => Ok(t),
            _ => Err(self),
        }
    }

    fn try_into_number_1(self) -> Result<isize, Self> {
        match self {
            Self::Number(t) => Ok(t),
            _ => Err(self),
        }
    }
}