lark-parser 0.1.0

Parser for the Lark language.
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
use derive_new::new;
use lark_debug_with::DebugWith;
use lark_span::{CurrentFile, Span, Spanned};
use std::fmt::{self, Debug};
use std::marker::PhantomData;

/// This enum describes which state to transition into next.
/// The LexerEmit nested inside of the LexerNext describes
/// what happens before the transition
#[derive(Debug)]
pub enum LexerNext<Delegate: LexerDelegateTrait> {
    EOF,
    Remain(LexerAccumulate<Delegate>),
    Transition(LexerAccumulate<Delegate>, Delegate),
    PushState(LexerAccumulate<Delegate>, Delegate),
    PopState(LexerAccumulate<Delegate>),
}

impl<Delegate: LexerDelegateTrait> LexerNext<Delegate> {
    pub fn begin(state: Delegate) -> Self {
        LexerNext::Transition(LexerAccumulate::Begin, state)
    }

    /// A sigil is a single character that represents a token
    pub fn sigil(token: Delegate::Token) -> Self {
        LexerNext::Remain(LexerAccumulate::Emit {
            before: Some(LexerAction::Consume(1)),
            after: None,
            token,
        })
    }

    /// Move into the state `state`, and once it recognizes something,
    /// emit the token `token` -- this is for multicharacter tokens.
    pub fn emit(token: Delegate::Token, state: Delegate) -> Self {
        LexerNext::Transition(
            LexerAccumulate::Emit {
                before: None,
                after: None,
                token,
            },
            state,
        )
    }
}

/// This enum describes whether to emit the current token.
#[derive(Debug)]
pub enum LexerAccumulate<Delegate: LexerDelegateTrait> {
    /// Start a new token. There should be no accumulated characters
    /// yet.
    Begin,

    /// Don't accumulate any characters or emit anything
    Nothing,

    /// Don't emit anything, but continue to accumulate characters
    /// in the current token
    Continue(LexerAction),

    /// Start a new token after possibly consuming some characters.
    /// Those characters are ignored, and are not part of any token.
    Skip(LexerAction),

    /// Emit a token after accumulating some characters into it.
    /// Possibly skip some characters afterward.
    Emit {
        before: Option<LexerAction>,
        after: Option<LexerAction>,
        token: Delegate::Token,
    },
}

impl<Delegate: LexerDelegateTrait> From<LexerAccumulate<Delegate>> for LexerNext<Delegate> {
    fn from(accum: LexerAccumulate<Delegate>) -> LexerNext<Delegate> {
        LexerNext::Remain(accum)
    }
}

impl<Delegate: LexerDelegateTrait> LexerAccumulate<Delegate> {
    pub fn and_remain(self) -> LexerNext<Delegate> {
        LexerNext::Remain(self)
    }

    pub fn and_transition(self, state: Delegate) -> LexerNext<Delegate> {
        LexerNext::Transition(self, state)
    }

    pub fn and_push(self, state: Delegate) -> LexerNext<Delegate> {
        LexerNext::PushState(self, state)
    }

    pub fn and_pop(self) -> LexerNext<Delegate> {
        LexerNext::PopState(self)
    }
}

impl<Delegate: LexerDelegateTrait> From<LexerAction> for LexerAccumulate<Delegate> {
    fn from(action: LexerAction) -> LexerAccumulate<Delegate> {
        LexerAccumulate::Continue(action)
    }
}

impl<Delegate: LexerDelegateTrait> From<LexerAction> for LexerNext<Delegate> {
    fn from(action: LexerAction) -> LexerNext<Delegate> {
        LexerNext::Remain(LexerAccumulate::Continue(action))
    }
}

impl<Delegate: LexerDelegateTrait> LexerAccumulate<Delegate> {
    pub fn emit(token: Delegate::Token) -> LexerAccumulate<Delegate> {
        LexerAccumulate::Emit {
            before: None,
            after: None,
            token: token,
        }
    }

    pub fn before(self, action: LexerAction) -> LexerAccumulate<Delegate> {
        match self {
            LexerAccumulate::Emit {
                before: None,
                after,
                token,
            } => LexerAccumulate::Emit {
                before: Some(action),
                after,
                token,
            },

            other => panic!("Can't add a before action to {:?}", other),
        }
    }

