Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

WIP Pattern Matching (Stage 0) #635

Closed
wants to merge 4 commits into from
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
78 changes: 77 additions & 1 deletion ast/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ These are the core Babylon AST node types.
- [NewExpression](#newexpression)
- [SequenceExpression](#sequenceexpression)
- [DoExpression](#doexpression)
- [MatchExpression](#matchexpression)
- [Template Literals](#template-literals)
- [TemplateLiteral](#templateliteral)
- [TaggedTemplateExpression](#taggedtemplateexpression)
Expand Down Expand Up @@ -930,10 +931,85 @@ A sequence expression, i.e., a comma-separated sequence of expressions.
```js
interface DoExpression <: Expression {
type: "DoExpression";
body: BlockStatement
body: BlockStatement;
}
```

## MatchExpression

```js
interface MatchExpression <: Expression {
type: "MatchExpression";
discriminant: Expression;
patterns: [ MatchExpressionClause ];
}
```

### MatchExpressionClause

```js
interface MatchExpressionClause {
type: "MatchExpressionClause";
pattern: MatchExpressionPattern;
body: BlockStatement | Expression;
}
```

### MatchExpressionPatterns

```js
interface MatchExpressionPattern <: Node { }
```

#### ObjectMatchExpressionPattern

```js
interface AssignmentPropertyMatchExpressionPattern <: ObjectProperty {
value: MatchExpressionPattern;
}

interface ObjectPattern <: MatchExpressionPattern {
type: "ObjectMatchExpressionPattern";
properties: [ AssignmentPropertyMatchExpressionPattern | RestElementMatchExpressionPattern ];
}
```

#### ArrayPattern

```js
interface ArrayMatchExpressionPattern <: MatchExpressionPattern {
type: "ArrayPattern";
elements: [ MatchExpressionPattern | null | RestElementMatchExpressionPattern ];
}
```

#### RestElement

```js
interface RestElementMatchExpressionPattern <: MatchExpressionPattern {
type: "RestElement";
argument: MatchExpressionPattern;
}
```

#### AssignmentPattern

```js
interface AssignmentPattern <: MatchExpressionPattern {
type: "AssignmentPattern";
left: MatchExpressionPattern;
right: Expression;
}
```

#### ElseMatchExpressionClause

```js
interface ElseMatchExpressionClause <: MatchExpressionPattern {
type: "ElseMatchExpressionClause";
}
```

# Template Literals

## TemplateLiteral
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"prepublish": "cross-env BABEL_ENV=production yarn run build",
"preversion": "yarn run test && npm run changelog",
"test": "yarn run lint && yarn run flow && yarn run build -- -m && yarn run test-only",
"test-only": "ava",
"test-only": "ava --verbose",
"test-coverage": "cross-env BABEL_ENV=test yarn run build && nyc --reporter=json --reporter=text yarn run test-only",
"watch": "yarn run clean && rollup -c --watch"
},
Expand Down
100 changes: 99 additions & 1 deletion src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,27 @@ export default class ExpressionParser extends LValParser {
tt.parenR,
possibleAsync,
);
this.finishCallExpression(node);

if (base.name === "match" && this.match(tt.braceL)) {
if (!this.hasPlugin("patternMatching")) {
this.raise(
startPos,
"You can only use pattern-matching when the 'patternMatching' plugin is enabled.",
);
}

if (node.arguments.length !== 1) {
// @todo we are probably allowing trailing comma here which is wrong
this.raise(
this.state.start,
"You need to match exactly one expression!",
);
}

return this.parseMatch(node.arguments[0], node);
} else {
this.finishCallExpression(node);
}

if (possibleAsync && this.shouldParseAsyncArrow()) {
state.stop = true;
Expand Down Expand Up @@ -779,6 +799,83 @@ export default class ExpressionParser extends LValParser {
}
}

parseMatch(
discriminant: N.Expression,
previousNode: N.Node,
): N.MatchExpression {
const node = this.startNodeAtNode(previousNode);

node.discriminant = discriminant;
node.patterns = [];

this.expect(tt.braceL);

let hasNext = true;
while (hasNext) {
const patternNode = this.startNode();

// @todo rewrite it to match expr ;)
let pattern;
switch (this.state.type) {
case tt._else:
pattern = this.parseElseMatchClause();
break;

case tt.num:
pattern = this.parseLiteral(this.state.value, "NumericLiteral");
break;

case tt.bigint:
pattern = this.parseLiteral(this.state.value, "BigIntLiteral");
break;

case tt.string:
pattern = this.parseLiteral(this.state.value, "StringLiteral");
break;

case tt._null:
pattern = this.startNode();
this.next();
this.finishNode(node, "NullLiteral");
break;

case tt._true:
case tt._false:
pattern = this.parseBooleanLiteral();
break;

default:
pattern = this.parseBindingAtom();
break;
}
this.expect(tt.colon);

const body = this.match(tt.braceL)
? this.parseBlock(true)
: this.parseMaybeAssign();

this.finishNode(patternNode, "PatternNode");
patternNode.pattern = pattern;
patternNode.body = body;

node.patterns.push(patternNode);

hasNext = this.eat(tt.comma);
}

this.expect(tt.braceR);

this.finishNode(node, "MatchExpression");

return node;
}

parseElseMatchClause(): N.ElseMatchExpressionClause {
const node = this.startNode();
this.next();
return this.finishNode(node, "ElseMatchExpressionClause");
}

parseBooleanLiteral(): N.BooleanLiteral {
const node = this.startNode();
node.value = this.match(tt._true);
Expand Down Expand Up @@ -855,6 +952,7 @@ export default class ExpressionParser extends LValParser {
this.addExtra(node, "rawValue", value);
this.addExtra(node, "raw", this.input.slice(startPos, this.state.end));
node.value = value;

this.next();
return this.finishNode(node, type);
}
Expand Down
1 change: 1 addition & 0 deletions src/tokenizer/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export const keywords = {
typeof: new KeywordTokenType("typeof", { beforeExpr, prefix, startsExpr }),
void: new KeywordTokenType("void", { beforeExpr, prefix, startsExpr }),
delete: new KeywordTokenType("delete", { beforeExpr, prefix, startsExpr }),
match: new KeywordTokenType("match"),
};

// Map keyword names to token types.
Expand Down
16 changes: 16 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ export type SwitchStatement = NodeBase & {
cases: $ReadOnlyArray<SwitchCase>,
};

export type MatchExpression = NodeBase & {
type: "MatchExpression",
discriminant: Expression,
patterns: $ReadOnlyArray<MatchExpressionClause>,
};

export type MatchExpressionClause = NodeBase & {
type: "MatchExpressionClause",
pattern: PatternBase | ElseMatchExpressionClause,
body: BlockStatement | Expression,
};

export type ElseMatchExpressionClause = NodeBase & {
type: "ElseMatchClause",
};

export type SwitchCase = NodeBase & {
type: "SwitchCase",
test: ?Expression,
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/experimental/pattern-matching/basic/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
match (obj) {
{ x }: true,
else: false
}
Loading