Skip to main content

Crate lua_parse

Crate lua_parse 

Source
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

  • BlockCnt and LhsAssign are Option<Box<...>> chains; [enter_block] pushes, [leave_block] pops. FuncState.prev is likewise Option<Box<FuncState>>.
  • FuncState.f is Box<LuaProto> during compilation (owned, mutably accessible); it is consumed into the GC when the function closes.

Structs§

BlockCnt
In C: stack-allocated, chained via raw *previous pointer. In Rust: heap-allocated in an Option<Box<BlockCnt>> chain on FuncState.
ConsControl
Table-constructor accumulator. t is 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.
ExprDesc
Field t/f are patch-lists for short-circuit boolean evaluation.
ExprPayload
The payload of an ExprDesc, discriminated by its sibling ExprKind (ExprDesc::k). Only the field(s) named for the active kind are meaningful; the rest are unused for that kind.
FuncState
Per-function compilation state. One per function being compiled; nested functions chain through prev (innermost first), heap-allocated as an Option<Box<FuncState>> owned by LexState.
LabelDesc
LexState
The parser’s working state: the current and lookahead tokens, the active FuncState chain, dynamic name/label/goto data, and the parser-side flags for Lua 5.5 global scoping. It embeds the lexer’s own state in Self::lex and 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 prev as an Option<Box<...>>.
LuaState
Re-export of the VM state type the parser threads through codegen. Per-thread Lua execution state.
ScopeBarrier
TokenValue
Semantic info attached to a token.
VarDesc
A compile-time variable descriptor. const_val is only meaningful when kind == VarKind::CompileTimeConst; for every other kind it is unused. Kept as a flat struct for the same reason as ExprPayload (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]. NoBinOpr is the “not a binary operator” sentinel returned by [getbinopr].
ExprKind
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 in ExprPayload.
OpCode
Re-export of the canonical opcode enum so downstream crates that consume emitted prototypes can name opcodes without depending on lua-code directly. The opcode enum is owned by lua-vm (the VM hot path decodes against it directly); lua-code re-exports it so codegen and the VM share one definition. OpCode::from_u32 is the canonical decoder. TODO(multiversion, Step 0 deferred): this OpCode is a DUPLICATE of the canonical one in lua-code/src/opcodes.rs:87. The Step-0 plan wanted them consolidated to one owner (lua-code) with lua-vm depending on it, but that creates a DEPENDENCY CYCLE: lua-code/Cargo.toml already depends on lua-vm, so lua-vm cannot depend back on lua-code. Consolidating therefore requires moving the canonical OpCode/OP_MODES/Instruction definitions DOWN into lua-types (which lua-types/src/opcode.rs already reserves) and pointing both lua-vm and lua-code at it — plus reconciling variant-name skew between the two copies (lua-vm uses BXOrK/BXOr, lua-code uses BXorK/BXor; lua-vm also has LoadKx/GetUpval aliases) and the InstructionExt decode 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) vs lua-code/src/opcodes.rs:87 (canonical).
UnOpr
Unary operators recognized by the expression parser. NoUnOpr is 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§

TokenKind
Token kinds cross the lexer boundary as plain i32 (TK_*), matching the i32 that lua_lex emits 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.