Expand description
Lua parser — translates the token stream produced by the lexer into
bytecode prototypes (LuaProto).
This crate is the recursive-descent parser with single-pass bytecode codegen
folded in: the front end pairs with lua-lex for tokens and lua-code for
opcode tables. It is verified by the bytecode-parity structural oracle plus
the behavioral suite (see GRADUATED.md); it is no longer a line-by-line
mirror of any C source, so do not reach for lparser.c/lcode.c to reason
about it.
§Ownership shapes
BlockCntandLhsAssignareOption<Box<...>>chains; [enter_block] pushes, [leave_block] pops.FuncState.previs likewiseOption<Box<FuncState>>.FuncState.fisBox<LuaProto>during compilation (owned, mutably accessible); it is consumed into the GC when the function closes.
Structs§
- Block
Cnt - In C: stack-allocated, chained via raw
*previouspointer. In Rust: heap-allocated in anOption<Box<BlockCnt>>chain on FuncState. - Cons
Control - Table-constructor accumulator.
tis a copy of the table-expression descriptor rather than a reference into the caller; a routine that mutates it must write the result back into the caller’s descriptor itself. - DynData
- C stored C-style dynamic arrays (arr/n/size); Rust uses Vec.
- Expr
Desc - Field
t/fare patch-lists for short-circuit boolean evaluation. - Expr
Payload - The payload of an
ExprDesc, discriminated by its siblingExprKind(ExprDesc::k). Only the field(s) named for the active kind are meaningful; the rest are unused for that kind. - Func
State - Per-function compilation state. One per function being compiled; nested
functions chain through
prev(innermost first), heap-allocated as anOption<Box<FuncState>>owned byLexState. - Label
Desc - LexState
- The parser’s working state: the current and lookahead tokens, the active
FuncStatechain, dynamic name/label/goto data, and the parser-side flags for Lua 5.5globalscoping. It embeds the lexer’s own state inSelf::lexand drives it through [lex_next] / [lex_lookahead]; the two layers stay separate so the lexer remains usable on its own. - LexToken
- LhsAssign
- One link in a multiple-assignment target list, chained innermost-first
through
prevas anOption<Box<...>>. - LuaState
- Re-export of the VM state type the parser threads through codegen. Per-thread Lua execution state.
- Scope
Barrier - Token
Value - Semantic info attached to a token.
- VarDesc
- A compile-time variable descriptor.
const_valis only meaningful whenkind == VarKind::CompileTimeConst; for every other kind it is unused. Kept as a flat struct for the same reason asExprPayload(see that type’s note and the Sprint-1 honest-negative).
Enums§
- BinOpr
- Binary operators recognized by the expression parser, in the discriminant
order that indexes [
PRIORITY].NoBinOpris the “not a binary operator” sentinel returned by [getbinopr]. - Expr
Kind - The descriptor kind of a parsed expression: where its value currently lives
and how to materialize it. Drives every
discharge/exp2*decision in codegen. The associated data each kind carries lives inExprPayload. - OpCode
- Re-export of the canonical opcode enum so downstream crates that consume
emitted prototypes can name opcodes without depending on
lua-codedirectly. The opcode enum is owned bylua-vm(the VM hot path decodes against it directly);lua-codere-exports it so codegen and the VM share one definition.OpCode::from_u32is the canonical decoder. TODO(multiversion, Step 0 deferred): thisOpCodeis a DUPLICATE of the canonical one inlua-code/src/opcodes.rs:87. The Step-0 plan wanted them consolidated to one owner (lua-code) withlua-vmdepending on it, but that creates a DEPENDENCY CYCLE:lua-code/Cargo.tomlalready depends onlua-vm, solua-vmcannot depend back onlua-code. Consolidating therefore requires moving the canonicalOpCode/OP_MODES/Instructiondefinitions DOWN intolua-types(whichlua-types/src/opcode.rsalready reserves) and pointing bothlua-vmandlua-codeat it — plus reconciling variant-name skew between the two copies (lua-vmusesBXOrK/BXOr,lua-codeusesBXorK/BXor;lua-vmalso hasLoadKx/GetUpvalaliases) and theInstructionExtdecode trait that lives here. That is a larger refactor than the Step-0 scaffold; deferred to keep 5.4 green. Duplicate sites:lua-vm/src/vm.rs:45(this enum) vslua-code/src/opcodes.rs:87(canonical). - UnOpr
- Unary operators recognized by the expression parser.
NoUnOpris the “this token is not a unary operator” sentinel returned by [getunopr]. - VarKind
Constants§
- TK_AND
- TK_
BREAK - TK_
CONCAT - TK_
DBCOLON - TK_DO
- TK_DOTS
- TK_ELSE
- TK_
ELSEIF - TK_END
- TK_EOS
- TK_EQ
- TK_
FALSE - TK_FLT
- TK_FOR
- TK_
FUNCTION - TK_GE
- TK_GOTO
- TK_IDIV
- TK_IF
- TK_IN
- TK_INT
- TK_LE
- TK_
LOCAL - TK_NAME
- TK_NE
- TK_NIL
- TK_NOT
- TK_OR
- TK_
REPEAT - TK_
RETURN - TK_SHL
- TK_SHR
- TK_
STRING - TK_THEN
- TK_TRUE
- TK_
UNTIL - TK_
WHILE
Functions§
- nvarstack
- Returns the number of variables currently occupying registers. LUAI_FUNC visibility.
- parse
- Top-level entry point: parses a whole chunk and returns the prototype of its
main function. The caller wraps this
Box<LuaProto>into a closure and hands it to the GC; that boundary is the public contract of this crate.
Type Aliases§
- Token
Kind - Token kinds cross the lexer boundary as plain
i32(TK_*), matching thei32thatlua_lexemits and the error formatters read. These constants are the parser’s view of that boundary; they are deliberately not an enum so the lexer/parser interface stays a single integer contract.