All Projects → boolangery → py-lua-parser

boolangery / py-lua-parser

Licence: MIT license
A Lua parser and AST builder written in Python.

Programming Languages

python
139335 projects - #7 most used programming language
ANTLR
299 projects

Labels

Projects that are alternatives of or similar to py-lua-parser

parser-reflection
Parser Reflection API - Provides source code analysis without loading classes into the PHP memory
Stars: ✭ 97 (+40.58%)
Mutual labels:  ast
snapdragon-util
Utilities for the snapdragon parser/compiler.
Stars: ✭ 17 (-75.36%)
Mutual labels:  ast
MarkdownSyntax
☄️ A Type-safe Markdown parser in Swift.
Stars: ✭ 65 (-5.8%)
Mutual labels:  ast
deco
Minimalist Function Decorators for Elixir
Stars: ✭ 21 (-69.57%)
Mutual labels:  ast
open-fortran-parser-xml
XML output generator for Open Fortran Parser, and Python wrapper for it.
Stars: ✭ 21 (-69.57%)
Mutual labels:  ast
nast
A block-based intermediate representation for document-like content.
Stars: ✭ 35 (-49.28%)
Mutual labels:  ast
gox
JSX for Go
Stars: ✭ 165 (+139.13%)
Mutual labels:  ast
parcera
Grammar-based Clojure(script) parser
Stars: ✭ 100 (+44.93%)
Mutual labels:  ast
lilt
LILT: noun, A characteristic rising and falling of the voice when speaking; a pleasant gentle accent.
Stars: ✭ 18 (-73.91%)
Mutual labels:  ast
codeparser
Parse Wolfram Language source code as abstract syntax trees (ASTs) or concrete syntax trees (CSTs)
Stars: ✭ 84 (+21.74%)
Mutual labels:  ast
TypeScriptAST
.NET port of Microsoft's TypeScript parser for simple AST manipulation
Stars: ✭ 37 (-46.38%)
Mutual labels:  ast
subpy
Python subsets
Stars: ✭ 41 (-40.58%)
Mutual labels:  ast
vscode-ast
Show JavaScript / TypeScript Abstract Syntax Tree
Stars: ✭ 34 (-50.72%)
Mutual labels:  ast
BBob
⚡️Blazing-fast js-bbcode-parser, bbcode js, that transforms and parses to AST with plugin support in pure javascript, no dependencies
Stars: ✭ 133 (+92.75%)
Mutual labels:  ast
sql-parser
Parse SQL (select) statements into abstract syntax tree (AST) and convert ASTs back to SQL.
Stars: ✭ 230 (+233.33%)
Mutual labels:  ast
rehype-dom
HTML processor to parse and compile with browser APIs, powered by plugins
Stars: ✭ 20 (-71.01%)
Mutual labels:  ast
language-rust
Parser and pretty-printer for the Rust language
Stars: ✭ 78 (+13.04%)
Mutual labels:  ast
pyccolo
Declarative instrumentation for Python.
Stars: ✭ 70 (+1.45%)
Mutual labels:  ast
sass-lint-auto-fix
Automatically resolve s(a|c)ss linting issues
Stars: ✭ 93 (+34.78%)
Mutual labels:  ast
rector-doctrine
Rector upgrades rules for Doctrine
Stars: ✭ 37 (-46.38%)
Mutual labels:  ast

py-lua-parser

https://travis-ci.org/boolangery/py-lua-parser.svg?branch=master

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

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

Warning

Experimental feature

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 !"
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].