-
Notifications
You must be signed in to change notification settings - Fork 45
Improve parser suppport for regexp group names #184
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
Merged
DmitrySoshnikov
merged 2 commits into
DmitrySoshnikov:master
from
rbuckton:fixUnicodeGroupNames
Aug 26, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// based on https://github.com/microsoft/TypeScript/tree/master/scripts/regenerate-unicode-identifier-parts.js | ||
|
||
/** @param {number} i */ | ||
function toHex4Digits(i) { | ||
let s = i.toString(16); | ||
while (s.length < 4) { | ||
s = '0' + s; | ||
} | ||
if (s.length > 4) throw new Error('Invalid Hex4Digits value'); | ||
return s; | ||
} | ||
|
||
class NonSurrogateRange { | ||
/** @param {number} codePoint */ | ||
constructor(codePoint) { | ||
this.firstCodePoint = codePoint; | ||
this.lastCodePoint = codePoint; | ||
} | ||
toString() { | ||
let text = '\\u' + toHex4Digits(this.firstCodePoint); | ||
if (this.lastCodePoint !== this.firstCodePoint) { | ||
text += '-\\u' + toHex4Digits(this.lastCodePoint); | ||
} | ||
return text; | ||
} | ||
} | ||
|
||
class LeadSurrogateRange { | ||
/** @param {number} leadSurrogate */ | ||
constructor(leadSurrogate) { | ||
this.leadSurrogate = leadSurrogate; | ||
/** @type {TrailSurrogateRange[]} */ | ||
this.ranges = []; | ||
} | ||
|
||
toString() { | ||
return ( | ||
'\\u' + | ||
toHex4Digits(this.leadSurrogate) + | ||
'[' + | ||
this.ranges.join('') + | ||
']' | ||
); | ||
} | ||
} | ||
|
||
class TrailSurrogateRange { | ||
/** @param {number} trailSurrogate */ | ||
constructor(trailSurrogate) { | ||
this.firstTrailSurrogate = trailSurrogate; | ||
this.lastTrailSurrogate = trailSurrogate; | ||
} | ||
toString() { | ||
let text = '\\u' + toHex4Digits(this.firstTrailSurrogate); | ||
if (this.lastTrailSurrogate !== this.firstTrailSurrogate) { | ||
text += '-\\u' + toHex4Digits(this.lastTrailSurrogate); | ||
} | ||
return text; | ||
} | ||
} | ||
|
||
class Writer { | ||
constructor() { | ||
/** @type {number} */ | ||
this.lastCodePoint = -1; | ||
/** @type {NonSurrogateRange[]} */ | ||
this.nonSurrogateRanges = []; | ||
/** @type {LeadSurrogateRange[]} */ | ||
this.surrogateRanges = []; | ||
/** @type {NonSurrogateRange} */ | ||
this.nonSurrogateRange; | ||
/** @type {LeadSurrogateRange} */ | ||
this.leadSurrogateRange; | ||
/** @type {TrailSurrogateRange} */ | ||
this.trailSurrogateRange; | ||
} | ||
|
||
/** @param {number} codePoint */ | ||
push(codePoint) { | ||
if (codePoint <= this.lastCodePoint) | ||
throw new Error('Code points must be added in order.'); | ||
this.lastCodePoint = codePoint; | ||
|
||
if (codePoint < MAX_UNICODE_NON_SURROGATE) { | ||
if ( | ||
this.nonSurrogateRange && | ||
this.nonSurrogateRange.lastCodePoint === codePoint - 1 | ||
) { | ||
this.nonSurrogateRange.lastCodePoint = codePoint; | ||
return; | ||
} | ||
this.nonSurrogateRange = new NonSurrogateRange(codePoint); | ||
this.nonSurrogateRanges.push(this.nonSurrogateRange); | ||
} else { | ||
const leadSurrogate = Math.floor((codePoint - 0x10000) / 0x400) + 0xd800; | ||
const trailSurrogate = ((codePoint - 0x10000) % 0x400) + 0xdc00; | ||
if ( | ||
!this.leadSurrogateRange || | ||
this.leadSurrogateRange.leadSurrogate !== leadSurrogate | ||
) { | ||
this.trailSurrogateRange = undefined; | ||
this.leadSurrogateRange = new LeadSurrogateRange(leadSurrogate); | ||
this.surrogateRanges.push(this.leadSurrogateRange); | ||
} | ||
|
||
if ( | ||
this.trailSurrogateRange && | ||
this.trailSurrogateRange.lastTrailSurrogate === trailSurrogate - 1 | ||
) { | ||
this.trailSurrogateRange.lastTrailSurrogate = trailSurrogate; | ||
return; | ||
} | ||
|
||
this.trailSurrogateRange = new TrailSurrogateRange(trailSurrogate); | ||
this.leadSurrogateRange.ranges.push(this.trailSurrogateRange); | ||
} | ||
} | ||
|
||
toString() { | ||
let first = this.nonSurrogateRanges.join(''); | ||
let second = this.surrogateRanges.join('|'); | ||
return first && second | ||
? `([${first}]|${second})` | ||
: first | ||
? `[${first}]` | ||
: second | ||
? `(${second})` | ||
: ''; | ||
} | ||
} | ||
|
||
const MAX_UNICODE_NON_SURROGATE = 0xffff; | ||
const MAX_UNICODE_CODEPOINT = 0x10ffff; | ||
const isStart = c => /\p{ID_Start}/u.test(c); | ||
const isContinue = c => /\p{ID_Continue}/u.test(c); | ||
|
||
let idStartWriter = new Writer(); | ||
let idContinueWriter = new Writer(); | ||
|
||
for (let cp = 0; cp <= MAX_UNICODE_CODEPOINT; cp++) { | ||
const ch = String.fromCodePoint(cp); | ||
if (isStart(ch)) { | ||
idStartWriter.push(cp); | ||
} | ||
if (isContinue(ch)) { | ||
idContinueWriter.push(cp); | ||
} | ||
} | ||
|
||
console.log(`/** | ||
* Generated by scripts/generate-unicode-id-parts.js on node ${ | ||
process.version | ||
} with unicode ${process.versions.unicode} | ||
* based on http://www.unicode.org/reports/tr31/ and https://tc39.es/ecma262/#sec-names-and-keywords | ||
* U_ID_START corresponds to the ID_Start property, and U_ID_CONTINUE corresponds to ID_Continue property. | ||
*/`); | ||
console.log('U_ID_START ' + idStartWriter.toString()); | ||
console.log('U_ID_CONTINUE ' + idContinueWriter.toString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this depends on a Node version, and might be flaky/inconsistent? I'm wondering if it's possible just do a predictable subset, and update it periodically (even if manually).
Also, where/when will we run this script? Is it a part of the build script?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was run manually. Its unlikely this will change often, usually only if you choose to target a new version of NodeJS that itself includes a new version of the Unicode standard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, we can add it to the build script as one of the steps at some point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll copy a comment I made on the TypeScript implementation here: microsoft/TypeScript#32096 (comment)
The build script could be simplified and made to not rely on the current Node.js/V8 + Unicode version:
Links to how other tooling implements this: