-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformula.cpp
More file actions
345 lines (312 loc) · 12 KB
/
formula.cpp
File metadata and controls
345 lines (312 loc) · 12 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "formula.h"
#include "statement.h"
Formula::Value Formula::Evaluate(const ISheet& sheet) const {
return statement.get()->Evaluate(sheet);
}
std::string Formula::GetExpression() const {
return statement.get()->ToString();
}
std::vector<Position> Formula::GetReferencedCells() const {
return references;
}
void Formula::ModifyStatementRowPositions(Ast::Statement* root, int before, int count) {
auto cell_op = dynamic_cast<Ast::CellOperation*>(root);
if (cell_op != nullptr) {
if (cell_op->pos.row >= before) {
cell_op->pos.row += count;
}
return;
}
auto binary_op = dynamic_cast<Ast::BinaryOperation*>(root);
if (binary_op != nullptr) {
ModifyStatementRowPositions(binary_op->lhs.get(), before, count);
ModifyStatementRowPositions(binary_op->rhs.get(), before, count);
return;
}
auto unary_op = dynamic_cast<Ast::UnaryOperation*>(root);
if (unary_op != nullptr) {
ModifyStatementRowPositions(unary_op->rhs.get(), before, count);
return;
}
auto parentesis_op = dynamic_cast<Ast::ParentesisOperation*>(root);
if (parentesis_op != nullptr) {
ModifyStatementRowPositions(parentesis_op->body.get(), before, count);
return;
}
}
Formula::HandlingResult Formula::HandleInsertedRows(int before, int count) {
int n_of_changed = 0;
for (auto& pos : references) {
if (pos.row >= before) {
++n_of_changed;
pos.row += count;
}
}
ModifyStatementRowPositions(statement.get(), before, count);
if (!n_of_changed) {
return HandlingResult::NothingChanged;
}
return HandlingResult::ReferencesRenamedOnly;
}
void Formula::ModifyStatementColumnPositions(Ast::Statement* root, int before, int count) {
auto cell_op = dynamic_cast<Ast::CellOperation*>(root);
if (cell_op != nullptr) {
if (cell_op->pos.col >= before) {
cell_op->pos.col += count;
}
return;
}
auto binary_op = dynamic_cast<Ast::BinaryOperation*>(root);
if (binary_op != nullptr) {
ModifyStatementColumnPositions(binary_op->lhs.get(), before, count);
ModifyStatementColumnPositions(binary_op->rhs.get(), before, count);
return;
}
auto unary_op = dynamic_cast<Ast::UnaryOperation*>(root);
if (unary_op != nullptr) {
ModifyStatementColumnPositions(unary_op->rhs.get(), before, count);
return;
}
auto parentesis_op = dynamic_cast<Ast::ParentesisOperation*>(root);
if (parentesis_op != nullptr) {
ModifyStatementColumnPositions(parentesis_op->body.get(), before, count);
return;
}
}
Formula::HandlingResult Formula::HandleInsertedCols(int before, int count) {
int n_of_changed = 0;
for (auto& pos : references) {
if (pos.col >= before) {
++n_of_changed;
pos.col += count;
}
}
ModifyStatementColumnPositions(statement.get(), before, count);
if (!n_of_changed) {
return HandlingResult::NothingChanged;
}
return HandlingResult::ReferencesRenamedOnly;
}
void Formula::DeleteStatementRowPositions(Ast::Statement* root, int first, int count) {
auto cell_op = dynamic_cast<Ast::CellOperation*>(root);
if (cell_op != nullptr) {
if (cell_op->pos.row >= first + count) {
cell_op->pos.row -= count;
return;
}
if (cell_op->pos.row >= first) {
cell_op->pos.row = -1;
return;
}
}
auto binary_op = dynamic_cast<Ast::BinaryOperation*>(root);
if (binary_op != nullptr) {
DeleteStatementRowPositions(binary_op->lhs.get(), first, count);
DeleteStatementRowPositions(binary_op->rhs.get(), first, count);
return;
}
auto unary_op = dynamic_cast<Ast::UnaryOperation*>(root);
if (unary_op != nullptr) {
DeleteStatementRowPositions(unary_op->rhs.get(), first, count);
return;
}
auto parentesis_op = dynamic_cast<Ast::ParentesisOperation*>(root);
if (parentesis_op != nullptr) {
DeleteStatementRowPositions(parentesis_op->body.get(), first, count);
return;
}
}
void Formula::DeleteStatementColumnPositions(Ast::Statement* root, int first, int count) {
auto cell_op = dynamic_cast<Ast::CellOperation*>(root);
if (cell_op != nullptr) {
if (cell_op->pos.col >= first + count) {
cell_op->pos.col -= count;
return;
}
if (cell_op->pos.col >= first) {
cell_op->pos.col = -1;
return;
}
}
auto binary_op = dynamic_cast<Ast::BinaryOperation*>(root);
if (binary_op != nullptr) {
DeleteStatementColumnPositions(binary_op->lhs.get(), first, count);
DeleteStatementColumnPositions(binary_op->rhs.get(), first, count);
return;
}
auto unary_op = dynamic_cast<Ast::UnaryOperation*>(root);
if (unary_op != nullptr) {
DeleteStatementColumnPositions(unary_op->rhs.get(), first, count);
return;
}
auto parentesis_op = dynamic_cast<Ast::ParentesisOperation*>(root);
if (parentesis_op != nullptr) {
DeleteStatementColumnPositions(parentesis_op->body.get(), first, count);
return;
}
}
Formula::HandlingResult Formula::HandleDeletedRows(int first, int count) {
int n_of_changed = 0;
int n_of_deleted = 0;
for (auto it = begin(references); it != end(references);) {
if (it->row >= first + count) {
++n_of_changed;
it->row -= count;
++it;
continue;
}
if (it->row >= first) {
++n_of_deleted;
it = references.erase(it);
continue;
}
++it;
}
DeleteStatementRowPositions(statement.get(), first, count);
if (n_of_deleted) {
return HandlingResult::ReferencesChanged;
}
else if (n_of_changed) {
return HandlingResult::ReferencesRenamedOnly;
}
else {
return HandlingResult::NothingChanged;
}
}
Formula::HandlingResult Formula::HandleDeletedCols(int first, int count) {
int n_of_changed = 0;
int n_of_deleted = 0;
for (auto it = begin(references); it != end(references);) {
if (it->col >= first + count) {
++n_of_changed;
it->col -= count;
++it;
continue;
}
if (it->col >= first) {
++n_of_deleted;
it = references.erase(it);
continue;
}
++it;
}
DeleteStatementColumnPositions(statement.get(), first, count);
if (n_of_deleted) {
return HandlingResult::ReferencesChanged;
}
else if (n_of_changed) {
return HandlingResult::ReferencesRenamedOnly;
}
else {
return 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 FormulaException("Error when lexing: " + msg);
}
};
class StatementListener : public FormulaListener {
std::stack<std::unique_ptr<Ast::Statement>> statement_stack;
std::vector<Position> references;
virtual void enterMain(FormulaParser::MainContext* /*ctx*/) override {}
virtual void exitMain(FormulaParser::MainContext* /*ctx*/) override {}
virtual void enterUnaryOp(FormulaParser::UnaryOpContext* /*ctx*/) override {}
virtual void exitUnaryOp(FormulaParser::UnaryOpContext* ctx) override {
auto unary_op = std::make_unique<Ast::UnaryOperation>();
unary_op->operation_type = ctx->ADD() ? Ast::OperationType::Add : Ast::OperationType::Sub;
unary_op->rhs = std::move(statement_stack.top());
statement_stack.pop();
statement_stack.push(move(unary_op));
}
virtual void enterParens(FormulaParser::ParensContext* ctx) override {}
virtual void exitParens(FormulaParser::ParensContext* ctx) override {
auto parentesis_op = std::make_unique<Ast::ParentesisOperation>();
parentesis_op->body = std::move(statement_stack.top());
statement_stack.pop();
statement_stack.push(std::move(parentesis_op));
}
virtual void enterLiteral(FormulaParser::LiteralContext* ctx) override {}
virtual void exitLiteral(FormulaParser::LiteralContext* ctx) override {
auto value_op = std::make_unique<Ast::ValueOperation>();
value_op->value = std::stod(ctx->NUMBER()->getText());
statement_stack.push(move(value_op));
}
virtual void enterCell(FormulaParser::CellContext* ctx) override {}
virtual void exitCell(FormulaParser::CellContext* ctx) override {
auto cell_op = std::make_unique<Ast::CellOperation>();
Position pos = Position::FromString(ctx->CELL()->getText());
cell_op.get()->pos = pos;
references.push_back(pos);
statement_stack.push(std::move(cell_op));
}
virtual void enterBinaryOp(FormulaParser::BinaryOpContext* ctx) override {}
virtual void exitBinaryOp(FormulaParser::BinaryOpContext* ctx) override {
auto binary_op = std::make_unique<Ast::BinaryOperation>();
auto rhs = std::move(statement_stack.top());
statement_stack.pop();
auto lhs = std::move(statement_stack.top());
statement_stack.pop();
if (ctx->ADD()) {
binary_op->operation_type = Ast::OperationType::Add;
}
else if (ctx->SUB()) {
binary_op->operation_type = Ast::OperationType::Sub;
}
else if (ctx->DIV()) {
binary_op->operation_type = Ast::OperationType::Div;
}
else {
binary_op->operation_type = Ast::OperationType::Mul;
}
binary_op->rhs = std::move(rhs);
binary_op->lhs = std::move(lhs);
statement_stack.push(std::move(binary_op));
}
virtual void enterEveryRule(antlr4::ParserRuleContext* /*ctx*/) override {}
virtual void exitEveryRule(antlr4::ParserRuleContext* /*ctx*/) override {}
virtual void visitTerminal(antlr4::tree::TerminalNode* /*node*/) override {}
virtual void visitErrorNode(antlr4::tree::ErrorNode* /*node*/) override {}
public:
std::unique_ptr<Ast::Statement> GetResult() {
return std::move(statement_stack.top());
}
std::vector<Position> GetReferences() {
std::sort(begin(references), end(references));
references.erase(std::unique(begin(references), end(references)), end(references));
return std::move(references);
}
};
std::unique_ptr<IFormula> ParseFormula(std::string expression) {
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;
try {
tree = parser.main();
}
catch (const std::exception& ex) {
throw FormulaException(ex.what());
}
StatementListener statement_listener;
antlr4::tree::ParseTreeWalker::DEFAULT.walk(&statement_listener, tree);
auto formula = std::make_unique<Formula>();
formula.get()->statement = statement_listener.GetResult();
formula.get()->references = statement_listener.GetReferences();
for (const auto& ref : formula.get()->references) {
if (!ref.IsValid()) {
throw FormulaException("invalid ref in formula");
}
}
return formula;
}