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).

§C source

reference/lua-5.4.7/src/lparser.c (1968 lines, 95 functions)

§Design notes (Phase A)

  • BlockCnt and LhsAssign form intrusive linked lists in C via raw pointers to stack-allocated nodes. In Rust they become Option<Box<...>> chains; enter_block pushes, leave_block pops.
  • FuncState.prev similarly uses Option<Box<FuncState>>.
  • FuncState.f is Box<LuaProto> during compilation (owned, mutably accessible). types.tsv maps it to GcRef<LuaProto> but interior- mutability via Rc<RefCell<...>> would be too noisy; Phase B can switch. PORT NOTE: FuncState.f is Box, not GcRef.
  • LexState is logically defined in lua-lex; a minimal stub is declared here for Phase A. Phase B will replace with lua_lex::LexState once inter-crate deps are wired.
  • Cross-crate calls to lua_code::luaK_* and lua_lex::luaX_* are written as qualified paths and will resolve in Phase B.
  • LuaState is from lua-vm; referenced here as an unresolved import.

Structs§

BlockCnt
C: BlockCnt — one nested block scope (defined in lparser.c, not header). In C: stack-allocated, chained via raw *previous pointer. In Rust: heap-allocated in an Option<Box<BlockCnt>> chain on FuncState.
ConsControl
C: ConsControl — state for parsing a table constructor. PORT NOTE: C stores expdesc *t as a pointer to the caller’s expdesc. Rust stores a copy of the table descriptor; callers must sync back if they mutate it. Phase B may restructure.
DynData
C: Dyndata — parser-local mutable lists (active vars, gotos, labels). C stored C-style dynamic arrays (arr/n/size); Rust uses Vec.
ExprDesc
C: expdesc — describes a potentially-deferred expression/variable. Field t/f are patch-lists for short-circuit boolean evaluation.
ExprPayload
C: the u union inside expdesc. PORT NOTE: C uses a union; all arms share memory. Rust keeps all fields in one struct for Phase A simplicity. Phase B may refactor to a proper enum.
FuncState
C: FuncState — per-function compile-time state. In C: stack-allocated in body(), chained via raw *prev pointer. In Rust: heap-allocated via Option<Box<FuncState>> in LexState.
LabelDesc
C: Labeldesc — a pending goto statement or an active label.
LexState
C: LexState — per-chunk lexer + parser state. PORT NOTE: This is a Phase A stub. In Phase B, LexState lives in lua-lex and lua-parse imports it. FuncState will move here or be passed separately. The fs field creates a circular-crate dependency that Phase B must resolve (likely: both live in one crate).
LexToken
C: Token from llex.h.
LhsAssign
C: LHS_assign — chain of assignment left-hand-side variables. In C: stack-allocated, chained via raw *prev. In Rust: Option<Box<...>>.
LuaState
Per-thread Lua execution state.
TokenValue
Semantic info attached to a token. C: SemInfo from llex.h; Rust analogue is TokenValue.
VarDesc
C: Vardesc — describes an active local variable during compilation. PORT NOTE: C uses a union (vd fields + k for const value). Rust keeps all fields in a struct. The const_val field is only meaningful when kind == VarKind::CompileTimeConst.

Enums§

BinOpr
C: BinOpr
ExprKind
C: expkind — the kind of a deferred expression or variable. Variants correspond exactly to the C enum in lparser.h.
OpCode
All opcodes for the Lua 5.4 virtual machine.
UnOpr
C: UnOpr
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
C: int luaY_nvarstack(FuncState *fs) Returns the number of variables currently occupying registers. LUAI_FUNC visibility.
parse
C: LClosure *luaY_parser(lua_State *L, ZIO *z, Mbuffer *buff, Dyndata *dyd, const char *name, int firstchar) Top-level entry point: parses a chunk and returns the main LClosure. LUAI_FUNC visibility.

Type Aliases§

TokenKind