22const { iterator } = Symbol
33
44
5- const tokens = prepare ( {
6- '\'\'\'' : 'Multiline' ,
7- '[^\\s,:\\[\\]\\{\\}]+' : 'Word' ,
8- '[^\\S\\n]+' : 'Space' ,
9- '(\\r)?\\n' : 'Newline' ,
10- '\\{' : 'ObjectStart' ,
11- '\\}' : 'ObjectEnd' ,
12- '\\[' : 'ArrayStart' ,
13- '\\]' : 'ArrayEnd' ,
14- ',' : 'Comma' ,
15- ':' : 'Colon'
16- } )
17-
18-
19- function prepare ( tokens ) {
20- return Object
21- . entries ( tokens )
22- . map ( toToken ) ;
23- }
24-
25- function toToken ( [ pattern , token ] ) {
26- return [ toRegex ( pattern ) , token ] ;
27- }
28-
29- function toRegex ( pattern ) {
30- return new RegExp ( `^(${ pattern } )` , 'u' ) ;
31- }
5+ const tokens = [
6+
7+ [ 'ObjectStart' , / ^ \{ / ] ,
8+ [ 'ObjectEnd' , / ^ \} / ] ,
9+
10+ [ 'ArrayStart' , / ^ \[ / ] ,
11+ [ 'ArrayEnd' , / ^ \] / ] ,
12+
13+ [ 'Comma' , / ^ , / ] ,
14+ [ 'Colon' , / ^ : / ] ,
15+
16+ [ 'Newline' , / ^ \r ? \n / ] ,
17+ [ 'Space' , / ^ [ \s ] + / , 0 ] ,
18+
19+ [ 'MultiString' , / ^ ' ' ' ( [ \s \S ] * ?) ' ' ' / u , 1 ] ,
20+ [ 'MultiComment' , / ^ \/ \* ( [ \s \S ] * ?) \* \/ / u ] ,
21+ [ 'SingleString' , / ^ ' ( ( [ ^ \\ ] | ( \\ [ " ' \\ / b f n r t ] ) | ( \\ u \d { 4 } ) ) * ?) ' / , 1 ] ,
22+ [ 'DoubleString' , / ^ " ( ( [ ^ \\ ] | ( \\ [ " ' \\ / b f n r t ] ) | ( \\ u \d { 4 } ) ) * ?) " / , 1 ] ,
23+ [ 'Comment' , / ^ ( # ) | ( \/ \/ ) ( [ ^ \n ] * ) / u ] ,
24+ [ 'Member' , / ^ [ ^ \s , : \[ \] \{ \} ] [ ^ \s ] * / , 0 ] ,
25+ [ 'Quoteless' , / ^ [ ^ \s , : \[ \] \{ \} ] [ ^ \n ] * / , 0 ]
26+ ]
3227
3328
3429export default class Tokenizer {
3530
36- #position;
31+ #position = 0 ;
3732 #string;
3833
3934 constructor ( string ) {
40- this . #position = 0 ;
41- this . #string = string ;
35+ this . #string = string
36+ . trim ( ) ;
4237 }
4338
4439
@@ -61,16 +56,21 @@ export default class Tokenizer {
6156
6257 const string = this . #string
6358
64- for ( const [ regex , type ] of tokens )
59+ for ( const [ type , regex , match ] of tokens )
6560 if ( regex . test ( string ) ) {
6661
67- const match = string . match ( regex ) ;
62+ const found = string . match ( regex ) ;
63+
64+ console . log ( type , found ) ;
65+
66+ this . #string = string . substring ( found [ 0 ] . length ) ;
6867
69- this . #string = string . substring ( match [ 0 ] . length ) ;
68+ const token = { type } ;
7069
71- const [ _ , value ] = match ;
70+ if ( match != null )
71+ token . value = found [ match ] ;
7272
73- return { type , value }
73+ return token ;
7474 }
7575
7676 throw `No Token Found for '${ string } '` ;
0 commit comments