-
Notifications
You must be signed in to change notification settings - Fork 0
/
formula.cpp
96 lines (70 loc) · 2.94 KB
/
formula.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "formula.h"
#include "ast_formula_listener.h"
#include "FormulaLexer.h"
#include <set>
class Formula : public IFormula {
private:
Ast::Tree tree_;
public:
explicit Formula(Ast::Tree tree): tree_(std::move(tree)) {}
Value Evaluate(const ISheet& sheet) const override {
return tree_.Evaluate(sheet);
}
std::string GetExpression() const override {
return tree_.BuildExpression();
}
std::vector<Position> GetReferencedCells() const override {
return tree_.GetReferencedCells();
}
HandlingResult HandleInsertedRows(int before, int count) override {
size_t updated_cell_params = tree_.HandleInsertedRows(before, count);
return updated_cell_params > 0 ? HandlingResult::ReferencesRenamedOnly : HandlingResult::NothingChanged;
}
HandlingResult HandleInsertedCols(int before, int count) override {
size_t updated_cell_params = tree_.HandleInsertedCols(before, count);
return updated_cell_params > 0 ? HandlingResult::ReferencesRenamedOnly : HandlingResult::NothingChanged;
}
HandlingResult HandleDeletedRows(int first, int count) override {
auto result = tree_.HandleDeletedRows(first, count);
if (result.first > 0) return HandlingResult::ReferencesChanged;
return result.second > 0 ? HandlingResult::ReferencesRenamedOnly : HandlingResult::NothingChanged;
}
HandlingResult HandleDeletedCols(int first, int count) override {
auto result = tree_.HandleDeletedCols(first, count);
if (result.first > 0) return HandlingResult::ReferencesChanged;
return result.second > 0 ? HandlingResult::ReferencesRenamedOnly : HandlingResult::NothingChanged;
}
};
class BailErrorListener : public antlr4::BaseErrorListener {
public:
void syntaxError(
antlr4::Recognizer* /* recognizer */,
antlr4::Token* /* offendingSymbol */,
size_t /* line */,
size_t /* charPositionInLine */,
const std::string& msg,
std::exception_ptr /* e */
) override {
throw std::runtime_error("Error when lexing: " + msg);
}
};
std::unique_ptr<IFormula> ParseFormula(std::string expression) {
try {
antlr4::ANTLRInputStream input(expression);
FormulaLexer lexer(&input);
BailErrorListener error_listener;
lexer.removeErrorListeners();
lexer.addErrorListener(&error_listener);
antlr4::CommonTokenStream tokens(&lexer);
FormulaParser parser(&tokens);
auto error_handler = std::make_shared<antlr4::BailErrorStrategy>();
parser.setErrorHandler(error_handler);
parser.removeErrorListeners();
antlr4::tree::ParseTree* tree = parser.main();
AstFormulaListener listener;
antlr4::tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
return std::make_unique<Formula>(listener.GetResult());
} catch (const std::exception& e) {
throw FormulaException(e.what());
}
}