Skip to content

Commit 6ea5f0a

Browse files
committed
Add support of callable parameter attributes
1 parent f6f9430 commit 6ea5f0a

File tree

3 files changed

+120
-83
lines changed

3 files changed

+120
-83
lines changed

resources/grammar.php

Lines changed: 100 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -130,77 +130,79 @@
130130
new \Phplrt\Parser\Grammar\Optional(48),
131131
new \Phplrt\Parser\Grammar\Lexeme('T_ANGLE_BRACKET_CLOSE', false),
132132
new \Phplrt\Parser\Grammar\Concatenation([49, 45, 50, 51, 52]),
133-
new \Phplrt\Parser\Grammar\Repetition(134, 1, INF),
133+
new \Phplrt\Parser\Grammar\Repetition(136, 1, INF),
134134
new \Phplrt\Parser\Grammar\Concatenation([16, 59]),
135135
new \Phplrt\Parser\Grammar\Concatenation([59]),
136136
new \Phplrt\Parser\Grammar\Optional(54),
137137
new \Phplrt\Parser\Grammar\Alternation([55, 56]),
138-
new \Phplrt\Parser\Grammar\Concatenation([154]),
138+
new \Phplrt\Parser\Grammar\Concatenation([156]),
139139
new \Phplrt\Parser\Grammar\Concatenation([67, 71, 72]),
140-
new \Phplrt\Parser\Grammar\Concatenation([86, 59]),
140+
new \Phplrt\Parser\Grammar\Concatenation([88, 59]),
141141
new \Phplrt\Parser\Grammar\Lexeme('T_PARENTHESIS_OPEN', false),
142142
new \Phplrt\Parser\Grammar\Optional(60),
143143
new \Phplrt\Parser\Grammar\Lexeme('T_PARENTHESIS_CLOSE', false),
144144
new \Phplrt\Parser\Grammar\Optional(61),
145145
new \Phplrt\Parser\Grammar\Concatenation([2, 62, 63, 64, 65]),
146-
new \Phplrt\Parser\Grammar\Concatenation([73, 75]),
146+
new \Phplrt\Parser\Grammar\Concatenation([74, 73]),
147147
new \Phplrt\Parser\Grammar\Lexeme('T_COMMA', false),
148148
new \Phplrt\Parser\Grammar\Concatenation([68, 67]),
149149
new \Phplrt\Parser\Grammar\Lexeme('T_COMMA', false),
150150
new \Phplrt\Parser\Grammar\Repetition(69, 0, INF),
151151
new \Phplrt\Parser\Grammar\Optional(70),
152-
new \Phplrt\Parser\Grammar\Concatenation([76, 77]),
152+
new \Phplrt\Parser\Grammar\Concatenation([75, 77]),
153+
new \Phplrt\Parser\Grammar\Optional(54),
154+
new \Phplrt\Parser\Grammar\Concatenation([78, 79]),
153155
new \Phplrt\Parser\Grammar\Lexeme('T_ASSIGN', true),
154-
new \Phplrt\Parser\Grammar\Optional(74),
155-
new \Phplrt\Parser\Grammar\Alternation([80, 83]),
156+
new \Phplrt\Parser\Grammar\Optional(76),
157+
new \Phplrt\Parser\Grammar\Alternation([82, 85]),
156158
new \Phplrt\Parser\Grammar\Optional(28),
157-
new \Phplrt\Parser\Grammar\Concatenation([59, 85]),
159+
new \Phplrt\Parser\Grammar\Concatenation([59, 87]),
158160
new \Phplrt\Parser\Grammar\Lexeme('T_ELLIPSIS', true),
159-
new \Phplrt\Parser\Grammar\Concatenation([79, 78]),
161+
new \Phplrt\Parser\Grammar\Concatenation([81, 80]),
160162
new \Phplrt\Parser\Grammar\Lexeme('T_ELLIPSIS', true),
161-
new \Phplrt\Parser\Grammar\Optional(81),
162-
new \Phplrt\Parser\Grammar\Concatenation([78, 82]),
163+
new \Phplrt\Parser\Grammar\Optional(83),
164+
new \Phplrt\Parser\Grammar\Concatenation([80, 84]),
163165
new \Phplrt\Parser\Grammar\Lexeme('T_AMP', true),
164-
new \Phplrt\Parser\Grammar\Optional(84),
166+
new \Phplrt\Parser\Grammar\Optional(86),
165167
new \Phplrt\Parser\Grammar\Lexeme('T_COLON', false),
166-
new \Phplrt\Parser\Grammar\Concatenation([102, 105]),
167-
new \Phplrt\Parser\Grammar\Concatenation([100, 101]),
168+
new \Phplrt\Parser\Grammar\Concatenation([104, 107]),
169+
new \Phplrt\Parser\Grammar\Concatenation([102, 103]),
168170
new \Phplrt\Parser\Grammar\Lexeme('T_COMMA', false),
169-
new \Phplrt\Parser\Grammar\Concatenation([89, 88]),
171+
new \Phplrt\Parser\Grammar\Concatenation([91, 90]),
172+
new \Phplrt\Parser\Grammar\Optional(92),
173+
new \Phplrt\Parser\Grammar\Concatenation([89, 93]),
170174
new \Phplrt\Parser\Grammar\Optional(90),
171-
new \Phplrt\Parser\Grammar\Concatenation([87, 91]),
172-
new \Phplrt\Parser\Grammar\Optional(88),
173175
new \Phplrt\Parser\Grammar\Lexeme('T_COMMA', false),
174176
new \Phplrt\Parser\Grammar\Lexeme('T_BRACE_OPEN', false),
175-
new \Phplrt\Parser\Grammar\Alternation([92, 93]),
176-
new \Phplrt\Parser\Grammar\Optional(94),
177+
new \Phplrt\Parser\Grammar\Alternation([94, 95]),
178+
new \Phplrt\Parser\Grammar\Optional(96),
177179
new \Phplrt\Parser\Grammar\Lexeme('T_BRACE_CLOSE', false),
178-
new \Phplrt\Parser\Grammar\Concatenation([95, 96, 97, 98]),
180+
new \Phplrt\Parser\Grammar\Concatenation([97, 98, 99, 100]),
179181
new \Phplrt\Parser\Grammar\Lexeme('T_ELLIPSIS', true),
180182
new \Phplrt\Parser\Grammar\Optional(53),
181-
new \Phplrt\Parser\Grammar\Concatenation([108, 109]),
183+
new \Phplrt\Parser\Grammar\Concatenation([110, 111]),
182184
new \Phplrt\Parser\Grammar\Lexeme('T_COMMA', false),
183-
new \Phplrt\Parser\Grammar\Concatenation([103, 102]),
184-
new \Phplrt\Parser\Grammar\Repetition(104, 0, INF),
185-
new \Phplrt\Parser\Grammar\Concatenation([110, 113, 114, 112]),
186-
new \Phplrt\Parser\Grammar\Concatenation([112]),
185+
new \Phplrt\Parser\Grammar\Concatenation([105, 104]),
186+
new \Phplrt\Parser\Grammar\Repetition(106, 0, INF),
187+
new \Phplrt\Parser\Grammar\Concatenation([112, 115, 116, 114]),
188+
new \Phplrt\Parser\Grammar\Concatenation([114]),
187189
new \Phplrt\Parser\Grammar\Optional(54),
188-
new \Phplrt\Parser\Grammar\Alternation([106, 107]),
190+
new \Phplrt\Parser\Grammar\Alternation([108, 109]),
189191
new \Phplrt\Parser\Grammar\Alternation([3, 23, 21]),
190192
new \Phplrt\Parser\Grammar\Lexeme('T_QMARK', true),
191193
new \Phplrt\Parser\Grammar\Concatenation([59]),
192-
new \Phplrt\Parser\Grammar\Optional(111),
194+
new \Phplrt\Parser\Grammar\Optional(113),
193195
new \Phplrt\Parser\Grammar\Lexeme('T_COLON', false),
194-
new \Phplrt\Parser\Grammar\Alternation([53, 99]),
195-
new \Phplrt\Parser\Grammar\Optional(115),
196-
new \Phplrt\Parser\Grammar\Concatenation([2, 116]),
197-
new \Phplrt\Parser\Grammar\Concatenation([155]),
198-
new \Phplrt\Parser\Grammar\Optional(121),
199-
new \Phplrt\Parser\Grammar\Concatenation([118, 119]),
200-
new \Phplrt\Parser\Grammar\Concatenation([124, 125, 126, 59, 127, 59]),
201-
new \Phplrt\Parser\Grammar\Concatenation([28, 121]),
202-
new \Phplrt\Parser\Grammar\Alternation([120, 122]),
203-
new \Phplrt\Parser\Grammar\Alternation([128, 129, 130, 131, 132, 133]),
196+
new \Phplrt\Parser\Grammar\Alternation([53, 101]),
197+
new \Phplrt\Parser\Grammar\Optional(117),
198+
new \Phplrt\Parser\Grammar\Concatenation([2, 118]),
199+
new \Phplrt\Parser\Grammar\Concatenation([157]),
200+
new \Phplrt\Parser\Grammar\Optional(123),
201+
new \Phplrt\Parser\Grammar\Concatenation([120, 121]),
202+
new \Phplrt\Parser\Grammar\Concatenation([126, 127, 128, 59, 129, 59]),
203+
new \Phplrt\Parser\Grammar\Concatenation([28, 123]),
204+
new \Phplrt\Parser\Grammar\Alternation([122, 124]),
205+
new \Phplrt\Parser\Grammar\Alternation([130, 131, 132, 133, 134, 135]),
204206
new \Phplrt\Parser\Grammar\Alternation([59, 28]),
205207
new \Phplrt\Parser\Grammar\Lexeme('T_QMARK', false),
206208
new \Phplrt\Parser\Grammar\Lexeme('T_COLON', false),
@@ -210,48 +212,48 @@
210212
new \Phplrt\Parser\Grammar\Lexeme('T_LTE', true),
211213
new \Phplrt\Parser\Grammar\Lexeme('T_ANGLE_BRACKET_OPEN', true),
212214
new \Phplrt\Parser\Grammar\Lexeme('T_ANGLE_BRACKET_CLOSE', true),
213-
new \Phplrt\Parser\Grammar\Concatenation([137, 135, 138, 139]),
214-
new \Phplrt\Parser\Grammar\Concatenation([140, 143]),
215+
new \Phplrt\Parser\Grammar\Concatenation([139, 137, 140, 141]),
216+
new \Phplrt\Parser\Grammar\Concatenation([142, 145]),
215217
new \Phplrt\Parser\Grammar\Lexeme('T_COMMA', false),
216218
new \Phplrt\Parser\Grammar\Lexeme('T_ATTR_OPEN', false),
217-
new \Phplrt\Parser\Grammar\Optional(136),
219+
new \Phplrt\Parser\Grammar\Optional(138),
218220
new \Phplrt\Parser\Grammar\Lexeme('T_SQUARE_BRACKET_CLOSE', false),
219-
new \Phplrt\Parser\Grammar\Concatenation([2, 145]),
221+
new \Phplrt\Parser\Grammar\Concatenation([2, 147]),
220222
new \Phplrt\Parser\Grammar\Lexeme('T_COMMA', false),
221-
new \Phplrt\Parser\Grammar\Concatenation([141, 140]),
222-
new \Phplrt\Parser\Grammar\Repetition(142, 0, INF),
223-
new \Phplrt\Parser\Grammar\Concatenation([150, 146, 151, 152, 153]),
224-
new \Phplrt\Parser\Grammar\Optional(144),
223+
new \Phplrt\Parser\Grammar\Concatenation([143, 142]),
224+
new \Phplrt\Parser\Grammar\Repetition(144, 0, INF),
225+
new \Phplrt\Parser\Grammar\Concatenation([152, 148, 153, 154, 155]),
226+
new \Phplrt\Parser\Grammar\Optional(146),
225227
new \Phplrt\Parser\Grammar\Concatenation([59]),
226228
new \Phplrt\Parser\Grammar\Lexeme('T_COMMA', false),
227-
new \Phplrt\Parser\Grammar\Concatenation([147, 146]),
229+
new \Phplrt\Parser\Grammar\Concatenation([149, 148]),
228230
new \Phplrt\Parser\Grammar\Lexeme('T_COMMA', false),
229231
new \Phplrt\Parser\Grammar\Lexeme('T_PARENTHESIS_OPEN', false),
230-
new \Phplrt\Parser\Grammar\Repetition(148, 0, INF),
231-
new \Phplrt\Parser\Grammar\Optional(149),
232+
new \Phplrt\Parser\Grammar\Repetition(150, 0, INF),
233+
new \Phplrt\Parser\Grammar\Optional(151),
232234
new \Phplrt\Parser\Grammar\Lexeme('T_PARENTHESIS_CLOSE', false),
233-
new \Phplrt\Parser\Grammar\Concatenation([123]),
234-
new \Phplrt\Parser\Grammar\Concatenation([156, 159]),
235-
new \Phplrt\Parser\Grammar\Concatenation([160, 163]),
235+
new \Phplrt\Parser\Grammar\Concatenation([125]),
236+
new \Phplrt\Parser\Grammar\Concatenation([158, 161]),
237+
new \Phplrt\Parser\Grammar\Concatenation([162, 165]),
236238
new \Phplrt\Parser\Grammar\Lexeme('T_OR', false),
237-
new \Phplrt\Parser\Grammar\Concatenation([157, 155]),
238-
new \Phplrt\Parser\Grammar\Optional(158),
239-
new \Phplrt\Parser\Grammar\Concatenation([164]),
239+
new \Phplrt\Parser\Grammar\Concatenation([159, 157]),
240+
new \Phplrt\Parser\Grammar\Optional(160),
241+
new \Phplrt\Parser\Grammar\Concatenation([166]),
240242
new \Phplrt\Parser\Grammar\Lexeme('T_AMP', false),
241-
new \Phplrt\Parser\Grammar\Concatenation([161, 156]),
242-
new \Phplrt\Parser\Grammar\Optional(162),
243-
new \Phplrt\Parser\Grammar\Alternation([167, 165]),
244-
new \Phplrt\Parser\Grammar\Concatenation([168, 172]),
243+
new \Phplrt\Parser\Grammar\Concatenation([163, 158]),
244+
new \Phplrt\Parser\Grammar\Optional(164),
245+
new \Phplrt\Parser\Grammar\Alternation([169, 167]),
246+
new \Phplrt\Parser\Grammar\Concatenation([170, 174]),
245247
new \Phplrt\Parser\Grammar\Lexeme('T_QMARK', true),
246-
new \Phplrt\Parser\Grammar\Concatenation([166, 165]),
247-
new \Phplrt\Parser\Grammar\Alternation([175, 29, 20, 66, 117]),
248+
new \Phplrt\Parser\Grammar\Concatenation([168, 167]),
249+
new \Phplrt\Parser\Grammar\Alternation([177, 29, 20, 66, 119]),
248250
new \Phplrt\Parser\Grammar\Lexeme('T_SQUARE_BRACKET_OPEN', true),
249251
new \Phplrt\Parser\Grammar\Lexeme('T_SQUARE_BRACKET_CLOSE', false),
250-
new \Phplrt\Parser\Grammar\Concatenation([169, 170]),
251-
new \Phplrt\Parser\Grammar\Repetition(171, 0, INF),
252+
new \Phplrt\Parser\Grammar\Concatenation([171, 172]),
253+
new \Phplrt\Parser\Grammar\Repetition(173, 0, INF),
252254
new \Phplrt\Parser\Grammar\Lexeme('T_PARENTHESIS_OPEN', false),
253255
new \Phplrt\Parser\Grammar\Lexeme('T_PARENTHESIS_CLOSE', false),
254-
new \Phplrt\Parser\Grammar\Concatenation([173, 59, 174]),
256+
new \Phplrt\Parser\Grammar\Concatenation([175, 59, 176]),
255257
],
256258
'reducers' => [
257259
0 => static function (\Phplrt\Parser\Context $ctx, $children) {
@@ -415,7 +417,23 @@
415417
type: $children[0] ?? null,
416418
);
417419
},
418-
67 => static function (\Phplrt\Parser\Context $ctx, $children) {
420+
67 => function (\Phplrt\Parser\Context $ctx, $children) {
421+
// The "$offset" variable is an auto-generated
422+
$offset = $ctx->lastProcessedToken->getOffset();
423+
424+
$result = \end($children);
425+
426+
if ($children[0] instanceof Node\Stmt\Attribute\AttributeGroupsListNode) {
427+
if ($this->attributes === false) {
428+
throw FeatureNotAllowedException::fromFeature('callable parameter attributes', $offset);
429+
}
430+
431+
$result->attributes = $children[0];
432+
}
433+
434+
return $result;
435+
},
436+
73 => static function (\Phplrt\Parser\Context $ctx, $children) {
419437
// The "$offset" variable is an auto-generated
420438
$offset = $ctx->lastProcessedToken->getOffset();
421439

@@ -430,7 +448,7 @@
430448
$children[0]->optional = true;
431449
return $children[0];
432450
},
433-
73 => static function (\Phplrt\Parser\Context $ctx, $children) {
451+
75 => static function (\Phplrt\Parser\Context $ctx, $children) {
434452
if (\count($children) === 1) {
435453

436454
return $children[0];
@@ -439,7 +457,7 @@
439457
$children[0]->name = $children[1];
440458
return $children[0];
441459
},
442-
76 => static function (\Phplrt\Parser\Context $ctx, $children) {
460+
78 => static function (\Phplrt\Parser\Context $ctx, $children) {
443461
if (!isset($children[1])) {
444462
return $children[0];
445463
}
@@ -452,7 +470,7 @@
452470
$children[1]->variadic = true;
453471
return $children[1];
454472
},
455-
78 => static function (\Phplrt\Parser\Context $ctx, $children) {
473+
80 => static function (\Phplrt\Parser\Context $ctx, $children) {
456474
$argument = new Node\Stmt\Callable\ParameterNode($children[0]);
457475

458476
if (\count($children) !== 1) {
@@ -461,7 +479,7 @@
461479

462480
return $argument;
463481
},
464-
87 => static function (\Phplrt\Parser\Context $ctx, $children) {
482+
89 => static function (\Phplrt\Parser\Context $ctx, $children) {
465483
// The "$offset" variable is an auto-generated
466484
$offset = $ctx->lastProcessedToken->getOffset();
467485

@@ -488,7 +506,7 @@
488506

489507
return new Node\Stmt\Shape\FieldsListNode($children);
490508
},
491-
99 => function (\Phplrt\Parser\Context $ctx, $children) {
509+
101 => function (\Phplrt\Parser\Context $ctx, $children) {
492510
// The "$offset" variable is an auto-generated
493511
$offset = $ctx->lastProcessedToken->getOffset();
494512

@@ -516,7 +534,7 @@
516534

517535
return \array_filter([$parameters, $fields]);
518536
},
519-
102 => function (\Phplrt\Parser\Context $ctx, $children) {
537+
104 => function (\Phplrt\Parser\Context $ctx, $children) {
520538
// The "$offset" variable is an auto-generated
521539
$offset = $ctx->lastProcessedToken->getOffset();
522540

@@ -532,7 +550,7 @@
532550

533551
return $result;
534552
},
535-
106 => static function (\Phplrt\Parser\Context $ctx, $children) {
553+
108 => static function (\Phplrt\Parser\Context $ctx, $children) {
536554
$name = $children[0];
537555
$value = \array_pop($children);
538556

@@ -547,10 +565,10 @@
547565
default => new Node\Stmt\Shape\NamedFieldNode($name, $value, $optional),
548566
};
549567
},
550-
107 => static function (\Phplrt\Parser\Context $ctx, $children) {
568+
109 => static function (\Phplrt\Parser\Context $ctx, $children) {
551569
return new Node\Stmt\Shape\ImplicitFieldNode($children[0]);
552570
},
553-
117 => static function (\Phplrt\Parser\Context $ctx, $children) {
571+
119 => static function (\Phplrt\Parser\Context $ctx, $children) {
554572
$fields = $parameters = null;
555573

556574
// Shape fields
@@ -569,7 +587,7 @@
569587
$fields,
570588
);
571589
},
572-
123 => function (\Phplrt\Parser\Context $ctx, $children) {
590+
125 => function (\Phplrt\Parser\Context $ctx, $children) {
573591
// The "$offset" variable is an auto-generated
574592
$offset = $ctx->lastProcessedToken->getOffset();
575593

@@ -620,21 +638,21 @@
620638
$children[4],
621639
);
622640
},
623-
134 => static function (\Phplrt\Parser\Context $ctx, $children) {
641+
136 => static function (\Phplrt\Parser\Context $ctx, $children) {
624642
return new Node\Stmt\Attribute\AttributeGroupNode($children);
625643
},
626-
140 => static function (\Phplrt\Parser\Context $ctx, $children) {
644+
142 => static function (\Phplrt\Parser\Context $ctx, $children) {
627645
return new Node\Stmt\Attribute\AttributeNode(
628646
$children[0],
629647
);
630648
},
631-
144 => static function (\Phplrt\Parser\Context $ctx, $children) {
649+
146 => static function (\Phplrt\Parser\Context $ctx, $children) {
632650
return new Node\Stmt\Attribute\AttributeArgumentsListNode($children);
633651
},
634-
146 => static function (\Phplrt\Parser\Context $ctx, $children) {
652+
148 => static function (\Phplrt\Parser\Context $ctx, $children) {
635653
return new Node\Stmt\Attribute\AttributeArgumentNode($children[0]);
636654
},
637-
155 => function (\Phplrt\Parser\Context $ctx, $children) {
655+
157 => function (\Phplrt\Parser\Context $ctx, $children) {
638656
// The "$offset" variable is an auto-generated
639657
$offset = $ctx->lastProcessedToken->getOffset();
640658

@@ -648,7 +666,7 @@
648666

649667
return $children;
650668
},
651-
156 => function (\Phplrt\Parser\Context $ctx, $children) {
669+
158 => function (\Phplrt\Parser\Context $ctx, $children) {
652670
// The "$offset" variable is an auto-generated
653671
$offset = $ctx->lastProcessedToken->getOffset();
654672

@@ -662,14 +680,14 @@
662680

663681
return $children;
664682
},
665-
164 => static function (\Phplrt\Parser\Context $ctx, $children) {
683+
166 => static function (\Phplrt\Parser\Context $ctx, $children) {
666684
if (\is_array($children)) {
667685
return new Node\Stmt\NullableTypeNode($children[1]);
668686
}
669687

670688
return $children;
671689
},
672-
165 => function (\Phplrt\Parser\Context $ctx, $children) {
690+
167 => function (\Phplrt\Parser\Context $ctx, $children) {
673691
// The "$offset" variable is an auto-generated
674692
$offset = $ctx->lastProcessedToken->getOffset();
675693

resources/grammar/callable.pp2

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,24 @@ CallableType -> {
2626
CallableParameters -> {
2727
return new Node\Stmt\Callable\ParametersListNode($children);
2828
}
29-
: OptionalCallableParameter() (::T_COMMA:: OptionalCallableParameter())* ::T_COMMA::?
29+
: CallableParameter() (::T_COMMA:: CallableParameter())* ::T_COMMA::?
30+
;
31+
32+
CallableParameter -> {
33+
$result = \end($children);
34+
35+
if ($children[0] instanceof Node\Stmt\Attribute\AttributeGroupsListNode) {
36+
if ($this->attributes === false) {
37+
throw FeatureNotAllowedException::fromFeature('callable parameter attributes', $offset);
38+
}
39+
40+
$result->attributes = $children[0];
41+
}
42+
43+
return $result;
44+
}
45+
: AttributeGroupsList()?
46+
OptionalCallableParameter()
3047
;
3148

3249
OptionalCallableParameter -> {

src/Node/Stmt/Callable/ParameterNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use TypeLang\Parser\Node\Literal\VariableLiteralNode;
88
use TypeLang\Parser\Node\Node;
9+
use TypeLang\Parser\Node\Stmt\Attribute\AttributeGroupsListNode;
910
use TypeLang\Parser\Node\Stmt\TypeStatement;
1011

1112
final class ParameterNode extends Node implements \Stringable
@@ -16,6 +17,7 @@ public function __construct(
1617
public bool $output = false,
1718
public bool $variadic = false,
1819
public bool $optional = false,
20+
public ?AttributeGroupsListNode $attributes = null,
1921
) {
2022
assert($type !== null || $name !== null, new \TypeError(
2123
'Required indication of the type or name of the parameter (one of)',

0 commit comments

Comments
 (0)