Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem in pp.dump #9

Closed
hnes opened this issue Jun 15, 2017 · 7 comments
Closed

Problem in pp.dump #9

hnes opened this issue Jun 15, 2017 · 7 comments
Labels

Comments

@hnes
Copy link

hnes commented Jun 15, 2017

The command line below will simply throw an error if without this commit .

$cat parse.lua
local parser = require "lua-parser.parser"
local pp = require "lua-parser.pp"

if #arg ~= 1 then
    print("Usage: parse.lua <string>")
    os.exit(1)
end

local ast, error_msg = parser.parse(arg[1], "example.lua")
if not ast then
    print(error_msg)
    os.exit(1)
end

pp.dump(ast)
os.exit(0)

$ lua parse.lua "x=2;local function x() end"
lua: /usr/share/lua/5.1/lua-parser/pp.lua:314: bad argument #3 to 'format' (string expected, got nil)
stack traceback:
        [C]: in function 'format'
        /usr/share/lua/5.1/lua-parser/pp.lua:314: in function 'dump'
        /usr/share/lua/5.1/lua-parser/pp.lua:319: in function 'dump'
        /usr/share/lua/5.1/lua-parser/pp.lua:319: in function 'dump'
        parse_str.lua:54: in main chunk
        [C]: ?

Expected output after this commit:

{
  [tag] = Block
  [pos] = 1
  [1] = {
    [tag] = Set
    [pos] = 1
    [1] = {
      [tag] = VarList
      [pos] = 1
      [1] = {
        [tag] = Id
        [pos] = 1
        [1] = x
      }
    }
    [2] = {
      [tag] = ExpList
      [pos] = 3
      [1] = {
        [tag] = Number
        [pos] = 3
        [1] = 2
      }
    }
  }
  [2] = {
    [tag] = Localrec
    [pos] = 11
    [1] = {
      [1] = {
        [tag] = Id
        [pos] = 20
        [1] = x
      }
    }
    [2] = {
      [1] = {
        [tag] = Function
        [pos] = 21
        [1] = {
        }
        [2] = {
          [tag] = Block
          [pos] = 24
        }
      }
    }
  }
}

I don't know whether this is just a little implementation problem within pp.dump or actually due to the inconsistence of the AST which generated by the parser.

Thanks a lot and all best to you. lua-parser is a pretty cool project 👍

@hnes hnes mentioned this issue Jun 15, 2017
@hnes hnes changed the title Problem in pp.dump report Problem in pp.dump Jun 16, 2017
@hnes
Copy link
Author

hnes commented Jun 16, 2017

Detail:

    [tag] = Localrec
    [pos] = 11
    [1] = {
      [1] = {
        [tag] = Id
        [pos] = 20
        [1] = x
      }
    }
    [tag] = Localrec
    [pos] = 11
    [1] = {
      [tag] = nil              --  <<---
      [pos] = nil              --  <<---
      [1] = {
        [tag] = Id
        [pos] = 20
        [1] = x
      }
    }

@andremm
Copy link
Owner

andremm commented Jun 16, 2017

The command line below will simply throw an error if without this commit .

What error?
Do you mean the error is printing nil on the screen?

Thanks a lot and all best to you. lua-parser is a pretty cool project 👍

Thank you for using lua-parser! :)

@hnes
Copy link
Author

hnes commented Jun 16, 2017

The error is that when string.format find the '%s' in its 1st arg refers a nil value, it will simply throw an error.

> return string.format("%s[tag] = %s\n", string.rep(" ", 2), nil)
stdin:1: bad argument #3 to 'format' (string expected, got nil)
stack traceback:
        [C]: in function 'format'
        stdin:1: in main chunk
        [C]: ?
>

@hnes
Copy link
Author

hnes commented Jun 16, 2017

pp.lua:line:314:

function pp.dump (t, i)
  if i == nil then i = 0 end
  io.write(string.format("{\n"))
  io.write(string.format("%s[tag] = %s\n", string.rep(" ", i+2), t.tag))
  io.write(string.format("%s[pos] = %s\n", string.rep(" ", i+2), t.pos))
  for k,v in ipairs(t) do
    io.write(string.format("%s[%s] = ", string.rep(" ", i+2), tostring(k)))
    if type(v) == "table" then
      pp.dump(v,i+2)
    else
      io.write(string.format("%s\n", tostring(v)))
    end
  end
  io.write(string.format("%s}\n", string.rep(" ", i)))
end

And the error below would occur sometimes as the member "tag" and "pos" is not always present in a table which is in the AST ( Sorry for that I did not explain it clearly before).

$ lua parse.lua "x=2;local function x() end"
lua: /usr/share/lua/5.1/lua-parser/pp.lua:314: bad argument #3 to 'format' (string expected, got nil)
stack traceback:
        [C]: in function 'format'
        /usr/share/lua/5.1/lua-parser/pp.lua:314: in function 'dump'
        /usr/share/lua/5.1/lua-parser/pp.lua:319: in function 'dump'
        /usr/share/lua/5.1/lua-parser/pp.lua:319: in function 'dump'
        parse_str.lua:54: in main chunk
        [C]: ?

@andremm
Copy link
Owner

andremm commented Jun 16, 2017

$ lua parse.lua "x=2;local function x() end"
lua: /usr/share/lua/5.1/lua-parser/pp.lua:314: bad argument #3 to 'format' (string expected, got nil)
stack traceback:
       [C]: in function 'format'
       /usr/share/lua/5.1/lua-parser/pp.lua:314: in function 'dump'
       /usr/share/lua/5.1/lua-parser/pp.lua:319: in function 'dump'
       /usr/share/lua/5.1/lua-parser/pp.lua:319: in function 'dump'
       parse_str.lua:54: in main chunk
       [C]: ?

Thanks! It looks like this is an error specific to Lua version 5.1. I just pushed a commit that should solve this problem. Can you please check whether it works?

@hnes
Copy link
Author

hnes commented Jun 16, 2017

No problem anymore. Thanks a lot :D 👍

@andremm
Copy link
Owner

andremm commented Jun 16, 2017

No problem anymore. Thanks a lot :D 👍

Thanks for the report!

@andremm andremm closed this as completed Jun 16, 2017
@andremm andremm added the bug label Sep 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants