Skip to content

Commit

Permalink
DepParser now throws FormulaError.
Browse files Browse the repository at this point in the history
  • Loading branch information
LesterLyu committed Aug 4, 2020
1 parent 2726f64 commit 5d18640
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 56 deletions.
21 changes: 10 additions & 11 deletions grammar/dependency/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {FormulaHelpers} = require('../../formulas/helpers');
const {Parser} = require('../parsing');
const lexer = require('../lexing');
const Utils = require('./utils');
const {formatChevrotainError} = require('../utils');

class DepParser {

Expand Down Expand Up @@ -139,29 +140,27 @@ class DepParser {
* Parse an excel formula and return the dependencies
* @param {string} inputText
* @param {{row: number, col: number, sheet: string}} position
* @param {boolean} [printOnError=true] print errors if true.
* @param {boolean} [ignoreError=true] print errors if true.
* @returns {Array.<{}>}
*/
parse(inputText, position, printOnError = true) {
parse(inputText, position, ignoreError = false) {
if (inputText.length === 0) throw Error('Input must not be empty.');
this.data = [];
this.position = position;
const lexResult = lexer.lex(inputText);
this.parser.input = lexResult.tokens;
try {
let res = this.parser.formulaWithBinaryOp();
const res = this.parser.formulaWithBinaryOp();
this.checkFormulaResult(res);
if (this.parser.errors.length > 0 && printOnError) {
const error = this.parser.errors[0];
console.warn(`Error in ${inputText}:`);
console.warn(error)
}
} catch (e) {
if (printOnError) {
console.warn(`Error in ${inputText}:`);
console.warn(e);
if (!ignoreError) {
throw FormulaError.ERROR(e.message, e);
}
}
if (this.parser.errors.length > 0 && !ignoreError) {
const error = this.parser.errors[0];
throw formatChevrotainError(error, inputText);
}

return this.data;
}
Expand Down
112 changes: 67 additions & 45 deletions test/grammar/errors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const expect = require('chai').expect;
const FormulaError = require('../../formulas/error');
const {FormulaParser} = require('../../grammar/hooks');
const {DepParser} = require('../../grammar/dependency/hooks');
const {MAX_ROW, MAX_COLUMN} = require('../../index');

const parser = new FormulaParser({
Expand All @@ -25,56 +26,69 @@ const parser = new FormulaParser({
}
);

const depParser = new DepParser({
onVariable: variable => {
return 'aaaa' === variable ? {from: {row: 1, col: 1}, to: {row: 2, col: 2}} : {row: 1, col: 1};
}
});

const parsers = [parser, depParser];
const names = ['', ' (DepParser)']

const position = {row: 1, col: 1, sheet: 'Sheet1'};

describe('#ERROR! Error handling', () => {
it('should handle NotAllInputParsedException', function () {
try {
parser.parse('SUM(1))', position);
} catch (e) {
expect(e).to.be.instanceof(FormulaError);
expect(e.details.errorLocation.line).to.eq(1);
expect(e.details.errorLocation.column).to.eq(7);
expect(e.name).to.eq('#ERROR!');
expect(e.details.name).to.eq('NotAllInputParsedException');
return;
}
throw Error('Should not reach here.');
});
parsers.forEach((parser, idx) => {
it('should handle NotAllInputParsedException' + names[idx], function () {
try {
parser.parse('SUM(1))', position);
} catch (e) {
expect(e).to.be.instanceof(FormulaError);
expect(e.details.errorLocation.line).to.eq(1);
expect(e.details.errorLocation.column).to.eq(7);
expect(e.name).to.eq('#ERROR!');
expect(e.details.name).to.eq('NotAllInputParsedException');
return;
}
throw Error('Should not reach here.');
});

it('should handle lexing error', function () {
try {
parser.parse('SUM(1)$', position);
} catch (e) {
expect(e).to.be.instanceof(FormulaError);
expect(e.details.errorLocation.line).to.eq(1);
expect(e.details.errorLocation.column).to.eq(7);
expect(e.name).to.eq('#ERROR!');
return;
}
throw Error('Should not reach here.');
});
it('should handle lexing error' + names[idx], function () {
try {
parser.parse('SUM(1)$', position);
} catch (e) {
expect(e).to.be.instanceof(FormulaError);
expect(e.details.errorLocation.line).to.eq(1);
expect(e.details.errorLocation.column).to.eq(7);
expect(e.name).to.eq('#ERROR!');
return;
}
throw Error('Should not reach here.');

it('should handle Parser error []', function () {
try {
parser.parse('SUM([Sales.xlsx]Jan!B2:B5)', position);
} catch (e) {
expect(e).to.be.instanceof(FormulaError);
expect(e.name).to.eq('#ERROR!');
return;
}
throw Error('Should not reach here.');
});
});

it('should handle Parser error', function () {
try {
parser.parse('SUM(B2:B5, "123"+)', position);
} catch (e) {
expect(e).to.be.instanceof(FormulaError);
expect(e.name).to.eq('#ERROR!');
return;
}
throw Error('Should not reach here.');
it('should handle Parser error []' + names[idx], function () {
try {
parser.parse('SUM([Sales.xlsx]Jan!B2:B5)', position);
} catch (e) {
expect(e).to.be.instanceof(FormulaError);
expect(e.name).to.eq('#ERROR!');
return;
}
throw Error('Should not reach here.');
});

it('should handle Parser error' + names[idx], function () {
try {
parser.parse('SUM(B2:B5, "123"+)', position);
} catch (e) {
expect(e).to.be.instanceof(FormulaError);
expect(e.name).to.eq('#ERROR!');
return;
}
throw Error('Should not reach here.');

});
});

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

});

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

it('should not throw error when ignoreError = true (DepParser)', function () {
try {
depParser.parse('SUM(*()', position, true);
} catch (e) {
throw Error('Should not reach here.');
}
});
});

0 comments on commit 5d18640

Please sign in to comment.