Skip to content

Commit e195b8e

Browse files
committed
Fixing character group parsing with punctuation symbols
`/ ([.|,])/g` is now valid regexp
1 parent 5fdc4d6 commit e195b8e

File tree

3 files changed

+162
-1
lines changed

3 files changed

+162
-1
lines changed

src/parser/__tests__/parser-basic-test.js

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,163 @@ describe('basic', () => {
140140
});
141141
});
142142

143+
it('character class with punctuation symbols', () => {
144+
expect(re('/[!"#$%&\'()*+,./:;<=>?@\^_`{|}~-]/')).toEqual({
145+
type: 'RegExp',
146+
body: {
147+
type: 'CharacterClass',
148+
expressions: [
149+
{
150+
type: 'Char',
151+
kind: 'simple',
152+
value: '!'
153+
},
154+
{
155+
type: 'Char',
156+
kind: 'simple',
157+
value: '"'
158+
},
159+
{
160+
type: 'Char',
161+
kind: 'simple',
162+
value: '#'
163+
},
164+
{
165+
type: 'Char',
166+
kind: 'simple',
167+
value: '$'
168+
},
169+
{
170+
type: 'Char',
171+
kind: 'simple',
172+
value: '%'
173+
},
174+
{
175+
type: 'Char',
176+
kind: 'simple',
177+
value: '&'
178+
},
179+
{
180+
type: 'Char',
181+
kind: 'simple',
182+
value: '\''
183+
},
184+
{
185+
type: 'Char',
186+
kind: 'simple',
187+
value: '('
188+
},
189+
{
190+
type: 'Char',
191+
kind: 'simple',
192+
value: ')'
193+
},
194+
{
195+
type: 'Char',
196+
kind: 'simple',
197+
value: '*'
198+
},
199+
{
200+
type: 'Char',
201+
kind: 'simple',
202+
value: '+'
203+
},
204+
{
205+
type: 'Char',
206+
kind: 'simple',
207+
value: ','
208+
},
209+
{
210+
type: 'Char',
211+
kind: 'simple',
212+
value: '.'
213+
},
214+
{
215+
type: 'Char',
216+
kind: 'simple',
217+
value: '/'
218+
},
219+
{
220+
type: 'Char',
221+
kind: 'simple',
222+
value: ':'
223+
},
224+
{
225+
type: 'Char',
226+
kind: 'simple',
227+
value: ';'
228+
},
229+
{
230+
type: 'Char',
231+
kind: 'simple',
232+
value: '<'
233+
},
234+
{
235+
type: 'Char',
236+
kind: 'simple',
237+
value: '='
238+
},
239+
{
240+
type: 'Char',
241+
kind: 'simple',
242+
value: '>'
243+
},
244+
{
245+
type: 'Char',
246+
kind: 'simple',
247+
value: '?'
248+
},
249+
{
250+
type: 'Char',
251+
kind: 'simple',
252+
value: '@'
253+
},
254+
{
255+
type: 'Char',
256+
kind: 'simple',
257+
value: '^'
258+
},
259+
{
260+
type: 'Char',
261+
kind: 'simple',
262+
value: '_'
263+
},
264+
{
265+
type: 'Char',
266+
kind: 'simple',
267+
value: '`'
268+
},
269+
{
270+
type: 'Char',
271+
kind: 'simple',
272+
value: '{'
273+
},
274+
{
275+
type: 'Char',
276+
kind: 'simple',
277+
value: '|'
278+
},
279+
{
280+
type: 'Char',
281+
kind: 'simple',
282+
value: '}'
283+
},
284+
{
285+
type: 'Char',
286+
kind: 'simple',
287+
value: '~'
288+
},
289+
{
290+
type: 'Char',
291+
kind: 'simple',
292+
value: '-'
293+
}
294+
]
295+
},
296+
flags: ''
297+
});
298+
});
299+
143300
it('capturing group numbers', () => {
144301
expect(re('/(?:)(a)(?:)(?<name>b)/')).toEqual({
145302
type: 'RegExp',

src/parser/generated/regexp-tree.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ const lexRules = [[/^#[^\n]+/, function() { /* skip comments */ }, ["x"]],
339339
[/^-/, function() { return 'DASH' }, ["class"]],
340340
[/^\//, function() { return 'CHAR' }, ["class"]],
341341
[/^#/, function() { return 'CHAR' }, ["class"]],
342+
[/^\|/, function() { return 'CHAR' }, ["class"]],
343+
[/^\./, function() { return 'CHAR' }, ["class"]],
342344
[/^\{/, function() { return 'CHAR' }, ["class"]],
343345
[/^\{\d+\}/, function() { return 'RANGE_EXACT' }, ],
344346
[/^\{\d+,\}/, function() { return 'RANGE_OPEN' }, ],
@@ -381,7 +383,7 @@ const lexRules = [[/^#[^\n]+/, function() { /* skip comments */ }, ["x"]],
381383
[/^[^*?+\[()]/, function() { return 'CHAR' }, ],
382384
[/^\[\^/, function() { this.pushState('class'); return 'NEG_CLASS' }, ],
383385
[/^\[/, function() { this.pushState('class'); return 'L_BRACKET' }, ]];
384-
const lexRulesByConditions = {"INITIAL":[7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,26,27,28,29,30,31,32,33,37,38,39,40,41,42,43,44,45,46,47],"x":[0,1,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,26,27,28,29,30,31,32,33,37,38,39,40,41,42,43,44,45,46,47],"class":[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47]};
386+
const lexRulesByConditions = {"INITIAL":[9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,28,29,30,31,32,33,34,35,39,40,41,42,43,44,45,46,47,48,49],"x":[0,1,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,28,29,30,31,32,33,34,35,39,40,41,42,43,44,45,46,47,48,49],"class":[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49]};
385387

386388
const EOF_TOKEN = {
387389
type: EOF,

src/parser/regexp.bnf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ GROUP_NAME ([\w$]|\\'u'[0-9a-fA-F]{4}|\\'u{'[0-9a-fA-F]{1,}'}')+
5555
<class>'-' return 'DASH'
5656
<class>'/' return 'CHAR'
5757
<class>'#' return 'CHAR'
58+
<class>'|' return 'CHAR'
59+
<class>'.' return 'CHAR'
5860

5961
<class>'{' return 'CHAR'
6062

0 commit comments

Comments
 (0)