{"id":9910,"library":"luaparser","title":"Lua Parser","description":"luaparser is a Python library for parsing Lua code into an Abstract Syntax Tree (AST). It provides a programmatic interface to analyze, manipulate, and generate Lua code. The current version is 4.0.0, and it maintains an active, though irregular, release cadence addressing bugs and adding features.","status":"active","version":"4.0.0","language":"en","source_language":"en","source_url":"https://github.com/boolangery/py-lua-parser","tags":["lua","parser","ast","language-parsing","static-analysis"],"install":[{"cmd":"pip install luaparser","lang":"bash","label":"Install luaparser"}],"dependencies":[],"imports":[{"symbol":"parse","correct":"from luaparser import parse"},{"symbol":"Node","correct":"from luaparser.ast import Node"}],"quickstart":{"code":"from luaparser import parse\n\nlua_code = \"\"\"\n-- This is a simple Lua script\nlocal function greet(name)\n    print(\"Hello, \" .. name .. \"!\")\nend\n\ngreet(\"World\")\n\"\"\"\n\n# Parse the Lua code into an AST\nast = parse(lua_code)\n\n# Print a representation of the AST\n# In luaparser 3.3.1+, parse() no longer prints directly\nprint(ast.show())","lang":"python","description":"Parses a simple Lua script string and prints its Abstract Syntax Tree (AST) representation to the console."},"warnings":[{"fix":"Ensure all Lua source files are saved with UTF-8 encoding. If not possible, pre-read the file with its correct encoding and pass the string to `parse()`.","message":"Source file encoding changed from local encoding to UTF-8. Parsing files with non-UTF-8 encodings will now raise a `UnicodeDecodeError` by default.","severity":"breaking","affected_versions":"4.0.0 and later"},{"fix":"Access the `raw` attribute of `String` nodes for the unescaped string value. Example: `string_node.raw`.","message":"The `String` AST node's internal representation for string values changed. The `s` attribute now stores bytes, and a new `raw` attribute stores the unescaped string. Code directly accessing `s` for string content may break.","severity":"breaking","affected_versions":"4.0.0 and later"},{"fix":"Review Lua code for strict grammar compliance. The `ParseError` message should indicate the location of the offending token.","message":"The parser is now stricter: it fails immediately on an unknown token instead of attempting to skip it. This means previously 'tolerated' invalid Lua code will now raise a `ParseError`.","severity":"breaking","affected_versions":"4.0.0 and later"},{"fix":"Update AST traversal logic to check the type of `Index.idx`. If it's a `Name` node, access `Index.idx.id` for the index name. If it's a `String` node (for older versions or specific cases), use `Index.idx.raw` (for 4.0.0+) or `Index.idx.value` (for 3.x).","message":"The `Index.idx` attribute type changed from a `String` node to a `Name` node. Code expecting `Index.idx` to have a `.value` attribute for the index string will break.","severity":"breaking","affected_versions":"3.0.0 and later"},{"fix":"After parsing, call `print(ast.show())` to see the AST representation.","message":"The `parse()` function no longer prints the AST directly to `stdout` upon execution. Users must explicitly call `ast.show()` or `print(ast.show())` to visualize the parsed tree.","severity":"gotcha","affected_versions":"3.3.1 and later"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure your Lua source files are saved in UTF-8. If not possible, read the file with the correct encoding explicitly and pass the string content to `parse()`.","cause":"Attempting to parse a Lua file that is not UTF-8 encoded after upgrading to luaparser >= 4.0.0. The parser now defaults to UTF-8.","error":"UnicodeDecodeError: 'charmap' codec can't decode byte 0x93 in position 123: character maps to <undefined>"},{"fix":"Update your AST traversal to correctly handle the type of `Index.idx`. For `luaparser >= 3.0.0`, if `isinstance(idx_node, luaparser.ast.Name)`, use `idx_node.id`. If `isinstance(idx_node, luaparser.ast.String)`, use `idx_node.raw` (for 4.0.0+) or `idx_node.value` (for 3.x).","cause":"This error occurs when code expects `Index.idx` to be a `String` node (with `.value`) but it's a `Name` node (with `.id`) in luaparser >= 3.0.0, or vice-versa if trying to access `.id` on an old `String` node.","error":"AttributeError: 'Name' object has no attribute 'value' (or 'String' object has no attribute 'id')"},{"fix":"Examine the Lua code at the specified line and column for any syntax errors or non-standard constructs. Ensure it strictly conforms to Lua grammar.","cause":"After upgrading to luaparser >= 4.0.0, the parser is stricter and does not skip unknown tokens, leading to an immediate error for invalid Lua syntax that might have been tolerated in older versions.","error":"luaparser.errors.ParseError: Unknown token at line X, column Y: '?'"}]}