mica 0.7.1

A simple, user-friendly, embeddable scripting 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
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
//! The lexer.

use std::{fmt, rc::Rc};

use crate::ll::error::{LanguageError, LanguageErrorKind, Location};

/// The kind of a token.
#[derive(Debug, Clone, PartialEq)]
pub enum TokenKind {
    Number(f64),
    String(Rc<str>),
    // NOTE: Long strings \\ are joined into one string literal by the parser.
    LongString(Rc<str>),

    Identifier(Rc<str>),

    Nil,
    True,
    False,

    Do,
    If,
    Elif,
    Else,
    While,
    For,
    In,
    Func,
    End,
    Break,
    Return,

    Struct,
    Trait,
    Impl,
    As,
    Constructor,
    Static,

    Plus,  // +
    Minus, // -
    Star,  // *
    Slash, // /

    Bang,         // !
    And,          // and
    Or,           // or
    Equal,        // ==
    NotEqual,     // !=
    Less,         // <
    Greater,      // >
    LessEqual,    // <=
    GreaterEqual, // >=

    Assign, // =
    Dot,    // .
    Colon,  // :
    At,     // @

    LeftParen,    // (
    RightParen,   // )
    LeftBracket,  // [
    RightBracket, // ]
    Comma,        // ,

    Eof,
}

/// A token kind paired with its source code location.
#[derive(Debug, Clone)]
pub struct Token {
    pub kind: TokenKind,
    pub location: Location,
}

/// Lexer state.
pub struct Lexer {
    pub module_name: Rc<str>,
    input: String,
    location: Location,
    token_start: Location,
}

impl Lexer {
    /// The EOF sentinel character.
    const EOF: char = '\0';

    /// Creates a new lexer.
    pub fn new(module_name: Rc<str>, input: String) -> Self {
        Self { module_name, input, location: Default::default(), token_start: Default::default() }
    }

    /// Emits an error.
    fn error(&self, kind: LanguageErrorKind) -> LanguageError {
        self.error_at(self.location, kind)
    }

    /// Emits an error at a specific location.
    fn error_at(&self, location: Location, kind: LanguageErrorKind) -> LanguageError {
        LanguageError::Compile { module_name: Rc::clone(&self.module_name), kind, location }
    }

    /// Emits a token at the `token_start` location.
    fn token(&self, kind: TokenKind) -> Token {
        Token { kind, location: self.token_start }
    }

    /// Returns the character at the current position.
    fn get(&self) -> char {
        self.input[self.location.byte..].chars().next().unwrap_or(Self::EOF)
    }

    /// Advances the current position by a character.
    fn advance(&mut self) {
        self.location.byte += self.get().len_utf8();
        self.location.column += 1;
    }

    /// Advances the source location to the next line.
    fn advance_line(&mut self) {
        self.location.line += 1;
        self.location.column = 1;
    }

    /// Skips whitespace characters.
    fn skip_whitespace(&mut self) {
        loop {
            match self.get() {
                ' ' | '\t' => {
                    self.advance();
                }
                '#' => {
                    while self.get() != '\n' {
                        self.advance();
                    }
                }
                '\n' => {
                    self.advance();
                    self.advance_line();
                }
                _ => break,
            }
        }
    }

    /// Returns whether the character is a digit that's part of a number literal.
    fn is_digit_or_underscore(c: char, radix: u32) -> bool {
        c.is_digit(radix) || c == '_'
    }

    /// Collects digits into a string.
    fn collect_digits(&mut self, output: &mut String, radix: u32) -> Result<(), LanguageError> {
        let start_location = self.location;
        let mut had_digits = false;
        while Self::is_digit_or_underscore(self.get(), radix) {
            if self.get() != '_' {
                had_digits = true;
                output.push(self.get());
            }
            self.advance();
        }
        if !had_digits {
            return Err(self.error_at(start_location, LanguageErrorKind::UnderscoresWithoutDigits));
        }
        Ok(())
    }

    /// Parses a number.
    fn number(&mut self) -> Result<f64, LanguageError> {
        let mut number = String::new();

        self.collect_digits(&mut number, 10)?;
        if self.get() == '.' {
            let dot = self.location.byte;
            number.push(self.get());
            self.advance();
            if Self::is_identifier_start_char(self.get()) {
                // Special case: backtrack to the dot if we find an identifier after the decimal
                // point. We want to parse this as a method call.
                self.location.byte = dot;
            } else if Self::is_digit_or_underscore(self.get(), 10) {
                self.collect_digits(&mut number, 10)?;
            } else {
                return Err(self.error(LanguageErrorKind::MissingDigitsAfterDecimalPoint));
            }
        }
        if let 'e' | 'E' = self.get() {
            number.push(self.get());
            self.advance();
            if let '+' | '-' = self.get() {
                number.push(self.get());
                self.advance();
            }
            if !Self::is_digit_or_underscore(self.get(), 10) {
                return Err(self.error(LanguageErrorKind::MissingExponent));
            }
            self.collect_digits(&mut number, 10)?;
        }

        // Parsing here must succeed as we only allow decimal digits and a decimal point '.'.
        let number = number.parse().unwrap();
        Ok(number)
    }

    /// Parses a 32-bit integer with the specified radix.
    fn integer(&mut self, radix: u32) -> Result<u32, LanguageError> {
        let start_location = self.location;
        let mut number = String::new();
        self.collect_digits(&mut number, radix)?;
        if number.is_empty() {
            return Err(self.error_at(start_location, LanguageErrorKind::UnderscoresWithoutDigits));
        }
        u32::from_str_radix(&number, radix)
            .map_err(|_| self.error_at(start_location, LanguageErrorKind::IntLiteralOutOfRange))
    }

    /// Parses an extended `\16:123`-style integer with explicit radix.
    fn integer_with_explicit_radix(&mut self) -> Result<u32, LanguageError> {
        let radix_location = self.location;
        let radix = self.integer(10)?;
        if !(2..=36).contains(&radix) {
            return Err(self.error_at(radix_location, LanguageErrorKind::IntRadixOutOfRange));
        }
        if self.get() != ':' {
            return Err(self.error(LanguageErrorKind::ColonExpectedAfterRadix));
        }
        self.advance();
        self.integer(radix)
    }

    /// Parses an extended `\b1100`-style integer with a constant radix.
    fn integer_with_constant_radix(&mut self, radix: u32) -> Result<Token, LanguageError> {
        self.advance(); // Skip over the initial character (eg. b or x)
        let number = self.integer(radix)? as f64;
        Ok(self.token(TokenKind::Number(number)))
    }

    /// Parses a character inside of a string.
    fn string_char(&mut self) -> Result<char, LanguageError> {
        let c = self.get();
        self.advance();
        Ok(match c {
            '\\' => {
                let escape = self.get();
                let escape_char_location = self.location;
                self.advance();
                match escape {
                    '\'' => '\'',
                    '"' => '"',
                    '\\' => '\\',
                    'n' => '\n',
                    'r' => '\r',
                    't' => '\t',
                    'u' => {
                        let left_brace_location = self.location;
                        if self.get() != '{' {
                            return Err(self.error(LanguageErrorKind::UEscapeLeftBraceExpected));
                        }
                        self.advance();
                        let digits_location = self.location;
                        let mut number = String::new();
                        self.collect_digits(&mut number, 16)?;
                        if self.get() != '}' {
                            return Err(self.error_at(
                                left_brace_location,
                                LanguageErrorKind::UEscapeMissingRightBrace,
                            ));
                        }
                        self.advance();
                        if number.is_empty() {
                            return Err(
                                self.error_at(digits_location, LanguageErrorKind::UEscapeEmpty)
                            );
                        }
                        // The only error here is guaranteed to be overflow.
                        let scalar_value = u32::from_str_radix(&number, 16).map_err(|_| {
                            self.error_at(digits_location, LanguageErrorKind::UEscapeOutOfRange)
                        })?;
                        char::try_from(scalar_value).map_err(|_| {
                            self.error_at(digits_location, LanguageErrorKind::UEscapeOutOfRange)
                        })?
                    }
                    other => {
                        return Err(self.error_at(
                            escape_char_location,
                            LanguageErrorKind::InvalidEscape(other),
                        ))
                    }
                }
            }
            other => other,
        })
    }

