• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

boolangery/py-lua-parser: A Lua parser and AST builder written in Python.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

boolangery/py-lua-parser

开源软件地址(OpenSource Url):

https://github.com/boolangery/py-lua-parser

开源编程语言(OpenSource Language):

Python 96.6%

开源软件介绍(OpenSource Introduction):

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 !"
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
}



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ucla-vision/entropy-sgd: Lua implementation of Entropy-SGD发布时间:2022-08-17
下一篇:
folke/which-key.nvim: 发布时间:2022-08-17
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap