silkworm_syn 0.1.0-dev.1

Parser for the Yarn interactive dialogue language. Internal dependency of silkworm.
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
use arrayvec::ArrayVec;
use thiserror::Error;

use crate::token::{self, KeywordClass, Token};
use crate::Span;

mod parse;

use self::parse::parse_token;

const MODE_STACK_CAPACITY: usize = 256;

/// Block-level modes used to modify lexer behavior.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[repr(u8)]
#[doc(hidden)]
pub enum BlockMode {
    Header,
    Body,
}

/// Inline modes used to modify lexer behavior.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[repr(u8)]
#[doc(hidden)]
pub enum InlineMode {
    /// After start of line and before any content.
    StartOfLine,
    /// Header key.
    HeaderKey,
    /// Title or tags header values after their respective keywords.
    HeaderTitleOrTagsValue,
    /// Unspecified line body.
    FreeText,
    /// Hashtag
    Hashtag,
    /// After `//#` and before line break.
    Meta,
    /// Between `<<` and `>>`.
    Command,
    /// Right after `[[`., before `|`.
    OptionTextOrTarget,
    /// Between `[[` and `]]`, after `|`.
    OptionTarget,
    /// Between `[` and `]`.
    FormatFunction,
    /// After an opening `"` and before a closing one.
    StringLiteral,
    /// After an opening `` ` `` and before a closing one.
    InterpolatedStringLiteral,
    /// Between `{` and `}`.
    Interpolation,
}

impl InlineMode {
    fn may_contain_text(self) -> bool {
        use InlineMode as I;

        match self {
            I::FreeText
            | I::Hashtag
            | I::OptionTextOrTarget
            | I::StringLiteral
            | I::InterpolatedStringLiteral => true,
            _ => false,
        }
    }

    fn may_contain_escape(self) -> bool {
        use InlineMode as I;

        match self {
            I::Hashtag | I::StringLiteral | I::InterpolatedStringLiteral => true,
            _ => false,
        }
    }

    fn may_contain_text_interpolation(self) -> bool {
        use InlineMode as I;

        match self {
            I::FreeText | I::OptionTextOrTarget | I::InterpolatedStringLiteral => true,
            _ => false,
        }
    }

    fn may_contain_commands(self) -> bool {
        use InlineMode as I;

        match self {
            I::StartOfLine | I::FreeText => true,
            _ => false,
        }
    }

    fn may_contain_whitespace(self) -> bool {
        !self.may_contain_text()
    }

    fn is_text_interrupting_symbol(self, c: char) -> bool {
        use InlineMode as I;

        match self {
            I::FreeText => match c {
                '\\' | '/' | '[' | '{' | '#' | '<' => true,
                _ => false,
            },
            I::Hashtag => match c {
                '\\' | '/' | '#' => true,
                _ => false,
            },
            I::OptionTextOrTarget => match c {
                '\\' | '-' | '[' | '{' | '|' | ']' => true,
                _ => false,
            },
            I::StringLiteral => match c {
                '\\' | '"' => true,
                _ => false,
            },
            I::InterpolatedStringLiteral => match c {
                '\\' | '[' | '{' | '`' => true,
                _ => false,
            },
            _ => panic!("{:?} is not a textual mode", self),
        }
    }
}

impl KeywordClass {
    fn may_appear_in(self, block_mode: BlockMode, inline_mode: InlineMode) -> bool {
        match self {
            KeywordClass::Used | KeywordClass::Reserved => true,
            KeywordClass::HeaderKey => {
                block_mode == BlockMode::Header
                    && match inline_mode {
                        InlineMode::StartOfLine | InlineMode::HeaderKey => true,
                        _ => false,
                    }
            }
            KeywordClass::Pragma | KeywordClass::FeatureName => inline_mode == InlineMode::Meta,
        }
    }
}

/// A fatal lexing error.
#[derive(Debug, Error)]
pub enum ErrorKind {
    #[error("the inline mode stack is too deep, use less nested expressions")]
    StackTooDeep,
}

/// A spanned fatal lexing error.
#[derive(Debug, Error)]
#[error("error {kind} at {token:?}")]
pub struct Error {
    pub kind: ErrorKind,
    pub token: Token,
}

/// A lexing stream over a source string that can be consumed as an iterator.
#[derive(Clone, Debug)]
pub struct LexStream<'a> {
    src: &'a str,
    pos: u32,

    fatal: bool,
    eof_emitted: EofState,

    block_mode: BlockMode,
    inline_stack: ArrayVec<[InlineMode; MODE_STACK_CAPACITY]>,

    last_indent: u32,
    current_line_indent: u32,

    delayed: Option<Token>,
}

#[derive(Copy, Clone, Debug)]
enum EofState {
    NotEmitted,
    LastUnIndentEmitted,
    Emitted,
}

impl<'a> LexStream<'a> {
    /// Creates a new lexing stream.
    pub fn new(src: &'a str, pos: u32) -> Self {
        Self::with_modes(src, pos, BlockMode::Header, InlineMode::StartOfLine)
    }

