Skip to content

Commit

Permalink
Initial implementation of basic F# pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
mAAdhaTTah committed Feb 3, 2019
1 parent e123ff8 commit 2fd61a8
Show file tree
Hide file tree
Showing 15 changed files with 807 additions and 13 deletions.
58 changes: 48 additions & 10 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -321,8 +321,16 @@ export default class ExpressionParser extends LValParser {

if (op === tt.pipeline) {
this.expectPlugin("pipelineOperator");
const wasInPipeline = this.state.inPipeline;
this.state.inPipeline = true;
this.checkPipelineAtInfixOperator(left, leftStartPos);

if (
this.getPluginOption("pipelineOperator", "proposal") === "fsharp" &&
!wasInPipeline
) {
node.left = this.makePipelineHead(left);
}
} else if (op === tt.nullishCoalescing) {
this.expectPlugin("nullishCoalescingOperator");
}
Expand Down Expand Up @@ -376,18 +384,21 @@ export default class ExpressionParser extends LValParser {
prec: number,
noIn: ?boolean,
): N.Expression {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
switch (op) {
case tt.pipeline:
if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
return this.withTopicPermittingContext(() => {
return this.parseSmartPipelineBody(
this.parseExprOpBaseRightExpr(op, prec, noIn),
startPos,
startLoc,
);
});
switch (this.getPluginOption("pipelineOperator", "proposal")) {
case "smart":
return this.withTopicPermittingContext(() => {
return this.parseSmartPipelineBody(
this.parseExprOpBaseRightExpr(op, prec, noIn),
startPos,
startLoc,
);
});
case "fsharp":
return this.parseFSharpPipelineBody(op, prec, noIn);
}
// falls through

Expand Down Expand Up @@ -2271,4 +2282,31 @@ export default class ExpressionParser extends LValParser {
this.state.topicContext.maxTopicIndex >= 0
);
}

makePipelineHead(left: N.Expression): N.Expression {
const node = this.startNode();
node.head = left;

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

parseFSharpPipelineBody(
op: TokenType,
prec: number,
noIn: ?boolean,
): N.Expression {
const startPos = this.state.start;
const startLoc = this.state.startLoc;

const node = this.startNodeAt(this.state.start, this.state.startLoc);
node.body = this.parseExprOp(
this.parseMaybeUnary(),
startPos,
startLoc,
op.rightAssociative ? prec - 1 : prec,
noIn,
);

return this.finishNode(node, "PipelineBody");
}
}
4 changes: 4 additions & 0 deletions packages/babel-parser/src/types.js
Expand Up @@ -568,6 +568,10 @@ export type SequenceExpression = NodeBase & {

// Pipelines

export type PipelineHead = NodeBase & {
type: "PipelineHead",
};

export type PipelineBody = NodeBase & {
type: "PipelineBody",
};
Expand Down
@@ -1,6 +1,4 @@
{
"plugins": [
["pipelineOperator", { "proposal": "invalid" }]
],
"plugins": [["pipelineOperator", { "proposal": "invalid" }]],
"throws": "'pipelineOperator' requires 'proposal' option whose value should be one of: 'minimal', 'smart', 'fsharp'"
}
@@ -0,0 +1 @@
x => x |> inc |> double
@@ -0,0 +1,3 @@
{
"plugins": [["pipelineOperator", { "proposal": "fsharp" }]]
}
@@ -0,0 +1,215 @@
{
"type": "File",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"program": {
"type": "Program",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "x"
},
"name": "x"
}
],
"body": {
"type": "BinaryExpression",
"start": 5,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 23
}
},
"left": {
"type": "BinaryExpression",
"start": 5,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 13
}
},
"left": {
"type": "PipelineHead",
"start": 7,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 6
}
},
"head": {
"type": "Identifier",
"start": 5,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 6
},
"identifierName": "x"
},
"name": "x"
}
},
"operator": "|>",
"right": {
"type": "PipelineBody",
"start": 10,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 13
}
},
"body": {
"type": "Identifier",
"start": 10,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "inc"
},
"name": "inc"
}
}
},
"operator": "|>",
"right": {
"type": "PipelineBody",
"start": 17,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 23
}
},
"body": {
"type": "Identifier",
"start": 17,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 23
},
"identifierName": "double"
},
"name": "double"
}
}
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
x => x |> inc |> double
@@ -0,0 +1,3 @@
{
"plugins": [["pipelineOperator", { "proposal": "fsharp" }]]
}

0 comments on commit 2fd61a8

Please sign in to comment.