py-lua-parser
A Lua parser and AST builder written in Python.
It’s both a development library and a command line tool.
Installation:
The package can be installed through pip:
$ python3.6 -m pip install luaparser
It will install the shell command ‘luaparser’.
Compatibility with Lua grammar
3.2.1 => Lua 5.3 grammar 3.2.1+ => Lua 5.4 grammar
Options
These are the command-line flags:
Usage: luaparser [options] filename
CLI Options:
--version Show program's version number and exit
-h, --help Show this help message and exit
-s, --source Source passed in a string
-x, --xml Set output format to xml
-o, --output Write output to file
Quickstart
Node structure
Each node contains the following data:
class Node:
"""Base class for AST node."""
comments: Comments
first_token: Optional[Token]
last_token: Optional[Token]
start_char: Optional[int]
stop_char: Optional[int]
line: Optional[int]
Working on AST tree
Minimal exemple:
from luaparser import ast
src = """
local function sayHello()
print('hello world !')
end
sayHello()
"""
tree = ast.parse(src)
print(ast.to_pretty_str(tree))
will display:
Chunk: {} 1 key
body: {} 1 key
Block: {} 1 key
body: [] 2 items
0: {} 1 key
LocalFunction: {} 3 keys
name: {} 1 key
Name: {} 1 key
id: "sayHello"
args: [] 0 item
body: [] 1 item
0: {} 1 key
Call: {} 2 keys
func: {} 1 key
Name: {} 1 key
id: "print"
args: [] 1 item
0: {} 1 key
String: {} 1 key
s: "hello world !"
1: {} 1 key
Call: {} 2 keys
func: {} 1 key
Name: {} 1 key
id: "sayHello"
args: [] 0 item
You can run through the list of all the nodes in the tree using ast.walk(tree):
from luaparser import ast
from luaparser import astnodes
tree = ast.parse("local foo = 'bar'")
for node in ast.walk(tree):
if isinstance(node, astnodes.Name):
process(node)
Alternatively, you can use a node visitor:
from luaparser import ast
from luaparser import astnodes
src = "local a = 42"
class NumberVisitor(ast.ASTVisitor):
def visit_Number(self, node):
print('Number value = ' + str(node.n))
tree = ast.parse(src)
NumberVisitor().visit(tree)
Rendering lua code
exp = Chunk(Block([
Forin(
targets=[Name('k'), Name('v')],
iter=[
Invoke(
source=Name('bar'),
func=Name('foo'),
args=[Number(42)]
)
],
body=Block([
Call(func=Name('print'), args=[Name('k'), Name('v')])
]),
)
]))
print(ast.to_lua_source(exp))
Will render:
for k, v in bar:foo(42) do
print(k, v)
end
Command line
Given:
local function log(msg)
print(msg)
end
log("hello world !")
$ luaparser source.lua
Will output:
{
"Chunk": {
"body": {
"Block": {
"body": [
{
"LocalFunction": {
"name": {
"Name": {
"id": "log"
}
},
"args": [
{
"Name": {
"id": "msg"
}
}
],
"body": {
"Block": {
"body": [
{
"Call": {
"func": {
"Name": {
"id": "print"
}
},
"args": [
{
"Name": {
"id": "msg"
}
}
]
}
}
]
}
}
}
},
{
"Call": {
"func": {
"Name": {
"id": "log"
}
},
"args": [
{
"String": {
"s": "hello world !"
}
}
]
}
}
]
}
}
}
}
Command line
Documentation can be built with Sphinx:
$ cd doc
$ pip install -r requirements.txt
$ make html
Metadata
Release files for luaparser 4.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| luaparser-4.2.0.tar.gz | 62.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| luaparser-4.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 131.0 kB
Release files / luaparser-4.2.0.tar.gz
| Download URL | luaparser-4.2.0.tar.gz |
|---|---|
| Size | 62.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
315e1588b23500befaf824604c2d0ea75daf894b5507126636a8cfa9dc3d7636
|
|
BLAKE2b-256 checksum How to use checksums |
2464574e8dadf7dcc08b5c8435756b6a44ea382d14b4d3c19669dbbab112532f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / luaparser-4.2.0-py3-none-any.whl
| Download URL | luaparser-4.2.0-py3-none-any.whl |
|---|---|
| Size | 69.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f73b1a66ff51351d28f067348ebbb4533401fc761926654d0448e34531ce3a34
|
|
BLAKE2b-256 checksum How to use checksums |
5f496d585ffffd47732c497ca3a4137f20b817fdf41598668619652d8b11d7e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Security for luaparser 4.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.