@@ -61,15 +61,13 @@ export class Parser {
61
61
return new Parser ( input ) . toAST ( ) ;
62
62
}
63
63
64
- private eatToken ( value : string = "" ) : u32 {
65
- const current = this . currentToken ;
66
- if ( value . length > 0 && current != value ) {
64
+ private eatToken ( value : u32 = - 1 ) : u32 {
65
+ const token = this . currentToken . charCodeAt ( 0 ) as u32 ;
66
+ if ( value != - 1 && token != value ) {
67
67
throw new Error ( "invalid token" ) ;
68
68
}
69
-
70
- this . cursor ++ ;
71
- this . currentToken = this . input . charAt ( this . cursor ) ;
72
- return current . charCodeAt ( 0 ) ;
69
+ this . currentToken = this . input . charAt ( ++ this . cursor ) ;
70
+ return token ;
73
71
}
74
72
75
73
private more ( ) : bool {
@@ -78,7 +76,7 @@ export class Parser {
78
76
79
77
private resetCursor ( ) : void {
80
78
this . cursor = 0 ;
81
- this . currentToken = this . input . charAt ( this . cursor ) ;
79
+ this . currentToken = this . input . charAt ( 0 ) ;
82
80
}
83
81
84
82
private toAST ( ) : AST {
@@ -89,7 +87,7 @@ export class Parser {
89
87
private parseCharacter ( ) : Node {
90
88
let token = this . currentToken . charCodeAt ( 0 ) ;
91
89
if ( token == 0x5c /* \ */ ) {
92
- this . eatToken ( "\\" ) ;
90
+ this . eatToken ( 0x5c ) ;
93
91
token = this . currentToken . charCodeAt ( 0 ) ;
94
92
if ( isSpecialCharacter ( token ) ) {
95
93
this . eatToken ( ) ;
@@ -106,7 +104,7 @@ export class Parser {
106
104
}
107
105
108
106
if ( token == CharClass . Dot ) {
109
- this . eatToken ( "." ) ;
107
+ this . eatToken ( CharClass . Dot ) ;
110
108
return new CharacterClassNode ( CharClass . Dot ) ;
111
109
}
112
110
@@ -116,7 +114,7 @@ export class Parser {
116
114
private maybeParseRepetitionRange ( ) : Range {
117
115
// snapshot
118
116
const previousCursor = this . cursor ;
119
- this . eatToken ( "{" ) ;
117
+ this . eatToken ( 0x7b /* { */ ) ;
120
118
121
119
let range = new Range ( ) ;
122
120
@@ -138,7 +136,7 @@ export class Parser {
138
136
digitStr = "" ;
139
137
range . to = - 1 ;
140
138
} else if ( token == 0x7d /* } */ ) {
141
- this . eatToken ( "}" ) ;
139
+ this . eatToken ( 0x7d /* } */ ) ;
142
140
// close brace, this is a single value range
143
141
return range ;
144
142
} else {
@@ -153,7 +151,7 @@ export class Parser {
153
151
} else {
154
152
range . to = digitStr . length ? < i32 > parseInt ( digitStr ) : - 1 ;
155
153
if ( token == 0x7d /* } */ ) {
156
- this . eatToken ( "}" ) ;
154
+ this . eatToken ( 0x7d /* } */ ) ;
157
155
// close brace, end of range
158
156
return range ;
159
157
} else {
@@ -167,7 +165,7 @@ export class Parser {
167
165
168
166
// repetition not found - reset state
169
167
this . cursor = previousCursor ;
170
- this . currentToken = this . input . charAt ( this . cursor ) ;
168
+ this . currentToken = this . input . charAt ( previousCursor ) ;
171
169
172
170
return range ;
173
171
}
@@ -180,14 +178,14 @@ export class Parser {
180
178
if ( token == 0x29 /* ) */ ) break ;
181
179
// @ts -ignore
182
180
if ( token == 0x7c /* | */ ) {
183
- this . eatToken ( "|" ) ;
181
+ this . eatToken ( 0x7c /* | */ ) ;
184
182
const left = nodes . length > 1 ? new ConcatenationNode ( nodes ) : nodes [ 0 ] ;
185
183
nodes = [ new AlternationNode ( left , this . parseSequence ( ) ) ] ;
186
184
// @ts -ignore
187
185
} else if ( token == 0x28 /* ( */ ) {
188
- this . eatToken ( "(" ) ;
186
+ this . eatToken ( 0x28 /* ( */ ) ;
189
187
nodes . push ( new GroupNode ( this . parseSequence ( ) ) ) ;
190
- this . eatToken ( ")" ) ;
188
+ this . eatToken ( 0x29 /* ) */ ) ;
191
189
// @ts -ignore
192
190
} else if ( token == 0x7b /* { */ ) {
193
191
const range = this . maybeParseRepetitionRange ( ) ;
@@ -215,10 +213,10 @@ export class Parser {
215
213
216
214
private parseCharacterSet ( ) : CharacterSetNode {
217
215
let chars = "" ;
218
- this . eatToken ( "[" ) ;
216
+ this . eatToken ( 0x5b /* [ */ ) ;
219
217
const negated = this . currentToken == "^" ;
220
218
if ( negated ) {
221
- this . eatToken ( "^" ) ;
219
+ this . eatToken ( 0x5e /* ^ */ ) ;
222
220
}
223
221
while (
224
222
this . currentToken != "]" ||
@@ -229,7 +227,7 @@ export class Parser {
229
227
this . eatToken ( ) ;
230
228
// TODO error if we run out of chars?
231
229
}
232
- this . eatToken ( "]" ) ;
230
+ this . eatToken ( 0x5d /* ] */ ) ;
233
231
return new CharacterSetNode ( chars , negated ) ;
234
232
}
235
233
}
0 commit comments