Skip to content

Commit 9d013cb

Browse files
authored
Merge pull request #52 from xp-framework/feature/pipelines
Add syntactical support for pipelines with `|>` and `?|>`
2 parents efc22ec + 3a7f158 commit 9d013cb

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/main/php/lang/ast/Tokens.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ class Tokens {
1717
'=' => ['===', '=>', '=='],
1818
'!' => ['!==', '!='],
1919
'&' => ['&&=', '&&', '&='],
20-
'|' => ['||=', '||', '|='],
20+
'|' => ['||=', '||', '|=', '|>'],
2121
'^' => ['^='],
2222
'+' => ['+=', '++'],
2323
'-' => ['-=', '--', '->'],
2424
'*' => ['**=', '*=', '**'],
2525
'/' => ['/=', '//', '/*'],
2626
'~' => ['~='],
2727
'%' => ['%='],
28-
'?' => ['?->', '??=', '?:', '??'],
28+
'?' => ['?->', '?|>', '??=', '?:', '??'],
2929
'.' => ['...', '.='],
3030
':' => ['::'],
3131
'[' => [],
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php namespace lang\ast\nodes;
2+
3+
use lang\ast\Node;
4+
5+
class PipeExpression extends Node {
6+
public $kind= 'pipe';
7+
public $expression, $target;
8+
9+
public function __construct($expression, $target, $line= -1) {
10+
$this->expression= $expression;
11+
$this->target= $target;
12+
$this->line= $line;
13+
}
14+
15+
/** @return iterable */
16+
public function children() {
17+
return [$this->expression, $this->target];
18+
}
19+
}

src/main/php/lang/ast/syntax/PHP.class.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
NullSafeInstanceExpression,
4444
OffsetExpression,
4545
Parameter,
46+
PipeExpression,
4647
Property,
4748
ReturnStatement,
4849
ScopeExpression,
@@ -174,6 +175,16 @@ public function __construct() {
174175
return new ScopeExpression($scope, $expr, $token->line);
175176
});
176177

178+
$this->infix('|>', 40, function($parse, $token, $left) {
179+
return new PipeExpression($left, $this->expression($parse, 40), $left->line);
180+
});
181+
182+
$this->infix('?|>', 40, function($parse, $node, $left) {
183+
$value= new PipeExpression($left, $this->expression($parse, 40), $left->line);
184+
$value->kind= 'nullsafepipe';
185+
return $value;
186+
});
187+
177188
$this->infix('(', 100, function($parse, $token, $left) {
178189

179190
// Resolve ambiguity by looking ahead: `func(...)` which is a first-class
@@ -207,7 +218,7 @@ public function __construct() {
207218
return new OffsetExpression($left, $expr, $left->line);
208219
});
209220

210-
$this->infix('?', 80, function($parse, $token, $left) {
221+
$this->infix('?', 30, function($parse, $token, $left) {
211222
$when= $this->expression($parse, 0);
212223
$parse->expecting(':', 'ternary');
213224
$else= $this->expression($parse, 0);

0 commit comments

Comments
 (0)