From e1fb33446a6ab5bdd692f9c16c054aa942f31ff7 Mon Sep 17 00:00:00 2001 From: Eliott Dumeix Date: Sun, 27 Oct 2024 12:01:30 +0100 Subject: [PATCH] fix comment parsing with Assign, fix #39 --- luaparser/builder.py | 4 ++-- luaparser/tests/test_integration.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/luaparser/builder.py b/luaparser/builder.py index 8cab936..07c404d 100644 --- a/luaparser/builder.py +++ b/luaparser/builder.py @@ -173,7 +173,7 @@ def visitStat_assignment(self, ctx: LuaParser.Stat_assignmentContext): return self.add_context(ctx, Assign( targets=_listify(self.visit(ctx.varlist())), values=_listify(self.visit(ctx.explist())), - )) + ), allow_right_ctx=True) # Visit a parse tree produced by LuaParser#stat_functioncall. def visitStat_functioncall(self, ctx: LuaParser.Stat_functioncallContext): @@ -185,7 +185,7 @@ def visitStat_label(self, ctx: LuaParser.Stat_labelContext): # Visit a parse tree produced by LuaParser#stat_break. def visitStat_break(self, ctx: LuaParser.Stat_breakContext): - return self.add_context(ctx, Break()) + return self.add_context(ctx, Break(), allow_right_ctx=True) # Visit a parse tree produced by LuaParser#stat_goto. def visitStat_goto(self, ctx: LuaParser.Stat_gotoContext): diff --git a/luaparser/tests/test_integration.py b/luaparser/tests/test_integration.py index 3f0b238..af6998a 100644 --- a/luaparser/tests/test_integration.py +++ b/luaparser/tests/test_integration.py @@ -344,3 +344,31 @@ def test_cont_int_12(self): ]) ) self.assertEqual(exp, tree) + + # Comments with Chinese characters are discarded #39 + def test_cont_int_13(self): + tree = ast.parse(textwrap.dedent(""" + function setupRichText() + richText.fitArea = false -- 是否根据内容自适应高度 + richText.fitPerHeight = nil -- 自适应的单行高度 + return richText + end + """)) + exp = Chunk( + Block([ + Function( + name=Name("setupRichText"), + args=[], + body=Block([ + Assign([Index(Name("fitArea"), Name("richText"))], [FalseExpr()], comments=[ + Comment('-- 是否根据内容自适应高度') + ]), + Assign([Index(Name("fitPerHeight"), Name("richText"))], [Nil()], comments=[ + Comment('-- 自适应的单行高度') + ]), + Return([Name("richText")]) + ]) + ) + ]) + ) + self.assertEqual(exp, tree)