@@ -102,13 +102,6 @@ public static function parse(Parser $parser, TokensList $list, array $options =
102
102
*/
103
103
$ type = 0 ;
104
104
105
- /**
106
- * Whether an alias is expected
107
- *
108
- * @bool
109
- */
110
- $ alias = false ;
111
-
112
105
++$ list ->idx ; // Skip 'CASE'
113
106
114
107
for (; $ list ->idx < $ list ->count ; ++$ list ->idx ) {
@@ -217,7 +210,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
217
210
);
218
211
} else {
219
212
220
- // Parse alias for CASE statement
213
+ // Parse for alias of CASE expression
214
+ $ asFound = false ;
221
215
for (; $ list ->idx < $ list ->count ; ++$ list ->idx ) {
222
216
$ token = $ list ->tokens [$ list ->idx ];
223
217
@@ -228,39 +222,43 @@ public static function parse(Parser $parser, TokensList $list, array $options =
228
222
continue ;
229
223
}
230
224
231
- //
225
+ // Handle optional AS keyword before alias
232
226
if ($ token ->type === Token::TYPE_KEYWORD
233
227
&& $ token ->keyword === 'AS ' ){
234
228
235
- if ($ alias ) {
229
+ if ($ asFound || ! empty ( $ ret -> alias ) ) {
236
230
$ parser ->error (
237
- 'An alias was expected . ' ,
231
+ 'Potential duplicate alias of CASE expression . ' ,
238
232
$ token
239
233
);
240
234
break ;
241
235
}
242
- $ alias = true ;
236
+ $ asFound = true ;
243
237
continue ;
244
238
}
245
239
246
- if ($ alias ){
240
+ if ($ asFound
241
+ || $ token ->type === Token::TYPE_STRING
242
+ || ($ token ->type === Token::TYPE_SYMBOL && !$ token ->flags & Token::FLAG_SYMBOL_VARIABLE )
243
+ || $ token ->type === Token::TYPE_NONE
244
+ ){
247
245
248
246
// An alias is expected (the keyword `AS` was previously found).
249
247
if (!empty ($ ret ->alias )) {
250
248
$ parser ->error ('An alias was previously found. ' , $ token );
251
249
break ;
252
250
}
253
251
$ ret ->alias = $ token ->value ;
254
- $ alias = false ;
252
+ $ asFound = false ;
255
253
256
254
continue ;
257
255
}
258
256
259
257
break ;
260
258
}
261
- if ($ alias ) {
259
+ if ($ asFound ) {
262
260
$ parser ->error (
263
- 'An alias was expected. ' ,
261
+ 'An alias was expected after AS . ' ,
264
262
$ list ->tokens [$ list ->idx - 1 ]
265
263
);
266
264
}
0 commit comments