#include "engine/parser.hpp" namespace htab { namespace { class PrattParser { public: explicit PrattParser(std::vector tokens) : tokens_(std::move(tokens)) {} ParseResult parse() { if (peek().kind == TokenKind::Eof) { return {nullptr, false, "Empty formula"}; } auto expr = parse_expr(0, false); if (!expr) { return {nullptr, false, error_.empty() ? "Syntax error" : error_}; } if (peek().kind != TokenKind::Eof) { return {nullptr, false, "Unexpected trailing token: " + peek().text}; } return {std::move(expr), true, ""}; } private: const Token& peek() const { return tokens_[pos_]; } const Token& advance() { const Token& t = tokens_[pos_]; if (pos_ + 1 < tokens_.size()) ++pos_; return t; } static bool is_ref_capable(const Expr* e) { if (!e) return false; switch (e->kind) { case Expr::Kind::CellRef: case Expr::Kind::Range: case Expr::Kind::Call: return true; case Expr::Kind::Binary: { auto* b = static_cast(e); return b->op == BinOp::Intersect || b->op == BinOp::Union; } default: return false; } } static bool infix_bp(TokenKind kind, int& lbp, int& rbp, BinOp& op) { switch (kind) { case TokenKind::Eq: lbp = 10; rbp = 11; op = BinOp::Eq; return true; case TokenKind::Neq: lbp = 10; rbp = 11; op = BinOp::Neq; return true; case TokenKind::Lt: lbp = 10; rbp = 11; op = BinOp::Lt; return true; case TokenKind::Gt: lbp = 10; rbp = 11; op = BinOp::Gt; return true; case TokenKind::Le: lbp = 10; rbp = 11; op = BinOp::Le; return true; case TokenKind::Ge: lbp = 10; rbp = 11; op = BinOp::Ge; return true; case TokenKind::Amp: lbp = 20; rbp = 21; op = BinOp::Concat; return true; case TokenKind::Plus: lbp = 30; rbp = 31; op = BinOp::Add; return true; case TokenKind::Minus: lbp = 30; rbp = 31; op = BinOp::Sub; return true; case TokenKind::Star: lbp = 40; rbp = 41; op = BinOp::Mul; return true; case TokenKind::Slash: lbp = 40; rbp = 41; op = BinOp::Div; return true; case TokenKind::Caret: lbp = 50; rbp = 51; op = BinOp::Pow; return true; default: return false; } } std::shared_ptr parse_expr(int min_bp, bool allow_union) { auto lhs = parse_nud(); if (!lhs) return nullptr; while (true) { const Token& t = peek(); if (t.kind == TokenKind::Invalid) { error_ = "Invalid token: " + t.text; return nullptr; } // Range operator ':' (precedence 90, left-assoc) if (t.kind == TokenKind::Colon) { if (90 < min_bp) break; advance(); auto rhs = parse_expr(91, allow_union); if (!rhs) return nullptr; lhs = std::make_shared(std::move(lhs), std::move(rhs)); continue; } // Implicit intersection operator ' ' (precedence 85, left-assoc) if (t.had_space_before && is_ref_capable(lhs.get()) && (t.kind == TokenKind::CellRef || t.kind == TokenKind::LParen || t.kind == TokenKind::Ident)) { if (85 < min_bp) break; auto rhs = parse_expr(86, allow_union); if (!rhs) return nullptr; lhs = std::make_shared(BinOp::Intersect, std::move(lhs), std::move(rhs)); continue; } // Union operator ',' inside parenthesized expressions (precedence 80, left-assoc) if (allow_union && t.kind == TokenKind::Comma) { if (80 < min_bp) break; advance(); auto rhs = parse_expr(81, allow_union); if (!rhs) return nullptr; lhs = std::make_shared(BinOp::Union, std::move(lhs), std::move(rhs)); continue; } // Postfix '%' (precedence 60, below unary prefix 70 and above '^' 50) if (t.kind == TokenKind::Percent) { if (60 < min_bp) break; advance(); lhs = std::make_shared(std::move(lhs)); continue; } int lbp = 0, rbp = 0; BinOp op = BinOp::Add; if (infix_bp(t.kind, lbp, rbp, op)) { if (lbp < min_bp) break; advance(); auto rhs = parse_expr(rbp, allow_union); if (!rhs) return nullptr; lhs = std::make_shared(op, std::move(lhs), std::move(rhs)); continue; } break; } return lhs; } std::shared_ptr parse_nud() { const Token& t = peek(); switch (t.kind) { case TokenKind::Number: { double v = t.num_val; advance(); return std::make_shared(Value::make_number(v)); } case TokenKind::String: { std::string s = t.text; advance(); return std::make_shared(Value::make_string(std::move(s))); } case TokenKind::Bool: { bool b = t.bool_val; advance(); return std::make_shared(Value::make_bool(b)); } case TokenKind::Error: { ErrorCode err = t.err_val; advance(); return std::make_shared(Value::make_error(err)); } case TokenKind::CellRef: { CellRef ref = t.ref_val; advance(); return std::make_shared(std::move(ref)); } case TokenKind::Ident: { std::string name = t.text; advance(); if (peek().kind == TokenKind::LParen) { advance(); // consume '(' std::vector> args; if (peek().kind != TokenKind::RParen) { while (true) { auto arg = parse_expr(0, false); if (!arg) return nullptr; args.push_back(std::move(arg)); if (peek().kind == TokenKind::Comma) { advance(); continue; } break; } } if (peek().kind != TokenKind::RParen) { error_ = "Expected ')' in call to " + name; return nullptr; } advance(); return std::make_shared(std::move(name), std::move(args)); } return std::make_shared(std::move(name)); } case TokenKind::Plus: case TokenKind::Minus: { char op = (t.kind == TokenKind::Plus) ? '+' : '-'; advance(); auto rhs = parse_expr(70, false); if (!rhs) return nullptr; return std::make_shared(op, std::move(rhs)); } case TokenKind::LParen: { advance(); auto inner = parse_expr(0, true); if (!inner) return nullptr; if (peek().kind != TokenKind::RParen) { error_ = "Expected ')'"; return nullptr; } advance(); return inner; } default: error_ = "Unexpected token: " + t.text; return nullptr; } } std::vector tokens_; size_t pos_ = 0; std::string error_; }; } // namespace ParseResult Parser::parse_formula(std::string_view formula) { auto tokens = Lexer::tokenize(formula, true); PrattParser p(std::move(tokens)); return p.parse(); } } // namespace htab