Skip to content

Commit

Permalink
Improved the SyntaxError exeptions thrown by the Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
davafons committed Apr 16, 2019
1 parent 779f18c commit c97545e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions examples/input-after-end-error.egg
@@ -0,0 +1,4 @@
do(
define(x, 3),
print(x)
) moreinput
15 changes: 13 additions & 2 deletions lib/parse.js
Expand Up @@ -25,7 +25,7 @@ class Parser {
const result = this.parseExpression();

if(this.lookahead !== null) {
throw new SyntaxError(`Unexpected input after the end of parsing`);
throw this._parserSyntaxError("Unexpected input after the end of file.");
}

return result;
Expand All @@ -49,6 +49,9 @@ class Parser {
let expr = {type: 'word', name: this.lookahead.value };

return this.parseApply(expr);

} else {
throw this._parserSyntaxError("Unrecognized token while parsing the expression.");
}
}

Expand Down Expand Up @@ -76,9 +79,10 @@ class Parser {

if(!this.lookahead || (this.lookahead.type !== 'COMMA' &&
this.lookahead.type !== 'RP')) {
throw new SyntaxError(`Missing , or )`);
throw this._parserSyntaxError(`Missing , or ) after the expression`);
}

// Don't consume RPs as argument tokens
if(this.lookahead && this.lookahead.type !== 'RP') {
this.lookahead = this.getToken();
}
Expand Down Expand Up @@ -128,6 +132,13 @@ class Parser {
tokenRegex.WHITES.lastIndex = this.lastIndex;
tokenRegex.NEWLINE.lastIndex = this.lastIndex;
}

_parserSyntaxError(message) {
let start = (this.lookahead) ? this.lookahead.start : this.lastIndex;

return new SyntaxError(`Parser::Line ${this.line}::Col ${start}\n${message}\n${this.program.slice(start-1, start + 10)}`);
}

}

Parser.TOKENS = [tokenRegex.NUMBER, tokenRegex.STRING, tokenRegex.WORD,
Expand Down

0 comments on commit c97545e

Please sign in to comment.