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"