    /// Parses a string.
    fn string(&mut self, raw: bool) -> Result<String, LanguageError> {
        self.advance();
        let mut result = String::new();
        while self.get() != '"' {
            if self.get() == Self::EOF {
                return Err(self.error(LanguageErrorKind::MissingClosingQuote));
            }
            if self.get() == '\n' {
                return Err(self.error(LanguageErrorKind::LineBreakInStringIsNotAllowed));
            }
            result.push(if !raw {
                self.string_char()?
            } else {
                let c = self.get();
                self.advance();
                c
            });
        }
        self.advance();
        Ok(result)
    }

    /// Parses a character literal 'a'.
    fn character(&mut self) -> Result<char, LanguageError> {
        if self.get() != '\'' {
            return Err(self.error(LanguageErrorKind::CharacterMissingOpeningApostrophe));
        }
        self.advance();
        let c = self.string_char()?;
        if self.get() != '\'' {
            return Err(self.error(LanguageErrorKind::CharacterMissingClosingApostrophe));
        }
        self.advance();
        Ok(c)
    }

    /// Parses a long string literal \\.
    fn long_string(&mut self) -> String {
        let mut string = String::new();
        while !matches!(self.get(), '\n' | Self::EOF) {
            // NOTE: Newlines are added in by the parser.
            // CR (from CRLF line breaks) is handled like a normal character and thus is preserved.
            string.push(self.get());
            self.advance();
        }
        string
    }

    /// Parses an extended (or "backslash") literal, that is, a literal that begins with a
    /// backslash, is discriminated by a single character following the backslash, and continues
    /// onward.
    fn extended_literal(&mut self) -> Result<Token, LanguageError> {
        self.advance(); // Skip backslash
        match self.get() {
            'r' => {
                self.advance();
                let content = self.string(true)?;
                Ok(self.token(TokenKind::String(Rc::from(content))))
            }
            'u' => {
                self.advance();
                let c = self.character()? as u32 as f64;
                Ok(self.token(TokenKind::Number(c)))
            }
            '\\' => {
                self.advance();
                let content = self.long_string();
                Ok(self.token(TokenKind::LongString(Rc::from(content))))
            }
            '0'..='9' => {
                let number = self.integer_with_explicit_radix()? as f64;
                Ok(self.token(TokenKind::Number(number)))
            }
            'b' | 'B' => self.integer_with_constant_radix(2),
            // NOTE: Do not request an uppercase O, this is intentional so as to prevent confusion
            // between 0 and O.
            'o' => self.integer_with_constant_radix(8),
            'x' | 'X' => self.integer_with_constant_radix(16),
            other => Err(self.error(LanguageErrorKind::InvalidBackslashLiteral(other))),
        }
    }

    /// Parses a single character token.
    fn single_char_token(&mut self, kind: TokenKind) -> Token {
        self.advance();
        self.token(kind)
    }

    /// Parses a token that's either one or two characters.
    fn single_or_double_char_token(
        &mut self,
        single: TokenKind,
        second: char,
        double: TokenKind,
    ) -> Token {
        self.advance();
        if self.get() == second {
            self.advance();
            self.token(double)
        } else {
            self.token(single)
        }
    }

    /// Returns whether `c` can be the first character of an identifier.
    fn is_identifier_start_char(c: char) -> bool {
        c.is_alphabetic() || c == '_'
    }

    /// Returns whether `c` can be a continuing character of an identifier.
    fn is_identifier_char(c: char) -> bool {
        c.is_alphanumeric() || c == '_'
    }

