-- Tokenizer for spreadsheet formulas. -- -- =SUM(A1:B4) * 2 >= IF(C1 <> "", "ok", "non") -- -- Tokens are { kind = "num"|"str"|"ref"|"ident"|"op"|"punct"|"eof", -- value = ..., pos = <1-based column> }. local M = {} local byte = string.byte local sub = string.sub local format = string.format local function byte_at(text, index) return byte(text, index) or 0 end local function is_alpha(b) return (b >= 65 and b <= 90) or (b >= 97 and b <= 122) or b == 95 end local function is_digit(b) return b >= 48 and b <= 57 end local function is_alnum(b) return is_alpha(b) or is_digit(b) end local TWO_CHAR = { ["<="] = true, [">="] = true, ["<>"] = true, ["!="] = true } local ONE_CHAR = { ["+"] = true, ["-"] = true, ["*"] = true, ["/"] = true, ["%"] = true, ["^"] = true, ["&"] = true, ["="] = true, ["<"] = true, [">"] = true, } local function token(kind, value, pos) return { kind = kind, value = value, pos = pos } end function M.tokenize(text) local tokens = {} local i = 1 local n = #text while i <= n do local c = sub(text, i, i) local b = byte_at(text, i) if b == 32 or b == 9 or b == 10 or b == 13 then i = i + 1 elseif is_digit(b) or (c == "." and is_digit(byte_at(text, i + 1))) then local start = i while is_digit(byte_at(text, i)) do i = i + 1 end if byte_at(text, i) == 46 then i = i + 1 while is_digit(byte_at(text, i)) do i = i + 1 end end local exponent = byte_at(text, i) if exponent == 101 or exponent == 69 then local save = i i = i + 1 local sign = byte_at(text, i) if sign == 43 or sign == 45 then i = i + 1 end if is_digit(byte_at(text, i)) then while is_digit(byte_at(text, i)) do i = i + 1 end else i = save end end local literal = sub(text, start, i - 1) tokens[#tokens + 1] = token("num", tonumber(literal), start) elseif c == '"' or c == "'" then local quote = c local start = i i = i + 1 local parts = {} local closed = false while i <= n do local ch = sub(text, i, i) if ch == "\\" then local escaped = sub(text, i + 1, i + 1) if escaped == "n" then parts[#parts + 1] = "\n" elseif escaped == "t" then parts[#parts + 1] = "\t" elseif escaped == "\\" then parts[#parts + 1] = "\\" elseif escaped == '"' then parts[#parts + 1] = '"' elseif escaped == "'" then parts[#parts + 1] = "'" elseif escaped == "" then error(format("unterminated string at %d", start), 0) else parts[#parts + 1] = escaped end i = i + 2 elseif ch == quote then closed = true i = i + 1 break else parts[#parts + 1] = ch i = i + 1 end end if not closed then error(format("unterminated string at %d", start), 0) end tokens[#tokens + 1] = token("str", table.concat(parts), start) elseif is_alpha(b) or c == "$" then local start = i i = i + 1 while i <= n do local next_byte = byte_at(text, i) if is_alnum(next_byte) or next_byte == 36 then i = i + 1 else break end end local word = sub(text, start, i - 1) local letters, digits = string.match(word, "^%$?([A-Za-z]+)%$?(%d+)$") if letters then tokens[#tokens + 1] = token("ref", { text = word, letters = letters, row = tonumber(digits), }, start) else tokens[#tokens + 1] = token("ident", word, start) end elseif c == "(" or c == ")" or c == "," or c == ":" then tokens[#tokens + 1] = token("punct", c, i) i = i + 1 else local pair = sub(text, i, i + 1) if TWO_CHAR[pair] then if pair == "!=" then pair = "<>" end tokens[#tokens + 1] = token("op", pair, i) i = i + 2 elseif ONE_CHAR[c] then tokens[#tokens + 1] = token("op", c, i) i = i + 1 else error(format("unexpected character %q at %d", c, i), 0) end end end tokens[#tokens + 1] = token("eof", "", n + 1) return tokens end return M