Skip to content

Commit f28c985

Browse files
Protryonmichaelficarra
authored andcommitted
Fix class control value (#9)
1 parent 08cc098 commit f28c985

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

src/index.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ class PatternAcceptorState {
4545
if (this.empty()) {
4646
return null;
4747
}
48-
return String.fromCodePoint(this.pattern.codePointAt(this.index));
48+
if (this.unicode) {
49+
return String.fromCodePoint(this.pattern.codePointAt(this.index));
50+
}
51+
return this.pattern.charAt(this.index);
4952
}
5053

5154
skipCodePoint() {
@@ -344,7 +347,7 @@ const acceptCharacterEscape = anyOf(
344347
if (character === null) {
345348
return { matched: false };
346349
}
347-
return { matched: true, value: character.codePointAt(0) % 32 };
350+
return { matched: true, value: character.charCodeAt(0) % 32 };
348351
}),
349352
backtrackOnFailure(state => {
350353
if (!state.eat('0') || state.eatAny(...decimalDigits)) {
@@ -395,7 +398,7 @@ const acceptCharacterEscape = anyOf(
395398
if (value === null) {
396399
return { matched: false };
397400
}
398-
return { matched: true, value: value.codePointAt(0) };
401+
return { matched: true, value: value.charCodeAt(0) };
399402
}),
400403
state => {
401404
if (!state.unicode || !state.eat('/')) {
@@ -432,34 +435,43 @@ const acceptCharacterClass = backtrackOnFailure(state => {
432435
subState => {
433436
return { matched: !!subState.eat('b'), value: 0x0008 };
434437
},
438+
subState => {
439+
if (!subState.unicode) {
440+
return { matched: false };
441+
}
442+
return acceptDecimalEscape(subState);
443+
},
435444
subState => {
436445
return { matched: subState.unicode && !!subState.eat('-'), value: '-'.charCodeAt(0) };
437446
},
438447
backtrackOnFailure(subState => {
439448
if (subState.unicode || !subState.eat('c')) {
440449
return { matched: false };
441450
}
442-
let eaten = subState.eatAny(...decimalDigits, '_');
443-
return { matched: !!eaten, value: eaten };
451+
let character = subState.eatAny(...decimalDigits, '_');
452+
if (character === null) {
453+
return { matched: false };
454+
}
455+
return { matched: true, value: character.charCodeAt(0) % 32 };
444456
}),
445457
acceptCharacterClassEscape,
446458
acceptCharacterEscape
447459
);
448460

449461
const acceptClassAtomNoDash = localState => {
450-
if (localState.eat('\\')) {
451-
return anyOf(
452-
acceptClassEscape,
453-
backtrackOnFailure(subState => {
454-
if (subState.match('c')) {
455-
return { matched: true, value: 0x005C }; // reverse solidus
456-
}
457-
return { matched: false };
458-
})
459-
)(localState);
462+
if (localState.match('\\')) {
463+
let ret = backtrackOnFailure(subState => {
464+
subState.eat('\\');
465+
return acceptClassEscape(subState);
466+
})(localState);
467+
if (ret.matched) {
468+
return ret;
469+
} else if (!localState.match('\\c') || localState.unicode) {
470+
return { matched: false };
471+
}
460472
}
461473
let nextCodePoint = localState.nextCodePoint();
462-
if (nextCodePoint === null) {
474+
if (nextCodePoint === null || nextCodePoint === ']' || nextCodePoint === '-') {
463475
return { matched: false };
464476
}
465477
localState.skipCodePoint();
@@ -468,7 +480,7 @@ const acceptCharacterClass = backtrackOnFailure(state => {
468480

469481
const acceptClassAtom = localState => {
470482
if (localState.eat('-')) {
471-
return { matched: true, value: '-'.codePointAt(0) };
483+
return { matched: true, value: '-'.charCodeAt(0) };
472484
}
473485
return acceptClassAtomNoDash(localState);
474486
};
@@ -499,7 +511,6 @@ const acceptCharacterClass = backtrackOnFailure(state => {
499511
return { matched: true };
500512
}
501513
return acceptNonEmptyClassRangesNoDash(localState);
502-
503514
};
504515

505516
const acceptNonEmptyClassRanges = localState => {

test/literal-regexp-expression.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ suite('Parser', () => {
138138
/{5,X}/
139139
/{5,10X}/
140140
/[\c5]/
141-
/[\c10]/u
141+
/[\c10]/
142142
/[\${'\\5'}]/
143143
/(?:)/
144144
/(?:X)/
@@ -161,7 +161,9 @@ suite('Parser', () => {
161161
/t{5/
162162
/[💩-💫]/u
163163
/[\u{1F4A9}-\u{1F4AB}]/u
164-
/\c/`);
164+
/[\c0-\c9]/
165+
/\c/
166+
/[\c]/`);
165167
const regexToFail = preprocessRegexList(String.raw`/[/
166168
/(?<=t|v|X|.|$||)/
167169
/(?<!t|v|X|.|$||)/
@@ -207,7 +209,12 @@ suite('Parser', () => {
207209
/5{5,1G}/u
208210
/\k/u
209211
/[💫-💩]/u
210-
/[\u{1F4AB}-\u{1F4A9}]/u`);
212+
/[\u{1F4AB}-\u{1F4A9}]/u
213+
/[\c9-\c0]/
214+
/[\c]/u
215+
/[\c1]/u
216+
/[\c10]/u
217+
/[🌷-🌸]/`);
211218
regexToPass.forEach(args => testRegexSuccess(...args));
212219
regexToFail.forEach(args => testRegexFailure(...args));
213220
});

0 commit comments

Comments
 (0)