    /// Parses an identifier.
    fn identifier(&mut self) -> &str {
        let start = self.location.byte;
        while Self::is_identifier_char(self.get()) {
            self.advance();
        }
        let end = self.location.byte;
        &self.input[start..end]
    }

    /// Returns which keyword this identifier corresponds to, or `None` if the identifier is not
    /// reserved.
    fn keyword(identifier: &str) -> Option<TokenKind> {
        Some(match identifier {
            "nil" => TokenKind::Nil,
            "true" => TokenKind::True,
            "false" => TokenKind::False,

            "and" => TokenKind::And,
            "or" => TokenKind::Or,

            "do" => TokenKind::Do,
            "if" => TokenKind::If,
            "elif" => TokenKind::Elif,
            "else" => TokenKind::Else,
            "while" => TokenKind::While,
            "for" => TokenKind::For,
            "in" => TokenKind::In,
            "func" => TokenKind::Func,
            "end" => TokenKind::End,
            "break" => TokenKind::Break,
            "return" => TokenKind::Return,

            "struct" => TokenKind::Struct,
            "impl" => TokenKind::Impl,
            "trait" => TokenKind::Trait,
            "as" => TokenKind::As,
            "constructor" => TokenKind::Constructor,
            "static" => TokenKind::Static,

            _ => return None,
        })
    }

    /// Parses the next token and returns it.
    pub fn next_token(&mut self) -> Result<Token, LanguageError> {
        self.skip_whitespace();
        self.token_start = self.location;

        match self.get() {
            '0'..='9' => {
                let number = self.number()?;
                Ok(self.token(TokenKind::Number(number)))
            }
            '"' => {
                let string = self.string(false)?;
                Ok(self.token(TokenKind::String(Rc::from(string))))
            }
            '\\' => Ok(self.extended_literal()?),

            c if Self::is_identifier_start_char(c) => {
                let identifier = self.identifier();
                Ok(if let Some(keyword) = Self::keyword(identifier) {
                    self.token(keyword)
                } else {
                    let identifier = Rc::from(identifier);
                    self.token(TokenKind::Identifier(identifier))
                })
            }

            '+' => Ok(self.single_char_token(TokenKind::Plus)),
            '-' => Ok(self.single_char_token(TokenKind::Minus)),
            '*' => Ok(self.single_char_token(TokenKind::Star)),
            '/' => Ok(self.single_char_token(TokenKind::Slash)),

            '=' => Ok(self.single_or_double_char_token(TokenKind::Assign, '=', TokenKind::Equal)),
            '!' => Ok(self.single_or_double_char_token(TokenKind::Bang, '=', TokenKind::NotEqual)),
            '<' => Ok(self.single_or_double_char_token(TokenKind::Less, '=', TokenKind::LessEqual)),
            '>' => Ok(self.single_or_double_char_token(
                TokenKind::Greater,
                '=',
                TokenKind::GreaterEqual,
            )),

            '.' => Ok(self.single_char_token(TokenKind::Dot)),
            ':' => Ok(self.single_char_token(TokenKind::Colon)),
            '@' => Ok(self.single_char_token(TokenKind::At)),

            '(' => Ok(self.single_char_token(TokenKind::LeftParen)),
            ')' => Ok(self.single_char_token(TokenKind::RightParen)),
            '[' => Ok(self.single_char_token(TokenKind::LeftBracket)),
            ']' => Ok(self.single_char_token(TokenKind::RightBracket)),
            ',' => Ok(self.single_char_token(TokenKind::Comma)),
            Self::EOF => Ok(self.token(TokenKind::Eof)),
            other => Err(self.error(LanguageErrorKind::InvalidCharacter(other))),
        }
    }

    /// Peeks at what the next token's going to be without advancing the lexer's position.
    pub fn peek_token(&mut self) -> Result<Token, LanguageError> {
        let location = self.location;
        let token = self.next_token()?;
        self.location = location;
        Ok(token)
    }
}

impl fmt::Debug for Lexer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Lexer").finish_non_exhaustive()
    }
}