Improved error handling for most calculations

This commit is contained in:
Stefan Zermatten
2021-02-12 11:00:44 +02:00
parent fed87f0a84
commit 2b345c1f77
12 changed files with 80 additions and 201 deletions

View File

@@ -28,10 +28,21 @@ export function parse(string){
if (results.length === 1){
return results[0];
} else if (results.length === 0){
// Valid parsing up until now, but need more. Unexpected end of input.
return null;
// Valid parsing up until now, but need more
throw new EndOfInputError('Unexpected end of input');
} else {
console.warn('Grammar is ambiguous!', {string, results});
return results[0];
}
}
export function prettifyParseError(e){
if (e.message) e = e.message
return e.toString().split('.')[0];
}
class EndOfInputError extends Error {
constructor(message = '', ...args) {
super(message, ...args);
}
}