Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #46 from spahnke/regex-flags
Browse files Browse the repository at this point in the history
Improve tokenization of regular expressions
  • Loading branch information
rebornix authored Oct 1, 2018
2 parents 0aea831 + ce3a065 commit 66b5497
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
35 changes: 34 additions & 1 deletion src/javascript/javascript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ testTokenization('javascript', [
]
}],

[{
[{
line: 'x = /foo/.test(\'\')',
tokens: [
{ startIndex: 0, type: 'identifier.js' },
Expand All @@ -446,6 +446,39 @@ testTokenization('javascript', [
]
}],

[{
line: '/foo/',
tokens: [
{ startIndex: 0, type: 'regexp.js' }
]
}],

[{
line: '/foo/g',
tokens: [
{ startIndex: 0, type: 'regexp.js' },
{ startIndex: 5, type: 'keyword.other.js' }
]
}],

[{
line: '/foo/gimsuy',
tokens: [
{ startIndex: 0, type: 'regexp.js' },
{ startIndex: 5, type: 'keyword.other.js' }
]
}],

[{
line: '/foo/q', // invalid flag
tokens: [
{ startIndex: 0, type: 'delimiter.js' },
{ startIndex: 1, type: 'identifier.js' },
{ startIndex: 4, type: 'delimiter.js' },
{ startIndex: 5, type: 'identifier.js' }
]
}],

[{
line: 'x = 1 + f(2 / 3, /foo/)',
tokens: [
Expand Down
33 changes: 33 additions & 0 deletions src/typescript/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,39 @@ testTokenization('typescript', [
]
}],

[{
line: '/foo/',
tokens: [
{ startIndex: 0, type: 'regexp.ts' }
]
}],

[{
line: '/foo/g',
tokens: [
{ startIndex: 0, type: 'regexp.ts' },
{ startIndex: 5, type: 'keyword.other.ts' }
]
}],

[{
line: '/foo/gimsuy',
tokens: [
{ startIndex: 0, type: 'regexp.ts' },
{ startIndex: 5, type: 'keyword.other.ts' }
]
}],

[{
line: '/foo/q', // invalid flag
tokens: [
{ startIndex: 0, type: 'delimiter.ts' },
{ startIndex: 1, type: 'identifier.ts' },
{ startIndex: 4, type: 'delimiter.ts' },
{ startIndex: 5, type: 'identifier.ts' }
]
}],

[{
line: 'x = 1 + f(2 / 3, /foo/)',
tokens: [
Expand Down
4 changes: 2 additions & 2 deletions src/typescript/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const language = {
{ include: '@whitespace' },

// regular expression: ensure it is terminated before beginning (otherwise it is an opeator)
[/\/(?=([^\\\/]|\\.)+\/([gimuy]*)(\s*)(\.|;|\/|,|\)|\]|\}|$))/, { token: 'regexp', bracket: '@open', next: '@regexp' }],
[/\/(?=([^\\\/]|\\.)+\/([gimsuy]*)(\s*)(\.|;|\/|,|\)|\]|\}|$))/, { token: 'regexp', bracket: '@open', next: '@regexp' }],

// delimiters and operators
[/[()\[\]]/, '@brackets'],
Expand Down Expand Up @@ -190,7 +190,7 @@ export const language = {
[/[^\\\/]/, 'regexp'],
[/@regexpesc/, 'regexp.escape'],
[/\\\./, 'regexp.invalid'],
['/', { token: 'regexp', bracket: '@close' }, '@pop'],
[/(\/)([gimsuy]*)/, [{ token: 'regexp', bracket: '@close', next: '@pop' }, 'keyword.other']],
],

regexrange: [
Expand Down

0 comments on commit 66b5497

Please sign in to comment.