diff --git a/src/main/php/lang/ast/Tokens.class.php b/src/main/php/lang/ast/Tokens.class.php index c8b278f..886533f 100755 --- a/src/main/php/lang/ast/Tokens.class.php +++ b/src/main/php/lang/ast/Tokens.class.php @@ -17,7 +17,7 @@ class Tokens { '=' => ['===', '=>', '=='], '!' => ['!==', '!='], '&' => ['&&=', '&&', '&='], - '|' => ['||=', '||', '|='], + '|' => ['||=', '||', '|=', '|>'], '^' => ['^='], '+' => ['+=', '++'], '-' => ['-=', '--', '->'], @@ -25,7 +25,7 @@ class Tokens { '/' => ['/=', '//', '/*'], '~' => ['~='], '%' => ['%='], - '?' => ['?->', '??=', '?:', '??'], + '?' => ['?->', '?|>', '??=', '?:', '??'], '.' => ['...', '.='], ':' => ['::'], '[' => [], diff --git a/src/main/php/lang/ast/nodes/PipeExpression.class.php b/src/main/php/lang/ast/nodes/PipeExpression.class.php new file mode 100755 index 0000000..43c4d44 --- /dev/null +++ b/src/main/php/lang/ast/nodes/PipeExpression.class.php @@ -0,0 +1,19 @@ +expression= $expression; + $this->target= $target; + $this->line= $line; + } + + /** @return iterable */ + public function children() { + return [$this->expression, $this->target]; + } +} \ No newline at end of file diff --git a/src/main/php/lang/ast/syntax/PHP.class.php b/src/main/php/lang/ast/syntax/PHP.class.php index 80186a6..8aa88ab 100755 --- a/src/main/php/lang/ast/syntax/PHP.class.php +++ b/src/main/php/lang/ast/syntax/PHP.class.php @@ -43,6 +43,7 @@ NullSafeInstanceExpression, OffsetExpression, Parameter, + PipeExpression, Property, ReturnStatement, ScopeExpression, @@ -174,6 +175,16 @@ public function __construct() { return new ScopeExpression($scope, $expr, $token->line); }); + $this->infix('|>', 40, function($parse, $token, $left) { + return new PipeExpression($left, $this->expression($parse, 40), $left->line); + }); + + $this->infix('?|>', 40, function($parse, $node, $left) { + $value= new PipeExpression($left, $this->expression($parse, 40), $left->line); + $value->kind= 'nullsafepipe'; + return $value; + }); + $this->infix('(', 100, function($parse, $token, $left) { // Resolve ambiguity by looking ahead: `func(...)` which is a first-class @@ -207,7 +218,7 @@ public function __construct() { return new OffsetExpression($left, $expr, $left->line); }); - $this->infix('?', 80, function($parse, $token, $left) { + $this->infix('?', 30, function($parse, $token, $left) { $when= $this->expression($parse, 0); $parse->expecting(':', 'ternary'); $else= $this->expression($parse, 0);