Skip to content

Commit 3c4ee3c

Browse files
ensure capture groups are returned in the correct order
1 parent be6f7b5 commit 3c4ee3c

File tree

4 files changed

+22
-29
lines changed

4 files changed

+22
-29
lines changed

assembly/__spec_tests__/generated.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ it("line: 149 - matches (ab|a)b*c against 'abc'", () => {
692692
expect(match.matches[0]).toBe("abc");
693693
expect(match.matches[1]).toBe("ab");
694694
});
695-
xit("line: 150 - matches ((a)(b)c)(d) against 'abcd'", () => {
695+
it("line: 150 - matches ((a)(b)c)(d) against 'abcd'", () => {
696696
const match = exec("((a)(b)c)(d)", "abcd", "");
697697
expect(match.matches[0]).toBe("abcd");
698698
expect(match.matches[1]).toBe("abc");
@@ -974,14 +974,14 @@ it("line: 186 - matches ^.+!([^!]+!)([^!]+)$ against 'foo!bar!bas'", () => {
974974
expect(match.matches[1]).toBe("bar!");
975975
expect(match.matches[2]).toBe("bas");
976976
});
977-
xit("line: 187 - matches ((foo)|(bar))!bas against 'bar!bas'", () => {
977+
it("line: 187 - matches ((foo)|(bar))!bas against 'bar!bas'", () => {
978978
const match = exec("((foo)|(bar))!bas", "bar!bas", "");
979979
expect(match.matches[0]).toBe("bar!bas");
980980
expect(match.matches[1]).toBe("bar");
981981
expect(match.matches[2]).toBe("");
982982
expect(match.matches[3]).toBe("bar");
983983
});
984-
xit("line: 188 - matches ((foo)|(bar))!bas against 'foo!bar!bas'", () => {
984+
it("line: 188 - matches ((foo)|(bar))!bas against 'foo!bar!bas'", () => {
985985
const match = exec("((foo)|(bar))!bas", "foo!bar!bas", "");
986986
expect(match.matches[0]).toBe("bar!bas");
987987
expect(match.matches[1]).toBe("bar");
@@ -994,12 +994,12 @@ it("line: 189 - matches ((foo)|(bar))!bas against 'foo!bas'", () => {
994994
expect(match.matches[1]).toBe("foo");
995995
expect(match.matches[2]).toBe("foo");
996996
});
997-
xit("line: 190 - matches ((foo)|bar)!bas against 'bar!bas'", () => {
997+
it("line: 190 - matches ((foo)|bar)!bas against 'bar!bas'", () => {
998998
const match = exec("((foo)|bar)!bas", "bar!bas", "");
999999
expect(match.matches[0]).toBe("bar!bas");
10001000
expect(match.matches[1]).toBe("bar");
10011001
});
1002-
xit("line: 191 - matches ((foo)|bar)!bas against 'foo!bar!bas'", () => {
1002+
it("line: 191 - matches ((foo)|bar)!bas against 'foo!bar!bas'", () => {
10031003
const match = exec("((foo)|bar)!bas", "foo!bar!bas", "");
10041004
expect(match.matches[0]).toBe("bar!bas");
10051005
expect(match.matches[1]).toBe("bar");

assembly/nfa/nfa.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export class State {
3434

3535
export class GroupStartMarkerState extends State {
3636
location: i32 = -1;
37+
// a bit yucky - storing transient state in the state machine!
38+
capture: string = "";
39+
// captures from the path through the NFA that reaches the end are flagged
40+
flagged: bool = false;
3741

3842
constructor(next: State) {
3943
super();
@@ -47,18 +51,16 @@ export class GroupStartMarkerState extends State {
4751
}
4852

4953
export class GroupEndMarkerState extends State {
50-
// a bit yucky - storing transient state in the state machine!
51-
capture: string = "";
52-
// captures from the path through the NFA that reaches the end are flagged
53-
flagged: bool = false;
54-
5554
constructor(next: State, public startMarker: GroupStartMarkerState) {
5655
super();
5756
this.transitions.push(next);
5857
}
5958

6059
matches(input: string, position: u32): MatchResult {
61-
this.capture = input.substring(this.startMarker.location, position);
60+
this.startMarker.capture = input.substring(
61+
this.startMarker.location,
62+
position
63+
);
6264
return MatchResult.Ignore;
6365
}
6466
}

assembly/regexp.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { State, Automata, GroupEndMarkerState, MatchResult } from "./nfa/nfa";
1+
import { State, Automata, GroupStartMarkerState, MatchResult } from "./nfa/nfa";
22
import { walker as nfaWalker } from "./nfa/walker";
33
import { ConcatenationNode, AssertionNode, NodeType } from "./parser/node";
44
import { Char } from "./char";
@@ -46,8 +46,8 @@ function recursiveBacktrackingSearch(
4646
);
4747
if (match != null) {
4848
// when unwinding the stack after a successful match, flag the captured values
49-
if (state instanceof GroupEndMarkerState) {
50-
(state as GroupEndMarkerState).flagged = true;
49+
if (state instanceof GroupStartMarkerState) {
50+
(state as GroupStartMarkerState).flagged = true;
5151
}
5252
return match;
5353
}
@@ -67,7 +67,7 @@ export class Match {
6767
}
6868
}
6969

70-
let gm = new Array<GroupEndMarkerState>();
70+
let gm = new Array<GroupStartMarkerState>();
7171

7272
export class RegExp {
7373
lastIndex: i32 = 0;
@@ -77,7 +77,7 @@ export class RegExp {
7777
private nfa: Automata;
7878
private endOfInput: bool = false;
7979
private startOfInput: bool = false;
80-
private groupMarkers: GroupEndMarkerState[];
80+
private groupMarkers: GroupStartMarkerState[];
8181

8282
constructor(private regex: string, public flags: string | null = null) {
8383
const ast = Parser.toAST(regex);
@@ -100,10 +100,10 @@ export class RegExp {
100100
this.nfa = Automata.toNFA(ast, this.ignoreCase);
101101

102102
// find all the group marker states
103-
gm = new Array<GroupEndMarkerState>();
103+
gm = new Array<GroupStartMarkerState>();
104104
nfaWalker(this.nfa.start, (state) => {
105-
if (state instanceof GroupEndMarkerState) {
106-
gm.push(state as GroupEndMarkerState);
105+
if (state instanceof GroupStartMarkerState) {
106+
gm.push(state as GroupStartMarkerState);
107107
}
108108
});
109109
this.groupMarkers = gm;

spec/test-generator.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,9 @@ const knownIssues = {
99
"issue with parsing the test itself": [28, 58, 59, 78, 212, 213],
1010
"issue with generating the test": [61, 62, 64, 76, 209, 210],
1111
"unsupported POSIX regex syntax": [54, 55, 56],
12-
"issue that require triage": [199, 202, 204, 205, 206, 207],
12+
"issue that require triage": [133, 199, 202, 204, 205, 206, 207],
1313
// I can't find a good reference that describes this behaviour!
1414
"BUG: doesn't support anchors within capture groups": [20],
15-
// https://github.com/ColinEberhardt/assemblyscript-regex/issues/2
16-
"BUG: Should not return captured values for non-matching alternations": [
17-
133,
18-
150,
19-
187,
20-
188,
21-
190,
22-
191,
23-
],
2415
};
2516

2617
const hasKnownIssue = (index) => {

0 commit comments

Comments
 (0)