Skip to content

Commit bf6db0a

Browse files
committed
Add gte, lte, gt and lt ternary expressions support
1 parent 5376bab commit bf6db0a

11 files changed

+285
-29
lines changed

resources/grammar.php

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
new \Phplrt\Parser\Grammar\Concatenation([49, 45, 50, 51, 52]),
132132
new \Phplrt\Parser\Grammar\Concatenation([16, 56]),
133133
new \Phplrt\Parser\Grammar\Concatenation([56]),
134-
new \Phplrt\Parser\Grammar\Concatenation([128]),
134+
new \Phplrt\Parser\Grammar\Concatenation([129]),
135135
new \Phplrt\Parser\Grammar\Concatenation([64, 68, 69]),
136136
new \Phplrt\Parser\Grammar\Concatenation([83, 56]),
137137
new \Phplrt\Parser\Grammar\Lexeme('T_PARENTHESIS_OPEN', false),
@@ -188,43 +188,44 @@
188188
new \Phplrt\Parser\Grammar\Alternation([53, 96]),
189189
new \Phplrt\Parser\Grammar\Optional(110),
190190
new \Phplrt\Parser\Grammar\Concatenation([2, 111]),
191-
new \Phplrt\Parser\Grammar\Concatenation([129]),
191+
new \Phplrt\Parser\Grammar\Concatenation([130]),
192192
new \Phplrt\Parser\Grammar\Optional(116),
193193
new \Phplrt\Parser\Grammar\Concatenation([113, 114]),
194-
new \Phplrt\Parser\Grammar\Concatenation([119, 56, 120, 56, 121, 56]),
194+
new \Phplrt\Parser\Grammar\Concatenation([119, 120, 121, 56, 122, 56]),
195195
new \Phplrt\Parser\Grammar\Concatenation([28, 116]),
196196
new \Phplrt\Parser\Grammar\Alternation([115, 117]),
197-
new \Phplrt\Parser\Grammar\Alternation([122, 123, 124, 125, 126, 127]),
197+
new \Phplrt\Parser\Grammar\Alternation([123, 124, 125, 126, 127, 128]),
198+
new \Phplrt\Parser\Grammar\Alternation([56, 28]),
198199
new \Phplrt\Parser\Grammar\Lexeme('T_QMARK', false),
199200
new \Phplrt\Parser\Grammar\Lexeme('T_COLON', false),
200201
new \Phplrt\Parser\Grammar\Lexeme('T_EQ', true),
201202
new \Phplrt\Parser\Grammar\Lexeme('T_NEQ', true),
202-
new \Phplrt\Parser\Grammar\Lexeme('T_LTE', true),
203203
new \Phplrt\Parser\Grammar\Lexeme('T_GTE', true),
204+
new \Phplrt\Parser\Grammar\Lexeme('T_LTE', true),
204205
new \Phplrt\Parser\Grammar\Lexeme('T_ANGLE_BRACKET_OPEN', true),
205206
new \Phplrt\Parser\Grammar\Lexeme('T_ANGLE_BRACKET_CLOSE', true),
206207
new \Phplrt\Parser\Grammar\Concatenation([118]),
207-
new \Phplrt\Parser\Grammar\Concatenation([130, 133]),
208-
new \Phplrt\Parser\Grammar\Concatenation([134, 137]),
208+
new \Phplrt\Parser\Grammar\Concatenation([131, 134]),
209+
new \Phplrt\Parser\Grammar\Concatenation([135, 138]),
209210
new \Phplrt\Parser\Grammar\Lexeme('T_OR', false),
210-
new \Phplrt\Parser\Grammar\Concatenation([131, 129]),
211-
new \Phplrt\Parser\Grammar\Optional(132),
212-
new \Phplrt\Parser\Grammar\Concatenation([138]),
211+
new \Phplrt\Parser\Grammar\Concatenation([132, 130]),
212+
new \Phplrt\Parser\Grammar\Optional(133),
213+
new \Phplrt\Parser\Grammar\Concatenation([139]),
213214
new \Phplrt\Parser\Grammar\Lexeme('T_AMP', false),
214-
new \Phplrt\Parser\Grammar\Concatenation([135, 130]),
215-
new \Phplrt\Parser\Grammar\Optional(136),
216-
new \Phplrt\Parser\Grammar\Alternation([141, 139]),
217-
new \Phplrt\Parser\Grammar\Concatenation([142, 146]),
215+
new \Phplrt\Parser\Grammar\Concatenation([136, 131]),
216+
new \Phplrt\Parser\Grammar\Optional(137),
217+
new \Phplrt\Parser\Grammar\Alternation([142, 140]),
218+
new \Phplrt\Parser\Grammar\Concatenation([143, 147]),
218219
new \Phplrt\Parser\Grammar\Lexeme('T_QMARK', true),
219-
new \Phplrt\Parser\Grammar\Concatenation([140, 139]),
220-
new \Phplrt\Parser\Grammar\Alternation([149, 29, 20, 63, 112]),
220+
new \Phplrt\Parser\Grammar\Concatenation([141, 140]),
221+
new \Phplrt\Parser\Grammar\Alternation([150, 29, 20, 63, 112]),
221222
new \Phplrt\Parser\Grammar\Lexeme('T_SQUARE_BRACKET_OPEN', true),
222223
new \Phplrt\Parser\Grammar\Lexeme('T_SQUARE_BRACKET_CLOSE', false),
223-
new \Phplrt\Parser\Grammar\Concatenation([143, 144]),
224-
new \Phplrt\Parser\Grammar\Repetition(145, 0, INF),
224+
new \Phplrt\Parser\Grammar\Concatenation([144, 145]),
225+
new \Phplrt\Parser\Grammar\Repetition(146, 0, INF),
225226
new \Phplrt\Parser\Grammar\Lexeme('T_PARENTHESIS_OPEN', false),
226227
new \Phplrt\Parser\Grammar\Lexeme('T_PARENTHESIS_CLOSE', false),
227-
new \Phplrt\Parser\Grammar\Concatenation([147, 56, 148]),
228+
new \Phplrt\Parser\Grammar\Concatenation([148, 56, 149]),
228229
],
229230
'reducers' => [
230231
0 => static function (\Phplrt\Parser\Context $ctx, $children) {
@@ -522,6 +523,22 @@
522523
$children[0],
523524
$children[2],
524525
),
526+
'T_GTE' => new Node\Stmt\Condition\GreaterOrEqualThanConditionNode(
527+
$children[0],
528+
$children[2],
529+
),
530+
'T_ANGLE_BRACKET_CLOSE' => new Node\Stmt\Condition\GreaterThanConditionNode(
531+
$children[0],
532+
$children[2],
533+
),
534+
'T_LTE' => new Node\Stmt\Condition\LessOrEqualThanConditionNode(
535+
$children[0],
536+
$children[2],
537+
),
538+
'T_ANGLE_BRACKET_OPEN' => new Node\Stmt\Condition\LessThanConditionNode(
539+
$children[0],
540+
$children[2],
541+
),
525542
default => throw SemanticException::fromInvalidConditionalOperator(
526543
$children[1]->getValue(),
527544
$offset,
@@ -534,7 +551,7 @@
534551
$children[4],
535552
);
536553
},
537-
129 => function (\Phplrt\Parser\Context $ctx, $children) {
554+
130 => function (\Phplrt\Parser\Context $ctx, $children) {
538555
// The "$offset" variable is an auto-generated
539556
$offset = $ctx->lastProcessedToken->getOffset();
540557

@@ -548,7 +565,7 @@
548565

549566
return $children;
550567
},
551-
130 => function (\Phplrt\Parser\Context $ctx, $children) {
568+
131 => function (\Phplrt\Parser\Context $ctx, $children) {
552569
// The "$offset" variable is an auto-generated
553570
$offset = $ctx->lastProcessedToken->getOffset();
554571

@@ -562,14 +579,14 @@
562579

563580
return $children;
564581
},
565-
138 => static function (\Phplrt\Parser\Context $ctx, $children) {
582+
139 => static function (\Phplrt\Parser\Context $ctx, $children) {
566583
if (\is_array($children)) {
567584
return new Node\Stmt\NullableTypeNode($children[1]);
568585
}
569586

570587
return $children;
571588
},
572-
139 => function (\Phplrt\Parser\Context $ctx, $children) {
589+
140 => function (\Phplrt\Parser\Context $ctx, $children) {
573590
// The "$offset" variable is an auto-generated
574591
$offset = $ctx->lastProcessedToken->getOffset();
575592

resources/grammar/ternary.pp2

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ TernaryExpressionOrLogicalType -> {
1919
$children[0],
2020
$children[2],
2121
),
22+
'T_GTE' => new Node\Stmt\Condition\GreaterOrEqualThanConditionNode(
23+
$children[0],
24+
$children[2],
25+
),
26+
'T_ANGLE_BRACKET_CLOSE' => new Node\Stmt\Condition\GreaterThanConditionNode(
27+
$children[0],
28+
$children[2],
29+
),
30+
'T_LTE' => new Node\Stmt\Condition\LessOrEqualThanConditionNode(
31+
$children[0],
32+
$children[2],
33+
),
34+
'T_ANGLE_BRACKET_OPEN' => new Node\Stmt\Condition\LessThanConditionNode(
35+
$children[0],
36+
$children[2],
37+
),
2238
default => throw SemanticException::fromInvalidConditionalOperator(
2339
$children[1]->getValue(),
2440
$offset,
@@ -40,16 +56,16 @@ OptionalTernaryExpressionSuffix
4056
;
4157

4258
TernaryExpressionSuffix
43-
: TernaryExpressionOperator() Type()
59+
: TernaryExpressionOperator() (Type() | VariableLiteral())
4460
::T_QMARK:: Type()
4561
::T_COLON:: Type()
4662
;
4763

4864
TernaryExpressionOperator
4965
: <T_EQ>
5066
| <T_NEQ>
51-
| <T_LTE>
5267
| <T_GTE>
68+
| <T_LTE>
5369
| <T_ANGLE_BRACKET_OPEN>
5470
| <T_ANGLE_BRACKET_CLOSE>
5571
;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Parser\Node\Stmt\Condition;
6+
7+
final class GreaterOrEqualThanConditionNode extends Condition {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Parser\Node\Stmt\Condition;
6+
7+
final class GreaterThanConditionNode extends Condition {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Parser\Node\Stmt\Condition;
6+
7+
final class LessOrEqualThanConditionNode extends Condition {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Parser\Node\Stmt\Condition;
6+
7+
final class LessThanConditionNode extends Condition {}

tests/Unit/Types/ArrayShapesTest.php renamed to tests/Unit/Types/ArrayShapeTypesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHPUnit\Framework\Attributes\Group;
88

99
#[Group('unit'), Group('type-lang/parser')]
10-
class ArrayShapesTest extends TypesTestCase
10+
class ArrayShapeTypesTest extends TypesTestCase
1111
{
1212
public function testFields(): void
1313
{

tests/Unit/Types/GenericsTest.php renamed to tests/Unit/Types/GenericTypesTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHPUnit\Framework\Attributes\Group;
88

99
#[Group('unit'), Group('type-lang/parser')]
10-
class GenericsTest extends TypesTestCase
10+
class GenericTypesTest extends TypesTestCase
1111
{
1212
public function testArguments(): void
1313
{
@@ -72,4 +72,21 @@ public function testNestedGeneric(): void
7272
Name(string)
7373
OUTPUT);
7474
}
75+
76+
public function testHintedGenericParam(): void
77+
{
78+
$this->assertTypeStatementSame('array<out T, in U>', <<<'OUTPUT'
79+
Stmt\NamedTypeNode
80+
Name(array)
81+
Stmt\Template\ArgumentsListNode
82+
Stmt\Template\ArgumentNode
83+
Identifier(out)
84+
Stmt\NamedTypeNode
85+
Name(T)
86+
Stmt\Template\ArgumentNode
87+
Identifier(in)
88+
Stmt\NamedTypeNode
89+
Name(U)
90+
OUTPUT);
91+
}
7592
}

tests/Unit/Types/LiteralsTest.php renamed to tests/Unit/Types/LiteralTypesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use TypeLang\Parser\Node\Literal\StringLiteralNode;
1414

1515
#[Group('unit'), Group('type-lang/parser')]
16-
class LiteralsTest extends TypesTestCase
16+
class LiteralTypesTest extends TypesTestCase
1717
{
1818
#[DataProvider('floatLiteralsDataProvider')]
1919
public function testFloatLiteral(string $expr, float $value): void

tests/Unit/Types/NamesTypeTest.php renamed to tests/Unit/Types/SimpleNamedTypesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHPUnit\Framework\Attributes\Group;
88

99
#[Group('unit'), Group('type-lang/parser')]
10-
class NamesTypeTest extends TypesTestCase
10+
class SimpleNamedTypesTest extends TypesTestCase
1111
{
1212
public function testSimpleType(): void
1313
{

0 commit comments

Comments
 (0)