Skip to content

Support hex, octal and binary number values #25

Description

@piface314

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions