40
40
IN THE SOFTWARE.
41
41
* */
42
42
43
- import { TokenType } from "./tokens" ;
44
- import { TokenizerErrors } from "./errors" ;
43
+ import { TokenType } from "./tokens" ;
44
+ import { TokenizerErrors } from "./errors" ;
45
45
46
46
export class Token {
47
47
type : TokenType ;
@@ -175,6 +175,13 @@ export class Tokenizer {
175
175
this . tokens . push ( new Token ( type , lexeme , line , col , this . current - lexeme . length ) )
176
176
}
177
177
178
+ private addStringToken ( type : TokenType ) {
179
+ const line = this . line
180
+ const col = this . col ;
181
+ const lexeme = this . source . slice ( this . start + 1 , this . current - 1 ) ;
182
+ this . tokens . push ( new Token ( type , lexeme , line , col , this . current - lexeme . length ) )
183
+ }
184
+
178
185
// Checks that the current character matches a pattern. If so the character is consumed, else nothing is consumed.
179
186
private matches ( pattern : string ) : boolean {
180
187
if ( this . isAtEnd ( ) ) {
@@ -196,25 +203,25 @@ export class Tokenizer {
196
203
private isDigit ( c : string ) : boolean {
197
204
return / ^ [ 0 - 9 ] / . test ( c ) ;
198
205
}
199
-
200
- private isHexa ( c : string ) : boolean {
206
+
207
+ private isHexa ( c : string ) : boolean {
201
208
return / ^ [ 0 - 9 A - F ] $ / i. test ( c ) ;
202
209
}
203
-
204
- private isOcta ( c : string ) : boolean {
210
+
211
+ private isOcta ( c : string ) : boolean {
205
212
return / ^ [ 0 - 7 ] / . test ( c ) ;
206
213
}
207
-
208
- private isBinary ( c : string ) : boolean {
214
+
215
+ private isBinary ( c : string ) : boolean {
209
216
return / ^ [ 0 - 1 ] / . test ( c ) ;
210
217
}
211
-
218
+
212
219
private isIdentifier ( c : string ) : boolean {
213
220
return c === '_' || this . isAlpha ( c ) || this . isDigit ( c ) ;
214
221
}
215
-
222
+
216
223
private baseNumber ( ) {
217
- switch ( this . peek ( ) ) {
224
+ switch ( this . peek ( ) ) {
218
225
case 'x' :
219
226
this . advance ( ) ;
220
227
if ( ! this . isHexa ( this . peek ( ) ) ) {
@@ -265,9 +272,9 @@ export class Tokenizer {
265
272
}
266
273
}
267
274
}
268
- this . addToken ( TokenType . NUMBER ) ;
275
+ this . addToken ( TokenType . NUMBER ) ;
269
276
}
270
-
277
+
271
278
private number ( ) {
272
279
while ( this . isDigit ( this . peek ( ) ) ) {
273
280
this . advance ( ) ;
@@ -292,7 +299,7 @@ export class Tokenizer {
292
299
this . advance ( ) ;
293
300
}
294
301
}
295
-
302
+
296
303
this . addToken ( TokenType . NUMBER ) ;
297
304
}
298
305
@@ -433,7 +440,18 @@ export class Tokenizer {
433
440
}
434
441
// Consume closing "
435
442
this . advance ( ) ;
436
- this . addToken ( TokenType . STRING ) ;
443
+ this . addStringToken ( TokenType . STRING ) ;
444
+ break ;
445
+ case '\'' :
446
+ while ( this . peek ( ) != '\'' && this . peek ( ) != '\n' && ! this . isAtEnd ( ) ) {
447
+ this . advance ( ) ;
448
+ }
449
+ if ( this . peek ( ) === '\n' || this . isAtEnd ( ) ) {
450
+ throw new TokenizerErrors . UnterminatedStringError ( this . line , this . col , this . source , this . start , this . current ) ;
451
+ }
452
+ // Consume closing '
453
+ this . advance ( ) ;
454
+ this . addStringToken ( TokenType . STRING ) ;
437
455
break ;
438
456
// Number... I wish JS had match statements :(
439
457
case '0' :
0 commit comments