Skip to content

Commit 5d18640

Browse files
committed
DepParser now throws FormulaError.
1 parent 2726f64 commit 5d18640

File tree

2 files changed

+77
-56
lines changed

2 files changed

+77
-56
lines changed

grammar/dependency/hooks.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {FormulaHelpers} = require('../../formulas/helpers');
33
const {Parser} = require('../parsing');
44
const lexer = require('../lexing');
55
const Utils = require('./utils');
6+
const {formatChevrotainError} = require('../utils');
67

78
class DepParser {
89

@@ -139,29 +140,27 @@ class DepParser {
139140
* Parse an excel formula and return the dependencies
140141
* @param {string} inputText
141142
* @param {{row: number, col: number, sheet: string}} position
142-
* @param {boolean} [printOnError=true] print errors if true.
143+
* @param {boolean} [ignoreError=true] print errors if true.
143144
* @returns {Array.<{}>}
144145
*/
145-
parse(inputText, position, printOnError = true) {
146+
parse(inputText, position, ignoreError = false) {
146147
if (inputText.length === 0) throw Error('Input must not be empty.');
147148
this.data = [];
148149
this.position = position;
149150
const lexResult = lexer.lex(inputText);
150151
this.parser.input = lexResult.tokens;
151152
try {
152-
let res = this.parser.formulaWithBinaryOp();
153+
const res = this.parser.formulaWithBinaryOp();
153154
this.checkFormulaResult(res);
154-
if (this.parser.errors.length > 0 && printOnError) {
155-
const error = this.parser.errors[0];
156-
console.warn(`Error in ${inputText}:`);
157-
console.warn(error)
158-
}
159155
} catch (e) {
160-
if (printOnError) {
161-
console.warn(`Error in ${inputText}:`);
162-
console.warn(e);
156+
if (!ignoreError) {
157+
throw FormulaError.ERROR(e.message, e);
163158
}
164159
}
160+
if (this.parser.errors.length > 0 && !ignoreError) {
161+
const error = this.parser.errors[0];
162+
throw formatChevrotainError(error, inputText);
163+
}
165164

166165
return this.data;
167166
}

test/grammar/errors.js

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const expect = require('chai').expect;
22
const FormulaError = require('../../formulas/error');
33
const {FormulaParser} = require('../../grammar/hooks');
4+
const {DepParser} = require('../../grammar/dependency/hooks');
45
const {MAX_ROW, MAX_COLUMN} = require('../../index');
56

67
const parser = new FormulaParser({
@@ -25,56 +26,69 @@ const parser = new FormulaParser({
2526
}
2627
);
2728

29+
const depParser = new DepParser({
30+
onVariable: variable => {
31+
return 'aaaa' === variable ? {from: {row: 1, col: 1}, to: {row: 2, col: 2}} : {row: 1, col: 1};
32+
}
33+
});
34+
35+
const parsers = [parser, depParser];
36+
const names = ['', ' (DepParser)']
37+
2838
const position = {row: 1, col: 1, sheet: 'Sheet1'};
2939

3040
describe('#ERROR! Error handling', () => {
31-
it('should handle NotAllInputParsedException', function () {
32-
try {
33-
parser.parse('SUM(1))', position);
34-
} catch (e) {
35-
expect(e).to.be.instanceof(FormulaError);
36-
expect(e.details.errorLocation.line).to.eq(1);
37-
expect(e.details.errorLocation.column).to.eq(7);
38-
expect(e.name).to.eq('#ERROR!');
39-
expect(e.details.name).to.eq('NotAllInputParsedException');
40-
return;
41-
}
42-
throw Error('Should not reach here.');
43-
});
41+
parsers.forEach((parser, idx) => {
42+
it('should handle NotAllInputParsedException' + names[idx], function () {
43+
try {
44+
parser.parse('SUM(1))', position);
45+
} catch (e) {
46+
expect(e).to.be.instanceof(FormulaError);
47+
expect(e.details.errorLocation.line).to.eq(1);
48+
expect(e.details.errorLocation.column).to.eq(7);
49+
expect(e.name).to.eq('#ERROR!');
50+
expect(e.details.name).to.eq('NotAllInputParsedException');
51+
return;
52+
}
53+
throw Error('Should not reach here.');
54+
});
4455

45-
it('should handle lexing error', function () {
46-
try {
47-
parser.parse('SUM(1)$', position);
48-
} catch (e) {
49-
expect(e).to.be.instanceof(FormulaError);
50-
expect(e.details.errorLocation.line).to.eq(1);
51-
expect(e.details.errorLocation.column).to.eq(7);
52-
expect(e.name).to.eq('#ERROR!');
53-
return;
54-
}
55-
throw Error('Should not reach here.');
56-
});
56+
it('should handle lexing error' + names[idx], function () {
57+
try {
58+
parser.parse('SUM(1)$', position);
59+
} catch (e) {
60+
expect(e).to.be.instanceof(FormulaError);
61+
expect(e.details.errorLocation.line).to.eq(1);
62+
expect(e.details.errorLocation.column).to.eq(7);
63+
expect(e.name).to.eq('#ERROR!');
64+
return;
65+
}
66+
throw Error('Should not reach here.');
5767

58-
it('should handle Parser error []', function () {
59-
try {
60-
parser.parse('SUM([Sales.xlsx]Jan!B2:B5)', position);
61-
} catch (e) {
62-
expect(e).to.be.instanceof(FormulaError);
63-
expect(e.name).to.eq('#ERROR!');
64-
return;
65-
}
66-
throw Error('Should not reach here.');
67-
});
68+
});
6869

69-
it('should handle Parser error', function () {
70-
try {
71-
parser.parse('SUM(B2:B5, "123"+)', position);
72-
} catch (e) {
73-
expect(e).to.be.instanceof(FormulaError);
74-
expect(e.name).to.eq('#ERROR!');
75-
return;
76-
}
77-
throw Error('Should not reach here.');
70+
it('should handle Parser error []' + names[idx], function () {
71+
try {
72+
parser.parse('SUM([Sales.xlsx]Jan!B2:B5)', position);
73+
} catch (e) {
74+
expect(e).to.be.instanceof(FormulaError);
75+
expect(e.name).to.eq('#ERROR!');
76+
return;
77+
}
78+
throw Error('Should not reach here.');
79+
});
80+
81+
it('should handle Parser error' + names[idx], function () {
82+
try {
83+
parser.parse('SUM(B2:B5, "123"+)', position);
84+
} catch (e) {
85+
expect(e).to.be.instanceof(FormulaError);
86+
expect(e.name).to.eq('#ERROR!');
87+
return;
88+
}
89+
throw Error('Should not reach here.');
90+
91+
});
7892
});
7993

8094
it('should handle error from functions', function () {
@@ -87,6 +101,7 @@ describe('#ERROR! Error handling', () => {
87101
return;
88102
}
89103
throw Error('Should not reach here.');
104+
90105
});
91106

92107
it('should handle errors in async', async function () {
@@ -99,5 +114,12 @@ describe('#ERROR! Error handling', () => {
99114
}
100115
throw Error('Should not reach here.');
101116
});
102-
});
103117

118+
it('should not throw error when ignoreError = true (DepParser)', function () {
119+
try {
120+
depParser.parse('SUM(*()', position, true);
121+
} catch (e) {
122+
throw Error('Should not reach here.');
123+
}
124+
});
125+
});

0 commit comments

Comments
 (0)