Skip to content

Commit e7ef350

Browse files
committed
Allow new lines in between everything and handle them correctly
1 parent 81de606 commit e7ef350

File tree

3 files changed

+273
-38
lines changed

3 files changed

+273
-38
lines changed

src/Parser/TokenIterator.php

+9
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ public function tryConsumeTokenType(int $tokenType): bool
205205
}
206206

207207

208+
/** @phpstan-impure */
209+
public function skipNewLineTokens(): void
210+
{
211+
do {
212+
$foundNewLine = $this->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
213+
} while ($foundNewLine === true);
214+
}
215+
216+
208217
private function detectNewline(): void
209218
{
210219
$value = $this->currentTokenValue();

src/Parser/TypeParser.php

+43-38
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private function subParse(TokenIterator $tokens): Ast\Type\TypeNode
9090
if ($tokens->isCurrentTokenValue('is')) {
9191
$type = $this->parseConditional($tokens, $type);
9292
} else {
93-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
93+
$tokens->skipNewLineTokens();
9494

9595
if ($tokens->isCurrentTokenType(Lexer::TOKEN_UNION)) {
9696
$type = $this->subParseUnion($tokens, $type);
@@ -112,9 +112,9 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode
112112
$startIndex = $tokens->currentTokenIndex();
113113

114114
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES)) {
115-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
115+
$tokens->skipNewLineTokens();
116116
$type = $this->subParse($tokens);
117-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
117+
$tokens->skipNewLineTokens();
118118

119119
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PARENTHESES);
120120

@@ -256,9 +256,9 @@ private function subParseUnion(TokenIterator $tokens, Ast\Type\TypeNode $type):
256256
$types = [$type];
257257

258258
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_UNION)) {
259-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
259+
$tokens->skipNewLineTokens();
260260
$types[] = $this->parseAtomic($tokens);
261-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
261+
$tokens->skipNewLineTokens();
262262
}
263263

264264
return new Ast\Type\UnionTypeNode($types);
@@ -284,9 +284,9 @@ private function subParseIntersection(TokenIterator $tokens, Ast\Type\TypeNode $
284284
$types = [$type];
285285

286286
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_INTERSECTION)) {
287-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
287+
$tokens->skipNewLineTokens();
288288
$types[] = $this->parseAtomic($tokens);
289-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
289+
$tokens->skipNewLineTokens();
290290
}
291291

292292
return new Ast\Type\IntersectionTypeNode($types);
@@ -306,15 +306,15 @@ private function parseConditional(TokenIterator $tokens, Ast\Type\TypeNode $subj
306306

307307
$targetType = $this->parse($tokens);
308308

309-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
309+
$tokens->skipNewLineTokens();
310310
$tokens->consumeTokenType(Lexer::TOKEN_NULLABLE);
311-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
311+
$tokens->skipNewLineTokens();
312312

313313
$ifType = $this->parse($tokens);
314314

315-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
315+
$tokens->skipNewLineTokens();
316316
$tokens->consumeTokenType(Lexer::TOKEN_COLON);
317-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
317+
$tokens->skipNewLineTokens();
318318

319319
$elseType = $this->subParse($tokens);
320320

@@ -335,15 +335,15 @@ private function parseConditionalForParameter(TokenIterator $tokens, string $par
335335

336336
$targetType = $this->parse($tokens);
337337

338-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
338+
$tokens->skipNewLineTokens();
339339
$tokens->consumeTokenType(Lexer::TOKEN_NULLABLE);
340-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
340+
$tokens->skipNewLineTokens();
341341

342342
$ifType = $this->parse($tokens);
343343

344-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
344+
$tokens->skipNewLineTokens();
345345
$tokens->consumeTokenType(Lexer::TOKEN_COLON);
346-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
346+
$tokens->skipNewLineTokens();
347347

348348
$elseType = $this->subParse($tokens);
349349

@@ -409,8 +409,13 @@ public function parseGeneric(TokenIterator $tokens, Ast\Type\IdentifierTypeNode
409409
$variances = [];
410410

411411
$isFirst = true;
412-
while ($isFirst || $tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
413-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
412+
while (
413+
$isFirst
414+
|| $tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)
415+
|| $tokens->tryConsumeTokenType(Lexer::TOKEN_UNION)
416+
|| $tokens->tryConsumeTokenType(Lexer::TOKEN_INTERSECTION)
417+
) {
418+
$tokens->skipNewLineTokens();
414419

415420
// trailing comma case
416421
if (!$isFirst && $tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET)) {
@@ -419,7 +424,7 @@ public function parseGeneric(TokenIterator $tokens, Ast\Type\IdentifierTypeNode
419424
$isFirst = false;
420425

421426
[$genericTypes[], $variances[]] = $this->parseGenericTypeArgument($tokens);
422-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
427+
$tokens->skipNewLineTokens();
423428
}
424429

425430
$type = new Ast\Type\GenericTypeNode($baseType, $genericTypes, $variances);
@@ -510,19 +515,19 @@ private function parseCallable(TokenIterator $tokens, Ast\Type\IdentifierTypeNod
510515
: [];
511516

512517
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES);
513-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
518+
$tokens->skipNewLineTokens();
514519

515520
$parameters = [];
516521
if (!$tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PARENTHESES)) {
517522
$parameters[] = $this->parseCallableParameter($tokens);
518-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
523+
$tokens->skipNewLineTokens();
519524
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
520-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
525+
$tokens->skipNewLineTokens();
521526
if ($tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PARENTHESES)) {
522527
break;
523528
}
524529
$parameters[] = $this->parseCallableParameter($tokens);
525-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
530+
$tokens->skipNewLineTokens();
526531
}
527532
}
528533

@@ -550,7 +555,7 @@ private function parseCallableTemplates(TokenIterator $tokens): array
550555

551556
$isFirst = true;
552557
while ($isFirst || $tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
553-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
558+
$tokens->skipNewLineTokens();
554559

555560
// trailing comma case
556561
if (!$isFirst && $tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET)) {
@@ -559,7 +564,7 @@ private function parseCallableTemplates(TokenIterator $tokens): array
559564
$isFirst = false;
560565

561566
$templates[] = $this->parseCallableTemplateArgument($tokens);
562-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
567+
$tokens->skipNewLineTokens();
563568
}
564569

565570
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
@@ -830,7 +835,7 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
830835
$unsealedType = null;
831836

832837
do {
833-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
838+
$tokens->skipNewLineTokens();
834839

835840
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET)) {
836841
return Ast\Type\ArrayShapeNode::createSealed($items, $kind);
@@ -839,14 +844,14 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
839844
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_VARIADIC)) {
840845
$sealed = false;
841846

842-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
847+
$tokens->skipNewLineTokens();
843848
if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) {
844849
if ($kind === Ast\Type\ArrayShapeNode::KIND_ARRAY) {
845850
$unsealedType = $this->parseArrayShapeUnsealedType($tokens);
846851
} else {
847852
$unsealedType = $this->parseListShapeUnsealedType($tokens);
848853
}
849-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
854+
$tokens->skipNewLineTokens();
850855
}
851856

852857
$tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA);
@@ -855,10 +860,10 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
855860

856861
$items[] = $this->parseArrayShapeItem($tokens);
857862

858-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
863+
$tokens->skipNewLineTokens();
859864
} while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA));
860865

861-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
866+
$tokens->skipNewLineTokens();
862867
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET);
863868

864869
if ($sealed) {
@@ -945,18 +950,18 @@ private function parseArrayShapeUnsealedType(TokenIterator $tokens): Ast\Type\Ar
945950
$startIndex = $tokens->currentTokenIndex();
946951

947952
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET);
948-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
953+
$tokens->skipNewLineTokens();
949954

950955
$valueType = $this->parse($tokens);
951-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
956+
$tokens->skipNewLineTokens();
952957

953958
$keyType = null;
954959
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
955-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
960+
$tokens->skipNewLineTokens();
956961

957962
$keyType = $valueType;
958963
$valueType = $this->parse($tokens);
959-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
964+
$tokens->skipNewLineTokens();
960965
}
961966

962967
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
@@ -978,10 +983,10 @@ private function parseListShapeUnsealedType(TokenIterator $tokens): Ast\Type\Arr
978983
$startIndex = $tokens->currentTokenIndex();
979984

980985
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET);
981-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
986+
$tokens->skipNewLineTokens();
982987

983988
$valueType = $this->parse($tokens);
984-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
989+
$tokens->skipNewLineTokens();
985990

986991
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
987992

@@ -1003,18 +1008,18 @@ private function parseObjectShape(TokenIterator $tokens): Ast\Type\ObjectShapeNo
10031008
$items = [];
10041009

10051010
do {
1006-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
1011+
$tokens->skipNewLineTokens();
10071012

10081013
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET)) {
10091014
return new Ast\Type\ObjectShapeNode($items);
10101015
}
10111016

10121017
$items[] = $this->parseObjectShapeItem($tokens);
10131018

1014-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
1019+
$tokens->skipNewLineTokens();
10151020
} while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA));
10161021

1017-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
1022+
$tokens->skipNewLineTokens();
10181023
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET);
10191024

10201025
return new Ast\Type\ObjectShapeNode($items);

0 commit comments

Comments
 (0)