@@ -522,51 +522,135 @@ private function parseCallableReturnType(TokenIterator $tokens): Ast\Type\TypeNo
522
522
$ startIndex = $ tokens ->currentTokenIndex ();
523
523
if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_NULLABLE )) {
524
524
$ type = $ this ->parseNullable ($ tokens );
525
+ if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_SQUARE_BRACKET )) {
526
+ $ type = $ this ->tryParseArrayOrOffsetAccess ($ tokens , $ this ->enrichWithAttributes (
527
+ $ tokens ,
528
+ $ type ,
529
+ $ startLine ,
530
+ $ startIndex
531
+ ));
532
+ }
533
+
534
+ return $ type ;
525
535
526
536
} elseif ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_OPEN_PARENTHESES )) {
527
537
$ type = $ this ->parse ($ tokens );
528
538
$ tokens ->consumeTokenType (Lexer::TOKEN_CLOSE_PARENTHESES );
539
+ if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_SQUARE_BRACKET )) {
540
+ $ type = $ this ->tryParseArrayOrOffsetAccess ($ tokens , $ type );
541
+ }
542
+
543
+ return $ type ;
529
544
} elseif ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_THIS_VARIABLE )) {
530
545
$ type = new Ast \Type \ThisTypeNode ();
546
+ if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_SQUARE_BRACKET )) {
547
+ $ type = $ this ->tryParseArrayOrOffsetAccess ($ tokens , $ this ->enrichWithAttributes (
548
+ $ tokens ,
549
+ $ type ,
550
+ $ startLine ,
551
+ $ startIndex
552
+ ));
553
+ }
554
+
555
+ return $ type ;
531
556
} else {
532
- $ type = new Ast \Type \IdentifierTypeNode ($ tokens ->currentTokenValue ());
533
- $ tokens ->consumeTokenType (Lexer::TOKEN_IDENTIFIER );
557
+ $ currentTokenValue = $ tokens ->currentTokenValue ();
558
+ $ tokens ->pushSavePoint (); // because of ConstFetchNode
559
+ if ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_IDENTIFIER )) {
560
+ $ type = new Ast \Type \IdentifierTypeNode ($ currentTokenValue );
561
+
562
+ if (!$ tokens ->isCurrentTokenType (Lexer::TOKEN_DOUBLE_COLON )) {
563
+ if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_ANGLE_BRACKET )) {
564
+ $ type = $ this ->parseGeneric (
565
+ $ tokens ,
566
+ $ this ->enrichWithAttributes (
567
+ $ tokens ,
568
+ $ type ,
569
+ $ startLine ,
570
+ $ startIndex
571
+ )
572
+ );
573
+ if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_SQUARE_BRACKET )) {
574
+ $ type = $ this ->tryParseArrayOrOffsetAccess ($ tokens , $ this ->enrichWithAttributes (
575
+ $ tokens ,
576
+ $ type ,
577
+ $ startLine ,
578
+ $ startIndex
579
+ ));
580
+ }
581
+
582
+ } elseif ($ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_SQUARE_BRACKET )) {
583
+ $ type = $ this ->tryParseArrayOrOffsetAccess ($ tokens , $ this ->enrichWithAttributes (
584
+ $ tokens ,
585
+ $ type ,
586
+ $ startLine ,
587
+ $ startIndex
588
+ ));
589
+
590
+ } elseif (in_array ($ type ->name , ['array ' , 'list ' , 'object ' ], true ) && $ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_CURLY_BRACKET ) && !$ tokens ->isPrecededByHorizontalWhitespace ()) {
591
+ if ($ type ->name === 'object ' ) {
592
+ $ type = $ this ->parseObjectShape ($ tokens );
593
+ } else {
594
+ $ type = $ this ->parseArrayShape ($ tokens , $ this ->enrichWithAttributes (
595
+ $ tokens ,
596
+ $ type ,
597
+ $ startLine ,
598
+ $ startIndex
599
+ ), $ type ->name );
600
+ }
601
+
602
+ if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_SQUARE_BRACKET )) {
603
+ $ type = $ this ->tryParseArrayOrOffsetAccess ($ tokens , $ this ->enrichWithAttributes (
604
+ $ tokens ,
605
+ $ type ,
606
+ $ startLine ,
607
+ $ startIndex
608
+ ));
609
+ }
610
+ }
534
611
535
- if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_ANGLE_BRACKET )) {
536
- $ type = $ this ->parseGeneric (
537
- $ tokens ,
538
- $ this ->enrichWithAttributes (
539
- $ tokens ,
540
- $ type ,
541
- $ startLine ,
542
- $ startIndex
543
- )
544
- );
545
-
546
- } elseif (in_array ($ type ->name , ['array ' , 'list ' , 'object ' ], true ) && $ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_CURLY_BRACKET ) && !$ tokens ->isPrecededByHorizontalWhitespace ()) {
547
- if ($ type ->name === 'object ' ) {
548
- $ type = $ this ->parseObjectShape ($ tokens );
612
+ return $ type ;
549
613
} else {
550
- $ type = $ this ->parseArrayShape ($ tokens , $ this ->enrichWithAttributes (
551
- $ tokens ,
552
- $ type ,
553
- $ startLine ,
554
- $ startIndex
555
- ), $ type ->name );
614
+ $ tokens ->rollback (); // because of ConstFetchNode
556
615
}
616
+ } else {
617
+ $ tokens ->dropSavePoint (); // because of ConstFetchNode
557
618
}
558
619
}
559
620
560
- if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_SQUARE_BRACKET )) {
561
- $ type = $ this ->tryParseArrayOrOffsetAccess ($ tokens , $ this ->enrichWithAttributes (
562
- $ tokens ,
563
- $ type ,
564
- $ startLine ,
565
- $ startIndex
566
- ));
621
+ $ exception = new ParserException (
622
+ $ tokens ->currentTokenValue (),
623
+ $ tokens ->currentTokenType (),
624
+ $ tokens ->currentTokenOffset (),
625
+ Lexer::TOKEN_IDENTIFIER ,
626
+ null ,
627
+ $ tokens ->currentTokenLine ()
628
+ );
629
+
630
+ if ($ this ->constExprParser === null ) {
631
+ throw $ exception ;
567
632
}
568
633
569
- return $ type ;
634
+ try {
635
+ $ constExpr = $ this ->constExprParser ->parse ($ tokens , true );
636
+ if ($ constExpr instanceof Ast \ConstExpr \ConstExprArrayNode) {
637
+ throw $ exception ;
638
+ }
639
+
640
+ $ type = new Ast \Type \ConstTypeNode ($ constExpr );
641
+ if ($ tokens ->isCurrentTokenType (Lexer::TOKEN_OPEN_SQUARE_BRACKET )) {
642
+ $ type = $ this ->tryParseArrayOrOffsetAccess ($ tokens , $ this ->enrichWithAttributes (
643
+ $ tokens ,
644
+ $ type ,
645
+ $ startLine ,
646
+ $ startIndex
647
+ ));
648
+ }
649
+
650
+ return $ type ;
651
+ } catch (LogicException $ e ) {
652
+ throw $ exception ;
653
+ }
570
654
}
571
655
572
656
0 commit comments