File tree 1 file changed +7
-16
lines changed
1 file changed +7
-16
lines changed Original file line number Diff line number Diff line change @@ -50,28 +50,19 @@ public function tokenize(string $input): TokenStream
50
50
{
51
51
$ input = str_replace ("\r" , '' , $ input );
52
52
$ pattern = '~( ' . implode (')|( ' , self ::Patterns) . ')~Amixu ' ;
53
- $ res = preg_match_all ($ pattern , $ input , $ tokens , PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL );
53
+ $ res = preg_match_all ($ pattern , $ input , $ matches , PREG_SET_ORDER );
54
54
if ($ res === false ) {
55
55
throw new Exception ('Invalid UTF-8 sequence. ' );
56
56
}
57
57
58
58
$ types = array_keys (self ::Patterns);
59
59
$ offset = 0 ;
60
- foreach ($ tokens as &$ token ) {
61
- $ type = null ;
62
- for ($ i = 1 ; $ i <= count ($ types ); $ i ++) {
63
- if (isset ($ token [$ i ])) {
64
- $ type = $ types [$ i - 1 ];
65
- if ($ type === Token::Char) {
66
- $ type = $ token [0 ];
67
- }
68
-
69
- break ;
70
- }
71
- }
72
-
73
- $ token = new Token ($ token [0 ], $ type );
74
- $ offset += strlen ($ token ->value );
60
+
61
+ $ tokens = [];
62
+ foreach ($ matches as $ match ) {
63
+ $ type = $ types [count ($ match ) - 2 ];
64
+ $ tokens [] = new Token ($ match [0 ], $ type === Token::Char ? $ match [0 ] : $ type );
65
+ $ offset += strlen ($ match [0 ]);
75
66
}
76
67
77
68
$ stream = new TokenStream ($ tokens );
You can’t perform that action at this time.
0 commit comments