Skip to content
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

Implement f# pipeline in parser #9450

Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 58 additions & 12 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -331,8 +331,11 @@ export default class ExpressionParser extends LValParser {
const prec = this.state.type.binop;
if (prec != null && (!noIn || !this.match(tt._in))) {
if (prec > minPrec) {
const node = this.startNodeAt(leftStartPos, leftStartLoc);
const operator = this.state.value;
if (operator === "|>" && this.state.inFSharpPipelineDirectBody) {
mAAdhaTTah marked this conversation as resolved.
Show resolved Hide resolved
return left;
}
const node = this.startNodeAt(leftStartPos, leftStartLoc);
node.left = left;
node.operator = operator;
if (
Expand Down Expand Up @@ -406,18 +409,23 @@ 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.withSoloAwaitPermittingContext(() => {
return this.parseFSharpPipelineBody(prec, noIn);
});
}
// falls through

Expand Down Expand Up @@ -1180,9 +1188,11 @@ export default class ExpressionParser extends LValParser {
const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
const oldYieldPos = this.state.yieldPos;
const oldAwaitPos = this.state.awaitPos;
const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
this.state.maybeInArrowParameters = true;
this.state.yieldPos = 0;
this.state.awaitPos = 0;
this.state.inFSharpPipelineDirectBody = false;

const innerStartPos = this.state.start;
const innerStartLoc = this.state.startLoc;
Expand Down Expand Up @@ -1236,6 +1246,7 @@ export default class ExpressionParser extends LValParser {
this.expect(tt.parenR);

this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;

let arrowNode = this.startNodeAt(startPos, startLoc);
if (
Expand Down Expand Up @@ -2116,7 +2127,9 @@ export default class ExpressionParser extends LValParser {
);
}

node.argument = this.parseMaybeUnary();
if (!this.state.soloAwait) {
node.argument = this.parseMaybeUnary();
}
return this.finishNode(node, "AwaitExpression");
}

Expand Down Expand Up @@ -2306,6 +2319,17 @@ export default class ExpressionParser extends LValParser {
}
}

withSoloAwaitPermittingContext<T>(callback: () => T): T {
const outerContextSoloAwaitState = this.state.soloAwait;
this.state.soloAwait = true;

try {
return callback();
} finally {
this.state.soloAwait = outerContextSoloAwaitState;
}
}

// Register the use of a primary topic reference (`#`) within the current
// topic context.
registerTopicReference(): void {
Expand All @@ -2322,4 +2346,26 @@ export default class ExpressionParser extends LValParser {
this.state.topicContext.maxTopicIndex >= 0
);
}

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

const node = this.startNode();
this.state.potentialArrowAt = this.state.start;
const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
this.state.inFSharpPipelineDirectBody = true;

node.body = this.parseExprOp(
this.parseMaybeUnary(),
startPos,
startLoc,
prec,
noIn,
);

this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;

return this.finishNode(node, "PipelineBody");
mAAdhaTTah marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion packages/babel-parser/src/plugin-utils.js
Expand Up @@ -38,7 +38,7 @@ export function getPluginOption(
return null;
}

const PIPELINE_PROPOSALS = ["minimal", "smart"];
const PIPELINE_PROPOSALS = ["minimal", "smart", "fsharp"];

export function validatePlugins(plugins: PluginList) {
if (hasPlugin(plugins, "decorators")) {
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-parser/src/tokenizer/state.js
Expand Up @@ -77,6 +77,10 @@ export default class State {
maxTopicIndex: null,
};

// For the F# plugin
soloAwait: boolean = false;
inFSharpPipelineDirectBody: boolean = false;

// Check whether we are in a (nested) class or not.
classLevel: number = 0;

Expand Down
@@ -1,4 +1,4 @@
{
"plugins": [["pipelineOperator", { "proposal": "invalid" }]],
"throws": "'pipelineOperator' requires 'proposal' option whose value should be one of: 'minimal', 'smart'"
"throws": "'pipelineOperator' requires 'proposal' option whose value should be one of: 'minimal', 'smart', 'fsharp'"
}
@@ -0,0 +1 @@
x => x |> [y => y + 1] |> z => z * 2
mAAdhaTTah marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,3 @@
{
"plugins": [["pipelineOperator", { "proposal": "fsharp" }]]
}