mirascript-core 0.1.105

Compiler and bytecode definitions for the MiraScript language
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
use std::{borrow::Cow, iter::zip, vec};

use winnow::{
    combinator::{alt, eof, fail, opt, peek, preceded},
    stream::{AsChar, Offset},
    token::{any, literal, one_of, take_till, take_while},
};

use super::{
    identifier::{identifier, is_identifier_special, is_identifier_start},
    prelude::*,
    tokens::token,
};

#[derive(Debug, Clone)]
pub enum StringFragment<'s> {
    Literal(&'s str),
    EscapedChar(char, &'s str),
    InvalidEscapedChar(SourceRange, DiagnosticCode),
    Interpolation(
        /// The dollar signs used for interpolation
        &'s str,
        /// The tokens inside the interpolation, will be taken to `TokenKind::InterpolatedString`
        Vec<Token<'s>>,
        /// The format string of the tokens inside the interpolation
        &'s str,
        /// The begin and end tokens for the interpolation, if any
        Option<Box<(Token<'s>, Token<'s>)>>,
    ),
    EndOfString,
    EndOfFile,
}

#[derive(Debug, Clone)]
pub struct StringInfo<'s> {
    pub leading_range: SourceRange,
    pub trailing_range: SourceRange,
    pub ats: usize,
    pub quote: Option<char>,
    pub content: Vec<StringFragment<'s>>,
}

const QUOTES: [char; 3] = ['\'', '"', '`'];

pub(super) fn string<'s>(i: &mut Input<'s>) -> Result<TokenKind<'s>> {
    let ((ats, quote), leading_range) = (take_while(0.., ['@']).map(str::len), one_of(QUOTES))
        .with_span()
        .parse_next(i)?;
    let info = StringInfo {
        leading_range,
        trailing_range: SourceRange::default(),
        ats,
        quote: Some(quote),
        content: vec![],
    };
    string_content(info).parse_next(i)
}

pub(super) fn string_content<'s>(mut info: StringInfo<'s>) -> impl Parser<'s, TokenKind<'s>> {
    move |i: &mut Input<'s>| {
        let unterminated = loop {
            let string_mode = info.quote.is_none();
            let frag = fragment(&mut info).parse_next(i)?;
            if matches!(frag, StringFragment::EndOfString) {
                break string_mode;
            } else if matches!(frag, StringFragment::EndOfFile) {
                break !string_mode;
            } else {
                info.content.push(frag);
            }
        };
        if !unterminated
            && info.content.len() == 1
            && let StringFragment::Literal(s) = info.content[0]
        {
            return Ok(TokenKind::String(Cow::Borrowed(s), info.clone().into()));
        }
        let mut errors = vec![];
        if unterminated {
            errors.push(SourceDiagnostic::new(
                SourceRange {
                    start: i.previous_token_end(),
                    end: i.previous_token_end(),
                },
                DiagnosticCode::UnterminatedString,
            ));
        }
        let has_interpolation = info
            .content
            .iter()
            .any(|frag| matches!(frag, StringFragment::Interpolation(..)));

        let mut extract_invalid = |mut range: SourceRange, code: DiagnosticCode| {
            range.start -= 1;
            errors.push(SourceDiagnostic::new(range, code));
            Cow::Borrowed("")
        };
        let token = if has_interpolation {
            let mut literals = vec![];
            let mut interpolations = vec![];
            let mut literal_pushed = false;
            for frag in info.content.iter_mut() {
                if let StringFragment::Interpolation(_, expr, fmt, _) = frag {
                    if !literal_pushed {
                        literals.push(Cow::Borrowed(""));
                    }
                    interpolations.push((std::mem::take(expr), *fmt));
                    literal_pushed = false;
                    continue;
                }
                let s: Cow<'s, str> = match frag {
                    StringFragment::Literal(s) => Cow::Borrowed(s),
                    StringFragment::EscapedChar(ch, _) => Cow::Owned(ch.to_string()),
                    StringFragment::InvalidEscapedChar(r, c) => extract_invalid(r.clone(), *c),
                    _ => unreachable!(),
                };
                if s.is_empty() {
                    continue;
                }
                if literal_pushed {
                    let last = literals.last_mut().unwrap();
                    last.to_mut().push_str(&s);
                } else {
                    literals.push(s);
                    literal_pushed = true;
                }
            }
            if !literal_pushed {
                literals.push(Cow::Borrowed(""));
            }
            interpolations.push((vec![], ""));
            TokenKind::InterpolatedString(
                zip(literals, interpolations)
                    .map(|(l, (e, f))| (l, e, f))
                    .collect(),
                info.clone().into(),
            )
        } else {
            let result = info.content.iter().fold(String::new(), |mut str, frag| {
                match frag {
                    StringFragment::Literal(s) => str.push_str(s),
                    StringFragment::EscapedChar(ch, _) => str.push(*ch),
                    StringFragment::InvalidEscapedChar(r, c) => {
                        let s = extract_invalid(r.clone(), *c);
                        str.push_str(&s);
                    }
                    _ => unreachable!(),
                }
                str
            });
            TokenKind::String(Cow::Owned(result), info.clone().into())
        };
        if !errors.is_empty() {
            return Ok(TokenKind::unknown_errors(token, errors));
        }
        Ok(token)
    }
}