    /// Creates a new lexing stream with given block and inline modes. This can cause
    /// surprising behavior outside very specific situations. Used for convenience methods
    /// in `Parse`.
    pub(crate) fn with_modes(
        src: &'a str,
        pos: u32,
        block_mode: BlockMode,
        inline_mode: InlineMode,
    ) -> Self {
        let mut inline_stack = ArrayVec::new();
        inline_stack.push(inline_mode);

        LexStream {
            src,
            pos,

            fatal: false,
            eof_emitted: EofState::NotEmitted,

            block_mode,
            inline_stack,

            last_indent: 0,
            current_line_indent: 0,

            delayed: None,
        }
    }

    /// Returns the current position of this stream in bytes into the source string.
    pub fn pos(&self) -> u32 {
        self.pos
    }

    fn push_inline_mode(&mut self, mode: InlineMode) -> Result<(), ErrorKind> {
        if let Some(inline_mode) = self.inline_stack.last_mut() {
            if *inline_mode == InlineMode::StartOfLine {
                *inline_mode = InlineMode::FreeText;
            }
        }

        self.inline_stack
            .try_push(mode)
            .map_err(|_| ErrorKind::StackTooDeep)
    }

    fn delay_token(&mut self, token: Token, new_kind: token::Kind) -> Token {
        if let Some(other_delayed) = self.delayed.replace(token) {
            panic!("already delayed token: {:?}", other_delayed);
        }

        let mut token = token;
        token.kind = new_kind;
        token.span.len = 0;

        token
    }

    fn indent_token(&mut self, current_token: Token) -> Option<Token> {
        use std::cmp::Ordering;

        let kind = match self.current_line_indent.cmp(&self.last_indent) {
            Ordering::Greater => Some(token::Kind::Indent),
            Ordering::Less => Some(token::Kind::UnIndent),
            Ordering::Equal => None,
        };

        kind.map(|kind| {
            let token = self.delay_token(current_token, kind);
            self.last_indent = self.current_line_indent;
            token
        })
    }
}

