claw-parser 0.2.6

The Claw language lexer and parser
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#![allow(clippy::upper_case_acronyms)]

use logos::Logos;

use miette::{Diagnostic, SourceSpan};
use thiserror::Error;

use claw_common::Source;

#[derive(Debug, PartialEq, Clone)]
pub struct TokenData {
    pub token: Token,
    pub span: SourceSpan,
}

#[derive(Error, Debug, Diagnostic)]
#[error("Unable to tokenize input")]
#[diagnostic()]
pub struct LexerError {
    #[source_code]
    src: Source,
    #[label("Here")]
    span: SourceSpan,
}

pub fn tokenize(src: Source, contents: &str) -> Result<Vec<TokenData>, LexerError> {
    let lexer = Token::lexer(contents);

    lexer
        .spanned()
        .map(|(token, span)| match token {
            Ok(token) => Ok(TokenData {
                token,
                span: SourceSpan::from(span),
            }),
            Err(_error) => Err(LexerError {
                src: src.clone(),
                span: span.into(),
            }),
        })
        .collect()
}

/// The Token type for the language.
#[derive(Logos, Debug, PartialEq, Clone)]
#[logos(error = ())]
#[logos(skip r"[ \t\r\n\f]+")]
#[logos(skip r"//[^\n]*")]
#[logos(subpattern word = r"[a-z][a-z0-9]*|[A-Z][A-Z0-9]*")]
#[logos(subpattern id = r"%?(?&word)(-(?&word))*")]
pub enum Token {
    /// Double-quoted string literal
    #[token("\"", parse_string_literal)]
    #[token("r", parse_raw_string_literal)]
    StringLiteral(String),

    /// A Decimal number literal
    #[regex(r"[0-9][_0-9]*", |lex| parse_decint_literal(lex.slice()))]
    #[regex(r"0b[01][_01]*", |lex| parse_bin_literal(lex.slice()))]
    #[regex(r"0x[0-9a-fA-F][_0-9a-fA-F]*", |lex| parse_hex_literal(lex.slice()))]
    IntLiteral(u64),

    /// A Decimal floating point literal
    #[regex(r"[0-9][_0-9]*\.[0-9][_0-9]*", |lex| parse_decfloat_literal(lex.slice()))]
    FloatLiteral(f64),

    /// An Identifier
    #[regex(r"(?&id)", |lex| lex.slice().to_string())]
    Identifier(String),

    // Keywords -----------------------------------------
    /// The Export Keyword
    #[token("export")]
    Export,

    /// The Import Keyword
    #[token("import")]
    Import,

    /// The From Keyword
    #[token("from")]
    From,

    /// The Function "func" Keyword
    #[token("func")]
    Func,

    /// The If Keyword
    #[token("if")]
    If,

    /// The For Keyword
    #[token("for")]
    For,

    /// The In Keyword
    #[token("in")]
    In,

    /// The Loop Keyword
    #[token("loop")]
    Loop,

    /// The Break Keyword
    #[token("break")]
    Break,

    /// The Continue Keyword
    #[token("continue")]
    Continue,

    /// The Return Keyword
    #[token("return")]
    Return,

    /// The Result Type Keyword
    #[token("result")]
    Result,

    /// The String Type Keyword
    #[token("string")]
    String,

    /// The Unsigned 8-bit Integer Type Keyword
    #[token("u8")]
    U8,

    /// The Unsigned 16-bit Integer Type Keyword
    #[token("u16")]
    U16,

    /// The Unsigned 32-bit Integer Type Keyword
    #[token("u32")]
    U32,

    /// The Unsigned 64-bit Integer Type Keyword
    #[token("u64")]
    U64,

    /// The Signed 8-bit Integer Type Keyword
    #[token("s8")]
    S8,

    /// The Signed 16-bit Integer Type Keyword
    #[token("s16")]
    S16,

    /// The Signed 32-bit Integer Type Keyword
    #[token("s32")]
    S32,

    /// The Signed 32-bit Integer Type Keyword
    #[token("s64")]
    S64,

    /// The 32-bit Floating-point Type Keyword
    #[token("f32")]
    F32,

    /// The 64-bit Floating-point Type Keyword
    #[token("f64")]
    F64,

    /// The As Keyword
    #[token("as")]
    As,

    /// The At Keyword
    #[token("at")]
    At,

    /// The Let Keyword
    #[token("let")]
    Let,

