@@ -22,11 +22,18 @@ class TypeParser
22
22
/** @var bool */
23
23
private $ useLinesAttributes ;
24
24
25
+ /** @var bool */
26
+ private $ useCommentsAttributes ;
27
+
25
28
/** @var bool */
26
29
private $ useIndexAttributes ;
27
30
28
31
/**
29
- * @param array{lines?: bool, indexes?: bool} $usedAttributes
32
+ * @param array{
33
+ * lines?: bool,
34
+ * indexes?: bool,
35
+ * comments?: bool
36
+ * } $usedAttributes
30
37
*/
31
38
public function __construct (
32
39
?ConstExprParser $ constExprParser = null ,
@@ -38,6 +45,7 @@ public function __construct(
38
45
$ this ->quoteAwareConstExprString = $ quoteAwareConstExprString ;
39
46
$ this ->useLinesAttributes = $ usedAttributes ['lines ' ] ?? false ;
40
47
$ this ->useIndexAttributes = $ usedAttributes ['indexes ' ] ?? false ;
48
+ $ this ->useCommentsAttributes = $ usedAttributes ['comments ' ] ?? false ;
41
49
}
42
50
43
51
/** @phpstan-impure */
@@ -66,15 +74,29 @@ public function parse(TokenIterator $tokens): Ast\Type\TypeNode
66
74
* @internal
67
75
* @template T of Ast\Node
68
76
* @param T $type
77
+ * @param list<Ast\Comment> $comments
69
78
* @return T
70
79
*/
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
72
81
{
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
+
73
91
if ($ this ->useLinesAttributes ) {
74
92
$ type ->setAttribute (Ast \Attribute::START_LINE , $ startLine );
75
93
$ type ->setAttribute (Ast \Attribute::END_LINE , $ tokens ->currentTokenLine ());
76
94
}
77
95
96
+ if ($ this ->useCommentsAttributes ) {
97
+ $ type ->setAttribute (Ast \Attribute::COMMENT , $ comments );
98
+ }
99
+
78
100
if ($ this ->useIndexAttributes ) {
79
101
$ type ->setAttribute (Ast \Attribute::START_INDEX , $ startIndex );
80
102
$ type ->setAttribute (Ast \Attribute::END_INDEX , $ tokens ->endIndexOfLastRelevantToken ());
@@ -136,6 +158,7 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode
136
158
return $ this ->enrichWithAttributes ($ tokens , $ type , $ startLine , $ startIndex );
137
159
}
138
160
161
+
139
162
if ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_THIS_VARIABLE )) {
140
163
$ type = $ this ->enrichWithAttributes ($ tokens , new Ast \Type \ThisTypeNode (), $ startLine , $ startIndex );
141
164
@@ -748,28 +771,24 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
748
771
$ items = [];
749
772
$ sealed = true ;
750
773
774
+ $ comments = [];
775
+
751
776
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
+ };
770
789
771
790
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 );
773
792
}
774
793
775
794
if ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_VARIADIC )) {
@@ -778,23 +797,15 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
778
797
break ;
779
798
}
780
799
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
+ );
798
809
799
810
$ tokens ->tryConsumeTokenType (Lexer::TOKEN_PHPDOC_EOL );
800
811
} while ($ tokens ->tryConsumeTokenType (Lexer::TOKEN_COMMA ));
@@ -805,25 +816,6 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
805
816
return new Ast \Type \ArrayShapeNode ($ items , $ sealed , $ kind );
806
817
}
807
818
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
- }
827
819
828
820
/** @phpstan-impure */
829
821
private function parseArrayShapeItem (TokenIterator $ tokens ): Ast \Type \ArrayShapeItemNode
0 commit comments