    pub fn and_then(self, action: LexerAction) -> LexerAccumulate<Delegate> {
        match self {
            LexerAccumulate::Emit {
                before,
                after: None,
                token,
            } => LexerAccumulate::Emit {
                before,
                after: Some(action),
                token,
            },

            other => panic!("Can't add an after action to {:?}", other),
        }
    }
}

#[derive(Debug, Copy, Clone)]
pub enum LexerAction {
    Consume(usize),
    Reconsume,
}

impl LexerAction {
    pub fn and_remain<Delegate: LexerDelegateTrait>(self) -> LexerNext<Delegate> {
        LexerNext::Remain(LexerAccumulate::Continue(self))
    }

    pub fn and_transition<Delegate: LexerDelegateTrait>(
        self,
        state: Delegate,
    ) -> LexerNext<Delegate> {
        LexerNext::Transition(LexerAccumulate::Continue(self), state)
    }

    pub fn and_push<Delegate: LexerDelegateTrait>(self, state: Delegate) -> LexerNext<Delegate> {
        LexerNext::PushState(LexerAccumulate::Continue(self), state)
    }

    pub fn and_pop<Delegate: LexerDelegateTrait>(self) -> LexerNext<Delegate> {
        LexerNext::PopState(LexerAccumulate::Continue(self))
    }

    pub fn and_continue<Delegate: LexerDelegateTrait>(self) -> LexerAccumulate<Delegate> {
        LexerAccumulate::Continue(self)
    }

    pub fn and_discard<Delegate: LexerDelegateTrait>(self) -> LexerAccumulate<Delegate> {
        LexerAccumulate::Skip(self)
    }

    pub fn and_emit<Delegate: LexerDelegateTrait>(
        self,
        token: Delegate::Token,
    ) -> LexerAccumulate<Delegate> {
        LexerAccumulate::Emit {
            before: Some(self),
            after: None,
            token: token,
        }
    }
}

pub fn reconsume() -> LexerAction {
    LexerAction::Reconsume
}

pub fn consume(c: char) -> LexerAction {
    LexerAction::Consume(c.len_utf8())
}

pub fn consume_str(s: &str) -> LexerAction {
    LexerAction::Consume(s.len())
}

pub fn begin<Delegate: LexerDelegateTrait>() -> LexerAccumulate<Delegate> {
    LexerAccumulate::Begin
}

pub fn eof<Delegate: LexerDelegateTrait>() -> LexerNext<Delegate> {
    LexerNext::EOF
}

pub trait LexerDelegateTrait: fmt::Debug + Clone + Copy + Sized {
    type Token: fmt::Debug + DebugWith + Copy;

    fn next(&self, c: Option<char>, rest: &'input str) -> LexerNext<Self>;

    fn top() -> Self;
}

#[derive(Debug, new)]
pub struct Tokenizer<'table, Delegate: LexerDelegateTrait> {
    input: &'table str,

    /// The rest of the input
    #[new(value = "input")]
    rest: &'table str,

    /// The beginning of the current token
    #[new(value = "input")]
    token_start: &'table str,

    /// The position of the token_start offset from the input
    #[new(default)]
    start_pos: usize,

    /// The current size of the token
    #[new(default)]
    token_len: usize,

    #[new(value = "Delegate::top()")]
    state: Delegate,

    #[new(value = "vec![]")]
    stack: Vec<Delegate>,

    #[new(default)]
    token: PhantomData<Delegate::Token>,
}

pub type TokenizerItem<Token> = Result<Spanned<Token, CurrentFile>, Span<CurrentFile>>;

impl<Delegate: LexerDelegateTrait + Debug> Iterator for Tokenizer<'table, Delegate> {
    type Item = TokenizerItem<Delegate::Token>;

    fn next(&mut self) -> Option<Self::Item> {
        let mut count = 0;
        loop {
            count += 1;

            if count > 1000 {
                return None;
            }

            // Get an action from the delegate
            let next = {
                let Tokenizer { state, rest, .. } = self;
                let next = rest.chars().next();

                log::trace!("next");
                log::trace!("          char={:?}", next);
                log::trace!("          rest={:?}", rest);

                state.next(next, rest)
            };

            self.trace("start");

            match self.step(next) {
                LoopCompletion::Return(v) => return self.emit(v),
                LoopCompletion::Continue => {}
            }
        }
    }
}

