luaparser


Nameluaparser JSON
Version 3.2.1 PyPI version JSON
download
home_pagehttps://github.com/boolangery/py-lua-parser
SummaryA lua parser in Python
upload_time2023-02-11 19:18:38
maintainer
docs_urlNone
authorEliott Dumeix
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            py-lua-parser
===============================================================================

.. image:: https://travis-ci.org/boolangery/py-lua-parser.svg?branch=master
    :target: https://travis-ci.org/boolangery/py-lua-parser
.. image:: https://img.shields.io/pypi/v/luaparser.svg
    :target: https://pypi.python.org/pypi/luaparser/
.. image:: https://img.shields.io/pypi/pyversions/luaparser.svg
    :target: https://pypi.python.org/pypi/luaparser/

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`:

.. code-block::

    $ python3.6 -m pip install luaparser

It will install the shell command 'luaparser'.


Options
------------------------------------------------------------------------------

These are the command-line flags:

Usage: luaparser [options] filename

.. code-block::

    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:

.. code-block:: python

	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:

.. code-block:: python

    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:

.. code-block::

    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):

.. code-block:: python

    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:

.. code-block:: python

    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
------------------------------------------------------------------------------

.. warning:: Experimental feature

.. code-block:: python

    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:

.. code-block:: lua

    for k, v in bar:foo(42) do
        print(k, v)
    end


Command line
==============================================================================

Given:

.. code-block:: lua

    local function log(msg)
      print(msg)
    end

    log("hello world !")


.. code-block:: bash

    $ luaparser source.lua


Will output:

.. code-block:: json

    {
        "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:

.. code-block::

    $ cd doc
    $ pip install -r requirements.txt
    $ make html

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/boolangery/py-lua-parser",
    "name": "luaparser",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Eliott Dumeix",
    "author_email": "eliott.dumeix@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/94/1c/56ba1f25c84fb202e3f047a7e15cd20e8635557c4d4e1018ef62fe81186c/luaparser-3.2.1.tar.gz",
    "platform": null,
    "description": "py-lua-parser\n===============================================================================\n\n.. image:: https://travis-ci.org/boolangery/py-lua-parser.svg?branch=master\n    :target: https://travis-ci.org/boolangery/py-lua-parser\n.. image:: https://img.shields.io/pypi/v/luaparser.svg\n    :target: https://pypi.python.org/pypi/luaparser/\n.. image:: https://img.shields.io/pypi/pyversions/luaparser.svg\n    :target: https://pypi.python.org/pypi/luaparser/\n\nA Lua parser and AST builder written in Python.\n\nIt's both a development library and a command line tool.\n\n\nInstallation:\n------------------------------------------------------------------------------\n\nThe package can be installed through `pip`:\n\n.. code-block::\n\n    $ python3.6 -m pip install luaparser\n\nIt will install the shell command 'luaparser'.\n\n\nOptions\n------------------------------------------------------------------------------\n\nThese are the command-line flags:\n\nUsage: luaparser [options] filename\n\n.. code-block::\n\n    CLI Options:\n      --version                     Show program's version number and exit\n      -h, --help                    Show this help message and exit\n      -s, --source                  Source passed in a string\n      -x, --xml                     Set output format to xml\n      -o, --output                  Write output to file\n\n\nQuickstart\n==============================================================================\n\nNode structure\n------------------------------------------------------------------------------\n\nEach node contains the following data:\n\n.. code-block:: python\n\n\tclass Node:\n\t\t\"\"\"Base class for AST node.\"\"\"\n\t\tcomments: Comments\n\t\tfirst_token: Optional[Token]\n\t\tlast_token: Optional[Token]\n\t\tstart_char: Optional[int]\n\t\tstop_char: Optional[int]\n\t\tline: Optional[int]\n\n\nWorking on AST tree\n------------------------------------------------------------------------------\n\nMinimal exemple:\n\n.. code-block:: python\n\n    from luaparser import ast\n\n    src = \"\"\"\n        local function sayHello()\n          print('hello world !')\n        end\n        sayHello()\n        \"\"\"\n\n    tree = ast.parse(src)\n    print(ast.to_pretty_str(tree))\n\nwill display:\n\n.. code-block::\n\n    Chunk: {} 1 key\n      body: {} 1 key\n        Block: {} 1 key\n          body: [] 2 items\n            0: {} 1 key\n              LocalFunction: {} 3 keys\n                name: {} 1 key\n                  Name: {} 1 key\n                    id: \"sayHello\"\n                args: [] 0 item\n                body: [] 1 item\n                  0: {} 1 key\n                    Call: {} 2 keys\n                      func: {} 1 key\n                        Name: {} 1 key\n                          id: \"print\"\n                      args: [] 1 item\n                        0: {} 1 key\n                          String: {} 1 key\n                            s: \"hello world !\"\n            1: {} 1 key\n              Call: {} 2 keys\n                func: {} 1 key\n                  Name: {} 1 key\n                    id: \"sayHello\"\n                args: [] 0 item\n\n\nYou can run through the list of all the nodes in the tree using ast.walk(tree):\n\n.. code-block:: python\n\n    from luaparser import ast\n    from luaparser import astnodes\n\n    tree = ast.parse(\"local foo = 'bar'\")\n\n    for node in ast.walk(tree):\n        if isinstance(node, astnodes.Name):\n            process(node)\n\n\nAlternatively, you can use a node visitor:\n\n.. code-block:: python\n\n    from luaparser import ast\n    from luaparser import astnodes\n\n    src = \"local a = 42\"\n\n    class NumberVisitor(ast.ASTVisitor):\n        def visit_Number(self, node):\n            print('Number value = ' + str(node.n))\n\n    tree = ast.parse(src)\n    NumberVisitor().visit(tree)\n\n\nRendering lua code\n------------------------------------------------------------------------------\n\n.. warning:: Experimental feature\n\n.. code-block:: python\n\n    exp = Chunk(Block([\n        Forin(\n            targets=[Name('k'), Name('v')],\n            iter=[\n                Invoke(\n                    source=Name('bar'),\n                    func=Name('foo'),\n                    args=[Number(42)]\n                )\n            ],\n            body=Block([\n                Call(func=Name('print'), args=[Name('k'), Name('v')])\n            ]),\n\n        )\n    ]))\n\n    print(ast.to_lua_source(exp))\n\n\nWill render:\n\n.. code-block:: lua\n\n    for k, v in bar:foo(42) do\n        print(k, v)\n    end\n\n\nCommand line\n==============================================================================\n\nGiven:\n\n.. code-block:: lua\n\n    local function log(msg)\n      print(msg)\n    end\n\n    log(\"hello world !\")\n\n\n.. code-block:: bash\n\n    $ luaparser source.lua\n\n\nWill output:\n\n.. code-block:: json\n\n    {\n        \"Chunk\": {\n            \"body\": {\n                \"Block\": {\n                    \"body\": [\n                        {\n                            \"LocalFunction\": {\n                                \"name\": {\n                                    \"Name\": {\n                                        \"id\": \"log\"\n                                    }\n                                },\n                                \"args\": [\n                                    {\n                                        \"Name\": {\n                                            \"id\": \"msg\"\n                                        }\n                                    }\n                                ],\n                                \"body\": {\n                                    \"Block\": {\n                                        \"body\": [\n                                            {\n                                                \"Call\": {\n                                                    \"func\": {\n                                                        \"Name\": {\n                                                            \"id\": \"print\"\n                                                        }\n                                                    },\n                                                    \"args\": [\n                                                        {\n                                                            \"Name\": {\n                                                                \"id\": \"msg\"\n                                                            }\n                                                        }\n                                                    ]\n                                                }\n                                            }\n                                        ]\n                                    }\n                                }\n                            }\n                        },\n                        {\n                            \"Call\": {\n                                \"func\": {\n                                    \"Name\": {\n                                        \"id\": \"log\"\n                                    }\n                                },\n                                \"args\": [\n                                    {\n                                        \"String\": {\n                                            \"s\": \"hello world !\"\n                                        }\n                                    }\n                                ]\n                            }\n                        }\n                    ]\n                }\n            }\n        }\n    }\n\nCommand line\n==============================================================================\n\nDocumentation can be built with Sphinx:\n\n.. code-block::\n\n    $ cd doc\n    $ pip install -r requirements.txt\n    $ make html\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A lua parser in Python",
    "version": "3.2.1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9f529f1dccca200d3fa9199a8759d41aa216bf521914a0babd71fbc351993126",
                "md5": "dda9c948e56cae9a05a84ed6a7ecab06",
                "sha256": "d6d45953f008110b65e406867ecc2390ab109250d34383fe82931c45e02cff71"
            },
            "downloads": -1,
            "filename": "luaparser-3.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "dda9c948e56cae9a05a84ed6a7ecab06",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 40438,
            "upload_time": "2023-02-11T19:18:36",
            "upload_time_iso_8601": "2023-02-11T19:18:36.742775Z",
            "url": "https://files.pythonhosted.org/packages/9f/52/9f1dccca200d3fa9199a8759d41aa216bf521914a0babd71fbc351993126/luaparser-3.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "941c56ba1f25c84fb202e3f047a7e15cd20e8635557c4d4e1018ef62fe81186c",
                "md5": "0c1596a1eb3080b334b4fe652973f7b8",
                "sha256": "ee817a59ce42fdfa91997a25b9db9b1a4191aee548c2379e36a348e2e9025269"
            },
            "downloads": -1,
            "filename": "luaparser-3.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "0c1596a1eb3080b334b4fe652973f7b8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 36707,
            "upload_time": "2023-02-11T19:18:38",
            "upload_time_iso_8601": "2023-02-11T19:18:38.689107Z",
            "url": "https://files.pythonhosted.org/packages/94/1c/56ba1f25c84fb202e3f047a7e15cd20e8635557c4d4e1018ef62fe81186c/luaparser-3.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-02-11 19:18:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "boolangery",
    "github_project": "py-lua-parser",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "luaparser"
}
        
Elapsed time: 0.04217s