Example #1
0
 def test_comment_in_table(self):
     tree = ast.parse(textwrap.dedent("""
         --- @table a table of constants
         local limits = {
           -- pre field 1
           HIGH = 127,    -- max rate limit
           -- pre field 2
           LOW  = 42,   -- min rate limit
           [true] = false, -- test
           "foo" -- just a value
           -- last
           ,toto -- toto value
           ,
           Model = true -- model
         }
         """))
     print(ast.toPrettyStr(tree))
     exp = Chunk(Block([
         LocalAssign(
             [Name('limits')],
             [Table([
                 Field(Name('HIGH'),  Number(127),   [Comment('-- pre field 1'), Comment('-- max rate limit')]),
                 Field(Name('LOW'),   Number(42),    [Comment('-- pre field 2'), Comment('-- min rate limit')]),
                 Field(TrueExpr(),    FalseExpr(),   [Comment('-- test')]),
                 Field(Number(1),     String('foo'), [Comment('-- just a value')]),
                 Field(Number(2),     Name('toto'), [Comment('-- last'), Comment('-- toto value')]),
                 Field(Name('Model'), TrueExpr(), [Comment('-- model')])
             ])],
             [Comment('--- @table a table of constants')]
         )
     ]))
     #print(ast.toPrettyStr(exp))
     self.assertEqual(exp, tree)
Example #2
0
 def test_array(self):
     tree = ast.parse(
         textwrap.dedent(r'''
     foo = {
       1,    2,      4,
       8,    16,     32,
       64,   128,    256,
       512,  1024,   2048
     }
     '''))
     print(ast.toPrettyStr(tree))
     exp = Chunk(
         Block([
             Assign(targets=[Name('foo')],
                    values=[
                        Table([
                            Field(Number(1), Number(1)),
                            Field(Number(2), Number(2)),
                            Field(Number(3), Number(4)),
                            Field(Number(4), Number(8)),
                            Field(Number(5), Number(16)),
                            Field(Number(6), Number(32)),
                            Field(Number(7), Number(64)),
                            Field(Number(8), Number(128)),
                            Field(Number(9), Number(256)),
                            Field(Number(10), Number(512)),
                            Field(Number(11), Number(1024)),
                            Field(Number(12), Number(2048))
                        ])
                    ])
         ]))
     self.assertEqual(exp, tree)
Example #3
0
 def test_function_def_local(self):
     tree = ast.parse(r'local function _process() end')
     print(ast.toPrettyStr(tree))
     exp = Chunk(
         Block([
             LocalFunction(name=Name('_process'), args=[], body=Block([]))
         ]))
     self.assertEqual(exp, tree)
Example #4
0
 def test_concatenation(self):
     tree = ast.parse(r'str = "begin".."end"')
     print(ast.toPrettyStr(tree))
     exp = Chunk(
         Block([
             Assign(
                 targets=[Name('str')],
                 values=[Concat(left=String('begin'), right=String('end'))])
         ]))
     self.assertEqual(exp, tree)
Example #5
0
 def test_comment_before_global_assign(self):
     tree = ast.parse(textwrap.dedent("""
         -- rate limit
         rate_limit = 192
         """))
     print(ast.toPrettyStr(tree))
     exp = Chunk(Block([
         Assign(
             [Name('rate_limit')],
             [Number(192)],
             [Comment('-- rate limit')]
         )
     ]))
     self.assertEqual(exp, tree)
Example #6
0
 def test_function_def_indexed_name_global(self):
     tree = ast.parse(r'function t.a.b.c.f() end')
     print(ast.toPrettyStr(tree))
     exp = Chunk(
         Block([
             Function(name=Index(
                 idx=String('f'),
                 value=Index(idx=String('c'),
                             value=Index(idx=String('b'),
                                         value=Index(idx=String('a'),
                                                     value=Name('t'))))),
                      args=[],
                      body=Block([]))
         ]))
     self.assertEqual(exp, tree)
Example #7
0
 def test_comment_before_method(self):
     tree = ast.parse(textwrap.dedent("""       
         --- description
         --- @tparam string arg a string
         function Class:print(arg)
         end
         """))
     print(ast.toPrettyStr(tree))
     exp = Chunk(Block([
         Method(
             source=Name('Class'),
             name=Name('print'),
             args=[Name('arg')],
             body=Block([]),
             comments=[Comment('--- description'), Comment('--- @tparam string arg a string')]
         )
     ]))
     self.assertEqual(exp, tree)
Example #8
0
 def test_function_call_no_par_string(self):
     tree = ast.parse(r'print "hello"')
     print(ast.toPrettyStr(tree))
     exp = Chunk(Block([Call(func=Name('print'), args=[String('hello')])]))
     print(ast.toPrettyStr(exp))
     self.assertEqual(exp, tree)