| 1 | module lexer |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | fn test_syntax_abbreviation_tokens_keep_exact_spellings_positions_and_following_data() { |
| 6 | source := "#'a #`b #,c #,@d" |
| 7 | tokens := tokenize(source) or { panic(err) } |
| 8 | kinds := [TokenKind.syntax_quote, .identifier, .syntax_quasiquote, .identifier, .syntax_unquote, |
| 9 | .identifier, .syntax_unquote_splicing, .identifier, .eof] |
| 10 | assert tokens.len == kinds.len |
| 11 | for i, kind in kinds { |
| 12 | assert tokens[i].kind == kind |
| 13 | } |
| 14 | assert tokens[0].lexeme == "#'" && tokens[0].start == 0 && tokens[0].end == 2 |
| 15 | assert tokens[2].lexeme == '#`' && tokens[2].column == 5 |
| 16 | assert tokens[4].lexeme == '#,' && tokens[4].column == 9 |
| 17 | assert tokens[6].lexeme == '#,@' && tokens[6].column == 13 |
| 18 | mut alt := new_alt_scanner("#'x #`(a,#,b,#,@c)") or { panic(err) } |
| 19 | assert (alt.next() or { panic(err) }).span.kind == .syntax_quote |
| 20 | assert (alt.next() or { panic(err) }).span.kind == .identifier |
| 21 | assert (alt.next() or { panic(err) }).span.kind == .syntax_quasiquote |
| 22 | } |
| 23 | |
| 24 | fn test_syntax_abbreviation_scanning_remains_allocation_free_and_span_sized() { |
| 25 | source := "#'x #`(a #,b #,@c) ".repeat(10000) |
| 26 | mut storage := arena.new(chunk_size: 4096) |
| 27 | storage.push() |
| 28 | before := storage.used() |
| 29 | mut scanner := new_scanner(source) or { panic(err) } |
| 30 | mut count := 0 |
| 31 | for { |
| 32 | token := scanner.next() or { panic(err) } |
| 33 | if token.kind == .eof { break } |
| 34 | count++ |
| 35 | } |
| 36 | assert count > 10000 |
| 37 | assert storage.used() == before |
| 38 | assert sizeof(TokenSpan) == 40 |
| 39 | storage.pop() |
| 40 | storage.free() |
| 41 | } |
| 42 | |
| 43 | fn test_syntax_abbreviation_markers_stay_data_inside_strings_identifiers_and_comments() { |
| 44 | tokens := tokenize('"#\' #` #, #,@" |#\'| #| #` #, #,@ |# #\'x') or { panic(err) } |
| 45 | assert tokens.len == 5 |
| 46 | assert tokens[0].kind == .string_literal && tokens[1].kind == .identifier |
| 47 | assert tokens[2].kind == .syntax_quote && tokens[3].kind == .identifier |
| 48 | assert tokens[4].kind == .eof |
| 49 | } |
| 50 | |