    /// The Mut Keyword
    #[token("mut")]
    Mut,

    /// The Bool Keyword
    #[token("bool")]
    Bool,

    /// The True Keyword
    #[token("true")]
    True,

    /// The False Keyword
    #[token("false")]
    False,

    // Symbols -----------------------------------------
    /// Left Parenthesis Symbol "("
    #[token("(")]
    LParen,

    /// Right Parenthesis Symbol ")"
    #[token(")")]
    RParen,

    /// Left Brace Symbol "{"
    #[token("{")]
    LBrace,

    /// Right Brace Symbol "}"
    #[token("}")]
    RBrace,

    /// Left Bracket Symbol "["
    #[token("[")]
    LBracket,

    /// Right Bracket Symbol "]"
    #[token("]")]
    RBracket,

    /// The Comma Delimiter ","
    #[token(",")]
    Comma,

    /// The Period or Dot Operator "."
    #[token(".")]
    Dot,

    /// The Range Operator ".."
    #[token("..")]
    Range,

    /// Colon Symbol ":"
    #[token(":")]
    Colon,

    /// Semicolon Symbol ";"
    #[token(";")]
    Semicolon,

    /// Assignment Operator "="
    #[token("=")]
    Assign,

    /// The Right Arrow Symbol "->"
    #[token("->")]
    Arrow,

    /// Addition Operator "+"
    #[token("+")]
    Add,

    /// Subtraction Operator "-"
    #[token("-")]
    Sub,

    /// Multiplication Operator "*"
    #[token("*")]
    Mult,

    /// Division Operator "/"
    #[token("/")]
    Div,

    /// Modulo Operator "%"
    #[token("%")]
    Mod,

    /// Invert Operator "!"
    #[token("!")]
    Invert,

    /// Logical And Operator
    #[token("and")]
    LogicalAnd,

    /// Logical Or Operator
    #[token("or")]
    LogicalOr,

    /// Bitwise Or "|"
    #[token("|")]
    BitOr,

    /// Bitwise And "&"
    #[token("&")]
    BitAnd,

    /// Bitwise XOR "^"
    #[token("^")]
    BitXor,

    /// Bit Shift Left Operator "<<"
    #[token("<<")]
    BitShiftL,

    /// Bit Shift Right Operator ">>"
    #[token(">>")]
    BitShiftR,

    /// Arithmetic Shift Right Operator ">>>"
    #[token(">>>")]
    ArithShiftR,

    /// Bitwise-Or and Assign Operator "+="
    #[token("|=")]
    BitOrAssign,

    /// Bitwise-And and Assign Operator "+="
    #[token("&=")]
    BitAndAssign,

    /// Bitwsie-Xor and Assign Operator "+="
    #[token("^=")]
    BitXorAssign,

    /// Add and Assign Operator "+="
    #[token("+=")]
    AddAssign,

    /// Subtract and Assign Operator "-="
    #[token("-=")]
    SubAssign,

    /// Star Operator "*=" (used for multiply)
    #[token("*=")]
    StarAssign,

    /// Division Operator "/"
    #[token("/=")]
    DivAssign,

    /// Less-than Operator "<"
    #[token("<")]
    LT,

    /// Less-than or Equal Operator "<="
    #[token("<=")]
    LTE,

    /// Greater-than Operator ">"
    #[token(">")]
    GT,

    /// Greater-than or Equal Operator ">="
    #[token(">=")]
    GTE,

    /// Equals Operator "=="
    #[token("==")]
    EQ,

    // Not Equals Operator "!="
    #[token("!=")]
    NEQ,
}

impl std::fmt::Display for Token {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Token::StringLiteral(s) => write!(f, "\"{}\"", s),
            Token::IntLiteral(i) => write!(f, "{}", i),
            Token::FloatLiteral(float) => write!(f, "{:?}", float),
            Token::Identifier(ident) => write!(f, "{}", ident),
            Token::Export => write!(f, "export"),
            Token::Import => write!(f, "import"),
            Token::From => write!(f, "from"),
            Token::Func => write!(f, "func"),
            Token::If => write!(f, "if"),
            Token::For => write!(f, "for"),
            Token::In => write!(f, "in"),
            Token::Loop => write!(f, "loop"),
            Token::Break => write!(f, "break"),
            Token::Continue => write!(f, "continue"),
            Token::Return => write!(f, "return"),
            Token::Result => write!(f, "result"),
            Token::String => write!(f, "string"),
            Token::U8 => write!(f, "u8"),
            Token::U16 => write!(f, "u16"),
            Token::U32 => write!(f, "u32"),
            Token::U64 => write!(f, "u64"),
            Token::S8 => write!(f, "S8"),
            Token::S16 => write!(f, "S16"),
            Token::S32 => write!(f, "S32"),
            Token::S64 => write!(f, "s64"),
            Token::F32 => write!(f, "f32"),
            Token::F64 => write!(f, "f64"),
            Token::As => write!(f, "as"),
            Token::At => write!(f, "at"),
            Token::Let => write!(f, "let"),
            Token::Mut => write!(f, "mut"),
            Token::Bool => write!(f, "bool"),
            Token::True => write!(f, "true"),
            Token::False => write!(f, "false"),
            Token::LParen => write!(f, "("),
            Token::RParen => write!(f, ")"),
            Token::LBrace => write!(f, "{{"),
            Token::RBrace => write!(f, "}}"),
            Token::LBracket => write!(f, "["),
            Token::RBracket => write!(f, "]"),
            Token::Comma => write!(f, ","),
            Token::Dot => write!(f, "."),
            Token::Range => write!(f, ".."),
            Token::Colon => write!(f, ":"),
            Token::Semicolon => write!(f, ";"),
            Token::Assign => write!(f, "="),
            Token::Arrow => write!(f, "->"),
            Token::Add => write!(f, "+"),
            Token::Sub => write!(f, "-"),
            Token::Mult => write!(f, "*"),
            Token::Div => write!(f, "/"),
            Token::Mod => write!(f, "%"),
            Token::Invert => write!(f, "!"),
            Token::LogicalAnd => write!(f, "and"),
            Token::LogicalOr => write!(f, "or"),
            Token::BitOr => write!(f, "|"),
            Token::BitAnd => write!(f, "&"),
            Token::BitXor => write!(f, "^"),
            Token::BitShiftL => write!(f, "<<"),
            Token::BitShiftR => write!(f, ">>"),
            Token::ArithShiftR => write!(f, ">>>"),
            Token::BitOrAssign => write!(f, "|="),
            Token::BitAndAssign => write!(f, "&="),
            Token::BitXorAssign => write!(f, "^="),
            Token::AddAssign => write!(f, "+="),
            Token::SubAssign => write!(f, "-="),
            Token::StarAssign => write!(f, "*="),
            Token::DivAssign => write!(f, "/="),
            Token::LT => write!(f, "<"),
            Token::LTE => write!(f, "<="),
            Token::GT => write!(f, ">"),
            Token::GTE => write!(f, ">="),
            Token::EQ => write!(f, "=="),
            Token::NEQ => write!(f, "!="),
        }
    }
}

/// Parses a string according to the JSON string format in ECMA-404.
fn parse_string_literal(lex: &mut logos::Lexer<'_, Token>) -> Option<String> {
    let mut c_iter = lex.remainder().chars();
    let mut buf = String::new();

    while let Some(c) = c_iter.next() {
        // End the parse when you encounter another quote
        if c == '"' {
            lex.bump(1);
            return Some(buf);
        }

        // If slash, then parse an escaped character
        if c == '\\' {
            lex.bump(1);
            if let Some((c_esc, c_len)) = parse_escaped_char(&mut c_iter) {
                lex.bump(c_len);
                buf.push(c_esc);
            }
        } else {
            lex.bump(c.len_utf8());
            buf.push(c);
        }
    }

    None
}

/// Parses an escaped character according to the JSON string format in ECMA-404.
/// Takes in an iterator which starts after the beginning slash.
/// If successful, returns the produced char and the length of input consumed.
fn parse_escaped_char(lex: &mut std::str::Chars) -> Option<(char, usize)> {
    let res = match lex.next()? {
        '\"' => ('\"', 1),
        '\\' => ('\\', 1),
        '/' => ('/', 1),
        'b' => ('\u{0008}', 1),
        'f' => ('\u{000C}', 1),
        'n' => ('\n', 1),
        'r' => ('\r', 1),
        't' => ('\t', 1),
        'u' => {
            // Combine next for characters together, fail if they can't be found
            let next_4: [Option<char>; 4] = [lex.next(), lex.next(), lex.next(), lex.next()];
            let next_4: Option<Vec<char>> = next_4.iter().copied().collect();
            let next_4: String = next_4?.into_iter().collect();

            let code_point = u32::from_str_radix(&next_4, 16).ok()?;
            let new_c: char = std::char::from_u32(code_point)?;

            (new_c, 5)
        }
        _ => return None,
    };

    Some(res)
}

