@@ -10,83 +10,70 @@ import { dedentBlockStringValue } from './blockString';
10
10
import { type TokenKindEnum , TokenKind } from './tokenKind' ;
11
11
12
12
/**
13
- * Given a Source object, this returns a Lexer for that source.
13
+ * Given a Source object, creates a Lexer for that source.
14
14
* A Lexer is a stateful stream generator in that every time
15
15
* it is advanced, it returns the next token in the Source. Assuming the
16
16
* source lexes, the final Token emitted by the lexer will be of kind
17
17
* EOF, after which the lexer will repeatedly return the same EOF token
18
18
* whenever called.
19
19
*/
20
- export function createLexer ( source : Source ) : Lexer {
21
- const startOfFileToken = new Tok ( TokenKind . SOF , 0 , 0 , 0 , 0 , null ) ;
22
- const lexer : Lexer = {
23
- source,
24
- lastToken : startOfFileToken ,
25
- token : startOfFileToken ,
26
- line : 1 ,
27
- lineStart : 0 ,
28
- advance : advanceLexer ,
29
- lookahead,
30
- } ;
31
- return lexer ;
32
- }
33
-
34
- function advanceLexer ( ) {
35
- this . lastToken = this . token ;
36
- const token = ( this . token = this . lookahead ( ) ) ;
37
- return token ;
38
- }
39
-
40
- function lookahead ( ) {
41
- let token = this . token ;
42
- if ( token . kind !== TokenKind . EOF ) {
43
- do {
44
- // Note: next is only mutable during parsing, so we cast to allow this.
45
- token = token . next || ( ( token : any ) . next = readToken ( this , token ) ) ;
46
- } while ( token . kind === TokenKind . COMMENT ) ;
47
- }
48
- return token ;
49
- }
50
-
51
- /**
52
- * The return type of createLexer.
53
- */
54
- export type Lexer = {
55
- source : Source ,
20
+ export class Lexer {
21
+ source : Source ;
56
22
57
23
/**
58
24
* The previously focused non-ignored token.
59
25
*/
60
- lastToken : Token ,
26
+ lastToken : Token ;
61
27
62
28
/**
63
29
* The currently focused non-ignored token.
64
30
*/
65
- token : Token ,
31
+ token : Token ;
66
32
67
33
/**
68
34
* The (1-indexed) line containing the current token.
69
35
*/
70
- line : number ,
36
+ line : number ;
71
37
72
38
/**
73
39
* The character offset at which the current line begins.
74
40
*/
75
- lineStart : number ,
41
+ lineStart : number ;
42
+
43
+ constructor ( source : Source ) {
44
+ const startOfFileToken = new Tok ( TokenKind . SOF , 0 , 0 , 0 , 0 , null ) ;
45
+
46
+ this . source = source ;
47
+ this . lastToken = startOfFileToken ;
48
+ this . token = startOfFileToken ;
49
+ this . line = 1 ;
50
+ this . lineStart = 0 ;
51
+ }
76
52
77
53
/**
78
54
* Advances the token stream to the next non-ignored token.
79
55
*/
80
- advance ( ) : Token ,
56
+ advance ( ) : Token {
57
+ this . lastToken = this . token ;
58
+ const token = ( this . token = this . lookahead ( ) ) ;
59
+ return token ;
60
+ }
81
61
82
62
/**
83
63
* Looks ahead and returns the next non-ignored token, but does not change
84
64
* the Lexer's state.
85
65
*/
86
- lookahead ( ) : Token ,
87
-
88
- ...
89
- } ;
66
+ lookahead ( ) : Token {
67
+ let token = this . token ;
68
+ if ( token . kind !== TokenKind . EOF ) {
69
+ do {
70
+ // Note: next is only mutable during parsing, so we cast to allow this.
71
+ token = token . next || ( ( token : any ) . next = readToken ( this , token ) ) ;
72
+ } while ( token . kind === TokenKind . COMMENT ) ;
73
+ }
74
+ return token ;
75
+ }
76
+ }
90
77
91
78
/**
92
79
* @internal
0 commit comments