Files
Colby Mchenry e32135171e feat(kernel): R7b Lua+Luau walker — one lua module, vendored-grammar-C lua v0.4.1, tree-sitter-luau 1.2.0 pin, both default-routed (#1384)
R7b batch 4 #2 (docs/design/lua-luau-kernel-port-checklist.md is the
authoritative quirk list). ONE walker for both dialects (ccpp precedent) —
the differences are exactly four: luau's type_definition aliases, the
`export `-slice isExported hook, the return-type signature suffix, and the
grammar handle.

Grammar prep is kernel-side only, no wasm change: lua is the SECOND
vendored-grammar-C language (the vendored wasm is the v0.4.1 tag, a revision
not on crates.io — tag artifacts compiled via build.rs, shas pinned); luau
is a plain crate pin =1.2.0 whose tarball is sha-identical to the tag (the
swift tag≠crate divergence does not recur). Grammar-parity rows replace the
bump gate entirely.

Preserved bug-for-bug (all probe-pinned): the require/visitNode-hook
ASYMMETRIES (top-level requires — including inside top-level if/for/while —
mint import nodes while the identical body-level statement emits
`calls "require"`; top-level `local x = foo()` initializers are invisible
while global `x = foo()` calls emit), the BFS string-win inside require args
(`require(script:WaitForChild("Kid"))` → import Kid) and Roblox instance
paths, receiver-QN methods (`M.sub.deep::chained`, `_G::installed`,
stack-QN nested globals like `render::leakedGlobal`), the raw-text callee
world (colon forms with `self` never stripped, bracket callees,
newline-glued chains byte-verbatim, the `(handler)` paren-conversion),
LUA_SPEC function-as-value capture with the `M.cb = cb` param-storage skip
and first-occurrence dedupe, LuaDoc `---` keeping a leading `- ` plus
`--!strict` joining docstring chains (block-comment docstrings keep interior
CRLF bytes), variable nodes at the IDENTIFIER with positional value pairing,
duplicate same-(kind,name,line) ids, and the lua↔luau isExported wire
divergence (lua functions: flag absent; luau functions: present-false;
methods: absent in both; variables: present-false in both; `export type`:
true).

Gates: parity sweeps first-run 0-diff on kong/lazy.nvim/lua-resty-core
(lua) + lune/Fusion (luau) — 1,734 clean files byte-parity, deferrals
1/0/0/3/8 matching the survey's both-arm predictions exactly (kong's 1 = a
deliberately invalid fixture; luau's = grammar-inherent generic type packs
and default type params); full-init dumps byte-identical kernel-vs-wasm ×4
(kong 157,650 dump lines); kernel-lua-parity suite (both torture fixtures +
in-memory CRLF variants + glue-chain, duplicate-id, and cross-dialect defer
pins + kernel-arm wire-flag pins); full suite 2,647 green ×2 with
CODEGRAPH_KERNEL_EXPECT=1. DEFAULT_ROUTED += lua, luau (18 langs).

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-20 18:42:14 -05:00

196 lines
4.5 KiB
C

#include <stdio.h>
#include "tree_sitter/alloc.h"
#include "tree_sitter/parser.h"
#include <wctype.h>
enum TokenType {
BLOCK_COMMENT_START,
BLOCK_COMMENT_CONTENT,
BLOCK_COMMENT_END,
BLOCK_STRING_START,
BLOCK_STRING_CONTENT,
BLOCK_STRING_END,
};
static inline void consume(TSLexer *lexer) { lexer->advance(lexer, false); }
static inline void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
static inline bool consume_char(char c, TSLexer *lexer) {
if (lexer->lookahead != c) {
return false;
}
consume(lexer);
return true;
}
static inline uint8_t consume_and_count_char(char c, TSLexer *lexer) {
uint8_t count = 0;
while (lexer->lookahead == c) {
++count;
consume(lexer);
}
return count;
}
static inline void skip_whitespaces(TSLexer *lexer) {
while (iswspace(lexer->lookahead)) {
skip(lexer);
}
}
typedef struct {
char ending_char;
uint8_t level_count;
} Scanner;
static inline void reset_state(Scanner *scanner) {
scanner->ending_char = 0;
scanner->level_count = 0;
}
void *tree_sitter_lua_external_scanner_create() {
Scanner *scanner = ts_calloc(1, sizeof(Scanner));
return scanner;
}
void tree_sitter_lua_external_scanner_destroy(void *payload) {
Scanner *scanner = (Scanner *)payload;
ts_free(scanner);
}
unsigned tree_sitter_lua_external_scanner_serialize(void *payload, char *buffer) {
Scanner *scanner = (Scanner *)payload;
buffer[0] = scanner->ending_char;
buffer[1] = (char)scanner->level_count;
return 2;
}
void tree_sitter_lua_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
Scanner *scanner = (Scanner *)payload;
if (length == 0) return;
scanner->ending_char = buffer[0];
if (length == 1) return;
scanner->level_count = buffer[1];
}
static bool scan_block_start(Scanner *scanner, TSLexer *lexer) {
if (consume_char('[', lexer)) {
uint8_t level = consume_and_count_char('=', lexer);
if (consume_char('[', lexer)) {
scanner->level_count = level;
return true;
}
}
return false;
}
static bool scan_block_end(Scanner *scanner, TSLexer *lexer) {
if (consume_char(']', lexer)) {
uint8_t level = consume_and_count_char('=', lexer);
if (scanner->level_count == level && consume_char(']', lexer)) {
return true;
}
}
return false;
}
static bool scan_block_content(Scanner *scanner, TSLexer *lexer) {
while (lexer->lookahead != 0) {
if (lexer->lookahead == ']') {
lexer->mark_end(lexer);
if (scan_block_end(scanner, lexer)) {
return true;
}
} else {
consume(lexer);
}
}
return false;
}
static bool scan_comment_start(Scanner *scanner, TSLexer *lexer) {
if (consume_char('-', lexer) && consume_char('-', lexer)) {
lexer->mark_end(lexer);
if (scan_block_start(scanner, lexer)) {
lexer->mark_end(lexer);
lexer->result_symbol = BLOCK_COMMENT_START;
return true;
}
}
return false;
}
static bool scan_comment_content(Scanner *scanner, TSLexer *lexer) {
if (scanner->ending_char == 0) { // block comment
if (scan_block_content(scanner, lexer)) {
lexer->result_symbol = BLOCK_COMMENT_CONTENT;
return true;
}
return false;
}
while (lexer->lookahead != 0) {
if (lexer->lookahead == scanner->ending_char) {
reset_state(scanner);
lexer->result_symbol = BLOCK_COMMENT_CONTENT;
return true;
}
consume(lexer);
}
return false;
}
bool tree_sitter_lua_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
Scanner *scanner = (Scanner *)payload;
if (valid_symbols[BLOCK_STRING_END] && scan_block_end(scanner, lexer)) {
reset_state(scanner);
lexer->result_symbol = BLOCK_STRING_END;
return true;
}
if (valid_symbols[BLOCK_STRING_CONTENT] && scan_block_content(scanner, lexer)) {
lexer->result_symbol = BLOCK_STRING_CONTENT;
return true;
}
if (valid_symbols[BLOCK_COMMENT_END] && scanner->ending_char == 0 && scan_block_end(scanner, lexer)) {
reset_state(scanner);
lexer->result_symbol = BLOCK_COMMENT_END;
return true;
}
if (valid_symbols[BLOCK_COMMENT_CONTENT] && scan_comment_content(scanner, lexer)) {
return true;
}
skip_whitespaces(lexer);
if (valid_symbols[BLOCK_STRING_START] && scan_block_start(scanner, lexer)) {
lexer->result_symbol = BLOCK_STRING_START;
return true;
}
if (valid_symbols[BLOCK_COMMENT_START]) {
if (scan_comment_start(scanner, lexer)) {
return true;
}
}
return false;
}