thenumbernine / lua-parser

Lua parser and abstract syntax tree in Lua
MIT License
72 stars 7 forks source link

AST tostring with table index of any keyword results in invalid lua code #7

Closed oezingle closed 1 month ago

oezingle commented 1 month ago

I found this issue while attempting to pass the parser library through itself.

At lua-parser/lua/tokenizer.lua::32, The index assignment of self.keywords['goto'] = true is incorrectly serialized back to self.keywords.goto = true, resulting in a lua interpreter error.

In fact, testing any keyword resulted in invalid lua code. Here's a demo:

local parser = require("lua-parser")

local code = [[
local keywords = {}

keywords['goto'] = true
keywords['and'] = true
keywords['break'] = true
keywords['do'] = true
keywords['else'] = true
keywords['elseif'] = true
keywords['end'] = true
keywords['false'] = true
keywords['for'] = true
keywords['function'] = true
keywords['if'] = true
keywords['in'] = true
keywords['local'] = true
keywords['nil'] = true
keywords['not'] = true
keywords['or'] = true
keywords['repeat'] = true
keywords['return'] = true
keywords['then'] = true
keywords['true'] = true
keywords['until'] = true
keywords['while'] = true
]] 

local ast = parser.parse(code)

local chunk, err = load(code, "original code")
if not chunk then
    print("error (original)", err)
else
    chunk()
end

local rebuilt = tostring(ast)
local chunk, err = load(rebuilt, "code rebuilt from AST")
if not chunk then
    print("error (rebuilt)", err)
else
    chunk()
end

I don't understand the codebase entirely, but I think adding a check to see if self.key.value is a lua keyword around lua-parser/lua/ast.lua:642 should resolve this issue.

Thanks!

thenumbernine commented 1 month ago

good catch, fixed in the latest submit, I gave a few nodes pointers back to the parser and have them now consider the parser's tokenizer's keyword table for determining what is considered a valid keyword.