Skip to main content

lua_lex/
lib.rs

1//! Lexical analyzer — port of `llex.c` + `llex.h`.
2//!
3//! Provides the Lua 5.4 lexer: character-by-character scanning of a [`ZIO`]
4//! input stream into [`Token`] values, with one-token lookahead.  The
5//! `llex.h` header is merged here per PORTING.md §1.
6//!
7//! # C source files
8//! - `reference/lua-5.4.7/src/llex.c`  (581 lines, 24 functions)
9//! - `reference/lua-5.4.7/src/llex.h`  (91 lines; merged here)
10//!
11//! # Design notes
12//! - `LexState.L` (back-pointer to `lua_State`) is removed.  All functions
13//!   that need `LuaState` receive it as `state: &mut LuaState`.
14//! - `Token.token` is `i32` in Phase A (matching the C `int token` field).
15//!   Single-byte tokens are their ASCII values; reserved-word tokens start at
16//!   `FIRST_RESERVED` (257).  A proper `TokenKind` enum is deferred to Phase B.
17//! - `save` / `save_and_next` are now fallible (`Result<(), LuaError>`); the
18//!   `?` operator replaces the C noreturn `lexerror` call on buffer overflow.
19//! - The `goto read_save / only_save / no_save` pattern in `read_string` is
20//!   translated via the local `EscapeResult` enum.
21
22// TODO(port): resolve remaining cross-crate calls (intern_str, table anchor,
23// number parsing, utf8 encoding) in Phase B.  Canonical cross-crate type
24// imports are now in place per harness/type-vocabulary.tsv (see below).
25
26use std::io::Write as IoWrite;
27
28// PORT NOTE: GcRef<T> = Rc<T> in Phases A–C; replaced by real GC pointer in Phase D.
29use lua_types::gc::GcRef;
30
31// Canonical cross-crate types: imported from owner crates per
32// harness/type-vocabulary.tsv.  See PORTING.md §7.
33pub use lua_types::LuaError;
34pub use lua_types::LuaString;
35pub use lua_vm::state::LuaState;
36pub use lua_vm::table::LuaTable;
37
38/// Placeholder for `LexBuffer` from `lua_vm::zio`.
39/// TODO(port): replace with `use lua_vm::zio::LexBuffer` in Phase B.
40/// types.tsv: Mbuffer → LexBuffer
41pub struct LexBuffer {
42    buffer: Vec<u8>,
43}
44
45impl LexBuffer {
46    pub fn new() -> Self {
47        LexBuffer { buffer: Vec::new() }
48    }
49
50    /// macros.tsv: luaZ_bufflen → buf.len()
51    pub fn len(&self) -> usize {
52        self.buffer.len()
53    }
54
55    /// macros.tsv: luaZ_sizebuffer → buf.capacity()
56    pub fn capacity(&self) -> usize {
57        self.buffer.capacity()
58    }
59
60    /// macros.tsv: luaZ_buffer → buf.as_mut_slice()
61    pub fn as_slice(&self) -> &[u8] {
62        &self.buffer
63    }
64
65    /// macros.tsv: luaZ_resetbuffer → buf.clear()
66    pub fn clear(&mut self) {
67        self.buffer.clear();
68    }
69
70    /// macros.tsv: luaZ_buffremove → buf.truncate_by(i)
71    pub fn truncate_by(&mut self, i: usize) {
72        let new_len = self.buffer.len().saturating_sub(i);
73        self.buffer.truncate(new_len);
74    }
75
76    /// allocated capacity. In C this changes `buffsize`, not the live byte
77    /// count `n`. The Rust analogue therefore manipulates `Vec::capacity`,
78    /// never `Vec::len` (otherwise `push_byte` would write past the live
79    /// content and leave embedded zero padding inside the token text).
80    pub fn resize(&mut self, _state: &mut LuaState, size: usize) -> Result<(), LuaError> {
81        if size < self.buffer.len() {
82            self.buffer.truncate(size);
83        }
84        if size > self.buffer.capacity() {
85            let extra = size - self.buffer.capacity();
86            self.buffer.reserve_exact(extra);
87        }
88        Ok(())
89    }
90
91    /// Append one byte to the live contents.  Panics if capacity exceeded
92    /// (callers must pre-check via `save`).
93    fn push_byte(&mut self, c: u8) {
94        self.buffer.push(c);
95    }
96}
97
98impl Default for LexBuffer {
99    fn default() -> Self {
100        Self::new()
101    }
102}
103
104/// Placeholder for `ZIO` from `lua_vm::zio`.
105/// TODO(port): replace with `use lua_vm::zio::ZIO` in Phase B.
106/// types.tsv: Zio → ZIO
107pub struct ZIO {
108    // TODO(port): full ZIO implementation lives in lua_vm::zio; this is a stub.
109    reader: Box<dyn FnMut() -> Option<Vec<u8>>>,
110    n: usize,
111    p: usize,
112    current_chunk: Vec<u8>,
113}
114
115impl ZIO {
116    /// Construct a ZIO from a reader callback that yields successive chunks.
117    pub fn new(reader: Box<dyn FnMut() -> Option<Vec<u8>>>) -> Self {
118        ZIO { reader, n: 0, p: 0, current_chunk: Vec::new() }
119    }
120
121    /// Construct a ZIO that yields the supplied bytes once and then EOZ.
122    pub fn from_bytes(bytes: Vec<u8>) -> Self {
123        let mut once = Some(bytes);
124        ZIO::new(Box::new(move || once.take()))
125    }
126
127    /// macros.tsv: zgetc → z.getc()
128    pub fn getc(&mut self) -> i32 {
129        if self.n > 0 {
130            self.n -= 1;
131            let b = self.current_chunk[self.p] as u8;
132            self.p += 1;
133            b as i32
134        } else {
135            self.fill()
136        }
137    }
138
139    fn fill(&mut self) -> i32 {
140        match (self.reader)() {
141            None => EOZ,
142            Some(chunk) if chunk.is_empty() => EOZ,
143            Some(chunk) => {
144                self.n = chunk.len() - 1;
145                self.current_chunk = chunk;
146                self.p = 0;
147                let b = self.current_chunk[self.p] as u8;
148                self.p += 1;
149                b as i32
150            }
151        }
152    }
153}
154
155// ── Constants ─────────────────────────────────────────────────────────────────
156
157// macros.tsv: FIRST_RESERVED → const FIRST_RESERVED: i32 = 257
158/// First token kind value that is not a single-byte character.
159/// Single-byte tokens are represented by their ASCII value (0-255).
160pub const FIRST_RESERVED: i32 = 257;
161
162// macros.tsv: LUA_ENV → const LUA_ENV: &[u8] = b"_ENV"
163/// Name of the global environment upvalue.
164pub const LUA_ENV: &[u8] = b"_ENV";
165
166// macros.tsv: NUM_RESERVED → const NUM_RESERVED: usize = (TK_WHILE - FIRST_RESERVED + 1) as usize
167/// Number of reserved words (keywords).
168pub const NUM_RESERVED: usize = (TK_WHILE - FIRST_RESERVED + 1) as usize;
169
170// macros.tsv: EOZ → const EOZ: i32 = -1
171/// End-of-stream sentinel returned by ZIO::getc.
172pub const EOZ: i32 = -1;
173
174// macros.tsv: MAX_SIZE → const MAX_SIZE: usize = ...
175const MAX_SIZE: usize = if std::mem::size_of::<usize>() < std::mem::size_of::<i64>() {
176    usize::MAX
177} else {
178    i64::MAX as usize
179};
180
181// macros.tsv: LUA_MIN_BUFFER → const LUA_MIN_BUFFER: usize = 32
182const LUA_MIN_BUFFER: usize = 32;
183
184// ── Token kind constants (ORDER RESERVED — matches C enum RESERVED) ───────────
185//
186// In C these are enum values.  In Rust we use i32 constants for Phase A
187// (faithful to `Token.token: int` in C) with a TODO for a proper enum in Phase B.
188//
189
190/// `and`
191pub const TK_AND: i32 = 257;
192/// `break`
193pub const TK_BREAK: i32 = 258;
194/// `do`
195pub const TK_DO: i32 = 259;
196/// `else`
197pub const TK_ELSE: i32 = 260;
198/// `elseif`
199pub const TK_ELSEIF: i32 = 261;
200/// `end`
201pub const TK_END: i32 = 262;
202/// `false`
203pub const TK_FALSE: i32 = 263;
204/// `for`
205pub const TK_FOR: i32 = 264;
206/// `function`
207pub const TK_FUNCTION: i32 = 265;
208/// `goto`
209pub const TK_GOTO: i32 = 266;
210/// `if`
211pub const TK_IF: i32 = 267;
212/// `in`
213pub const TK_IN: i32 = 268;
214/// `local`
215pub const TK_LOCAL: i32 = 269;
216/// `nil`
217pub const TK_NIL: i32 = 270;
218/// `not`
219pub const TK_NOT: i32 = 271;
220/// `or`
221pub const TK_OR: i32 = 272;
222/// `repeat`
223pub const TK_REPEAT: i32 = 273;
224/// `return`
225pub const TK_RETURN: i32 = 274;
226/// `then`
227pub const TK_THEN: i32 = 275;
228/// `true`
229pub const TK_TRUE: i32 = 276;
230/// `until`
231pub const TK_UNTIL: i32 = 277;
232/// `while`  (last keyword; NUM_RESERVED = TK_WHILE - FIRST_RESERVED + 1 = 22)
233pub const TK_WHILE: i32 = 278;
234/// `//`  (floor division)
235pub const TK_IDIV: i32 = 279;
236/// `..`  (concatenation)
237pub const TK_CONCAT: i32 = 280;
238/// `...` (vararg)
239pub const TK_DOTS: i32 = 281;
240/// `==`
241pub const TK_EQ: i32 = 282;
242/// `>=`
243pub const TK_GE: i32 = 283;
244/// `<=`
245pub const TK_LE: i32 = 284;
246/// `~=`
247pub const TK_NE: i32 = 285;
248/// `<<`
249pub const TK_SHL: i32 = 286;
250/// `>>`
251pub const TK_SHR: i32 = 287;
252/// `::`
253pub const TK_DBCOLON: i32 = 288;
254/// `<eof>`
255pub const TK_EOS: i32 = 289;
256/// `<number>`  (float literal)
257pub const TK_FLT: i32 = 290;
258/// `<integer>` (integer literal)
259pub const TK_INT: i32 = 291;
260/// `<name>`    (identifier)
261pub const TK_NAME: i32 = 292;
262/// `<string>`  (string literal)
263pub const TK_STRING: i32 = 293;
264
265// ORDER RESERVED — index 0 = TK_AND - FIRST_RESERVED, etc.
266/// Display strings for tokens, indexed by `token - FIRST_RESERVED`.
267pub static LUAX_TOKENS: &[&[u8]] = &[
268    // keywords (indices 0-21)
269    b"and", b"break", b"do", b"else", b"elseif",
270    b"end", b"false", b"for", b"function", b"goto", b"if",
271    b"in", b"local", b"nil", b"not", b"or", b"repeat",
272    b"return", b"then", b"true", b"until", b"while",
273    // other terminal symbols (indices 22-35)
274    b"//", b"..", b"...", b"==", b">=", b"<=", b"~=",
275    b"<<", b">>", b"::", b"<eof>",
276    b"<number>", b"<integer>", b"<name>", b"<string>",
277];
278
279// ── SemInfo / TokenValue ───────────────────────────────────────────────────────
280
281// types.tsv: SemInfo → TokenValue
282/// Semantic payload carried by a token.
283///
284/// Corresponds to `SemInfo` (a C union) in `llex.h`.  In Rust this is a
285/// discriminated union (enum).
286///
287/// # C mapping
288/// ```text
289/// SemInfo.r   → TokenValue::Float(f64)      (lua_Number)
290/// SemInfo.i   → TokenValue::Int(i64)        (lua_Integer)
291/// SemInfo.ts  → TokenValue::Str(GcRef<LuaString>)
292/// (no C field) → TokenValue::None           (default / unset)
293/// ```
294#[derive(Clone)]
295pub enum TokenValue {
296    /// No semantic value (default; used for single-byte and most multi-char tokens).
297    None,
298    /// Float literal payload.  C: `seminfo.r` (`lua_Number`).
299    Float(f64),
300    /// Integer literal payload.  C: `seminfo.i` (`lua_Integer`).
301    Int(i64),
302    /// String/name payload.  C: `seminfo.ts` (`TString *`).
303    Str(GcRef<LuaString>),
304}
305
306// ── Token ─────────────────────────────────────────────────────────────────────
307
308// types.tsv: Token → Token;  Token.token → i32 (Phase A; TODO: TokenKind enum Phase B)
309/// A single lexed token with its semantic payload.
310///
311/// `kind` is an `i32` whose value is either an ASCII byte code (for single-byte
312/// tokens like `+`, `-`, `[`) or one of the `TK_*` constants (for reserved
313/// words, multi-char symbols, and literals).
314///
315/// TODO(port): Phase B — replace `kind: i32` with a proper `TokenKind` enum
316/// covering both single-byte and named tokens (e.g. `TokenKind::Char(u8)` +
317/// named variants).
318#[derive(Clone)]
319pub struct Token {
320    pub kind: i32,
321    pub value: TokenValue,
322}
323
324impl Token {
325    /// Construct a token with no semantic value.
326    pub fn new(kind: i32) -> Self {
327        Token { kind, value: TokenValue::None }
328    }
329
330    /// The end-of-stream sentinel token.
331    pub fn eos() -> Self {
332        Token::new(TK_EOS)
333    }
334}
335
336// ── LexState ──────────────────────────────────────────────────────────────────
337
338// types.tsv: LexState → LexState;  LexState.L removed (thread via &mut LuaState)
339/// Per-chunk lexer (and shared parser) state.
340///
341/// Corresponds to `LexState` in `llex.h`.  Owns the input stream, token
342/// buffer, and current/lookahead tokens.
343///
344/// # C mapping (types.tsv)
345/// ```text
346/// LexState.current    → current: i32        (charint; -1 = EOZ)
347/// LexState.linenumber → linenumber: i32
348/// LexState.lastline   → lastline: i32
349/// LexState.t          → t: Token            (current token)
350/// LexState.lookahead  → lookahead: Token    (one-token lookahead)
351/// LexState.fs         → fs: Option<Box<FuncState>>   (parser state)
352/// LexState.L          → (removed; callers pass &mut LuaState)
353/// LexState.z          → z: ZIO              (owned input stream)
354/// LexState.buff       → buff: LexBuffer     (owned token-text buffer)
355/// LexState.h          → h: GcRef<LuaTable>  (string-anchor table)
356/// LexState.dyd        → dyd: DynData        (parser dynamic data)
357/// LexState.source     → source: GcRef<LuaString>
358/// LexState.envn       → envn: GcRef<LuaString>
359/// ```
360pub struct LexState {
361    pub current: i32,
362    pub linenumber: i32,
363    pub lastline: i32,
364    pub t: Token,
365    pub lookahead: Token,
366    // TODO(port): Box<FuncState> once FuncState lands in lua-parse (Phase B)
367    pub fs: Option<()>,
368    // PORT NOTE: C held a pointer; Rust owns the ZIO directly per types.tsv.
369    pub z: ZIO,
370    // PORT NOTE: C held a pointer; Rust owns the LexBuffer directly per types.tsv.
371    pub buff: LexBuffer,
372    // TODO(port): GcRef<LuaTable> once LuaTable is defined in Phase B
373    pub h: Option<GcRef<LuaTable>>,
374    /// Per-parse-session anchor for long strings. C-Lua's `ls->h` is a Lua
375    /// table that deduplicates all literal strings within a chunk (both short
376    /// and long), so e.g. `local s1 <const>="..."` and `local s2 <const>="..."`
377    /// with identical 50-byte payloads share one `TString` object — which is
378    /// what makes `string.format("%p", s1) == string.format("%p", s2)` hold.
379    /// Short strings already share identity via the global `interned_lt` pool,
380    /// but long strings (>LUAI_MAXSHORTLEN = 40) are not globally interned and
381    /// need this session-level map. Keyed by the string bytes; populated lazily
382    /// by `new_string`.
383    pub long_str_anchor: std::collections::HashMap<Vec<u8>, GcRef<LuaString>>,
384    // TODO(port): DynData once parser types land in Phase B
385    pub dyd: Option<()>,
386    pub source: GcRef<LuaString>,
387    pub envn: GcRef<LuaString>,
388}
389
390// ── Character-classification helpers ─────────────────────────────────────────
391//
392// These are simplified ASCII implementations for Phase A.
393// TODO(port): import from lua_vm::ctype in Phase B; the full table handles
394// the LUA_UCID (Unicode identifiers) flag and matches the C bit-table exactly.
395//
396// PORT NOTE: the C macros take `int` (not `char`) so they handle EOZ (-1) safely.
397// These Rust fns match that contract: EOZ returns false for all predicates.
398
399#[inline]
400fn is_digit(c: i32) -> bool {
401    c >= b'0' as i32 && c <= b'9' as i32
402}
403
404#[inline]
405fn is_xdigit(c: i32) -> bool {
406    (c >= b'0' as i32 && c <= b'9' as i32)
407        || (c >= b'a' as i32 && c <= b'f' as i32)
408        || (c >= b'A' as i32 && c <= b'F' as i32)
409}
410
411// ALPHABIT: ASCII letters + '_'
412#[inline]
413fn is_lalpha(c: i32) -> bool {
414    (c >= b'a' as i32 && c <= b'z' as i32)
415        || (c >= b'A' as i32 && c <= b'Z' as i32)
416        || c == b'_' as i32
417}
418
419#[inline]
420fn is_lalnum(c: i32) -> bool {
421    is_lalpha(c) || is_digit(c)
422}
423
424#[inline]
425fn is_space(c: i32) -> bool {
426    matches!(c, 9 | 10 | 11 | 12 | 13 | 32) // \t \n \v \f \r space
427}
428
429// PRINTBIT: printable ASCII (graph + space), i.e. 0x20-0x7E
430#[inline]
431fn is_print(c: i32) -> bool {
432    c >= 0x20 && c <= 0x7E
433}
434
435#[inline]
436fn curr_is_newline(ls: &LexState) -> bool {
437    ls.current == b'\n' as i32 || ls.current == b'\r' as i32
438}
439
440// ── Low-level stream helpers ───────────────────────────────────────────────────
441
442/// Advance the lexer by one character.
443///
444/// Corresponds to the `next(ls)` macro.  Named `advance` to avoid collision
445/// with Rust's iterator method.
446#[inline]
447fn advance(ls: &mut LexState) {
448    // macros.tsv: zgetc → z.getc()
449    ls.current = ls.z.getc();
450}
451
452/// Append character `c` to the token buffer, growing it if necessary.
453///
454/// On overflow calls [`lex_error`] which becomes `Err(LuaError::Syntax(...))`.
455///
456/// # C source
457/// ```c
458///
459/// //   Mbuffer *b = ls->buff;
460/// //   if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) {
461/// //     size_t newsize;
462/// //     if (luaZ_sizebuffer(b) >= MAX_SIZE/2)
463/// //       lexerror(ls, "lexical element too long", 0);
464/// //     newsize = luaZ_sizebuffer(b) * 2;
465/// //     luaZ_resizebuffer(ls->L, b, newsize);
466/// //   }
467/// //   b->buffer[luaZ_bufflen(b)++] = cast_char(c);
468/// // }
469/// ```
470fn save(ls: &mut LexState, state: &mut LuaState, c: i32) -> Result<(), LuaError> {
471    // macros.tsv: luaZ_bufflen → buf.len(); luaZ_sizebuffer → buf.capacity()
472    if ls.buff.len() + 1 > ls.buff.capacity() {
473        if ls.buff.capacity() >= MAX_SIZE / 2 {
474            return Err(lex_error(ls, b"lexical element too long", 0));
475        }
476        //    luaZ_resizebuffer(ls->L, b, newsize);
477        // macros.tsv: luaZ_resizebuffer → buf.resize(state, size)?
478        let newsize = ls.buff.capacity() * 2;
479        ls.buff.resize(state, newsize)?;
480    }
481    // macros.tsv: cast_char → x as i8  (C char is signed; Lua bytes stored as-is)
482    // PORT NOTE: we store the byte value directly; the i8 cast in C is for the
483    // C char type but the data is read back as unsigned via cast_uchar everywhere.
484    ls.buff.push_byte(c as u8);
485    Ok(())
486}
487
488/// Save the current character into the token buffer, then advance the stream.
489///
490/// Corresponds to the `save_and_next(ls)` macro.  Fallible because `save`
491/// may need to grow the buffer.
492#[inline]
493fn save_and_next(ls: &mut LexState, state: &mut LuaState) -> Result<(), LuaError> {
494    let c = ls.current;
495    save(ls, state, c)?;
496    advance(ls);
497    Ok(())
498}
499
500// ── Error helpers ─────────────────────────────────────────────────────────────
501
502// l_noret → -> !  but in Rust we return LuaError (callers wrap in Err(...))
503// error_sites.tsv: luaX_lexerror → return Err(LuaError::syntax_at(ls, "msg", token))
504/// Build a syntax error, optionally annotated with the offending token text.
505///
506/// Corresponds to the static `lexerror` function in `llex.c`.  In C this is
507/// `l_noret` (diverges via `luaD_throw`); in Rust it returns a `LuaError`
508/// value that callers wrap in `Err(...)`.
509///
510/// # C source
511/// ```c
512///
513/// //   msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber);
514/// //   if (token)
515/// //     luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
516/// //   luaD_throw(ls->L, LUA_ERRSYNTAX);
517/// // }
518/// ```
519pub fn lex_error(ls: &mut LexState, msg: &[u8], token: i32) -> LuaError {
520    const LUA_IDSIZE: usize = 60;
521    let mut buff = [0u8; LUA_IDSIZE];
522    let n = lua_vm::object::chunk_id(&mut buff[..], ls.source.as_bytes());
523    let src_part = &buff[..n];
524
525    let mut full_msg: Vec<u8> = Vec::new();
526    full_msg.extend_from_slice(src_part);
527    let _ = write!(full_msg, ":{}: ", ls.linenumber);
528    full_msg.extend_from_slice(msg);
529
530    if token != 0 {
531        let tok_text = txt_token(ls, token);
532        full_msg.extend_from_slice(b" near ");
533        full_msg.extend_from_slice(&tok_text);
534    }
535
536    LuaError::syntax_raw(&full_msg)
537}
538
539// LUAI_FUNC → pub(crate)
540// error_sites.tsv: luaX_syntaxerror → return Err(LuaError::syntax(format_args!("msg")))
541/// Report a syntax error at the current token.
542///
543/// # C source
544/// ```c
545///
546/// //   lexerror(ls, msg, ls->t.token);
547/// // }
548/// ```
549pub fn syntax_error(ls: &mut LexState, msg: &[u8]) -> LuaError {
550    let token = ls.t.kind;
551    lex_error(ls, msg, token)
552}
553
554/// Produce a human-readable representation of `token` for error messages.
555///
556/// For `TK_NAME`, `TK_STRING`, `TK_FLT`, `TK_INT`: formats the current
557/// token buffer contents as `'<text>'`.  For everything else, delegates to
558/// [`token2str`].
559///
560/// # C source
561/// ```c
562///
563/// //   switch (token) {
564/// //     case TK_NAME: case TK_STRING:
565/// //     case TK_FLT: case TK_INT:
566/// //       save(ls, '\0');
567/// //       return luaO_pushfstring(ls->L, "'%s'", luaZ_buffer(ls->buff));
568/// //     default:
569/// //       return luaX_token2str(ls, token);
570/// //   }
571/// // }
572/// ```
573///
574/// PORT NOTE: C calls `luaO_pushfstring` which pushes the string onto the
575/// Lua stack (stack-anchored temporary).  Rust returns `Vec<u8>` directly
576/// since there is no stack-based string lifecycle for error formatting.
577fn txt_token(ls: &mut LexState, token: i32) -> Vec<u8> {
578    match token {
579        t if t == TK_NAME || t == TK_STRING || t == TK_FLT || t == TK_INT => {
580            let mut v: Vec<u8> = Vec::new();
581            v.push(b'\'');
582            let buff = ls.buff.as_slice();
583            let trimmed = if buff.last() == Some(&0) { &buff[..buff.len() - 1] } else { buff };
584            v.extend_from_slice(trimmed);
585            v.push(b'\'');
586            v
587        }
588        _ => token2str_raw(token),
589    }
590}
591
592// LUAI_FUNC → pub(crate)
593/// Produce a human-readable token description (for error messages and the parser).
594///
595/// Single-byte printable tokens are formatted as `'X'`; non-printable as
596/// `'<\N>'`.  Reserved words and multi-char symbols are formatted as `'kw'`.
597/// Literal tokens (`<name>`, `<string>`, etc.) return the bare label.
598///
599/// # C source
600/// ```c
601///
602/// //   if (token < FIRST_RESERVED) {
603/// //     if (lisprint(token))
604/// //       return luaO_pushfstring(ls->L, "'%c'", token);
605/// //     else
606/// //       return luaO_pushfstring(ls->L, "'<\\%d>'", token);
607/// //   }
608/// //   else {
609/// //     const char *s = luaX_tokens[token - FIRST_RESERVED];
610/// //     if (token < TK_EOS)
611/// //       return luaO_pushfstring(ls->L, "'%s'", s);
612/// //     else
613/// //       return s;
614/// //   }
615/// // }
616/// ```
617///
618/// PORT NOTE: The `LexState` parameter is retained in the signature for API
619/// parity with the C export, but is unused in Rust because we don't push onto
620/// the Lua stack.  The real formatting is in [`token2str_raw`].
621pub fn token2str(_ls: &LexState, token: i32) -> Vec<u8> {
622    token2str_raw(token)
623}
624
625/// Inner implementation of [`token2str`] that does not need `LexState`.
626fn token2str_raw(token: i32) -> Vec<u8> {
627    if token < FIRST_RESERVED {
628        if is_print(token) {
629            vec![b'\'', token as u8, b'\'']
630        } else {
631            // PORT NOTE: uses write! to Vec<u8> to avoid String allocation for Lua data.
632            let mut v: Vec<u8> = Vec::new();
633            v.extend_from_slice(b"'<\\");
634            let _ = write!(&mut v, "{}", token);
635            v.extend_from_slice(b">'");
636            v
637        }
638    } else {
639        let idx = (token - FIRST_RESERVED) as usize;
640        let s = LUAX_TOKENS[idx];
641        if token < TK_EOS {
642            let mut v: Vec<u8> = Vec::with_capacity(s.len() + 2);
643            v.push(b'\'');
644            v.extend_from_slice(s);
645            v.push(b'\'');
646            v
647        } else {
648            s.to_vec()
649        }
650    }
651}
652
653// ── Public init / setup ───────────────────────────────────────────────────────
654
655// LUAI_FUNC → pub(crate)
656/// Initialise the lexer subsystem: intern all reserved words and fix them
657/// in the GC so they are never collected.
658///
659/// Must be called exactly once during VM startup via `luaX_init`.
660///
661/// # C source
662/// ```c
663///
664/// //   int i;
665/// //   TString *e = luaS_newliteral(L, LUA_ENV);  /* create env name */
666/// //   luaC_fix(L, obj2gco(e));  /* never collect this name */
667/// //   for (i=0; i<NUM_RESERVED; i++) {
668/// //     TString *ts = luaS_new(L, luaX_tokens[i]);
669/// //     luaC_fix(L, obj2gco(ts));  /* reserved words are never collected */
670/// //     ts->extra = cast_byte(i+1);  /* reserved word */
671/// //   }
672/// // }
673/// ```
674pub fn init(state: &mut LuaState) -> Result<(), LuaError> {
675    // macros.tsv: luaS_newliteral → state.intern_str(b"...")
676    // TODO(port): call state.intern_str(LUA_ENV) once LuaState has that method (Phase B)
677    let _e = intern_str_stub(state, LUA_ENV)?;
678
679    // macros.tsv: luaC_objbarrier / luaC_fix — GC fix; no-op in Phases A-C
680    // TODO(port): state.gc().fix(e) in Phase D
681
682    for i in 0..NUM_RESERVED {
683        // macros.tsv: luaS_new → state.intern_str(...)
684        // TODO(port): call state.intern_str(LUAX_TOKENS[i]) in Phase B
685        let ts = intern_str_stub(state, LUAX_TOKENS[i])?;
686
687        // TODO(port): state.gc().fix(ts.clone()) in Phase D
688
689        // macros.tsv: cast_byte → x as u8
690        // PORT NOTE: LuaString.extra uses Cell<u8> interior mutability.
691        // TODO(port): ts.set_extra((i + 1) as u8) — needs pub accessor on LuaString
692        let _ = ts; // suppress unused warning until Phase B
693    }
694
695    Ok(())
696}
697
698// LUAI_FUNC → pub(crate)
699/// Initialise `ls` for lexing a new chunk from stream `z`.
700///
701/// # C source
702/// ```c
703///
704/// //                         TString *source, int firstchar) {
705/// //   ls->t.token = 0;
706/// //   ls->L = L;
707/// //   ls->current = firstchar;
708/// //   ls->lookahead.token = TK_EOS;  /* no look-ahead token */
709/// //   ls->z = z;
710/// //   ls->fs = NULL;
711/// //   ls->linenumber = 1;
712/// //   ls->lastline = 1;
713/// //   ls->source = source;
714/// //   ls->envn = luaS_newliteral(L, LUA_ENV);  /* get env name */
715/// //   luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER);
716/// // }
717/// ```
718pub fn set_input(
719    state: &mut LuaState,
720    ls: &mut LexState,
721    z: ZIO,
722    source: GcRef<LuaString>,
723    firstchar: i32,
724) -> Result<(), LuaError> {
725    ls.t = Token::new(0);
726    ls.current = firstchar;
727    ls.lookahead = Token::eos();
728    ls.z = z;
729    ls.fs = None;
730    ls.linenumber = 1;
731    ls.lastline = 1;
732    ls.source = source;
733    // macros.tsv: luaS_newliteral → state.intern_str(b"...")
734    // TODO(port): state.intern_str(LUA_ENV) in Phase B
735    ls.envn = intern_str_stub(state, LUA_ENV)?;
736    // macros.tsv: luaZ_resizebuffer → buf.resize(state, size)?
737    ls.buff.resize(state, LUA_MIN_BUFFER)?;
738    Ok(())
739}
740
741// LUAI_FUNC → pub(crate)
742/// Create (or retrieve) a Lua string and anchor it in the parser's GC-protection
743/// table `ls.h` so it cannot be collected before the end of compilation.
744///
745/// Also internalises long strings so that each unique content has exactly one
746/// copy in memory.  The table `ls.h` is used as a set: the string is both the
747/// key and the value.
748///
749/// # C source
750/// ```c
751///
752/// //   lua_State *L = ls->L;
753/// //   TString *ts = luaS_newlstr(L, str, l);
754/// //   const TValue *o = luaH_getstr(ls->h, ts);
755/// //   if (!ttisnil(o))  /* string already present? */
756/// //     ts = keystrval(nodefromval(o));  /* get saved copy */
757/// //   else {
758/// //     TValue *stv = s2v(L->top.p++);  /* reserve stack space */
759/// //     setsvalue(L, stv, ts);           /* anchor the string */
760/// //     luaH_finishset(L, ls->h, stv, o, stv);  /* t[string] = string */
761/// //     luaC_checkGC(L);
762/// //     L->top.p--;                       /* remove string from stack */
763/// //   }
764/// //   return ts;
765/// // }
766/// ```
767pub(crate) fn new_string(
768    state: &mut LuaState,
769    ls: &mut LexState,
770    bytes: &[u8],
771) -> Result<GcRef<LuaString>, LuaError> {
772    // PORT NOTE: in C, the anchor table ls->h is a Lua table mapping the string
773    // to itself so a second occurrence of the same literal in the chunk returns
774    // the originally-created TString. We use a plain HashMap on LexState
775    // (`long_str_anchor`) for the equivalent dedup — sufficient because Phase
776    // A-C `GcRef<T>` is `Rc<T>` and identity is determined by the `Rc`
777    // allocation. Short strings already share identity via the global pool;
778    // long strings (>LUAI_MAXSHORTLEN) need this session-level map.
779    if let Some(existing) = ls.long_str_anchor.get(bytes) {
780        return Ok(existing.clone());
781    }
782    let ts = intern_str_stub(state, bytes)?;
783    ls.long_str_anchor.insert(bytes.to_vec(), ts.clone());
784    Ok(ts)
785}
786
787// ── Public advance / lookahead ─────────────────────────────────────────────────
788
789// LUAI_FUNC → pub(crate)
790/// Consume the current token; load the next one from the stream.
791///
792/// If a lookahead token was set, it becomes the current token without re-reading
793/// from the stream.
794///
795/// # C source
796/// ```c
797///
798/// //   ls->lastline = ls->linenumber;
799/// //   if (ls->lookahead.token != TK_EOS) {
800/// //     ls->t = ls->lookahead;
801/// //     ls->lookahead.token = TK_EOS;
802/// //   }
803/// //   else
804/// //     ls->t.token = llex(ls, &ls->t.seminfo);
805/// // }
806/// ```
807pub fn next(
808    state: &mut LuaState,
809    ls: &mut LexState,
810) -> Result<(), LuaError> {
811    ls.lastline = ls.linenumber;
812
813    if ls.lookahead.kind != TK_EOS {
814        // Clone to avoid borrow conflict; LuaString inside TokenValue is GcRef (Rc).
815        ls.t = ls.lookahead.clone();
816        ls.lookahead = Token::eos();
817    } else {
818        let mut val = TokenValue::None;
819        let kind = llex(state, ls, &mut val)?;
820        ls.t = Token { kind, value: val };
821    }
822    Ok(())
823}
824
825// LUAI_FUNC → pub(crate)
826/// Peek at the next token without consuming the current one.
827///
828/// The lookahead token is cached in `ls.lookahead` and returned.  Only one
829/// token of lookahead is supported; calling this twice without an intervening
830/// [`next`] is a logic error (asserted in debug builds).
831///
832/// # C source
833/// ```c
834///
835/// //   lua_assert(ls->lookahead.token == TK_EOS);
836/// //   ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
837/// //   return ls->lookahead.token;
838/// // }
839/// ```
840pub fn lookahead(
841    state: &mut LuaState,
842    ls: &mut LexState,
843) -> Result<i32, LuaError> {
844    // macros.tsv: lua_assert → debug_assert!
845    debug_assert!(
846        ls.lookahead.kind == TK_EOS,
847        "luaX_lookahead: lookahead already set"
848    );
849
850    let mut val = TokenValue::None;
851    let kind = llex(state, ls, &mut val)?;
852    ls.lookahead = Token { kind, value: val };
853
854    Ok(ls.lookahead.kind)
855}
856
857// ── Private lexer helpers ──────────────────────────────────────────────────────
858
859/// If the current character equals `c`, advance and return `true`.
860///
861/// # C source
862/// ```c
863///
864/// //   if (ls->current == c) { next(ls); return 1; }
865/// //   else return 0;
866/// // }
867/// ```
868fn check_next1(ls: &mut LexState, c: i32) -> bool {
869    if ls.current == c {
870        advance(ls);
871        true
872    } else {
873        false
874    }
875}
876
877/// If the current character is either of the two bytes in `set`, save-and-advance
878/// and return `true`.
879///
880/// # C source
881/// ```c
882///
883/// //   lua_assert(set[2] == '\0');
884/// //   if (ls->current == set[0] || ls->current == set[1]) {
885/// //     save_and_next(ls);
886/// //     return 1;
887/// //   }
888/// //   else return 0;
889/// // }
890/// ```
891fn check_next2(
892    ls: &mut LexState,
893    state: &mut LuaState,
894    set: &[u8; 2],
895) -> Result<bool, LuaError> {
896    if ls.current == set[0] as i32 || ls.current == set[1] as i32 {
897        save_and_next(ls, state)?;
898        Ok(true)
899    } else {
900        Ok(false)
901    }
902}
903
904/// Increment the line counter and consume the newline sequence.
905///
906/// Handles `\n`, `\r`, `\n\r`, and `\r\n`.
907///
908/// # C source
909/// ```c
910///
911/// //   int old = ls->current;
912/// //   lua_assert(currIsNewline(ls));
913/// //   next(ls);  /* skip '\n' or '\r' */
914/// //   if (currIsNewline(ls) && ls->current != old)
915/// //     next(ls);  /* skip '\n\r' or '\r\n' */
916/// //   if (++ls->linenumber >= MAX_INT)
917/// //     lexerror(ls, "chunk has too many lines", 0);
918/// // }
919/// ```
920fn inc_line_number(ls: &mut LexState, _state: &mut LuaState) -> Result<(), LuaError> {
921    // macros.tsv: lua_assert → debug_assert!
922    debug_assert!(curr_is_newline(ls), "inc_line_number: not at a newline");
923
924    let old = ls.current;
925    advance(ls);
926
927    if curr_is_newline(ls) && ls.current != old {
928        advance(ls);
929    }
930
931    // macros.tsv: MAX_INT → i32::MAX
932    ls.linenumber += 1;
933    if ls.linenumber >= i32::MAX {
934        return Err(lex_error(ls, b"chunk has too many lines", 0));
935    }
936    Ok(())
937}
938
939/// Scan a numeric literal (integer or float, decimal or hex).
940///
941/// The caller may have already read an initial dot.  Accepts the pattern:
942/// `%d(%x|%.|(Ee[+-]?))*` or `0[Xx](%x|%.|(Pp[+-]?))*`.
943///
944/// Returns `TK_INT` for integers, `TK_FLT` for floats.
945///
946/// # C source
947/// ```c
948///
949/// //   TValue obj;
950/// //   const char *expo = "Ee";
951/// //   int first = ls->current;
952/// //   lua_assert(lisdigit(ls->current));
953/// //   save_and_next(ls);
954/// //   if (first == '0' && check_next2(ls, "xX"))  /* hexadecimal? */
955/// //     expo = "Pp";
956/// //   for (;;) {
957/// //     if (check_next2(ls, expo))
958/// //       check_next2(ls, "-+");
959/// //     else if (lisxdigit(ls->current) || ls->current == '.')
960/// //       save_and_next(ls);
961/// //     else break;
962/// //   }
963/// //   if (lislalpha(ls->current))  /* numeral touching a letter? */
964/// //     save_and_next(ls);         /* force an error */
965/// //   save(ls, '\0');
966/// //   if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0)
967/// //     lexerror(ls, "malformed number", TK_FLT);
968/// //   if (ttisinteger(&obj)) { seminfo->i = ivalue(&obj); return TK_INT; }
969/// //   else { seminfo->r = fltvalue(&obj); return TK_FLT; }
970/// // }
971/// ```
972fn read_numeral(
973    state: &mut LuaState,
974    ls: &mut LexState,
975    seminfo: &mut TokenValue,
976) -> Result<i32, LuaError> {
977    let mut expo: &[u8; 2] = b"Ee";
978
979    let first = ls.current;
980
981    debug_assert!(is_digit(ls.current), "read_numeral: not at a digit");
982
983    save_and_next(ls, state)?;
984
985    if first == b'0' as i32 && check_next2(ls, state, b"xX")? {
986        expo = b"Pp";
987    }
988
989    loop {
990        if check_next2(ls, state, expo)? {
991            check_next2(ls, state, b"-+")?;
992        } else if is_xdigit(ls.current) || ls.current == b'.' as i32 {
993            //      save_and_next(ls);
994            save_and_next(ls, state)?;
995        } else {
996            break;
997        }
998    }
999
1000    if is_lalpha(ls.current) {
1001        save_and_next(ls, state)?;
1002    }
1003
1004    // In Rust, luaO_str2num will receive a byte slice; NUL is not needed.
1005    // We save 0 for parity with C, but our str2num stub ignores it.
1006    save(ls, state, 0)?;
1007
1008    //        lexerror(ls, "malformed number", TK_FLT);
1009    // macros.tsv: luaZ_buffer → buf.as_mut_slice()
1010    let buf = ls.buff.as_slice();
1011    let num_bytes = if buf.last() == Some(&0) { &buf[..buf.len() - 1] } else { buf };
1012    let mut obj = lua_types::LuaValue::Nil;
1013    if lua_vm::object::str2num(num_bytes, &mut obj) == 0 {
1014        return Err(lex_error(ls, b"malformed number", TK_FLT));
1015    }
1016    match obj {
1017        lua_types::LuaValue::Int(i) => {
1018            *seminfo = TokenValue::Int(i);
1019            Ok(TK_INT)
1020        }
1021        lua_types::LuaValue::Float(f) => {
1022            *seminfo = TokenValue::Float(f);
1023            Ok(TK_FLT)
1024        }
1025        _ => unreachable!("str2num returned non-numeric LuaValue"),
1026    }
1027}
1028
1029/// Scan a `[=*[` or `]=*]` sequence; leave the last bracket as current char.
1030///
1031/// Returns:
1032/// - `count + 2` if well-formed (where `count` is the number of `=` signs),
1033/// - `1` if a single bracket with no `=`s and no second bracket,
1034/// - `0` if malformed (e.g. `[==` with no closing bracket).
1035///
1036/// # C source
1037/// ```c
1038///
1039/// //   size_t count = 0;
1040/// //   int s = ls->current;
1041/// //   lua_assert(s == '[' || s == ']');
1042/// //   save_and_next(ls);
1043/// //   while (ls->current == '=') {
1044/// //     save_and_next(ls);
1045/// //     count++;
1046/// //   }
1047/// //   return (ls->current == s) ? count + 2
1048/// //          : (count == 0) ? 1
1049/// //          : 0;
1050/// // }
1051/// ```
1052fn skip_sep(
1053    state: &mut LuaState,
1054    ls: &mut LexState,
1055) -> Result<usize, LuaError> {
1056    let mut count: usize = 0;
1057    let s = ls.current;
1058    debug_assert!(s == b'[' as i32 || s == b']' as i32, "skip_sep: not at bracket");
1059
1060    save_and_next(ls, state)?;
1061
1062    while ls.current == b'=' as i32 {
1063        save_and_next(ls, state)?;
1064        count += 1;
1065    }
1066
1067    if ls.current == s {
1068        Ok(count + 2)
1069    } else if count == 0 {
1070        Ok(1)
1071    } else {
1072        Ok(0)
1073    }
1074}
1075
1076/// Scan a long string or long comment delimited by `[=*[` … `]=*]`.
1077///
1078/// `seminfo` is `Some` when reading a string literal; `None` when skipping a
1079/// long comment.  When `None`, buffer contents are discarded on each newline
1080/// to avoid wasting memory.
1081///
1082/// # C source
1083/// ```c
1084///
1085/// //   int line = ls->linenumber;
1086/// //   save_and_next(ls);  /* skip 2nd '[' */
1087/// //   if (currIsNewline(ls)) inclinenumber(ls);
1088/// //   for (;;) {
1089/// //     switch (ls->current) {
1090/// //       case EOZ: { /* error */
1091/// //         const char *what = (seminfo ? "string" : "comment");
1092/// //         const char *msg = luaO_pushfstring(..., what, line);
1093/// //         lexerror(ls, msg, TK_EOS);
1094/// //         break;
1095/// //       }
1096/// //       case ']': {
1097/// //         if (skip_sep(ls) == sep) {
1098/// //           save_and_next(ls);  /* skip 2nd ']' */
1099/// //           goto endloop;
1100/// //         }
1101/// //         break;
1102/// //       }
1103/// //       case '\n': case '\r': {
1104/// //         save(ls, '\n');
1105/// //         inclinenumber(ls);
1106/// //         if (!seminfo) luaZ_resetbuffer(ls->buff);
1107/// //         break;
1108/// //       }
1109/// //       default: {
1110/// //         if (seminfo) save_and_next(ls);
1111/// //         else next(ls);
1112/// //       }
1113/// //     }
1114/// //   } endloop:
1115/// //   if (seminfo)
1116/// //     seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep,
1117/// //                                      luaZ_bufflen(ls->buff) - 2 * sep);
1118/// // }
1119/// ```
1120fn read_long_string(
1121    state: &mut LuaState,
1122    ls: &mut LexState,
1123    seminfo: Option<&mut TokenValue>,
1124    sep: usize,
1125) -> Result<(), LuaError> {
1126    let line = ls.linenumber;
1127
1128    save_and_next(ls, state)?;
1129
1130    if curr_is_newline(ls) {
1131        inc_line_number(ls, state)?;
1132    }
1133
1134    // is_string: whether we are reading a string (true) or a comment (false)
1135    let is_string = seminfo.is_some();
1136
1137    loop {
1138        match ls.current {
1139            c if c == EOZ => {
1140                let what: &[u8] = if is_string { b"string" } else { b"comment" };
1141                // PORT NOTE: build message as Vec<u8> to avoid String allocation.
1142                let mut msg: Vec<u8> = Vec::new();
1143                msg.extend_from_slice(b"unfinished long ");
1144                msg.extend_from_slice(what);
1145                msg.extend_from_slice(b" (starting at line ");
1146                let _ = write!(&mut msg, "{}", line);
1147                msg.push(b')');
1148                return Err(lex_error(ls, &msg, TK_EOS));
1149            }
1150            c if c == b']' as i32 => {
1151                let s = skip_sep(state, ls)?;
1152                if s == sep {
1153                    save_and_next(ls, state)?;
1154                    break;
1155                }
1156                // else: the ']' sequence wasn't the closing delimiter; continue
1157            }
1158            c if c == b'\n' as i32 || c == b'\r' as i32 => {
1159                save(ls, state, b'\n' as i32)?;
1160                inc_line_number(ls, state)?;
1161                // macros.tsv: luaZ_resetbuffer → buf.clear()
1162                if !is_string {
1163                    ls.buff.clear();
1164                }
1165            }
1166            _ => {
1167                if is_string {
1168                    save_and_next(ls, state)?;
1169                } else {
1170                    advance(ls);
1171                }
1172            }
1173        }
1174    }
1175
1176    //      seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep,
1177    //                                       luaZ_bufflen(ls->buff) - 2 * sep);
1178    if let Some(out) = seminfo {
1179        // The buffer contains: sep bytes of '[=' + content + sep bytes of '=]'
1180        // We want the content in between.
1181        // PORT NOTE: per PORTING.md §4.3, capture the slice into an owned
1182        // Vec so the immutable borrow of ls.buff is dropped before the
1183        // mutable borrow needed by new_string.
1184        let buf = ls.buff.as_slice();
1185        let content: Vec<u8> = buf[sep..buf.len() - sep].to_vec();
1186        let ts = new_string(state, ls, &content)?;
1187        *out = TokenValue::Str(ts);
1188    }
1189    Ok(())
1190}
1191
1192/// Check `c` is non-zero (truthy); if not, save the current char and raise a
1193/// string-escape error.
1194///
1195/// # C source
1196/// ```c
1197///
1198/// //   if (!c) {
1199/// //     if (ls->current != EOZ)
1200/// //       save_and_next(ls);  /* add current to buffer for error message */
1201/// //     lexerror(ls, msg, TK_STRING);
1202/// //   }
1203/// // }
1204/// ```
1205fn esc_check(
1206    state: &mut LuaState,
1207    ls: &mut LexState,
1208    ok: bool,
1209    msg: &[u8],
1210) -> Result<(), LuaError> {
1211    if !ok {
1212        if ls.current != EOZ {
1213            save_and_next(ls, state)?;
1214        }
1215        return Err(lex_error(ls, msg, TK_STRING));
1216    }
1217    Ok(())
1218}
1219
1220/// Save-and-advance, then verify the new current char is a hex digit; return
1221/// its numeric value (0-15).
1222///
1223/// # C source
1224/// ```c
1225///
1226/// //   save_and_next(ls);
1227/// //   esccheck (ls, lisxdigit(ls->current), "hexadecimal digit expected");
1228/// //   return luaO_hexavalue(ls->current);
1229/// // }
1230/// ```
1231fn get_hexa(
1232    state: &mut LuaState,
1233    ls: &mut LexState,
1234) -> Result<u32, LuaError> {
1235    save_and_next(ls, state)?;
1236    esc_check(state, ls, is_xdigit(ls.current), b"hexadecimal digit expected")?;
1237    // TODO(port): call lua_vm::object::hex_value in Phase B
1238    Ok(hex_value_stub(ls.current))
1239}
1240
1241/// Scan a `\xNN` hex escape; return the decoded byte value.
1242///
1243/// # C source
1244/// ```c
1245///
1246/// //   int r = gethexa(ls);
1247/// //   r = (r << 4) + gethexa(ls);
1248/// //   luaZ_buffremove(ls->buff, 2);  /* remove saved chars from buffer */
1249/// //   return r;
1250/// // }
1251/// ```
1252fn read_hex_esc(
1253    state: &mut LuaState,
1254    ls: &mut LexState,
1255) -> Result<u32, LuaError> {
1256    let r = get_hexa(state, ls)?;
1257    let r = (r << 4) + get_hexa(state, ls)?;
1258    // macros.tsv: luaZ_buffremove → buf.truncate_by(i)
1259    ls.buff.truncate_by(2);
1260    Ok(r)
1261}
1262
1263/// Scan a `\u{XXXXXX}` UTF-8 escape; return the Unicode codepoint.
1264///
1265/// # C source
1266/// ```c
1267///
1268/// //   unsigned long r;
1269/// //   int i = 4;  /* chars to remove: '\', 'u', '{', first digit */
1270/// //   save_and_next(ls);  /* skip 'u' */
1271/// //   esccheck(ls, ls->current == '{', "missing '{'");
1272/// //   r = gethexa(ls);  /* must have at least one digit */
1273/// //   while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) {
1274/// //     i++;
1275/// //     esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large");
1276/// //     r = (r << 4) + luaO_hexavalue(ls->current);
1277/// //   }
1278/// //   esccheck(ls, ls->current == '}', "missing '}'");
1279/// //   next(ls);  /* skip '}' */
1280/// //   luaZ_buffremove(ls->buff, i);
1281/// //   return r;
1282/// // }
1283/// ```
1284fn read_utf8_esc(
1285    state: &mut LuaState,
1286    ls: &mut LexState,
1287) -> Result<u32, LuaError> {
1288    let mut i: usize = 4;
1289
1290    save_and_next(ls, state)?;
1291
1292    esc_check(state, ls, ls.current == b'{' as i32, b"missing '{'")?;
1293
1294    let mut r = get_hexa(state, ls)?;
1295
1296    // cast_void: discard return value
1297    loop {
1298        save_and_next(ls, state)?;
1299        if !is_xdigit(ls.current) {
1300            break;
1301        }
1302        i += 1;
1303        esc_check(state, ls, r <= (0x7FFF_FFFFu32 >> 4), b"UTF-8 value too large")?;
1304        // TODO(port): lua_vm::object::hex_value in Phase B
1305        r = (r << 4) + hex_value_stub(ls.current);
1306    }
1307
1308    esc_check(state, ls, ls.current == b'}' as i32, b"missing '}'")?;
1309
1310    advance(ls);
1311
1312    ls.buff.truncate_by(i);
1313
1314    Ok(r)
1315}
1316
1317/// Scan `\u{...}` and append the UTF-8 encoding of the codepoint to the buffer.
1318///
1319/// # C source
1320/// ```c
1321///
1322/// //   char buff[UTF8BUFFSZ];
1323/// //   int n = luaO_utf8esc(buff, readutf8esc(ls));
1324/// //   for (; n > 0; n--)
1325/// //     save(ls, buff[UTF8BUFFSZ - n]);
1326/// // }
1327/// ```
1328fn utf8_esc(
1329    state: &mut LuaState,
1330    ls: &mut LexState,
1331) -> Result<(), LuaError> {
1332    let codepoint = read_utf8_esc(state, ls)?;
1333
1334    // macros.tsv: UTF8BUFFSZ → const UTF8_BUF_SZ: usize = 8
1335    // TODO(port): call lua_vm::object::utf8_esc_encode(codepoint) in Phase B.
1336    // For Phase A, encode directly here.
1337    let encoded = utf8_encode_stub(codepoint);
1338
1339    for &b in &encoded {
1340        save(ls, state, b as i32)?;
1341    }
1342    Ok(())
1343}
1344
1345/// Scan a decimal escape `\ddd` (up to 3 digits); return the byte value.
1346///
1347/// # C source
1348/// ```c
1349///
1350/// //   int i;
1351/// //   int r = 0;
1352/// //   for (i = 0; i < 3 && lisdigit(ls->current); i++) {
1353/// //     r = 10*r + ls->current - '0';
1354/// //     save_and_next(ls);
1355/// //   }
1356/// //   esccheck(ls, r <= UCHAR_MAX, "decimal escape too large");
1357/// //   luaZ_buffremove(ls->buff, i);  /* remove read digits from buffer */
1358/// //   return r;
1359/// // }
1360/// ```
1361fn read_dec_esc(
1362    state: &mut LuaState,
1363    ls: &mut LexState,
1364) -> Result<u32, LuaError> {
1365    let mut i: usize = 0;
1366    let mut r: u32 = 0;
1367
1368    while i < 3 && is_digit(ls.current) {
1369        r = 10 * r + (ls.current as u32 - b'0' as u32);
1370        save_and_next(ls, state)?;
1371        i += 1;
1372    }
1373
1374    // UCHAR_MAX = 255 = u8::MAX
1375    esc_check(state, ls, r <= u8::MAX as u32, b"decimal escape too large")?;
1376
1377    ls.buff.truncate_by(i);
1378    Ok(r)
1379}
1380
1381/// Scan a short (single/double-quoted) string literal.
1382///
1383/// The C function uses `goto read_save / only_save / no_save` for escape
1384/// handling.  In Rust this is replaced by the `EscapeResult` enum.
1385///
1386/// # C source (see llex.c lines 382-442 for full listing)
1387fn read_string(
1388    state: &mut LuaState,
1389    ls: &mut LexState,
1390    del: i32,
1391    seminfo: &mut TokenValue,
1392) -> Result<(), LuaError> {
1393    // Encoding for what the escape sequence handler needs to do after decoding.
1394    //
1395    // read_save:  advance(ls), remove '\' from buffer, save decoded byte
1396    // only_save:  remove '\' from buffer, save decoded byte (no advance)
1397    // no_save:    nothing (just break from the escape case)
1398    enum EscapeResult {
1399        ReadSave(i32),
1400        OnlySave(i32),
1401        NoSave,
1402    }
1403
1404    save_and_next(ls, state)?;
1405
1406    while ls.current != del {
1407        match ls.current {
1408            c if c == EOZ => {
1409                return Err(lex_error(ls, b"unfinished string", TK_EOS));
1410            }
1411            c if c == b'\n' as i32 || c == b'\r' as i32 => {
1412                return Err(lex_error(ls, b"unfinished string", TK_STRING));
1413            }
1414            c if c == b'\\' as i32 => {
1415                save_and_next(ls, state)?;
1416
1417                // Inner switch on the escape character
1418                let esc = match ls.current {
1419                    c if c == b'a' as i32 => EscapeResult::ReadSave(b'\x07' as i32),
1420                    c if c == b'b' as i32 => EscapeResult::ReadSave(b'\x08' as i32),
1421                    c if c == b'f' as i32 => EscapeResult::ReadSave(b'\x0C' as i32),
1422                    c if c == b'n' as i32 => EscapeResult::ReadSave(b'\n' as i32),
1423                    c if c == b'r' as i32 => EscapeResult::ReadSave(b'\r' as i32),
1424                    c if c == b't' as i32 => EscapeResult::ReadSave(b'\t' as i32),
1425                    c if c == b'v' as i32 => EscapeResult::ReadSave(b'\x0B' as i32),
1426                    c if c == b'x' as i32 => {
1427                        let decoded = read_hex_esc(state, ls)?;
1428                        EscapeResult::ReadSave(decoded as i32)
1429                    }
1430                    c if c == b'u' as i32 => {
1431                        utf8_esc(state, ls)?;
1432                        EscapeResult::NoSave
1433                    }
1434                    c if c == b'\n' as i32 || c == b'\r' as i32 => {
1435                        inc_line_number(ls, state)?;
1436                        EscapeResult::OnlySave(b'\n' as i32)
1437                    }
1438                    c if c == b'\\' as i32 || c == b'"' as i32 || c == b'\'' as i32 => {
1439                        EscapeResult::ReadSave(c)
1440                    }
1441                    c if c == EOZ => EscapeResult::NoSave,
1442                    c if c == b'z' as i32 => {
1443                        ls.buff.truncate_by(1);
1444                        advance(ls);
1445                        while is_space(ls.current) {
1446                            if curr_is_newline(ls) {
1447                                inc_line_number(ls, state)?;
1448                            } else {
1449                                advance(ls);
1450                            }
1451                        }
1452                        EscapeResult::NoSave
1453                    }
1454                    _ => {
1455                        esc_check(
1456                            state, ls,
1457                            is_digit(ls.current),
1458                            b"invalid escape sequence",
1459                        )?;
1460                        let decoded = read_dec_esc(state, ls)?;
1461                        EscapeResult::OnlySave(decoded as i32)
1462                    }
1463                };
1464
1465                // Dispatch the C goto targets as match arms.
1466                match esc {
1467                    EscapeResult::ReadSave(c) => {
1468                        advance(ls);
1469                        ls.buff.truncate_by(1);
1470                        save(ls, state, c)?;
1471                    }
1472                    EscapeResult::OnlySave(c) => {
1473                        ls.buff.truncate_by(1);
1474                        save(ls, state, c)?;
1475                    }
1476                    EscapeResult::NoSave => {}
1477                }
1478            }
1479            _ => {
1480                save_and_next(ls, state)?;
1481            }
1482        }
1483    }
1484
1485    save_and_next(ls, state)?;
1486
1487    //                                     luaZ_bufflen(ls->buff) - 2);
1488    // Buffer contains: delimiter + content + delimiter; strip both delimiters.
1489    // PORT NOTE: capture into owned Vec to drop the borrow before new_string.
1490    let buf = ls.buff.as_slice();
1491    let content: Vec<u8> = if buf.len() >= 2 {
1492        buf[1..buf.len() - 1].to_vec()
1493    } else {
1494        Vec::new()
1495    };
1496    let ts = new_string(state, ls, &content)?;
1497    *seminfo = TokenValue::Str(ts);
1498    Ok(())
1499}
1500
1501/// Core lexer dispatch: consume and return the next raw token kind.
1502///
1503/// This is the heart of the lexer: a large `for`-`switch` loop that classifies
1504/// the current character and dispatches to the appropriate scanner.
1505///
1506/// # C source (see llex.c lines 445-562 for full listing)
1507fn llex(
1508    state: &mut LuaState,
1509    ls: &mut LexState,
1510    seminfo: &mut TokenValue,
1511) -> Result<i32, LuaError> {
1512    // macros.tsv: luaZ_resetbuffer → buf.clear()
1513    ls.buff.clear();
1514
1515    loop {
1516        match ls.current {
1517            c if c == b'\n' as i32 || c == b'\r' as i32 => {
1518                inc_line_number(ls, state)?;
1519                // PORT NOTE: skipcomment-equivalent. luaL_loadfile in C-Lua
1520                // strips a leading '#' line (Unix shebang). Our test harness
1521                // prepends a global-setup preamble to every official test, so
1522                // the script's '#' line is not at byte zero. Apply the same
1523                // rule at any token-scan line start: treat a line whose first
1524                // character is '#' as a single-line comment. This sits in
1525                // llex's dispatch loop (not inc_line_number) so it does not
1526                // affect newlines inside long-bracket strings.
1527                if ls.current == b'#' as i32 {
1528                    while !curr_is_newline(ls) && ls.current != EOZ {
1529                        advance(ls);
1530                    }
1531                }
1532            }
1533
1534            c if c == b' ' as i32
1535                || c == b'\x0C' as i32
1536                || c == b'\t' as i32
1537                || c == b'\x0B' as i32 =>
1538            {
1539                advance(ls);
1540            }
1541
1542            c if c == b'-' as i32 => {
1543                advance(ls);
1544                if ls.current != b'-' as i32 {
1545                    return Ok(b'-' as i32);
1546                }
1547                advance(ls);
1548
1549                if ls.current == b'[' as i32 {
1550                    let sep = skip_sep(state, ls)?;
1551                    ls.buff.clear();
1552                    if sep >= 2 {
1553                        read_long_string(state, ls, None, sep)?;
1554                        ls.buff.clear();
1555                        continue;
1556                    }
1557                }
1558                while !curr_is_newline(ls) && ls.current != EOZ {
1559                    advance(ls);
1560                }
1561                // loop continues (no token emitted for comments)
1562            }
1563
1564            c if c == b'[' as i32 => {
1565                let sep = skip_sep(state, ls)?;
1566                if sep >= 2 {
1567                    read_long_string(state, ls, Some(seminfo), sep)?;
1568                    return Ok(TK_STRING);
1569                } else if sep == 0 {
1570                    return Err(lex_error(ls, b"invalid long string delimiter", TK_STRING));
1571                }
1572                // sep == 1: plain '[', no long string
1573                return Ok(b'[' as i32);
1574            }
1575
1576            c if c == b'=' as i32 => {
1577                advance(ls);
1578                if check_next1(ls, b'=' as i32) {
1579                    return Ok(TK_EQ);
1580                }
1581                return Ok(b'=' as i32);
1582            }
1583
1584            c if c == b'<' as i32 => {
1585                advance(ls);
1586                if check_next1(ls, b'=' as i32) {
1587                    return Ok(TK_LE);
1588                } else if check_next1(ls, b'<' as i32) {
1589                    return Ok(TK_SHL);
1590                }
1591                return Ok(b'<' as i32);
1592            }
1593
1594            c if c == b'>' as i32 => {
1595                advance(ls);
1596                if check_next1(ls, b'=' as i32) {
1597                    return Ok(TK_GE);
1598                } else if check_next1(ls, b'>' as i32) {
1599                    return Ok(TK_SHR);
1600                }
1601                return Ok(b'>' as i32);
1602            }
1603
1604            c if c == b'/' as i32 => {
1605                advance(ls);
1606                if check_next1(ls, b'/' as i32) {
1607                    return Ok(TK_IDIV);
1608                }
1609                return Ok(b'/' as i32);
1610            }
1611
1612            c if c == b'~' as i32 => {
1613                advance(ls);
1614                if check_next1(ls, b'=' as i32) {
1615                    return Ok(TK_NE);
1616                }
1617                return Ok(b'~' as i32);
1618            }
1619
1620            c if c == b':' as i32 => {
1621                advance(ls);
1622                if check_next1(ls, b':' as i32) {
1623                    return Ok(TK_DBCOLON);
1624                }
1625                return Ok(b':' as i32);
1626            }
1627
1628            c if c == b'"' as i32 || c == b'\'' as i32 => {
1629                let del = ls.current;
1630                read_string(state, ls, del, seminfo)?;
1631                return Ok(TK_STRING);
1632            }
1633
1634            c if c == b'.' as i32 => {
1635                save_and_next(ls, state)?;
1636                if check_next1(ls, b'.' as i32) {
1637                    if check_next1(ls, b'.' as i32) {
1638                        return Ok(TK_DOTS);
1639                    }
1640                    return Ok(TK_CONCAT);
1641                } else if !is_digit(ls.current) {
1642                    return Ok(b'.' as i32);
1643                } else {
1644                    return read_numeral(state, ls, seminfo);
1645                }
1646            }
1647
1648            c if is_digit(c) => {
1649                return read_numeral(state, ls, seminfo);
1650            }
1651
1652            c if c == EOZ => {
1653                return Ok(TK_EOS);
1654            }
1655
1656            c => {
1657                if is_lalpha(c) {
1658                    loop {
1659                        save_and_next(ls, state)?;
1660                        if !is_lalnum(ls.current) {
1661                            break;
1662                        }
1663                    }
1664
1665                    // PORT NOTE: copy buffer bytes to drop borrow before new_string.
1666                    let content: Vec<u8> = ls.buff.as_slice().to_vec();
1667                    let ts = new_string(state, ls, &content)?;
1668
1669                    // PORT NOTE: canonical `lua_types::LuaString` lacks the `extra`
1670                    // byte that C-Lua uses to mark reserved words. Recover the
1671                    // keyword index directly from the interned bytes via the
1672                    // `LUAX_TOKENS` table; the first `NUM_RESERVED` entries are
1673                    // the keywords in declaration order, so token id =
1674                    // `FIRST_RESERVED + index`.
1675                    let reserved_token: Option<i32> = LUAX_TOKENS[..NUM_RESERVED]
1676                        .iter()
1677                        .position(|kw| *kw == content.as_slice())
1678                        .map(|i| FIRST_RESERVED + i as i32);
1679                    *seminfo = TokenValue::Str(ts);
1680
1681                    if let Some(tk) = reserved_token {
1682                        return Ok(tk);
1683                    } else {
1684                        return Ok(TK_NAME);
1685                    }
1686                } else {
1687                    let tok = ls.current;
1688                    advance(ls);
1689                    return Ok(tok);
1690                }
1691            }
1692        }
1693    }
1694}
1695
1696// ── Phase A stubs for cross-crate helpers ──────────────────────────────────────
1697//
1698// The functions below stand in for cross-crate calls that cannot resolve in
1699// Phase A.  They will be replaced by proper imports in Phase B.
1700
1701// TODO(port): replace with state.intern_str(bytes) once LuaState gains that
1702// method (from lua_vm::string::new_lstr wired in Phase B).
1703// TODO_ARCH(phase-b-reconcile): canonical LuaString is constructed via
1704// from_bytes; once LuaState::intern_str is wired, route through there instead.
1705fn intern_str_stub(
1706    state: &mut LuaState,
1707    bytes: &[u8],
1708) -> Result<GcRef<LuaString>, LuaError> {
1709    state.intern_str(bytes)
1710}
1711
1712// TODO(port): replace with lua_vm::object::hex_value(c) in Phase B.
1713fn hex_value_stub(c: i32) -> u32 {
1714    match c {
1715        c if c >= b'0' as i32 && c <= b'9' as i32 => (c - b'0' as i32) as u32,
1716        c if c >= b'a' as i32 && c <= b'f' as i32 => (c - b'a' as i32 + 10) as u32,
1717        c if c >= b'A' as i32 && c <= b'F' as i32 => (c - b'A' as i32 + 10) as u32,
1718        _ => 0,
1719    }
1720}
1721
1722// TODO(port): replace with lua_vm::object::utf8_esc_encode(codepoint) in Phase B.
1723/// Encode a Unicode codepoint as a Lua-extended UTF-8 byte sequence (1 to 6 bytes).
1724///
1725/// Faithful port of `luaO_utf8esc` from lobject.c.  Lua permits codepoints up
1726/// to `0x7FFFFFFF` (5- and 6-byte sequences are non-strict UTF-8 but accepted
1727/// by `\u{...}` escapes per literals.lua test cases).
1728fn utf8_encode_stub(codepoint: u32) -> Vec<u8> {
1729    debug_assert!(codepoint <= 0x7FFF_FFFF);
1730    if codepoint < 0x80 {
1731        return vec![codepoint as u8];
1732    }
1733    let mut x = codepoint;
1734    let mut mfb: u32 = 0x3f;
1735    let mut buf: Vec<u8> = Vec::with_capacity(8);
1736    loop {
1737        buf.push(0x80 | ((x & 0x3f) as u8));
1738        x >>= 6;
1739        mfb >>= 1;
1740        if x <= mfb {
1741            break;
1742        }
1743    }
1744    buf.push(((!mfb << 1) | x) as u8);
1745    buf.reverse();
1746    buf
1747}
1748
1749// ──────────────────────────────────────────────────────────────────────────────
1750// PORT STATUS
1751//   source:        src/llex.c  (581 lines, 24 functions)
1752//                  src/llex.h  (91 lines; merged)
1753//   target_crate:  lua-lex
1754//   confidence:    medium
1755//   todos:         18
1756//   port_notes:    12
1757//   unsafe_blocks: 0   (must be 0 outside explicit unsafe-budget crates)
1758//   notes:         Logic is faithful to the C.  The main structural differences:
1759//                  (1) LexState.L removed — state threaded via fn params;
1760//                  (2) save/save_and_next/inclinenumber/helpers are all fallible
1761//                  (Result<_, LuaError>) because lexerror is no longer noreturn;
1762//                  (3) goto read_save/only_save/no_save in read_string replaced
1763//                  by EscapeResult enum; (4) Cross-crate calls (intern_str,
1764//                  luaH_getstr/finishset, luaG_addinfo, luaO_str2num,
1765//                  luaO_hexavalue, luaO_utf8esc, luaC_fix, luaC_checkGC) are
1766//                  stubbed with TODO; (5) LuaError, LuaString, ZIO, LexBuffer,
1767//                  LuaState defined as local stubs — Phase B replaces with real
1768//                  imports once the crate graph is wired.  Key Phase B tasks:
1769//                  wire import paths; move LuaString.extra accessor to pub;
1770//                  implement luaX_newstring anchor-table logic.  Numeric
1771//                  literal parsing now delegates to lua_vm::object::str2num
1772//                  (handles hex integers with wrap-around and hex floats).
1773// ──────────────────────────────────────────────────────────────────────────────