Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,23 @@ const empty = [];
pp.parseTryStatement = function (node) {
this.next();

let indentLevel, isColon;
if (this.hasPlugin("lightscript")) {
indentLevel = this.state.indentLevel;
isColon = this.match(tt.colon);
if (isColon) this.pushBlockState("try", indentLevel);
}

node.block = this.parseBlock();
node.handler = null;

if (this.match(tt._catch)) {
const shouldParseCatch = this.match(tt._catch) && (
!this.hasPlugin("lightscript") ||
indentLevel === this.state.indentLevel ||
(!isColon && !this.matchBlockState("try", this.state.indentLevel))
);

if (shouldParseCatch) {
const clause = this.startNode();
this.next();

Expand All @@ -457,6 +470,11 @@ pp.parseTryStatement = function (node) {
this.checkLVal(clause.param, true, Object.create(null), "catch clause");
if (this.hasPlugin("lightscript")) {
this.expectParenFreeBlockStart(clause);
if (isColon) {
this.check(tt.colon);
} else {
this.check(tt.braceL);
}
} else {
this.expect(tt.parenR);
}
Expand All @@ -465,12 +483,34 @@ pp.parseTryStatement = function (node) {
}

node.guardedHandlers = empty;
node.finalizer = this.eat(tt._finally) ? this.parseBlock() : null;


if (this.hasPlugin("lightscript")) {
const shouldParseFinally = this.match(tt._finally) && (
indentLevel === this.state.indentLevel ||
(!isColon && !this.matchBlockState("try", this.state.indentLevel))
);
if (shouldParseFinally) {
this.next();
if (isColon) {
this.check(tt.colon);
} else {
this.check(tt.braceL);
}
node.finalizer = this.parseBlock();
} else {
node.finalizer = null;
}
} else {
node.finalizer = this.eat(tt._finally) ? this.parseBlock() : null;
}

if (!node.handler && !node.finalizer) {
this.raise(node.start, "Missing catch or finally clause");
}

if (this.hasPlugin("lightscript") && isColon) this.popBlockState();

return this.finishNode(node, "TryStatement");
};

Expand Down
27 changes: 16 additions & 11 deletions src/plugins/lightscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ pp.parseMultilineWhiteBlock = function(node, indentLevel) {

pp.parseWhiteBlock = function (isExpression?) {
const node = this.startNode(), indentLevel = this.state.indentLevel;

if (!this.eat(tt.colon)) this.unexpected(null, "Whitespace Block must start with a colon or arrow");
if (!this.eat(tt.colon)) this.unexpected(this.state.lastTokEnd, tt.colon);

// Oneline whiteblock
if (!this.isLineBreak()) {
Expand Down Expand Up @@ -383,12 +382,18 @@ pp.popBlockState = function() {

// c/p parseIfStatement

pp.parseIf = function (node, isExpression) {
pp.parseIf = function (node, isExpression, requireColon = null) {
const indentLevel = this.state.indentLevel;
this.next();
node.test = this.parseParenExpression();

const isColon = this.match(tt.colon);
const isColon = requireColon
? this.check(tt.colon)
: this.match(tt.colon);

// colon not allowed, parent `if` didn't use one.
if (isColon && requireColon === false) this.expect(tt.braceL);

if (isColon) this.pushBlockState("if", indentLevel);

if (isExpression) {
Expand Down Expand Up @@ -425,28 +430,28 @@ pp.parseIfAlternate = function (node, isExpression, ifIsWhiteBlock, ifIndentLeve
}

if (this.match(tt._elif)) {
return this.parseIf(this.startNode(), isExpression);
return this.parseIf(this.startNode(), isExpression, ifIsWhiteBlock);
}

if (this.eat(tt._else)) {
if (this.match(tt._if)) {
if (this.isLineBreak()) {
this.unexpected(this.state.lastTokEnd, "Illegal newline.");
}
return this.parseIf(this.startNode(), isExpression);
return this.parseIf(this.startNode(), isExpression, ifIsWhiteBlock);
}

if (this.isLineBreak()) {
this.unexpected(this.state.lastTokEnd, tt.colon);
if (ifIsWhiteBlock) {
return this.parseWhiteBlock(isExpression);
} else if (this.match(tt.colon)) {
this.expect(tt.braceL);
}

if (isExpression) {
if (this.match(tt.braceL)) {
return this.parseBlock(false);
} else if (!this.match(tt.colon)) {
return this.parseMaybeAssign();
} else {
return this.parseWhiteBlock(true);
return this.parseMaybeAssign();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export default class Tokenizer {
// whitespace and comments, and.

skipSpace() {
let isNewLine = false; // for lightscript
let isNewLine = this.state.pos === 0; // for lightscript
loop: while (this.state.pos < this.input.length) {
const ch = this.input.charCodeAt(this.state.pos);
switch (ch) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
{
"type": "File",
"start": 0,
"end": 28,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 8
"line": 4,
"column": 5
}
},
"program": {
"type": "Program",
"start": 0,
"end": 28,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 8
"line": 4,
"column": 5
}
},
"sourceType": "script",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 28,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 8
"line": 4,
"column": 5
}
},
"kind": "const",
Expand All @@ -50,15 +50,15 @@
{
"type": "VariableDeclarator",
"start": 0,
"end": 28,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 8
"line": 4,
"column": 5
}
},
"id": {
Expand All @@ -81,15 +81,15 @@
"init": {
"type": "IfExpression",
"start": 4,
"end": 28,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 3,
"column": 8
"line": 4,
"column": 5
}
},
"test": {
Expand Down Expand Up @@ -147,23 +147,23 @@
},
"consequent": {
"type": "BlockStatement",
"start": 12,
"end": 19,
"start": 13,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 12
"column": 13
},
"end": {
"line": 2,
"column": 5
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 16,
"end": 19,
"start": 17,
"end": 20,
"loc": {
"start": {
"line": 2,
Expand All @@ -176,8 +176,8 @@
},
"expression": {
"type": "StringLiteral",
"start": 16,
"end": 19,
"start": 17,
"end": 20,
"loc": {
"start": {
"line": 2,
Expand All @@ -198,21 +198,21 @@
],
"directives": [],
"extra": {
"curly": false
"curly": true
}
},
"alternate": {
"type": "StringLiteral",
"start": 25,
"end": 28,
"start": 30,
"end": 33,
"loc": {
"start": {
"line": 3,
"column": 5
"line": 4,
"column": 2
},
"end": {
"line": 3,
"column": 8
"line": 4,
"column": 5
}
},
"extra": {
Expand Down

This file was deleted.

Loading