@@ -22,11 +22,18 @@ class TypeParser
2222 /** @var bool */
2323 private $ useLinesAttributes ;
2424
25+ /** @var bool */
26+ private $ useCommentsAttributes ;
27+
2528 /** @var bool */
2629 private $ useIndexAttributes ;
2730
2831 /**
29- * @param array{lines?: bool, indexes?: bool} $usedAttributes
32+ * @param array{
33+ * lines?: bool,
34+ * indexes?: bool,
35+ * comments?: bool
36+ * } $usedAttributes
3037 */
3138 public function __construct (
3239 ?ConstExprParser $ constExprParser = null ,
@@ -38,6 +45,7 @@ public function __construct(
3845 $ this ->quoteAwareConstExprString = $ quoteAwareConstExprString ;
3946 $ this ->useLinesAttributes = $ usedAttributes ['lines ' ] ?? false ;
4047 $ this ->useIndexAttributes = $ usedAttributes ['indexes ' ] ?? false ;
48+ $ this ->useCommentsAttributes = $ usedAttributes ['comments ' ] ?? false ;
4149 }
4250
4351 /** @phpstan-impure */
@@ -66,15 +74,29 @@ public function parse(TokenIterator $tokens): Ast\Type\TypeNode
6674 * @internal
6775 * @template T of Ast\Node
6876 * @param T $type
77+ * @param list<Ast\Comment> $comments
6978 * @return T
7079 */
71- public function enrichWithAttributes (TokenIterator $ tokens , Ast \Node $ type , int $ startLine , int $ startIndex ): Ast \Node
80+ public function enrichWithAttributes (TokenIterator $ tokens , Ast \Node $ type , int $ startLine , int $ startIndex, array $ comments = [] ): Ast \Node
7281 {
82+ if ($ tokens ->currentTokenType () == Lexer::TOKEN_COMMENT ) {
83+ $ comments [] = new Ast \Comment (
84+ $ tokens ->currentTokenValue (),
85+ $ tokens ->currentTokenLine (),
86+ $ tokens ->currentTokenIndex ()
87+ );
88+ $ tokens ->next ();
89+ }
90+
7391 if ($ this ->useLinesAttributes ) {
7492 $ type ->setAttribute (Ast \Attribute::START_LINE , $ startLine );
7593 $ type ->setAttribute (Ast \Attribute::END_LINE , $ tokens ->currentTokenLine ());
7694 }
7795
96+ if ($ this ->useCommentsAttributes ) {
97+ $ type ->setAttribute (Ast \Attribute::COMMENT , $ comments );
98+ }
99+
78100 if ($ this ->useIndexAttributes ) {
79101 $ type ->setAttribute (Ast \Attribute::START_INDEX , $ startIndex );
80102 $ type ->setAttribute (Ast \Attribute::END_INDEX , $ tokens ->endIndexOfLastRelevantToken ());
@@ -136,6 +158,7 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode
136158 return $ this ->enrichWithAttributes ($ tokens , $ type , $ startLine , $ startIndex );
137159 }
138160
161+
139162 if ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_THIS_VARIABLE )) {
140163 $ type = $ this ->enrichWithAttributes ($ tokens , new Ast \Type \ThisTypeNode (), $ startLine , $ startIndex );
141164
@@ -748,28 +771,24 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
748771 $ items = [];
749772 $ sealed = true ;
750773
774+ $ comments = [];
775+
751776 do {
752- $ tokens ->pushSavePoint ();
753- $ tokens ->next ();
754- $ tab = "\t" ;
755- if ($ tokens ->currentTokenType () === Lexer::TOKEN_CLOSE_CURLY_BRACKET ) {
756- $ tab = '' ;
757- }
758- $ tokens ->rollback ();
759- if ($ tokens ->currentTokenType () === Lexer::TOKEN_PHPDOC_EOL ) {
760- $ startLine = $ tokens ->currentTokenLine ();
761- $ startIndex = $ tokens ->currentTokenIndex ();
762- $ items [] = $ this ->enrichWithAttributes (
763- $ tokens ,
764- new Ast \Type \IdentifierTypeNode ("\n$ tab " ),
765- $ startLine ,
766- $ startIndex
767- );
768- $ tokens ->next ();
769- }
777+ while (1 ) {
778+ if ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_PHPDOC_EOL )){
779+ continue ;
780+ }
781+ else if ($ tokens ->currentTokenType () == Lexer::TOKEN_COMMENT ) {
782+ $ comments [] = new Ast \Comment ($ tokens ->currentTokenValue (), $ tokens ->currentTokenLine (), $ tokens ->currentTokenIndex ());
783+ $ tokens ->next ();
784+ }
785+ else {
786+ break ;
787+ }
788+ };
770789
771790 if ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_CLOSE_CURLY_BRACKET )) {
772- return new Ast \Type \ArrayShapeNode ($ items , true , $ kind );
791+ return new Ast \Type \ArrayShapeNode ($ items , true , $ kind );
773792 }
774793
775794 if ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_VARIADIC )) {
@@ -778,23 +797,15 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
778797 break ;
779798 }
780799
781- if ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_COMMENT )) {
782- $ items [] = $ this ->parseSimpleComment ($ tokens );
783- }
784-
785- if ($ tokens ->currentTokenType () === Lexer::TOKEN_PHPDOC_EOL ) {
786- $ startLine = $ tokens ->currentTokenLine ();
787- $ startIndex = $ tokens ->currentTokenIndex ();
788- $ items [] = $ this ->enrichWithAttributes (
789- $ tokens ,
790- new Ast \Type \IdentifierTypeNode ("\n\t" ),
791- $ startLine ,
792- $ startIndex
793- );
794- $ tokens ->next ();
795- }
796-
797- $ items [] = $ this ->parseArrayShapeItem ($ tokens );
800+ $ startIndex = $ tokens ->currentTokenIndex ();
801+ $ startLine = $ tokens ->currentTokenLine ();
802+ $ items [] = $ this ->enrichWithAttributes (
803+ $ tokens ,
804+ $ this ->parseArrayShapeItem ($ tokens ),
805+ $ startLine ,
806+ $ startIndex ,
807+ $ comments
808+ );
798809
799810 $ tokens ->tryConsumeTokenType (Lexer::TOKEN_PHPDOC_EOL );
800811 } while ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_COMMA ));
@@ -805,25 +816,6 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
805816 return new Ast \Type \ArrayShapeNode ($ items , $ sealed , $ kind );
806817 }
807818
808- private function parseSimpleComment (TokenIterator $ tokens ): Ast \Type \CommentNode
809- {
810- $ startLine = $ tokens ->currentTokenLine ();
811- $ startIndex = $ tokens ->currentTokenIndex ();
812-
813- $ text = '' ;
814-
815- while ($ tokens ->currentTokenType () !== Lexer::TOKEN_PHPDOC_EOL ) {
816- $ text .= $ tokens ->currentTokenValue ();
817- $ tokens ->next (false );
818- }
819-
820- return $ this ->enrichWithAttributes (
821- $ tokens ,
822- new Ast \Type \CommentNode ($ text ),
823- $ startLine ,
824- $ startIndex
825- );
826- }
827819
828820 /** @phpstan-impure */
829821 private function parseArrayShapeItem (TokenIterator $ tokens ): Ast \Type \ArrayShapeItemNode
0 commit comments