@@ -6,11 +6,18 @@ import {
6
6
CharacterClassNode ,
7
7
CharacterRangeNode ,
8
8
NodeType ,
9
- Node ,
10
9
} from "../parser/node" ;
11
- import { Match } from "../regexp" ;
10
+
11
+ const enum MatcherType {
12
+ Character ,
13
+ CharacterRange ,
14
+ CharacterClass ,
15
+ CharacterSet ,
16
+ }
12
17
13
18
export class Matcher {
19
+ constructor ( readonly type : MatcherType ) { }
20
+
14
21
matches ( code : u32 ) : bool {
15
22
return false ;
16
23
}
@@ -50,7 +57,7 @@ export class Matcher {
50
57
51
58
export class CharacterMatcher extends Matcher {
52
59
constructor ( public character : Char ) {
53
- super ( ) ;
60
+ super ( MatcherType . Character ) ;
54
61
}
55
62
56
63
matches ( code : u32 ) : bool {
@@ -60,7 +67,7 @@ export class CharacterMatcher extends Matcher {
60
67
61
68
export class CharacterRangeMatcher extends Matcher {
62
69
constructor ( public from : u32 , public to : u32 ) {
63
- super ( ) ;
70
+ super ( MatcherType . CharacterRange ) ;
64
71
}
65
72
66
73
matches ( code : u32 ) : bool {
@@ -70,7 +77,7 @@ export class CharacterRangeMatcher extends Matcher {
70
77
71
78
export class CharacterClassMatcher extends Matcher {
72
79
constructor ( public charClass : Char ) {
73
- super ( ) ;
80
+ super ( MatcherType . CharacterClass ) ;
74
81
}
75
82
76
83
matches ( code : u32 ) : bool {
@@ -113,20 +120,34 @@ export class CharacterClassMatcher extends Matcher {
113
120
}
114
121
}
115
122
116
- // no closure support
117
- let _code : u32 ;
118
-
119
123
export class CharacterSetMatcher extends Matcher {
120
124
constructor ( public matchers : Matcher [ ] , public negated : bool ) {
121
- super ( ) ;
125
+ super ( MatcherType . CharacterSet ) ;
122
126
}
123
127
124
128
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 ;
130
150
}
151
+ return this . negated ? ! match : match ;
131
152
}
132
153
}
0 commit comments