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

Support hex, octal and binary number values #25

Open
piface314 opened this issue Feb 3, 2020 · 0 comments
Open

Support hex, octal and binary number values #25

piface314 opened this issue Feb 3, 2020 · 0 comments

Comments

@piface314
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant