| 1 | module lexer |
| 2 | |
| 3 | // TokenKind describes lexical syntax, not evaluation. For example, + and if are |
| 4 | // identifiers; the reader and evaluator decide what their bindings mean. |
| 5 | pub enum TokenKind { |
| 6 | lparen |
| 7 | rparen |
| 8 | vector_start |
| 9 | quote |
| 10 | quasiquote |
| 11 | unquote |
| 12 | unquote_splicing |
| 13 | dot |
| 14 | identifier |
| 15 | boolean |
| 16 | number |
| 17 | character |
| 18 | string_literal |
| 19 | eof |
| 20 | bytevector_start |
| 21 | datum_comment |
| 22 | label_definition |
| 23 | label_reference |
| 24 | syntax_quote |
| 25 | syntax_quasiquote |
| 26 | syntax_unquote |
| 27 | syntax_unquote_splicing |
| 28 | keyword_prefix |
| 29 | } |
| 30 | |
| 31 | // Token preserves its exact source spelling, including quotes and escapes. |
| 32 | // start..end is a half-open UTF-8 byte range. line and column are 1-based; |
| 33 | // columns count bytes, and a tab counts as one column. EOF has an empty range. |
| 34 | pub struct Token { |
| 35 | pub: |
| 36 | kind TokenKind |
| 37 | lexeme string |
| 38 | start int |
| 39 | end int |
| 40 | line int |
| 41 | column int |
| 42 | } |
| 43 | |
| 44 | // TokenSpan is a token returned by Scanner.next, with no owned text or token |
| 45 | // array. Keep the source while using its byte offsets. EOF has an empty range. |
| 46 | pub struct TokenSpan { |
| 47 | pub: |
| 48 | kind TokenKind |
| 49 | start int |
| 50 | end int |
| 51 | line int |
| 52 | column int |
| 53 | } |
| 54 | |
| 55 | // text returns the spelling from the source that was scanned. Fixed punctuation, |
| 56 | // booleans and single ASCII bytes share immutable literals; other spellings are |
| 57 | // copied only when requested. No result borrows the scanned source. |
| 58 | pub fn (t TokenSpan) text(source string) string { |
| 59 | return match t.kind { |
| 60 | .lparen { '(' } |
| 61 | .rparen { ')' } |
| 62 | .vector_start { '#(' } |
| 63 | .keyword_prefix { '#:' } |
| 64 | .quote { "'" } |
| 65 | .quasiquote { '`' } |
| 66 | .unquote { ',' } |
| 67 | .unquote_splicing { ',@' } |
| 68 | .syntax_quote { "#'" } |
| 69 | .syntax_quasiquote { '#`' } |
| 70 | .syntax_unquote { '#,' } |
| 71 | .syntax_unquote_splicing { '#,@' } |
| 72 | .boolean { |
| 73 | if t.end - t.start > 2 { return copy_spelling(source, t.start, t.end) } |
| 74 | match source[t.start + 1] { |
| 75 | `t` { '#t' } |
| 76 | `T` { '#T' } |
| 77 | `f` { '#f' } |
| 78 | else { '#F' } |
| 79 | } |
| 80 | } |
| 81 | .eof { '' } |
| 82 | else { |
| 83 | if t.end - t.start == 1 { |
| 84 | byte := source[t.start] |
| 85 | if byte < 128 { return single_byte_spelling(byte) } |
| 86 | } |
| 87 | copy_spelling(source, t.start, t.end) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // LexError identifies the first invalid token or escape. An unfinished string |
| 93 | // or character points to its opening delimiter. No partial token list escapes. |
| 94 | pub struct LexError { |
| 95 | Error |
| 96 | pub: |
| 97 | offset int |
| 98 | line int |
| 99 | column int |
| 100 | message string |
| 101 | incomplete bool |
| 102 | read_state_available bool |
| 103 | read_state SourceState |
| 104 | } |
| 105 | |
| 106 | // msg formats the location and explanation for V's IError interface. |
| 107 | pub fn (e &LexError) msg() string { |
| 108 | return '${e.line}:${e.column}: ${e.message}' |
| 109 | } |
| 110 | |
| 111 | // These ranges came from scanner spans. Preserve exact end offsets, including |
| 112 | // max_i32, which std substr/substr_unsafe otherwise interpret as a sentinel. |
| 113 | @[inline] |
| 114 | fn spelling_view(source string, start int, end int) string { |
| 115 | return unsafe { (&char(source.str + start)).vstring_literal_with_len(end - start) } |
| 116 | } |
| 117 | |
| 118 | @[inline] |
| 119 | fn copy_spelling(source string, start int, end int) string { |
| 120 | return spelling_view(source, start, end).clone() |
| 121 | } |
| 122 | |