/// Parses a raw string literal
fn parse_raw_string_literal(lex: &mut logos::Lexer<'_, Token>) -> Option<String> {
    let mut c_iter = lex.remainder().chars();
    let mut buf = String::new();

    let mut starting_hashes = 0;
    let mut starting_quote = false;

    while let Some(c) = c_iter.next() {
        lex.bump(c.len_utf8());
        if c == '"' {
            starting_quote = true;
            break;
        }
        if c == '#' {
            starting_hashes += 1;
        } else {
            return None;
        }
    }

    if !starting_quote {
        return None;
    }

    let mut seen_quote = false;
    let mut hash_count = 0;

    while let Some(c) = c_iter.next() {
        lex.bump(c.len_utf8());
        if seen_quote && c == '#' {
            hash_count += 1;

            if hash_count == starting_hashes {
                return Some(buf);
            }
            continue;
        }

        // Append the unused marker quote
        if seen_quote {
            buf.push('"');
        }
        // Reset the seen quote flag
        seen_quote = false;

        // Append the unused marker hashes
        for _ in 0..hash_count {
            buf.push('#');
        }
        // Reset the hash count
        hash_count = 0;

        if c == '"' {
            seen_quote = true;
        } else {
            buf.push(c);
        }
    }

    None
}

fn parse_decint_literal(s: &str) -> Option<u64> {
    s.replace('_', "").parse().ok()
}

fn parse_decfloat_literal(s: &str) -> Option<f64> {
    s.replace('_', "").parse().ok()
}

fn parse_bin_literal(s: &str) -> Option<u64> {
    u64::from_str_radix(&s[2..].replace('_', ""), 2).ok()
}

fn parse_hex_literal(s: &str) -> Option<u64> {
    u64::from_str_radix(&s[2..].replace('_', ""), 16).ok()
}

#[cfg(test)]
mod test {
    use super::*;
    use claw_common::make_source;
    use pretty_assertions::assert_eq;

    #[test]
    fn tokenize_func_declaration() {
        let contents = "func test(a: u32) -> u32";
        let src = make_source("test", contents);
        let ident_test = Token::Identifier("test".to_owned());
        let ident_a = Token::Identifier("a".to_owned());
        let output = vec![
            (Token::Func, SourceSpan::from(0..4)),
            (ident_test, SourceSpan::from(5..9)),
            (Token::LParen, SourceSpan::from(9..10)),
            (ident_a, SourceSpan::from(10..11)),
            (Token::Colon, SourceSpan::from(11..12)),
            (Token::U32, SourceSpan::from(13..16)),
            (Token::RParen, SourceSpan::from(16..17)),
            (Token::Arrow, SourceSpan::from(18..20)),
            (Token::U32, SourceSpan::from(21..24)),
        ]
        .into_iter()
        .map(to_token_data)
        .collect::<Vec<TokenData>>();

        match tokenize(src, contents) {
            Ok(tokens) => assert_eq!(output, tokens),
            Err(_) => panic!("Should not have failed"),
        }
    }

    #[test]
    fn tokenize_let() {
        let contents = r#"let a = "asdf\"";"#;
        let src = make_source("test", contents);
        let ident_a = Token::Identifier("a".to_owned());
        let string_asdf = Token::StringLiteral(String::from(r#"asdf""#));
        let output = vec![
            (Token::Let, SourceSpan::from(0..3)),
            (ident_a, SourceSpan::from(4..5)),
            (Token::Assign, SourceSpan::from(6..7)),
            (string_asdf, SourceSpan::from(8..16)),
            (Token::Semicolon, SourceSpan::from(16..17)),
        ]
        .into_iter()
        .map(to_token_data)
        .collect::<Vec<TokenData>>();

        match tokenize(src, contents) {
            Ok(tokens) => assert_eq!(output, tokens),
            Err(_) => panic!("Should not have failed"),
        }
    }

    fn to_token_data(d: (Token, SourceSpan)) -> TokenData {
        TokenData {
            token: d.0,
            span: d.1,
        }
    }
}