py-lua-parser
A Lua parser and AST builder written in Python, usable both as a library and as a command line tool. It reads Lua 5.1 to 5.5 source and gives back a tree of typed nodes that you can walk, rewrite and render back to Lua.
from luaparser import ast
tree = ast.parse("local x = 1 + 2")
print(ast.to_lua_source(tree))
Installation
$ python3 -m pip install luaparser
This installs the library and the luaparser shell command. Python 3.10 or
later is required.
Supported Lua versions
Lua source version |
Recommended |
Grammar support |
|---|---|---|
Lua 5.1 – 5.3 |
|
Supported |
Lua 5.4 |
|
Supported |
Lua 5.5 |
Unreleased (after 4.2.0) |
Supported |
Grammar support is backward compatible, so a recent release parses source
written for an older Lua version. Note that luaparser does not enforce
every compile-time restriction of a given Lua version, so a successful parse is
not a substitute for checking the source with that version’s luac.
Where to go next
Parsing – turning source into a tree, and what a node carries.
Walking and transforming – walking a tree, and rewriting it in place.
Rendering – going back to Lua source, or to a debug representation.
Command line – the
luaparsercommand.AST nodes – reference for every node type.
API reference – reference for the
astandprintersmodules.