Annotate more errors with expected token (#172)

Expanding on #150, this allows `unexpected()` to accept the expected token type instead of a message string.
This overload is then used in a couple more places (that independently implement a logic similar to `expect()`'s) to construct an `Unexpected token, expected FOO` message.
This commit is contained in:
Moti Zilberman 2016-10-14 23:10:09 +03:00 committed by Henry Zhu
parent 99fbcefa65
commit d3af158ba5
36 changed files with 45 additions and 41 deletions

View File

@ -27,7 +27,7 @@ pp.expectRelational = function (op) {
if (this.isRelational(op)) {
this.next();
} else {
this.unexpected();
this.unexpected(null, tt.relational);
}
};
@ -67,18 +67,22 @@ pp.isLineTerminator = function () {
// pretend that there is a semicolon at this position.
pp.semicolon = function () {
if (!this.isLineTerminator()) this.unexpected();
if (!this.isLineTerminator()) this.unexpected(null, tt.semi);
};
// Expect a token of a given type. If found, consume it, otherwise,
// raise an unexpected token error at given pos.
pp.expect = function (type, pos) {
return this.eat(type) || this.unexpected(pos, `Unexpected token, expected ${type.label}`);
return this.eat(type) || this.unexpected(pos, type);
};
// Raise an unexpected token error.
// Raise an unexpected token error. Can take the expected token type
// instead of a message string.
pp.unexpected = function (pos, message = "Unexpected token") {
this.raise(pos != null ? pos : this.state.start, message);
pp.unexpected = function (pos, messageOrType = "Unexpected token") {
if (messageOrType && typeof messageOrType === 'object' && messageOrType.label) {
messageOrType = `Unexpected token, expected ${messageOrType.label}`;
}
this.raise(pos != null ? pos : this.state.start, messageOrType);
};

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:6)"
"throws": "Unexpected token, expected ; (1:6)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:6)"
"throws": "Unexpected token, expected ; (1:6)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:2)"
"throws": "Unexpected token, expected ; (1:2)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:2)"
"throws": "Unexpected token, expected ; (1:2)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:2)"
"throws": "Unexpected token, expected ; (1:2)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:8)"
"throws": "Unexpected token, expected ; (1:8)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (2:4)"
"throws": "Unexpected token, expected ; (2:4)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (2:6)"
"throws": "Unexpected token, expected ; (2:6)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:6)"
"throws": "Unexpected token, expected ; (1:6)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:6)"
"throws": "Unexpected token, expected ; (1:6)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:21)"
"throws": "Unexpected token, expected ; (1:21)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (2:4)"
"throws": "Unexpected token, expected ; (2:4)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:14)"
"throws": "Unexpected token, expected ; (1:14)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:30)"
"throws": "Unexpected token, expected ; (1:30)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:20)"
"throws": "Unexpected token, expected ; (1:20)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:20)"
"throws": "Unexpected token, expected ; (1:20)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:28)"
"throws": "Unexpected token, expected ; (1:28)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:6)"
"throws": "Unexpected token, expected ; (1:6)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:6)"
"throws": "Unexpected token, expected ; (1:6)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:2)"
"throws": "Unexpected token, expected ; (1:2)"
}

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:16)"
"throws": "Unexpected token, expected ; (1:16)"
}

View File

@ -1,4 +1,4 @@
{
"throws": "Unexpected token (3:9)",
"throws": "Unexpected token, expected ; (3:9)",
"plugins": ["classProperties"]
}

View File

@ -1,4 +1,4 @@
{
"throws": "Unexpected token (3:8)",
"throws": "Unexpected token, expected ; (3:8)",
"plugins": ["classProperties"]
}