Skip to content

Parsing for safe call expressions, existential expressions #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
8 changes: 8 additions & 0 deletions src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,11 @@ pp.parseSubscripts = function (base, startPos, startLoc, noCalls) {
this.unexpected();
}
base = this.finishNode(node, "SafeMemberExpression");
} else if (this.hasPlugin("lightscript") && this.match(tt.question) && this.state.lastTokEnd === (this.state.pos - 1)) {
// A `?` immediately following an expr (no whitespace) could be a
// safecall, ternary, or existential.
const next = this.parseQuestionSubscript(base, startPos, startLoc, noCalls);
if (next) base = next; else return base;
} else if (this.hasPlugin("lightscript") && !noCalls && this.match(tt.tilde)) {
if (this.isNonIndentedBreakFrom(startPos)) {
this.unexpected(null, "Indentation required.");
Expand All @@ -429,6 +434,9 @@ pp.parseSubscripts = function (base, startPos, startLoc, noCalls) {
const right = this.match(tt._this) ? this.parseExprAtom() : this.parseIdentifier();
node.right = this.parseSubscripts(right, this.state.start, this.state.startLoc, true);

// Allow safe tilde calls (a~b?(c))
if (this.eat(tt.question)) node.safe = true;

this.expect(tt.parenL);
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
base = this.finishNode(node, "TildeCallExpression");
Expand Down
19 changes: 16 additions & 3 deletions src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,11 +834,18 @@ pp.flowParseTypeAnnotatableIdentifier = function () {
};

pp.typeCastToParameter = function (node) {
node.expression.typeAnnotation = node.typeAnnotation;
// Lightscript: Existential expressions look like optional flow types
// Unwrap them
let innerNode = node.expression;
if (this.hasPlugin("lightscript") && innerNode.type === "ExistentialExpression") {
innerNode = this.existentialToParameter(innerNode);
}

innerNode.typeAnnotation = node.typeAnnotation;

return this.finishNodeAt(
node.expression,
node.expression.type,
innerNode,
innerNode.type,
node.typeAnnotation.end,
node.typeAnnotation.loc.end
);
Expand Down Expand Up @@ -1049,6 +1056,9 @@ export default function (instance) {
return function (node, isBinding, contextDescription) {
if (node.type === "TypeCastExpression") {
return inner.call(this, this.typeCastToParameter(node), isBinding, contextDescription);
} else if (this.hasPlugin("lightscript") && node.type === "ExistentialExpression") {
// Existential expression looks like an optional flow parameter
return inner.call(this, this.existentialToParameter(node), isBinding, contextDescription);
} else {
return inner.call(this, node, isBinding, contextDescription);
}
Expand All @@ -1062,6 +1072,9 @@ export default function (instance) {
const expr = exprList[i];
if (expr && expr.type === "TypeCastExpression") {
exprList[i] = this.typeCastToParameter(expr);
} else if (this.hasPlugin("lightscript") && expr && expr.type === "ExistentialExpression") {
// Existential expression looks like an optional flow parameter
exprList[i] = this.existentialToParameter(expr);
}
}
return inner.call(this, exprList, isBinding, contextDescription);
Expand Down
49 changes: 49 additions & 0 deletions src/plugins/lightscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,55 @@ pp.isBitwiseOp = function () {
);
};

pp.parseSafeCall = function(expr, startPos, startLoc) {
this.expect(tt.parenL);
const node = this.startNodeAt(startPos, startLoc);
node.callee = expr;
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
node.safe = true;
return this.finishNode(node, "CallExpression");
};

pp.parseExistential = function(expr, startPos, startLoc) {
const node = this.startNodeAt(startPos, startLoc);
node.argument = expr;
return this.finishNode(node, "ExistentialExpression");
};

pp.parseQuestionSubscript = function(lhs, startPos, startLoc, noCalls) {
const questionPos = this.state.pos;
const questionLine = this.state.curLine;
const state = this.state.clone();

this.eat(tt.question);

// `?(` = safecall or poorly-formatted ternary
// TODO: lint rule for ternaries recommending space after ?
if (!noCalls && this.match(tt.parenL) && this.state.pos === (questionPos + 1)) {
try {
return this.parseSafeCall(lhs, startPos, startLoc);
} catch (e) {
this.state = state;
this.eat(tt.question);
}
}

// If the next token startsExpr, this is a ternary -- unwind recursive descent
if (this.state.type.startsExpr && this.state.curLine === questionLine) {
this.state = state;
return null;
}

// Otherwise this is an existential
return this.parseExistential(lhs, startPos, startLoc);
};

// Convert an existential to an optional flow parameter.
// Resolves grammar ambiguity in arrow function arg lists.
pp.existentialToParameter = function(node) {
node.argument.optional = true;
return node.argument;
};

export default function (instance) {

Expand Down
120 changes: 120 additions & 0 deletions test/fixtures/flow/optional-type/2/expected.lightscript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"type": "File",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"program": {
"type": "Program",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 6,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 14
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "f"
},
"name": "f"
},
"init": {
"type": "ExistentialExpression",
"start": 11,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 13
}
},
"argument": {
"type": "Identifier",
"start": 11,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 12
},
"identifierName": "x"
},
"name": "x"
},
"extra": {
"parenthesized": true,
"parenStart": 10
}
}
}
],
"kind": "const"
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x?
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"type": "File",
"start": 0,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
}
},
"program": {
"type": "Program",
"start": 0,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
}
},
"expression": {
"type": "ExistentialExpression",
"start": 0,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
}
},
"argument": {
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "x"
},
"name": "x"
}
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a?
b:
c
Loading