-
Notifications
You must be signed in to change notification settings - Fork 74
Enable unicode property escapes #119
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
Closed
modernserf
wants to merge
1
commit into
no-context:master
from
modernserf:support-unicode-property-escapes
Closed
Changes from all commits
Commits
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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -8,6 +8,11 @@ const python = require('./python') | |
|
|
||
| function lexAll(lexer) {return Array.from(lexer)} | ||
|
|
||
| let supportsUnicodePropertyEscapes = false | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe there's a better way to feature detect this? |
||
| try { | ||
| /\p{ID_Start}/u; | ||
| supportsUnicodePropertyEscapes = true | ||
| } catch (e) {} | ||
|
|
||
| describe('compiler', () => { | ||
|
|
||
|
|
@@ -29,13 +34,12 @@ describe('compiler', () => { | |
| expect(lex4.next()).toMatchObject({type: 'err', text: 'nope!'}) | ||
| }) | ||
|
|
||
| test("warns for /g, /y, /i, /m, /u", () => { | ||
| test("warns for /g, /y, /i, /m", () => { | ||
| expect(() => compile({ word: /foo/ })).not.toThrow() | ||
| expect(() => compile({ word: /foo/g })).toThrow('implied') | ||
| expect(() => compile({ word: /foo/i })).toThrow('not allowed') | ||
| expect(() => compile({ word: /foo/y })).toThrow('implied') | ||
| expect(() => compile({ word: /foo/m })).toThrow('implied') | ||
| expect(() => compile({ word: /foo/u })).toThrow('not allowed') | ||
| }) | ||
|
|
||
| // TODO warns if no lineBreaks: true | ||
|
|
@@ -147,6 +151,40 @@ describe('compiler', () => { | |
| expect(tokens.shift()).toMatchObject({type: 'number', value: '3.14'}) | ||
| }) | ||
|
|
||
| test('accepts unicode RegExps', () => { | ||
| const lexer = compile({ | ||
| uSequence: /\u{0075}+/u, | ||
| space: / +/u, | ||
| }) | ||
| lexer.reset('uuuuu uu uuu') | ||
| var tokens = lexAll(lexer).filter(t => t.type !== 'space') | ||
| expect(tokens.shift()).toMatchObject({type: 'uSequence', value: 'uuuuu'}) | ||
| expect(tokens.shift()).toMatchObject({type: 'uSequence', value: 'uu'}) | ||
| expect(tokens.shift()).toMatchObject({type: 'uSequence', value: 'uuu'}) | ||
| }) | ||
|
|
||
| test('accepts unicode property escapes in RegExps, where supported', () => { | ||
| if (!supportsUnicodePropertyEscapes) { return } | ||
| const lexer = compile({ | ||
| identifier: /[$_\p{ID_Start}][$\p{ID_Continue}]*/u, | ||
| space: / +/u, | ||
| operator: ["+"], | ||
| }) | ||
| lexer.reset('$foo π ভরা') | ||
| var tokens = lexAll(lexer).filter(t => t.type !== 'space') | ||
| expect(tokens.shift()).toMatchObject({type: 'identifier', value: '$foo'}) | ||
| expect(tokens.shift()).toMatchObject({type: 'identifier', value: 'π'}) | ||
| expect(tokens.shift()).toMatchObject({type: 'identifier', value: 'ভরা'}) | ||
| }) | ||
|
|
||
| test('rejects mixed unicode and non-unicode RegExps', () => { | ||
| expect(() => { | ||
| compile({ | ||
| uSequence: /\u{0075}+/u, | ||
| space: / +/, | ||
| }) | ||
| }).toThrow('RegExp /u flag must be used on all or no patterns') | ||
| }) | ||
| }) | ||
|
|
||
| describe('compiles literals', () => { | ||
|
|
||
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.
Every time we build a RegExp we need to add the
uflag, if applicable; otherwise it complains about "nothing to repeat" in patterns like/\u{0075}+/