Skip to main content

py-lua-parser

https://github.com/boolangery/py-lua-parser/actions/workflows/python-package.yml/badge.svg https://img.shields.io/pypi/v/luaparser.svg https://img.shields.io/pypi/pyversions/luaparser.svg

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

luaparser-4.1.0.tar.gz (59.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

luaparser-4.1.0-py3-none-any.whl (66.9 kB view details)

Uploaded Python 3

File details

Details for the file luaparser-4.1.0.tar.gz.

File metadata

  • Download URL: luaparser-4.1.0.tar.gz
  • Upload date:
  • Size: 59.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for luaparser-4.1.0.tar.gz
Algorithm Hash digest
SHA256 0e791e73d88f8a34674d2954758c253d3c034541e16c1f51f33bcb411d6ad286
MD5 712bf3cd998c9263e55bf0c5035bec38
BLAKE2b-256 f252a59f072b51c9e9ab5b62e43f31e062b603ebe2464165a4b809e7b105185e

See more details on using hashes here.

File details

Details for the file luaparser-4.1.0-py3-none-any.whl.

File metadata

  • Download URL: luaparser-4.1.0-py3-none-any.whl
  • Upload date:
  • Size: 66.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for luaparser-4.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 68fcf36f37dad00553a5996b970ab6d953125bc95e2ba9ba27875bac96225c84
MD5 6bacb3b2af6af2e87762dc46486b0dcf
BLAKE2b-256 3ebbbd367992b74895ed50234e63fd92fcaad23f33c05b8abe0319b3de142256

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page