lua-toml icon indicating copy to clipboard operation
lua-toml copied to clipboard

Support hex, octal and binary number values

Open piface314 opened this issue 5 years ago • 0 comments

TOML supports integer values in other bases, so it would be nice if this could be parsed. I tried inserting the code below to the parseNumber function, right after line 222, and it looks like it works ok.

local prefixes = { ["0x"] = 16, ["0o"] = 8, ["0b"] = 2 }
local base = prefixes[char(0) .. char(1)]
if base then
  step(2)
  local digits = ({ [2] = "[01]", [8] = "[0-7]", [16] = "%x" })[base]
  while(bounds()) do
    if char():match(digits) then
      num = num .. char()
    elseif char():match(ws) or char() == "#" or char():match(nl) or char() == "," or char() == "]" or char() == "}" then
      break
    elseif char() ~= "_" then
      err("Invalid number")
    end
    step()
  end
  if num == "" then
    err("Invalid number")
  end
  return {value = tonumber(num, base), type = "int"}
end

piface314 avatar Feb 03 '20 01:02 piface314