-- Recursive descent parser for spreadsheet formulas. -- Produces plain tables: { k = "num"|"str"|"bool"|"ref"|"range"|"un"|"bin"|"call", ... } -- -- Precedence (low to high): comparisons, &, + -, * / %, unary -, ^ -- Unary minus binds tighter than ^ (spreadsheet style: -2^2 = 4). local lexer = require("sheet.lexer") local format = require("sheet.format") local M = {} local PREC = { ["="] = 1, ["<>"] = 1, ["<"] = 1, [">"] = 1, ["<="] = 1, [">="] = 1, ["&"] = 2, ["+"] = 3, ["-"] = 3, ["*"] = 4, ["/"] = 4, ["%"] = 4, ["^"] = 6, } local Parser = {} Parser.__index = Parser function Parser.new(tokens) return setmetatable({ tokens = tokens, pos = 1 }, Parser) end function Parser:peek() return self.tokens[self.pos] end function Parser:next() local current = self.tokens[self.pos] self.pos = self.pos + 1 return current end function Parser:expect_punct(value) local current = self:peek() if current.kind ~= "punct" or current.value ~= value then error(string.format("expected %q at %d", value, current.pos), 0) end return self:next() end function Parser:parse_primary() local current = self:peek() if current.kind == "num" then self:next() return { k = "num", v = current.value } end if current.kind == "str" then self:next() return { k = "str", v = current.value } end if current.kind == "ref" then self:next() local column = format.column_index(current.value.letters) local row = current.value.row local reference = { k = "ref", col = column, row = row, key = format.column_label(column) .. row, text = current.value.text, } local following = self:peek() if following.kind == "punct" and following.value == ":" then self:next() local last = self:peek() if last.kind ~= "ref" then error(string.format("expected a cell after ':' at %d", last.pos), 0) end self:next() local column2 = format.column_index(last.value.letters) local row2 = last.value.row return { k = "range", c1 = math.min(column, column2), c2 = math.max(column, column2), r1 = math.min(row, row2), r2 = math.max(row, row2), } end return reference end if current.kind == "ident" then self:next() local following = self:peek() if following.kind == "punct" and following.value == "(" then self:next() local args = {} local closing = self:peek() if not (closing.kind == "punct" and closing.value == ")") then args[#args + 1] = self:parse_expression(1) while true do local separator = self:peek() if separator.kind == "punct" and separator.value == "," then self:next() args[#args + 1] = self:parse_expression(1) else break end end end self:expect_punct(")") return { k = "call", name = string.upper(current.value), args = args } end local upper = string.upper(current.value) if upper == "TRUE" then return { k = "bool", v = true } end if upper == "FALSE" then return { k = "bool", v = false } end error(string.format("unknown name %q at %d", current.value, current.pos), 0) end if current.kind == "punct" and current.value == "(" then self:next() local inner = self:parse_expression(1) self:expect_punct(")") return inner end error(string.format("unexpected token %q at %d", tostring(current.value), current.pos), 0) end function Parser:parse_unary() local current = self:peek() if current.kind == "op" and (current.value == "-" or current.value == "+") then self:next() local operand = self:parse_unary() if current.value == "-" then return { k = "un", op = "-", a = operand } end return { k = "un", op = "+", a = operand } end return self:parse_primary() end function Parser:parse_expression(minimum) local left = self:parse_unary() while true do local current = self:peek() if current.kind ~= "op" then break end local precedence = PREC[current.value] if precedence == nil or precedence < minimum then break end self:next() local next_minimum = precedence + 1 if current.value == "^" then next_minimum = precedence end local right = self:parse_expression(next_minimum) left = { k = "bin", op = current.value, a = left, b = right } end return left end -- Parses the body of a formula, i.e. without the leading '='. function M.parse(text) local tokens = lexer.tokenize(text) local parser = Parser.new(tokens) local ast = parser:parse_expression(1) local last = parser:peek() if last.kind ~= "eof" then error(string.format("unexpected %q at %d", tostring(last.value), last.pos), 0) end return ast end return M