Skip to content

Commit be6f7b5

Browse files
return captured values from paths that reach the NFA end
1 parent 04ad53d commit be6f7b5

File tree

6 files changed

+13
-6
lines changed

6 files changed

+13
-6
lines changed

assembly/__spec_tests__/generated.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ it("line: 34 - matches (a|b)c|a(b|c) against 'ac'", () => {
163163
expect(match.matches[0]).toBe("ac");
164164
expect(match.matches[1]).toBe("a");
165165
});
166-
xit("line: 35 - matches (a|b)c|a(b|c) against 'ab'", () => {
166+
it("line: 35 - matches (a|b)c|a(b|c) against 'ab'", () => {
167167
const match = exec("(a|b)c|a(b|c)", "ab", "");
168168
expect(match.matches[0]).toBe("ab");
169169
expect(match.matches[1]).toBe("");

assembly/__tests__/capture-group.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ it("supports capture groups", () => {
2727
expect(match.matches[2]).toBe("aaa");
2828
});
2929

30-
xit("should not return captured values for non-matching alternations", () => {
30+
it("should not return captured values for non-matching alternations", () => {
3131
const match = exec("(a|b)c|a(b|c)", "ab");
3232
expect(match.matches[0]).toBe("ab");
3333
expect(match.matches[1]).toBe("");

assembly/nfa/nfa.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export class GroupStartMarkerState extends State {
4949
export class GroupEndMarkerState extends State {
5050
// a bit yucky - storing transient state in the state machine!
5151
capture: string = "";
52+
// captures from the path through the NFA that reaches the end are flagged
53+
flagged: bool = false;
5254

5355
constructor(next: State, public startMarker: GroupStartMarkerState) {
5456
super();

assembly/regexp.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ function recursiveBacktrackingSearch(
4545
position
4646
);
4747
if (match != null) {
48+
// when unwinding the stack after a successful match, flag the captured values
49+
if (state instanceof GroupEndMarkerState) {
50+
(state as GroupEndMarkerState).flagged = true;
51+
}
4852
return match;
4953
}
5054
}
@@ -132,7 +136,9 @@ export class RegExp {
132136
// we have found a match
133137
if (matchStr != null) {
134138
const match = new Match(
135-
[matchStr!].concat(groupMarkers.map<string>((m) => m.capture)),
139+
[matchStr!].concat(
140+
groupMarkers.map<string>((m) => (m.flagged ? m.capture : ""))
141+
),
136142
matchIndex,
137143
str
138144
);

spec/test-generator.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const knownIssues = {
1414
"BUG: doesn't support anchors within capture groups": [20],
1515
// https://github.com/ColinEberhardt/assemblyscript-regex/issues/2
1616
"BUG: Should not return captured values for non-matching alternations": [
17-
35,
1817
133,
1918
150,
2019
187,

ts/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ globalAny.log = console.log;
55

66
import { RegExp } from "../assembly/regexp";
77

8-
const regexObj = new RegExp("^(([^!]+!)?([^!]+)|.+!([^!]+!)([^!]+))$");
9-
const match = regexObj.exec("foo!bar!bas");
8+
const regexObj = new RegExp("((a)(b)c)(d)");
9+
const match = regexObj.exec("abcd");
1010

1111
console.log(match);

0 commit comments

Comments
 (0)