impl<'a> Iterator for LexStream<'a> {
    type Item = Result<Token, Error>;
    fn next(&mut self) -> Option<Result<Token, Error>> {
        use token::Delim as D;
        use token::Kind as T;

        #[derive(Copy, Clone, Debug)]
        enum ModeChange {
            Push(InlineMode),
            Replace(InlineMode),
            ReplaceBlock(BlockMode),
            Keep,
            Pop,
            EndLine,
        }

        if self.fatal {
            return None;
        }

        if let Some(delayed) = self.delayed.take() {
            return Some(Ok(delayed));
        }

        if self.src.is_empty() {
            match self.eof_emitted {
                EofState::NotEmitted if self.last_indent > 0 => {
                    self.eof_emitted = EofState::LastUnIndentEmitted;
                    return Some(Ok(Token::new(T::UnIndent, Span::new(self.pos, 0))));
                }
                EofState::NotEmitted | EofState::LastUnIndentEmitted => {
                    self.eof_emitted = EofState::Emitted;
                    return Some(Ok(Token::new(T::Eof, Span::new(self.pos, 0))));
                }
                EofState::Emitted => return None,
            }
        }

        let inline_mode = self
            .inline_stack
            .last()
            .copied()
            .expect("stack should not be empty");
        let token = parse_token(self.src, self.pos, self.block_mode, inline_mode);
        self.pos += token.span.len;
        self.src = &self.src[token.span.len as usize..];

        if let T::Eof = token.kind {
            return Some(Ok(token));
        }

        let mode_change: ModeChange = {
            use InlineMode as I;
            use ModeChange as C;

            match token.kind {
                T::LineBreak => C::EndLine,
                T::Text => {
                    if inline_mode == I::StartOfLine {
                        C::Replace(I::FreeText)
                    } else {
                        C::Keep
                    }
                }
                T::Hash => {
                    if inline_mode == I::StartOfLine || inline_mode == I::FreeText {
                        C::Replace(I::Hashtag)
                    } else {
                        C::Keep
                    }
                }
                T::TripleDash => C::ReplaceBlock(BlockMode::Body),
                T::TripleEq => C::ReplaceBlock(BlockMode::Header),

                T::Pragma(_) => C::Push(I::Meta),

                T::OpenDelim(D::Bracket) => C::Push(I::FormatFunction),
                T::OpenDelim(D::DoubleBracket) => C::Push(I::OptionTextOrTarget),
                T::OpenDelim(D::DoubleAngleBracket) => C::Push(I::Command),
                T::OpenDelim(D::Brace) => C::Push(I::Interpolation),
                T::OpenDelim(D::DoubleQuote) => C::Push(I::StringLiteral),
                T::OpenDelim(D::Backtick) => C::Push(I::InterpolatedStringLiteral),

                T::CloseDelim(delim) => {
                    let pop_delim = match inline_mode {
                        I::FormatFunction => Some(D::Bracket),
                        I::OptionTextOrTarget => Some(D::DoubleBracket),
                        I::OptionTarget => Some(D::DoubleBracket),
                        I::Command => Some(D::DoubleAngleBracket),
                        I::Interpolation => Some(D::Brace),
                        I::StringLiteral => Some(D::DoubleQuote),
                        I::InterpolatedStringLiteral => Some(D::Backtick),
                        _ => None,
                    };

                    if pop_delim == Some(delim) {
                        C::Pop
                    } else {
                        C::Keep
                    }
                }

                T::Arrow => match inline_mode {
                    I::StartOfLine => C::Replace(I::FreeText),
                    I::OptionTextOrTarget => C::Replace(I::OptionTarget),
                    _ => C::Keep,
                },

                T::Pipe => {
                    if inline_mode == I::OptionTextOrTarget {
                        C::Replace(I::OptionTarget)
                    } else {
                        C::Keep
                    }
                }

                T::Keyword(token::Keyword::HeaderKey(header_key)) => match inline_mode {
                    I::StartOfLine | I::HeaderKey => match header_key {
                        token::HeaderKey::Title | token::HeaderKey::Tags => {
                            C::Replace(I::HeaderTitleOrTagsValue)
                        }
                    },
                    _ => panic!("header key keywords should not be parsed out of context"),
                },

                T::Colon => {
                    if inline_mode == I::HeaderKey {
                        C::Replace(I::FreeText)
                    } else {
                        C::Keep
                    }
                }

                _ => match inline_mode {
                    I::StartOfLine => {
                        if let T::Whitespace = token.kind {
                            self.current_line_indent += token.span.len;
                            C::Keep
                        } else {
                            match self.block_mode {
                                BlockMode::Header => C::Replace(I::HeaderKey),
                                BlockMode::Body => C::Replace(I::FreeText),
                            }
                        }
                    }
                    _ => C::Keep,
                },
            }
        };

        match mode_change {
            ModeChange::Push(mode) => {
                if let Err(kind) = self.push_inline_mode(mode) {
                    self.fatal = true;
                    return Some(Err(Error { kind, token }));
                }
            }
            ModeChange::Replace(mode) => {
                *self
                    .inline_stack
                    .last_mut()
                    .expect("stack should not be empty") = mode;
            }
            ModeChange::ReplaceBlock(mode) => self.block_mode = mode,
            ModeChange::Pop => {
                self.inline_stack.pop();
            }
            ModeChange::EndLine => {
                self.inline_stack.clear();
                self.inline_stack.push(InlineMode::StartOfLine);
                self.current_line_indent = 0;
            }
            ModeChange::Keep => {}
        }

        if inline_mode == InlineMode::StartOfLine {
            if let Some(new_mode) = self.inline_stack.last().copied() {
                if new_mode != inline_mode {
                    if let Some(token) = self.indent_token(token) {
                        return Some(Ok(token));
                    }
                }
            }
        }

        Some(Ok(token))
    }
}

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

    /// A simple sanity test indicating that the lexer works at least somewhat normal.
    /// Tests with proper output validation should be created later.
    #[test]
    fn it_works_at_all() {
        use super::token::Kind as T;

        let code = r#"
            //#! feature(foo, bar, baz)

            title: lex_sanity_test.foo.bar
            tags: foo bar baz すまん_不都合なことばかりで
            non_special_key: non special value
            ---
            No escapes: ¯\_(ツ)_/¯

            //# disable_feature(foo, bar, baz)
            <<if it_parses_strings("Foo\nBar\x3A baz\"\\\u{1234}") is true>>
                Yay {$yay}! #yay!
            <<else>>
                Oops {$oops}! //Oops!
            <<endif>>

            //# private
            <<if what_about_interpolation(`{$foo} + {$bar}`) is true>>
                Yay {what(`about {$here}?`)}! #yay!
            <<else>>
                Oops {$oops}! <<if $foo>> //Oops!
            <<endif>>

            [format_func {$expr} foo="bar"]
            
            <<move camera left>>
            <<unlockAchievement beganAdventure>>
            <<set $答え to 42>>

            <<set $private = $title is not $special>>

            [[Wow.Wow]] # foo // bar
            [[Wow.Wow(Fish, Life)]] # foo // bar
            [[Wow wow | Fish.Life]] # foo // bar
            [[Wow wow | Fish.Life()]] # foo // bar
            ===
        "#;

        let mut has_unknowns = false;

        let mut indent_pairing = 0;

        for token in LexStream::new(code, 0).take(1000) {
            let token = token.expect("should raise no internal errors");

            let spanned_code =
                &code[token.span.base as usize..(token.span.base + token.span.len) as usize];

            match token.kind {
                T::LineBreak => {
                    println!();
                }
                T::Whitespace => {
                    print!("{}", spanned_code);
                }
                T::Indent => {
                    indent_pairing += 1;
                    print!("<IDT> ");
                }
                T::UnIndent => {
                    indent_pairing -= 1;
                    print!("<UDT> ");
                }
                _ => {
                    if let T::Unknown = token.kind {
                        has_unknowns = true;
                    }

                    print!("{} ({:?}) ", spanned_code, token.kind);
                }
            }
        }

        if indent_pairing != 0 {
            panic!("indent unbalanced");
        }

        if has_unknowns {
            panic!("parsed tokens contained unknowns");
        }
    }
}