"""A small Rust lexer: enough to find item boundaries and `super::` paths without a parser (STRA-338).""" from dataclasses import dataclass IDENT_START = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_") IDENT_PART = IDENT_START | set("0123456789") @dataclass class Token: kind: str # ws comment string char lifetime ident number punct start: int end: int text: str def _skip_line_comment(text, i): end = text.find("\n", i) return len(text) if end < 0 else end def _skip_block_comment(text, i): depth, n = 0, len(text) while i < n: if text.startswith("/*", i): depth, i = depth + 1, i + 2 elif text.startswith("*/", i): depth, i = depth - 1, i + 2 if depth == 0: return i else: i += 1 return n def _skip_quoted(text, i, quote): """End index after the closing quote of a string or char literal that opens at `i`.""" i += 1 while i < len(text) and text[i] != quote: i += 2 if text[i] == "\\" else 1 return i + 1 def _raw_string_end(text, i): """End of a raw string `r"..."` / `r#"..."#` whose `r` is at `i` (after a `b` prefix too); None if it is not one.""" j = i + 1 while j < len(text) and text[j] == "#": j += 1 if j >= len(text) or text[j] != '"': return None closing = '"' + "#" * (j - i - 1) end = text.find(closing, j + 1) return len(text) if end < 0 else end + len(closing) def _char_or_lifetime_end(text, i): """(kind, end) of what starts with a `'`.""" if text[i + 1:i + 2] == "\\" or text[i + 2:i + 3] == "'": return "char", _skip_quoted(text, i, "'") j = i + 1 while j < len(text) and text[j] in IDENT_PART: j += 1 return "lifetime", j def _ident_end(text, i): j = i while j < len(text) and text[j] in IDENT_PART: j += 1 return j def _literal_prefix(text, i): """(kind, end) for r"", b"", br"", b'' starting at i, or None.""" for prefix in ("br", "r", "b"): if text.startswith(prefix, i) and prefix[-1] == "r": end = _raw_string_end(text, i + len(prefix) - 1) if end is not None: return "string", end if prefix == "b" and text.startswith('b"', i): return "string", _skip_quoted(text, i + 1, '"') if prefix == "b" and text.startswith("b'", i): return "char", _skip_quoted(text, i + 1, "'") return None def _simple_token(text, i): """(kind, end) for whitespace, comments and quoted literals, or None.""" c = text[i] if c.isspace(): j = i while j < len(text) and text[j].isspace(): j += 1 return "ws", j if text.startswith("//", i): return "comment", _skip_line_comment(text, i) if text.startswith("/*", i): return "comment", _skip_block_comment(text, i) return ("string", _skip_quoted(text, i, '"')) if c == '"' else None def _next_token(text, i): simple = _simple_token(text, i) if simple: return simple c = text[i] if c == "'": return _char_or_lifetime_end(text, i) if c in IDENT_START: return _literal_prefix(text, i) or ("ident", _ident_end(text, i)) return ("number", _ident_end(text, i)) if c.isdigit() else ("punct", i + 1) def tokenize(text): """All tokens of a Rust source text; they tile the text exactly.""" tokens, i = [], 0 while i < len(text): kind, end = _next_token(text, i) tokens.append(Token(kind, i, end, text[i:end])) i = end return tokens