luaparse is a JavaScript-based Lua parser that generates an Abstract Syntax Tree (AST) from Lua source code. Originally developed by Oskar Schöldström for his bachelor's thesis, it supports various Lua versions including '5.1', '5.2', '5.3', and 'LuaJIT'. The current stable version is 0.3.1, primarily a maintenance release addressing issues from 0.3.0. The library offers flexible parsing options, allowing control over comment storage, scope tracking, location and range information, and custom node/scope creation callbacks. It can be used in CommonJS environments, AMD, and directly in browsers via a global `luaparse` object. A key differentiator is its detailed configuration for handling different Lua versions and its comprehensive AST output, including specific options for character encoding interpretation due to the difference between JavaScript's Unicode strings and Lua's bytestrings.
npm install luaparseVerified import paths — ran on the pinned version, not inferred.
Parses a simple Lua script, including comments and location data, and logs the resulting AST. Also demonstrates accessing node properties.
To retrieve the actual string content, you need to explicitly set `encodingMode` to a value like `'x-user-defined'` or `'pseudo-latin1'` in the options passed to `luaparse.parse()`. Alternatively, for the raw string including quotes, use the `raw` property if available and not null.
If you require extended identifiers for LuaJIT syntax, you must explicitly pass `extendedIdentifiers: true` in the options object to `luaparse.parse()` in addition to `luaVersion: 'LuaJIT'`.
To correctly interpret string literal values, particularly those with non-ASCII characters or escape sequences, set `encodingMode` to `'x-user-defined'` or `'pseudo-latin1'` in the parser options.
For Node.js, use `const luaparse = require('luaparse');`. For browsers, include via a `<script>` tag and access the global `luaparse` object.Be aware that new features or fixes for complex parsing issues might not be actively developed. Consider reviewing the open issues on GitHub for known limitations.
Ensure you are using `const luaparse = require('luaparse');` for CommonJS modules, or that the library is correctly loaded as a global script in a browser environment.Pass `encodingMode: 'x-user-defined'` or `encodingMode: 'pseudo-latin1'` in the options object to `luaparse.parse()` to get processed string values. Alternatively, inspect the `raw` property for the original string literal including quotes.
Always invoke `parser.end(remainingInput)` after `parser.write()` calls to signal the end of the input stream and retrieve the final AST.
No dependency data recorded yet.