@@ -127,25 +127,31 @@ private static function string(\Iterator $fragments): \Iterator
127127 $ fragments ->next ();
128128
129129 $ buffer = Buffer::empty ();
130- $ escape = false ;
131130
132131 while ($ fragments ->valid ()) {
133- /** @var Fragment $fragment */
134- $ fragment = $ fragments ->current ();
132+ switch ($ fragments ->current ()->value ) {
133+ case $ delimiter :
134+ yield from $ buffer ->flush (TokenType::STRING_QUOTED );
135+ $ fragments ->next ();
136+ return ;
135137
136- if (!$ escape && $ fragment ->value === $ delimiter ) {
137- yield from $ buffer ->flush (TokenType::STRING_QUOTED );
138- $ fragments ->next ();
139- return ;
140- }
138+ case '\\' :
139+ $ buffer ->append ($ fragments ->current ());
140+ $ fragments ->next ();
141141
142- $ escape = $ fragment ->value === '\\' ;
142+ if (!$ fragments ->valid ()) {
143+ throw new \Exception ("@TODO: Unexpected end of input " );
144+ }
143145
144- if (! $ escape ) {
145- $ buffer -> append ( $ fragment );
146- }
146+ $ buffer -> append ( $ fragments -> current ());
147+ $ fragments -> next ( );
148+ break ;
147149
148- $ fragments ->next ();
150+ default :
151+ $ buffer ->append ($ fragments ->current ());
152+ $ fragments ->next ();
153+ break ;
154+ }
149155 }
150156 }
151157
@@ -162,40 +168,50 @@ public static function templateLiteral(\Iterator $fragments): \Iterator
162168
163169 $ fragments ->next ();
164170
165- $ escape = false ;
166-
167171 while ($ fragments ->valid ()) {
168- /** @var Fragment $fragment */
169- $ fragment = $ fragments ->current ();
170172
171- if (! $ escape && $ fragment -> value === ' ` ' ) {
172- yield from $ buffer -> flush (TokenType:: STRING_QUOTED );
173- $ buffer ->append ( $ fragments -> current () );
174- yield from $ buffer ->flush (TokenType:: TEMPLATE_LITERAL_END );
175- $ fragments -> next ( );
176- return ;
177- }
173+ switch ( $ fragments -> current ()-> value ) {
174+ case ' ` ' :
175+ yield from $ buffer ->flush (TokenType:: STRING_QUOTED );
176+ $ buffer ->append ( $ fragments -> current () );
177+ yield from $ buffer -> flush (TokenType:: TEMPLATE_LITERAL_END );
178+ $ fragments -> next () ;
179+ return ;
178180
179- if (!$ escape && $ fragment ->value === '$ ' ) {
180- $ dollarSignBuffer = Buffer::empty ()->append ($ fragments ->current ());
181- $ fragments ->next ();
182- $ nextFragment = $ fragments ->current ();
181+ case '$ ' :
182+ $ dollarSignBuffer = Buffer::empty ()->append ($ fragments ->current ());
183+ $ fragments ->next ();
183184
184- if ($ nextFragment ->value === '{ ' ) {
185- yield from $ buffer ->flush (TokenType::STRING_QUOTED );
186- yield from $ dollarSignBuffer ->flush (TokenType::DOLLAR );
187- yield from self ::block ($ fragments );
188- continue ;
189- }
190- }
185+ if (!$ fragments ->valid ()) {
186+ throw new \Exception ("@TODO: Unexpected end of input " );
187+ }
188+
189+ $ nextFragment = $ fragments ->current ();
191190
192- $ escape = $ fragment ->value === '\\' ;
191+ if ($ nextFragment ->value === '{ ' ) {
192+ yield from $ buffer ->flush (TokenType::STRING_QUOTED );
193+ yield from $ dollarSignBuffer ->flush (TokenType::DOLLAR );
194+ yield from self ::block ($ fragments );
195+ }
196+ break ;
193197
194- if (! $ escape ) {
195- $ buffer ->append ($ fragment );
196- }
198+ case '\\' :
199+ $ buffer ->append ($ fragments -> current () );
200+ $ fragments -> next ();
197201
198- $ fragments ->next ();
202+ if (!$ fragments ->valid ()) {
203+ throw new \Exception ("@TODO: Unexpected end of input " );
204+ }
205+
206+ $ buffer ->append ($ fragments ->current ());
207+ $ fragments ->next ();
208+ break ;
209+
210+ default :
211+ $ buffer ->append ($ fragments ->current ());
212+ $ fragments ->next ();
213+ break ;
214+ }
199215 }
200216 }
201217
0 commit comments