enum LoopCompletion<T> {
    Continue,
    Return(T),
}

impl<Delegate: LexerDelegateTrait + Debug> Tokenizer<'table, Delegate> {
    pub fn tokens(self) -> Result<Vec<Spanned<Delegate::Token, CurrentFile>>, Span<CurrentFile>> {
        self.collect()
    }

    fn error(&mut self, _: Option<char>) -> Span<CurrentFile> {
        // let token = &self.token_start[..self.token_size() as usize];
        // let (start_pos, end_pos) = self.consume_token(1);

        let span = Span::new(CurrentFile, self.start_pos, self.start_pos + self.token_len);

        log::debug!("lark::tokenize::error {:?}", span);

        span
    }

    fn trace(&self, prefix: &str) {
        log::trace!(target: "lark::tokenize", "{}", prefix);

        log::trace!(target: "lark::tokenize", "          pos={:?}", self.start_pos + self.token_len);

        let token_start = (self.start_pos) as usize;
        let token_end = token_start + self.token_len as usize;

        log::trace!(
            target: "lark::tokenize",
            "          token-start={:?}",
            &self.input[token_start..token_end]
        );

        log::trace!(target: "lark::tokenize", "          token-size={:?} state={:?}", self.token_len, self.state)
    }

    /// Take a LexerNext and process it
    fn step(
        &mut self,
        next: LexerNext<Delegate>,
    ) -> LoopCompletion<Option<TokenizerItem<Delegate::Token>>> {
        log::debug!("next: {:?}", next);
        match next {
            // If the delegate returned EOF, we're done
            LexerNext::EOF => {
                self.trace("EOF");
                return LoopCompletion::Return(None);
            }

            LexerNext::Remain(accum) => self.accumulate(accum),

            LexerNext::Transition(accum, state) => {
                let ret = self.accumulate(accum);
                self.transition(state);

                self.trace("transition");

                ret
            }

            LexerNext::PushState(action, state) => {
                let ret = self.accumulate(action);
                self.stack.push(state.clone());
                self.transition(state);

                ret
            }

            LexerNext::PopState(action) => {
                let ret = self.accumulate(action);
                let state = self.stack.pop().expect("Unexpected empty state stack");
                self.transition(state);

                ret
            }
        }
    }

    fn accumulate(
        &mut self,
        accum: LexerAccumulate<Delegate>,
    ) -> LoopCompletion<Option<TokenizerItem<Delegate::Token>>> {
        use self::LexerAccumulate::*;

        match accum {
            Begin => {
                assert!(
                    self.token_len == 0,
                    "Cannot begin a new token when there are already accumulated characters"
                );

                LoopCompletion::Continue
            }
            Nothing => LoopCompletion::Continue,
            Continue(action) => {
                self.action(action);
                LoopCompletion::Continue
            }
            Skip(action) => {
                self.action(action);

                // TODO: Extract into "finalization"?
                self.start_pos = self.start_pos + self.token_len;
                self.token_len = 0;
                LoopCompletion::Continue
            }

            Emit { before, token, .. } => {
                if let Some(before) = before {
                    self.action(before);
                }

                let start = self.start_pos;
                let len = self.token_len;
                self.start_pos = start + len;
                self.token_len = 0;

                LoopCompletion::Return(Some(Ok(Spanned::new(
                    token,
                    Span::new(CurrentFile, start, start + len),
                ))))
            }
        }
    }

    fn action(&mut self, action: LexerAction) {
        match action {
            LexerAction::Consume(n) => {
                self.token_len += n;
                self.rest = &self.rest[(n as usize)..];
            }
            LexerAction::Reconsume => {}
        }
    }

    fn transition(&mut self, state: Delegate) {
        log::debug!("transition {:?} -> {:?}", self.state, state);
        self.state = state;
    }

    fn emit(
        &mut self,
        token: Option<TokenizerItem<Delegate::Token>>,
    ) -> Option<TokenizerItem<Delegate::Token>> {
        match &token {
            None => log::debug!("-> EOF"),
            Some(Err(e)) => log::debug!("parse error {:?}", e.debug_with(&self.state)),
            Some(Ok(tok)) => log::debug!("emit {:?}", tok.debug_with(&self.state)),
        };

        token
    }
}