From 61eb99468429e18bab42e78111c0429befdf7b0e Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Sat, 24 Oct 2026 08:44:45 +0800 Subject: [PATCH 1/2] Keep pretty-printer character variable local --- lua-parser/pp.lua | 2 +- test-pp.lua | 11 +++++++++++ test.lua | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test-pp.lua diff --git a/lua-parser/pp.lua b/lua-parser/pp.lua index 649db38..5eb1ae6 100644 --- a/lua-parser/pp.lua +++ b/lua-parser/pp.lua @@ -18,7 +18,7 @@ end local function fixed_string (str) local new_str = "" for i=1,string.len(str) do - char = string.byte(str, i) + local char = string.byte(str, i) if char == 34 then new_str = new_str .. string.format("\\\"") elseif char == 92 then new_str = new_str .. string.format("\\\\") elseif char == 7 then new_str = new_str .. string.format("\\a") diff --git a/test-pp.lua b/test-pp.lua new file mode 100644 index 0000000..505d843 --- /dev/null +++ b/test-pp.lua @@ -0,0 +1,11 @@ +local pp = require "lua-parser.pp" +local saved_char = rawget(_G, "char") +local sentinel = {} +_G.char = sentinel +local ast = {{ tag = "Return", { tag = "String", "hello" } }} +assert(pp.tostring(ast) == '{ `Return{ `String "hello" } }') +assert(_G.char == sentinel, "pretty printing changed global char") +_G.char = nil +pp.tostring(ast) +assert(rawget(_G, "char") == nil, "pretty printing created global char") +_G.char = saved_char diff --git a/test.lua b/test.lua index 944215d..978ccb9 100755 --- a/test.lua +++ b/test.lua @@ -1,3 +1,5 @@ +dofile("test-pp.lua") + #!/usr/bin/env lua local parser = require "lua-parser.parser" From a3b6de36913694beec8ef277a331b2c205e28bbd Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Sat, 7 Nov 2026 10:19:23 +0800 Subject: [PATCH 2/2] Run pretty printer checks at end of test suite --- test.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test.lua b/test.lua index 978ccb9..700a209 100755 --- a/test.lua +++ b/test.lua @@ -1,5 +1,3 @@ -dofile("test-pp.lua") - #!/usr/bin/env lua local parser = require "lua-parser.parser" @@ -3779,4 +3777,7 @@ assert(r == e) r = parse(s) assert(r == e) +print("> testing pretty printer...") +dofile("test-pp.lua") + print("OK")