def test_lua(): code = """ f1() a1:a2(t1):a3(t2, t3) a4:f2(function() a5:f3(t4) end) x = f6() """ tree = ast.parse(code) print(ast.to_pretty_str(tree)) for x in walk_functions(code): print(x)
def test_cont_int_4(self): tree = ast.parse( textwrap.dedent(r''' local function sayHello() print('hello world !') end sayHello() ''')) pretty_str = ast.to_pretty_str(tree) exp = textwrap.dedent(r''' Chunk: {} 4 keys body: {} 4 keys Block: {} 4 keys body: [] 2 items 0: {} 1 key LocalFunction: {} 6 keys start_char: 1 stop_char: 56 name: {} 4 keys Name: {} 4 keys id: 'sayHello' args: [] 0 item body: {} 4 keys Block: {} 4 keys stop_char: 56 body: [] 1 item 0: {} 1 key Call: {} 5 keys func: {} 4 keys Name: {} 4 keys id: 'print' args: [] 1 item 0: {} 1 key String: {} 5 keys s: 'hello world !' delimiter: SINGLE_QUOTE 1: {} 1 key Call: {} 5 keys func: {} 4 keys Name: {} 4 keys id: 'sayHello' args: [] 0 item''') self.assertEqual(exp, pretty_str)
def test_cont_int_4(self): tree = ast.parse( textwrap.dedent(r""" local function sayHello() print('hello world !') end sayHello() """)) pretty_str = ast.to_pretty_str(tree) exp = textwrap.dedent(r""" Chunk: {} 2 keys body: {} 2 keys Block: {} 2 keys body: [] 2 items 0: {} 1 key LocalFunction: {} 4 keys name: {} 2 keys Name: {} 2 keys id: 'sayHello' args: [] 0 item body: {} 2 keys Block: {} 2 keys body: [] 1 item 0: {} 1 key Call: {} 3 keys func: {} 2 keys Name: {} 2 keys id: 'print' args: [] 1 item 0: {} 1 key String: {} 3 keys s: 'hello world !' delimiter: SINGLE_QUOTE 1: {} 1 key Call: {} 3 keys func: {} 2 keys Name: {} 2 keys id: 'sayHello' args: [] 0 item""") self.assertEqual(exp, pretty_str) ast.to_xml_str(tree)
from luaparser import ast src = """ local a = {} ; a.a = a a.a["a"] = function(a) return a end print(a.a("a"), #a) """ tree = ast.parse(src) print(ast.to_pretty_str(tree))
def main(): # parse options: parser = OptionParser(usage='usage: %prog [options] file|directory', version='%prog ' + luaparser.__version__) cli_group = OptionGroup(parser, "CLI Options") cli_group.add_option('-s', '--source', metavar='F', type='string', dest='source', help='source passed in a string') cli_group.add_option('-x', '--xml', action='store_true', dest='xml', help='set output format to xml', default=False) cli_group.add_option( '--pretty', action='store_true', dest='pretty', help='set output format to python ast pretty print style', default=False) cli_group.add_option('-o', '--output', metavar='F', type='string', dest='output', help='write output to file') parser.add_option_group(cli_group) (options, args) = parser.parse_args() # check argument: if not options.source and not len(args) > 0: abort('Expected a filepath') try: if options.source: tree = ast.parse(options.source) else: with open(args[0], 'r') as content_file: content = content_file.read() tree = ast.parse(content) # output format if options.pretty: output = ast.to_pretty_str(tree) elif options.xml: output = ast.to_xml_str(tree) else: output = ast.to_pretty_json(tree) # output if options.output: with open(options.output, 'w') as content_file: content_file.write(output) else: print(output) except SyntaxException as e: print('error: ' + str(e))
def main(): # parse options: parser = OptionParser( usage="usage: %prog [options] file|directory", version="%prog " + luaparser.__version__, ) cli_group = OptionGroup(parser, "CLI Options") cli_group.add_option( "-s", "--source", metavar="F", type="string", dest="source", help="source passed in a string", ) cli_group.add_option( "-x", "--xml", action="store_true", dest="xml", help="set output format to xml", default=False, ) cli_group.add_option( "--pretty", action="store_true", dest="pretty", help="set output format to python ast pretty print style", default=False, ) cli_group.add_option( "-o", "--output", metavar="F", type="string", dest="output", help="write output to file", ) parser.add_option_group(cli_group) (options, args) = parser.parse_args() # check argument: if not options.source and not len(args) > 0: abort("Expected a filepath") try: if options.source: tree = ast.parse(options.source) else: with open(args[0], "r") as content_file: content = content_file.read() tree = ast.parse(content) # output format if options.pretty: output = ast.to_pretty_str(tree) elif options.xml: output = ast.to_xml_str(tree) else: output = ast.to_pretty_json(tree) # output if options.output: with open(options.output, "w") as content_file: content_file.write(output) else: print(output) except SyntaxException as e: print("error: " + str(e))
def printChunk(self, src: str): tree = ast.parse(src) print(ast.to_pretty_str(tree)) return tree