fn fragment<'s>(info: &mut StringInfo<'s>) -> impl Parser<'s, StringFragment<'s>> {
    move |i: &mut Input<'s>| {
        alt((
            literal_str(info.quote, info.ats > 0).map(StringFragment::Literal),
            escaped_char(info.ats),
            eof.value(StringFragment::EndOfFile),
            maybe_end_of_string(info),
        ))
        .parse_next(i)
    }
}

fn maybe_end_of_string<'s>(info: &mut StringInfo<'s>) -> impl Parser<'s, StringFragment<'s>> {
    move |i: &mut Input<'s>| {
        let Some(quote) = info.quote else {
            return fail.parse_next(i);
        };
        let (s, r) = (literal(quote), take_while(0..=info.ats, ['@']))
            .take()
            .with_span()
            .parse_next(i)?;

        if s.len() == info.ats + 1 {
            info.trailing_range = r;
            Ok(StringFragment::EndOfString)
        } else {
            Ok(StringFragment::Literal(s))
        }
    }
}

fn literal_str<'s>(quote: Option<char>, verbatim: bool) -> impl Parser<'s, &'s str> {
    move |i: &mut Input<'s>| {
        take_till(1.., |ch| {
            ch == '$' || (!verbatim && ch == '\\') || (quote.is_some() && quote.unwrap() == ch)
        })
        .parse_next(i)
    }
}

fn escaped_char<'s>(ats: usize) -> impl Parser<'s, StringFragment<'s>> {
    move |i: &mut Input<'s>| {
        if ats == 0 {
            alt((
                preceded(literal("\\"), escaped_char_impl),
                move |i: &mut Input<'s>| {
                    let dollar = literal("$").parse_next(i)?;
                    interpolation(dollar).parse_next(i)
                },
            ))
            .parse_next(i)
        } else {
            // in verbatim string, '\\' escape is not allowed, only '$' is allowed
            let dollars = take_while(1..=ats, ['$']).parse_next(i)?;
            if dollars.len() == ats {
                // interpolation
                interpolation(dollars).parse_next(i)
            } else {
                // '$' is not escaped
                Ok(StringFragment::Literal(dollars))
            }
        }
    }
}

fn escaped_char_impl<'s>(i: &mut Input<'s>) -> Result<StringFragment<'s>> {
    fn escaped_char_1<'s>(i: &mut Input<'s>) -> Result<StringFragment<'s>> {
        let ch = any.parse_next(i)?;
        match ch {
            '0' => Ok(StringFragment::EscapedChar('\0', "0")),
            'r' => Ok(StringFragment::EscapedChar('\r', "r")),
            'n' => Ok(StringFragment::EscapedChar('\n', "n")),
            't' => Ok(StringFragment::EscapedChar('\t', "t")),
            'b' => Ok(StringFragment::EscapedChar('\x08', "b")),
            'f' => Ok(StringFragment::EscapedChar('\x0C', "f")),
            'v' => Ok(StringFragment::EscapedChar('\x0B', "v")),
            '\\' => Ok(StringFragment::EscapedChar('\\', "\\")),
            '"' => Ok(StringFragment::EscapedChar('"', "\"")),
            '\'' => Ok(StringFragment::EscapedChar('\'', "'")),
            '`' => Ok(StringFragment::EscapedChar('`', "`")),
            '$' => Ok(StringFragment::EscapedChar('$', "$")),
            _ => fail.parse_next(i),
        }
    }
    alt((
        escaped_char_1,
        (
            'x',
            (one_of(AsChar::is_hex_digit), one_of(AsChar::is_hex_digit)),
        )
            .take()
            .with_span()
            .map(|(s, r): (&str, _)| match u8::from_str_radix(&s[1..], 16) {
                Ok(ch) if ch <= 0x7f => StringFragment::EscapedChar(ch as char, s),
                _ => {
                    StringFragment::InvalidEscapedChar(r, DiagnosticCode::InvalidHexEscapeSequence)
                }
            }),
        ("u{", take_while(1.., AsChar::is_hex_digit), '}')
            .take()
            .with_span()
            .map(
                |(s, r): (&str, _)| match u32::from_str_radix(&s[2..s.len() - 1], 16) {
                    Ok(ch) => {
                        if let Some(c) = char::from_u32(ch) {
                            StringFragment::EscapedChar(c, s)
                        } else {
                            StringFragment::InvalidEscapedChar(
                                r,
                                DiagnosticCode::InvalidUnicodeEscapeSequence,
                            )
                        }
                    }
                    Err(_) => StringFragment::InvalidEscapedChar(
                        r,
                        DiagnosticCode::InvalidUnicodeEscapeSequence,
                    ),
                },
            ),
        any.span()
            .map(|r| StringFragment::InvalidEscapedChar(r, DiagnosticCode::InvalidEscapeSequence)),
        eof.value(StringFragment::EndOfFile),
    ))
    .parse_next(i)
}

fn format_string<'s>(i: &mut Input<'s>) -> Result<&'s str> {
    let start = i.checkpoint();

    let mut depth = 0; // 括号深度
    let mut in_class = false; // 是否在字符类 [... ] 内

    while !i.is_empty() {
        let checkpoint = i.checkpoint();

        match i.next_token() {
            Some('\\') => {
                // 转义:跳过下一个字符
                let _ = i.next_token();
            }
            Some('[') if !in_class => {
                in_class = true;
            }
            Some(']') if in_class => {
                in_class = false;
            }
            Some('(') if !in_class => {
                depth += 1;
            }
            Some(')') if !in_class => {
                if depth == 0 {
                    // 找到插值结束符,回退这个 ')'
                    // 返回从 start 到当前位置的字符串
                    let offset = checkpoint.offset_from(&start);
                    i.reset(&start);
                    return Ok(i.next_slice(offset));
                } else {
                    depth -= 1;
                }
            }
            Some(_) => {
                // 其他字符,继续
            }
            None => break,
        }
    }

    // 如果到这里说明没找到匹配的 ')'
    i.reset(&start);
    let offset = i.eof_offset();
    Ok(i.next_slice(offset))
}

fn interpolation<'s>(dollars: &'s str) -> impl Parser<'s, StringFragment<'s>> {
    move |i: &mut Input<'s>| -> Result<StringFragment<'s>> {
        // save cp
        let cp = i.checkpoint();

        let first = peek(opt(any)).parse_next(i)?;

        let part = match first {
            Some('{') => {
                // '$' block_expression
                let mut depth = 0;
                let tokens = match lex_impl(i, |t| {
                    if *t == Operator::OpenBrace {
                        depth += 1;
                    } else if *t == Operator::CloseBrace {
                        depth -= 1;
                    }
                    depth <= 0
                }) {
                    Ok(tokens) => tokens,
                    Err(e) => {
                        i.reset(&cp);
                        return Err(e);
                    }
                };
                StringFragment::Interpolation(dollars, tokens, "", None)
            }
            Some('(') => {
                // '$' '(' expression [ ':' format ] ')'
                let mut depth = 0;
                let mut brace_depth = 0;
                let mut has_format = false;
                let mut tokens = lex_impl(i, |t| {
                    if *t == Operator::OpenParen {
                        depth += 1;
                    } else if *t == Operator::CloseParen {
                        depth -= 1;
                    } else if *t == Operator::OpenBrace {
                        brace_depth += 1;
                    } else if *t == Operator::CloseBrace {
                        brace_depth -= 1;
                    } else if depth == 1 && brace_depth == 0 && *t == Operator::Colon {
                        has_format = true;
                        return true;
                    }
                    depth <= 0
                })
                .inspect_err(|_| {
                    i.reset(&cp);
                })?;

                let format = if has_format {
                    format_string.parse_next(i)?
                } else {
                    ""
                };

                let end = if has_format {
                    let last = tokens.pop_if(|f| *f == Operator::Colon);
                    debug_assert!(last.is_some());
                    Some(token(i, None)?)
                } else {
                    tokens.pop_if(|f| *f == Operator::CloseParen)
                }
                .unwrap_or_else(|| Token::empty(tokens.last().unwrap().range.end));
                let begin = tokens.remove(0);

                let filtered_tokens = if tokens.is_empty() {
                    vec![Token::empty(begin.range.end)]
                } else {
                    tokens
                };
                StringFragment::Interpolation(
                    dollars,
                    filtered_tokens,
                    format,
                    Some(Box::new((begin, end))),
                )
            }
            Some(ch) if is_identifier_start(ch) || is_identifier_special(ch) => {
                // '$' identifier
                let (mut kind, range) = identifier(true).with_span().parse_next(i)?;
                if kind == Keyword::Type {
                    kind = TokenKind::Identifier(Keyword::Type.into());
                }
                if let TokenKind::Keyword(kw) = kind
                    && !kw.is_constant()
                {
                    kind = TokenKind::unknown(
                        // Recover to nil for further analysis
                        Keyword::Nil,
                        range.clone(),
                        if kw.is_reserved() {
                            DiagnosticCode::InvalidReservedKeyword
                        } else {
                            DiagnosticCode::InvalidKeyword
                        },
                    );
                }
                let id = Token::new(kind, range);
                StringFragment::Interpolation(dollars, vec![id], "", None)
            }
            _ => {
                // invalid interpolation, return as literal
                StringFragment::Literal(dollars)
            }
        };

        Ok(part)
    }
}