@@ -102,13 +102,6 @@ public static function parse(Parser $parser, TokensList $list, array $options =
102102 */
103103 $ type = 0 ;
104104
105- /**
106- * Whether an alias is expected
107- *
108- * @bool
109- */
110- $ alias = false ;
111-
112105 ++$ list ->idx ; // Skip 'CASE'
113106
114107 for (; $ list ->idx < $ list ->count ; ++$ list ->idx ) {
@@ -217,7 +210,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
217210 );
218211 } else {
219212
220- // Parse alias for CASE statement
213+ // Parse for alias of CASE expression
214+ $ asFound = false ;
221215 for (; $ list ->idx < $ list ->count ; ++$ list ->idx ) {
222216 $ token = $ list ->tokens [$ list ->idx ];
223217
@@ -228,39 +222,43 @@ public static function parse(Parser $parser, TokensList $list, array $options =
228222 continue ;
229223 }
230224
231- //
225+ // Handle optional AS keyword before alias
232226 if ($ token ->type === Token::TYPE_KEYWORD
233227 && $ token ->keyword === 'AS ' ){
234228
235- if ($ alias ) {
229+ if ($ asFound || ! empty ( $ ret -> alias ) ) {
236230 $ parser ->error (
237- 'An alias was expected . ' ,
231+ 'Potential duplicate alias of CASE expression . ' ,
238232 $ token
239233 );
240234 break ;
241235 }
242- $ alias = true ;
236+ $ asFound = true ;
243237 continue ;
244238 }
245239
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+ ){
247245
248246 // An alias is expected (the keyword `AS` was previously found).
249247 if (!empty ($ ret ->alias )) {
250248 $ parser ->error ('An alias was previously found. ' , $ token );
251249 break ;
252250 }
253251 $ ret ->alias = $ token ->value ;
254- $ alias = false ;
252+ $ asFound = false ;
255253
256254 continue ;
257255 }
258256
259257 break ;
260258 }
261- if ($ alias ) {
259+ if ($ asFound ) {
262260 $ parser ->error (
263- 'An alias was expected. ' ,
261+ 'An alias was expected after AS . ' ,
264262 $ list ->tokens [$ list ->idx - 1 ]
265263 );
266264 }
0 commit comments