Skip to content

Commit

Permalink
Add parser support for the "regexp unicode sets" proposal (#14086)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Dec 31, 2021
1 parent 2810dc6 commit 9a41623
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/babel-parser/src/parser/error-message.js
Expand Up @@ -76,6 +76,8 @@ export const ErrorMessages = makeErrorTemplates(
ImportCallArity: "`import()` requires exactly %0.",
ImportCallNotNewExpression: "Cannot use new with import(...).",
ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
IncompatibleRegExpUVFlags:
"The 'u' and 'v' regular expression flags cannot be enabled at the same time.",
InvalidBigIntLiteral: "Invalid BigIntLiteral.",
InvalidCodePoint: "Code point out of bounds.",
InvalidCoverInitializedName: "Invalid shorthand property initializer.",
Expand Down
12 changes: 12 additions & 0 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -33,6 +33,8 @@ const VALID_REGEX_FLAGS = new Set([
charCodes.lowercaseY,
charCodes.lowercaseU,
charCodes.lowercaseD,
// This is only valid when using the regexpUnicodeSets plugin
charCodes.lowercaseV,
]);

// The following character codes are forbidden from being
Expand Down Expand Up @@ -1071,6 +1073,16 @@ export default class Tokenizer extends ParserErrors {
const char = String.fromCharCode(cp);

if (VALID_REGEX_FLAGS.has(cp)) {
if (cp === charCodes.lowercaseV) {
this.expectPlugin("regexpUnicodeSets", pos + 1);
if (mods.includes("u")) {
this.raise(pos + 1, Errors.IncompatibleRegExpUVFlags);
}
} else if (cp === charCodes.lowercaseU) {
if (mods.includes("v")) {
this.raise(pos + 1, Errors.IncompatibleRegExpUVFlags);
}
}
if (mods.includes(char)) {
this.raise(pos + 1, Errors.DuplicateRegExpFlags);
}
Expand Down
@@ -0,0 +1 @@
/a/v;
@@ -0,0 +1,3 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: \"regexpUnicodeSets\". (1:4)"
}
@@ -0,0 +1 @@
/a/v;
@@ -0,0 +1,26 @@
{
"type": "File",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"program": {
"type": "Program",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"expression": {
"type": "RegExpLiteral",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"extra": {
"raw": "/a/v"
},
"pattern": "a",
"flags": "v"
}
}
],
"directives": []
}
}
@@ -0,0 +1,3 @@
{
"plugins": ["regexpUnicodeSets"]
}
@@ -0,0 +1 @@
/a/ugv;
@@ -0,0 +1,29 @@
{
"type": "File",
"start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}},
"errors": [
"SyntaxError: The 'u' and 'v' regular expression flags cannot be enabled at the same time. (1:6)"
],
"program": {
"type": "Program",
"start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}},
"expression": {
"type": "RegExpLiteral",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"extra": {
"raw": "/a/ugv"
},
"pattern": "a",
"flags": "ugv"
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
/a/vu;
@@ -0,0 +1,29 @@
{
"type": "File",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"errors": [
"SyntaxError: The 'u' and 'v' regular expression flags cannot be enabled at the same time. (1:5)"
],
"program": {
"type": "Program",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"expression": {
"type": "RegExpLiteral",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"extra": {
"raw": "/a/vu"
},
"pattern": "a",
"flags": "vu"
}
}
],
"directives": []
}
}
1 change: 1 addition & 0 deletions packages/babel-parser/typings/babel-parser.d.ts
Expand Up @@ -153,6 +153,7 @@ export type ParserPlugin =
| "pipelineOperator"
| "placeholders"
| "privateIn" // Enabled by default
| "regexpUnicodeSets"
| "throwExpressions"
| "topLevelAwait"
| "typescript"
Expand Down

0 comments on commit 9a41623

Please sign in to comment.