module lexer import encoding.utf8 import unicode17 fn (s &Scanner) lexical_error(start int, line int, column int, message string, incomplete bool) LexError { return LexError{ offset: start, line: line, column: column, message: message, incomplete: incomplete } } // Fast ASCII callers retain byte predicates; this path handles the stopping byte. @[inline] fn source_boundary(source string, pos int) bool { if pos >= source.len { return true } c := source[pos] if is_delimiter(c) { return true } if c == `#` && pos + 1 < source.len && source[pos + 1] in [`|`, `;`, `!`] { return true } if c >= 128 { return unicode_source_boundary(source, pos) } return false } // The caller has already rejected ordinary delimiters and EOF. Keep this // extension-only work away from the tiny validated ASCII token exits. @[noinline] fn extended_source_boundary(source string, pos int) bool { c := source[pos] if c == `#` && pos + 1 < source.len && source[pos + 1] in [`|`, `;`, `!`] { return true } if c >= 128 { return unicode_source_boundary(source, pos) } return false } @[noinline] fn unicode_source_boundary(source string, pos int) bool { cp, _ := utf8.decode_rune_at(source, pos) return unicode17.whitespace(cp) } fn line_ending_width(source string, pos int) int { if pos >= source.len { return 0 } c := source[pos] if c == `\n` { return 1 } if c == `\r` { if pos + 1 < source.len && source[pos + 1] == `\n` { return 2 } if pos + 2 < source.len && source[pos + 1] == 0xc2 && source[pos + 2] == 0x85 { return 3 } return 1 } if pos + 1 < source.len && c == 0xc2 && source[pos + 1] == 0x85 { return 2 } if pos + 2 < source.len && c == 0xe2 && source[pos + 1] == 0x80 && source[pos + 2] == 0xa8 { return 3 } return 0 } fn (mut s Scanner) skip_line_ending(width int) { // A raw CR character literal consumes exactly one scalar, leaving its LF // or NEL delimiter for atmosphere. That delimiter completes the same line. trailing_cr := s.pos > 0 && s.source[s.pos - 1] == `\r` && s.source[s.pos] in [ `\n`, u8(0xc2), ] s.pos += width if !trailing_cr { s.line++ } s.column = 1 } fn (mut s Scanner) skip_scalar() { width := line_ending_width(s.source, s.pos) if width > 0 { s.skip_line_ending(width) return } _, size := utf8.decode_rune_at(s.source, s.pos) s.skip_bytes(size) } fn (mut s Scanner) block_comment(start int, line int, column int) ! { s.skip_bytes(2) mut depth := 1 for !s.at_end() { if s.pos + 1 < s.source.len { if s.source[s.pos] == `#` && s.source[s.pos + 1] == `|` { if depth == max_int { return s.lexical_error(start, line, column, 'block comment nesting exceeds supported range', false) } depth++ s.skip_bytes(2) continue } if s.source[s.pos] == `|` && s.source[s.pos + 1] == `#` { depth-- s.skip_bytes(2) if depth == 0 { return } continue } } s.skip_scalar() } return s.lexical_error(start, line, column, 'unterminated block comment', true) } fn (mut s Scanner) directive(start int, line int, column int) ! { mut end := start + 2 for end < s.source.len && !source_boundary(s.source, end) { end++ } // Special directive spelling ignores ASCII case independently of the // current identifier-folding mode. It still needs its own delimiter. if end < s.source.len && s.source[end] == `#` { return s.lexical_error(start, line, column, 'reader directive needs a delimiter', false) } if matches_ascii_name(s.source, start, end, '#!fold-case') { s.no_fold = false } else if matches_ascii_name(s.source, start, end, '#!no-fold-case') { s.no_fold = true } else if !matches_ascii_name(s.source, start, end, '#!r6rs') { return s.lexical_error(start, line, column, 'unknown reader directive', false) } s.skip_bytes(end - start) } fn hex_digit(c u8) int { if c >= `0` && c <= `9` { return int(c - `0`) } l := ascii_lower(c) return if l >= `a` && l <= `f` { int(l - `a`) + 10 } else { -1 } } // Source UTF8 is already valid; escaped scalar arithmetic is checked separately. fn (mut s Scanner) hex_escape(start int, line int, column int, terminated bool) !rune { first := s.pos mut value := u32(0) for s.pos < s.source.len { digit := hex_digit(s.source[s.pos]) if digit < 0 { break } if value > (u32(0x10ffff) - u32(digit)) / 16 { return s.lexical_error(start, line, column, 'hex escape exceeds Unicode scalar range', false) } value = value * 16 + u32(digit) s.skip_bytes(1) } if s.pos == first { return s.lexical_error(start, line, column, 'hex escape needs a digit', s.at_end()) } if terminated { if s.at_end() { return s.lexical_error(start, line, column, 'hex escape needs a terminating semicolon', true) } if s.source[s.pos] != `;` { return s.lexical_error(start, line, column, 'hex escape needs a terminating semicolon', false) } s.skip_bytes(1) } if value >= 0xd800 && value <= 0xdfff { return s.lexical_error(start, line, column, 'hex escape is a surrogate', false) } return rune(value) } fn mnemonic(c u8) rune { return match c { `a` { rune(7) } `b` { rune(8) } `t` { rune(9) } `n` { rune(10) } `v` { rune(11) } `f` { rune(12) } `r` { rune(13) } `"` { rune(34) } `\\` { rune(92) } `|` { rune(124) } else { rune(-1) } } } // Cursor is immediately after the backslash. Simple quote/backslash stays in // the old hot path; this cold helper validates new escapes/line continuations. fn (mut s Scanner) extra_escape(start int, line int, column int, identifier bool) ! { if s.at_end() { return s.lexical_error(start, line, column, 'unfinished escape', true) } c := s.source[s.pos] if c in [`x`, `X`] { s.skip_bytes(1) s.hex_escape(start, line, column, true)! return } if mnemonic(c) != rune(-1) { if identifier && c in [`"`, `\\`, `v`, `f`] { return s.lexical_error(start, line, column, 'invalid escaped identifier character', false) } s.skip_bytes(1) return } if identifier { return s.lexical_error(start, line, column, 'invalid identifier escape', false) } for !s.at_end() { cp, width := utf8.decode_rune_at(s.source, s.pos) if !unicode17.intraline_space(cp) { break } s.skip_bytes(width) } width := line_ending_width(s.source, s.pos) if width == 0 { return s.lexical_error(start, line, column, 'invalid string escape', s.at_end()) } s.skip_line_ending(width) for !s.at_end() { cp, size := utf8.decode_rune_at(s.source, s.pos) if !unicode17.intraline_space(cp) { break } s.skip_bytes(size) } } fn (mut s Scanner) barred_identifier(start int, line int, column int) ! { s.flags |= token_identifier_barred s.skip_bytes(1) for !s.at_end() { c := s.source[s.pos] if c == `|` { s.skip_bytes(1) return } if c == `\\` { s.flags |= token_identifier_escaped position, l, col := s.pos, s.line, s.column s.skip_bytes(1) s.extra_escape(position, l, col, true)! } else { s.skip_scalar() } } return s.lexical_error(start, line, column, 'unterminated vertical identifier', true) } fn (s &Scanner) initial_scalar(cp rune) bool { return if cp < 128 { is_initial(u8(cp)) } else { unicode17.identifier_initial(cp) } } fn (s &Scanner) subsequent_scalar(cp rune) bool { return if cp < 128 { is_subsequent(u8(cp)) } else { unicode17.identifier_subsequent(cp) } } // Called only after the original ASCII fast scan stops at an extension byte or // a peculiar identifier. It never allocates decoded spelling during scanning. fn (mut s Scanner) extended_identifier(start int, line int, column int) !TokenSpan { s.pos = start s.line = line s.column = column mut index := 0 mut peculiar := false mut prefix := [3]rune{} for !s.at_end() && !source_boundary(s.source, s.pos) { if s.source[s.pos] == `#` { break } mut cp := rune(0) escaped := s.source[s.pos] == `\\` if escaped { s.flags |= token_identifier_escaped x, l, col := s.pos, s.line, s.column s.skip_bytes(1) if s.at_end() || s.source[s.pos] !in [`x`, `X`] { return s.lexical_error(x, l, col, 'identifier escapes must be hex scalars', s.at_end()) } s.skip_bytes(1) cp = s.hex_escape(x, l, col, true)! } else { c, w := utf8.decode_rune_at(s.source, s.pos) cp = c s.skip_bytes(w) if c >= 128 { s.flags |= token_identifier_unicode } } if index < 3 { prefix[index] = cp } if index == 0 { peculiar = !escaped && cp in [`+`, `-`, `.`] if !escaped && !peculiar && !s.initial_scalar(cp) { return s.lexical_error(start, line, column, 'invalid identifier initial', false) } } else if !escaped && !s.subsequent_scalar(cp) { return s.lexical_error(start, line, column, 'invalid identifier character', false) } index++ } if index == 0 || (s.pos < s.source.len && !source_boundary(s.source, s.pos)) { return s.lexical_error(start, line, column, 'invalid identifier token', false) } if peculiar && index > 1 { second := prefix[1] sign_subsequent := s.initial_scalar(second) || second in [`+`, `-`, `@`] if prefix[0] == `.` { if !sign_subsequent && second != `.` { return s.lexical_error(start, line, column, 'invalid peculiar identifier', false) } } else if second == `.` { if index < 3 || !(s.initial_scalar(prefix[2]) || prefix[2] in [`+`, `-`, `@`, `.`]) { return s.lexical_error(start, line, column, 'invalid peculiar identifier', false) } } else if !sign_subsequent { return s.lexical_error(start, line, column, 'invalid peculiar identifier', false) } } return s.token(.identifier, start, line, column) } struct CharacterName { name string scalar rune } const character_names = [ CharacterName{'null', rune(0)}, CharacterName{'nul', rune(0)}, CharacterName{'alarm', rune(7)}, CharacterName{'backspace', rune(8)}, CharacterName{'tab', rune(9)}, CharacterName{'linefeed', rune(10)}, CharacterName{'newline', rune(10)}, CharacterName{'vtab', rune(11)}, CharacterName{'page', rune(12)}, CharacterName{'return', rune(13)}, CharacterName{'escape', rune(27)}, CharacterName{'esc', rune(27)}, CharacterName{'space', rune(32)}, CharacterName{'delete', rune(127)}, ]! fn character_name_value(source string, start int, end int, folding bool) rune { for index in 0 .. character_names.len { entry := &character_names[index] if unicode17.matches_ascii(source, start, end, entry.name, folding) { return entry.scalar } } return rune(-1) } // Decodes a validated token without allocating or revalidating its spelling. pub fn character_value(source string, start int, end int, folding bool) rune { first := start + 2 cp, width := utf8.decode_rune_at(source, first) if end - first == width { return cp } if cp in [`x`, `X`] { mut value := u32(0) for i in first + 1 .. end { value = value * 16 + u32(hex_digit(source[i])) } return rune(value) } return character_name_value(source, first, end, folding) }