Skip to content

Commit 6847db1

Browse files
replaces #21 (#22)
* opt * remove 'unreachable code' hack Co-authored-by: MaxGraey <[email protected]>
1 parent af0d510 commit 6847db1

File tree

2 files changed

+35
-40
lines changed

2 files changed

+35
-40
lines changed

assembly/nfa/matcher.ts

+35-14
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ import {
66
CharacterClassNode,
77
CharacterRangeNode,
88
NodeType,
9-
Node,
109
} from "../parser/node";
11-
import { Match } from "../regexp";
10+
11+
const enum MatcherType {
12+
Character,
13+
CharacterRange,
14+
CharacterClass,
15+
CharacterSet,
16+
}
1217

1318
export class Matcher {
19+
constructor(readonly type: MatcherType) {}
20+
1421
matches(code: u32): bool {
1522
return false;
1623
}
@@ -50,7 +57,7 @@ export class Matcher {
5057

5158
export class CharacterMatcher extends Matcher {
5259
constructor(public character: Char) {
53-
super();
60+
super(MatcherType.Character);
5461
}
5562

5663
matches(code: u32): bool {
@@ -60,7 +67,7 @@ export class CharacterMatcher extends Matcher {
6067

6168
export class CharacterRangeMatcher extends Matcher {
6269
constructor(public from: u32, public to: u32) {
63-
super();
70+
super(MatcherType.CharacterRange);
6471
}
6572

6673
matches(code: u32): bool {
@@ -70,7 +77,7 @@ export class CharacterRangeMatcher extends Matcher {
7077

7178
export class CharacterClassMatcher extends Matcher {
7279
constructor(public charClass: Char) {
73-
super();
80+
super(MatcherType.CharacterClass);
7481
}
7582

7683
matches(code: u32): bool {
@@ -113,20 +120,34 @@ export class CharacterClassMatcher extends Matcher {
113120
}
114121
}
115122

116-
// no closure support
117-
let _code: u32;
118-
119123
export class CharacterSetMatcher extends Matcher {
120124
constructor(public matchers: Matcher[], public negated: bool) {
121-
super();
125+
super(MatcherType.CharacterSet);
122126
}
123127

124128
matches(code: u32): bool {
125-
_code = code;
126-
if (!this.negated) {
127-
return this.matchers.some((m) => m.matches(_code));
128-
} else {
129-
return !this.matchers.some((m) => m.matches(_code));
129+
let match: bool = false;
130+
for (let i = 0, len = this.matchers.length; i < len; i++) {
131+
let matcher = this.matchers[i];
132+
switch (matcher.type) {
133+
case MatcherType.Character:
134+
match = (matcher as CharacterMatcher).matches(code);
135+
break;
136+
137+
case MatcherType.CharacterRange:
138+
match = (matcher as CharacterRangeMatcher).matches(code);
139+
break;
140+
141+
case MatcherType.CharacterClass:
142+
match = (matcher as CharacterClassMatcher).matches(code);
143+
break;
144+
145+
case MatcherType.CharacterSet:
146+
match = (matcher as CharacterSetMatcher).matches(code);
147+
break;
148+
}
149+
if (match) break;
130150
}
151+
return this.negated ? !match : match;
131152
}
132153
}

assembly/regexp.ts

-26
Original file line numberDiff line numberDiff line change
@@ -152,32 +152,6 @@ export class RegExp {
152152
// TODO: do we need this factory function, or can we invoke
153153
// the ctr via the loader?
154154
export function createRegExp(regex: string, flags: string): RegExp {
155-
/* ---------------- */
156-
/*
157-
This block of code is needed to avoid the following runtime error ...
158-
159-
RuntimeError: unreachable
160-
at assembly/nfa/matcher/Matcher#matches@virtual (wasm-function[240]:1)
161-
at assembly/nfa/matcher/CharacterSetMatcher#matches~anonymous|0 (wasm-function[241]:19)
162-
at ~lib/array/Array<assembly/nfa/matcher/Matcher>#some (wasm-function[242]:85)
163-
at assembly/nfa/matcher/CharacterSetMatcher#matches (wasm-function[244]:21)
164-
at assembly/nfa/nfa/MatcherState<assembly/nfa/matcher/CharacterSetMatcher>#matches (wasm-function[245]:8)
165-
at assembly/nfa/nfa/State#matches@virtual (wasm-function[250]:58)
166-
at assembly/regexp/recursiveBacktrackingSearch (wasm-function[184]:121)
167-
at assembly/regexp/recursiveBacktrackingSearch@varargs (wasm-function[185]:56)
168-
at assembly/regexp/RegExp#exec (wasm-function[192]:307)
169-
*/
170-
const matchers = new Array<Matcher>();
171-
matchers.push(new CharacterMatcher(Char.A));
172-
const charMatcher = new CharacterSetMatcher(matchers, false);
173-
const state = new MatcherState<CharacterSetMatcher>(
174-
charMatcher,
175-
new State(true)
176-
);
177-
const char = "a".charCodeAt(0);
178-
const doesMatch = state.matches(char) != null;
179-
/* ---------------- */
180-
181155
return new RegExp(regex, flags);
182156
}
183157

0 commit comments

Comments
 (0)