Skip to content

Commit 93683bf

Browse files
authored
Parse the PHP 8.5 pipe operator and (void) cast in php8 (#37)
$value |> trim(...) and (void) foo(); were syntax errors. The pipe is a new ExprBinaryPipe node at PHP 8.5's precedence - looser than concatenation, tighter than comparison - and left-associative. The (void) cast is a new ExprCastVoid node; as in PHP it is only allowed as a statement or a for expression, not as a value. T_PIPE and T_VOID_CAST are appended to the token list.
1 parent bd037f3 commit 93683bf

17 files changed

Lines changed: 12611 additions & 12017 deletions

File tree

‎internal/php8/php8.go‎

Lines changed: 2662 additions & 2580 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/php8/php8.y‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ import (
166166
%token <token> T_PUBLIC_SET
167167
%token <token> T_PROTECTED_SET
168168
%token <token> T_PRIVATE_SET
169+
%token <token> T_PIPE
170+
%token <token> T_VOID_CAST
169171

170172
%token <token> '"'
171173
%token <token> '`'
@@ -216,6 +218,7 @@ import (
216218
%left '&' T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG
217219
%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL T_SPACESHIP
218220
%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL
221+
%left T_PIPE
219222
%left T_SL T_SR
220223
%left '+' '-' '.'
221224
%left '*' '/' '%'
@@ -1106,6 +1109,19 @@ statement:
11061109
SemiColonTkn: $2,
11071110
}
11081111
}
1112+
// PHP 8.5 `(void) expr;` discards a result on purpose; it is a statement, not an expression.
1113+
| T_VOID_CAST expr ';'
1114+
{
1115+
$$ = &ast.StmtExpression{
1116+
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
1117+
Expr: &ast.ExprCastVoid{
1118+
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
1119+
CastTkn: $1,
1120+
Expr: $2,
1121+
},
1122+
SemiColonTkn: $3,
1123+
}
1124+
}
11091125
| T_UNSET '(' unset_variables possible_comma ')' ';'
11101126
{
11111127
$3.(*ast.StmtUnset).UnsetTkn = $1
@@ -3227,6 +3243,27 @@ non_empty_for_exprs:
32273243

32283244
$$ = $1
32293245
}
3246+
| non_empty_for_exprs ',' T_VOID_CAST expr
3247+
{
3248+
$1.(*ParserSeparatedList).SeparatorTkns = append($1.(*ParserSeparatedList).SeparatorTkns, $2)
3249+
$1.(*ParserSeparatedList).Items = append($1.(*ParserSeparatedList).Items, &ast.ExprCastVoid{
3250+
Position: yylex.(*Parser).builder.NewTokenNodePosition($3, $4),
3251+
CastTkn: $3,
3252+
Expr: $4,
3253+
})
3254+
3255+
$$ = $1
3256+
}
3257+
| T_VOID_CAST expr
3258+
{
3259+
$$ = &ParserSeparatedList{
3260+
Items: []ast.Vertex{&ast.ExprCastVoid{
3261+
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
3262+
CastTkn: $1,
3263+
Expr: $2,
3264+
}},
3265+
}
3266+
}
32303267
| expr
32313268
{
32323269
$$ = &ParserSeparatedList{
@@ -3906,6 +3943,16 @@ expr_without_variable:
39063943
IfFalse: $4,
39073944
}
39083945
}
3946+
// PHP 8.5 pipe: `$value |> trim(...)` passes the left side to the callable on the right.
3947+
| expr T_PIPE expr
3948+
{
3949+
$$ = &ast.ExprBinaryPipe{
3950+
Position: yylex.(*Parser).builder.NewNodesPosition($1, $3),
3951+
Left: $1,
3952+
OpTkn: $2,
3953+
Right: $3,
3954+
}
3955+
}
39093956
| expr T_COALESCE expr
39103957
{
39113958
$$ = &ast.ExprBinaryCoalesce{
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package php8_test
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/rectorphp/php-parser-in-go/internal/php8"
8+
"github.com/rectorphp/php-parser-in-go/pkg/ast"
9+
"github.com/rectorphp/php-parser-in-go/pkg/conf"
10+
"github.com/rectorphp/php-parser-in-go/pkg/errors"
11+
"github.com/rectorphp/php-parser-in-go/pkg/version"
12+
"github.com/rectorphp/php-parser-in-go/pkg/visitor/printer"
13+
)
14+
15+
// parsePHP85 parses PHP 8.5 source and returns the root plus every parser error
16+
// it collected.
17+
func parsePHP85(source string) (ast.Vertex, []*errors.Error) {
18+
var parserErrors []*errors.Error
19+
config := conf.Config{
20+
Version: &version.Version{Major: 8, Minor: 5},
21+
ErrorHandlerFunc: func(parserError *errors.Error) { parserErrors = append(parserErrors, parserError) },
22+
}
23+
lexer := php8.NewLexer([]byte(source), config)
24+
parser := php8.NewParser(lexer, config)
25+
parser.Parse()
26+
return parser.GetRootNode(), parserErrors
27+
}
28+
29+
func TestPipeOperator(test *testing.T) {
30+
root, parserErrors := parsePHP85(`<?php $slug = $title |> trim(...) |> strtolower(...);`)
31+
if len(parserErrors) != 0 {
32+
test.Fatalf("unexpected parser errors: %v", parserErrors)
33+
}
34+
35+
assign := root.(*ast.Root).Stmts[0].(*ast.StmtExpression).Expr.(*ast.ExprAssign)
36+
outer, ok := assign.Expr.(*ast.ExprBinaryPipe)
37+
if !ok {
38+
test.Fatalf("expected a pipe, got %T", assign.Expr)
39+
}
40+
// the pipe is left-associative: ($title |> trim(...)) |> strtolower(...)
41+
if _, ok := outer.Left.(*ast.ExprBinaryPipe); !ok {
42+
test.Errorf("expected the left side to be the inner pipe, got %T", outer.Left)
43+
}
44+
if _, ok := outer.Right.(*ast.ExprFunctionCall); !ok {
45+
test.Errorf("expected the right side to be a first-class callable, got %T", outer.Right)
46+
}
47+
}
48+
49+
// The pipe binds tighter than comparison and looser than concatenation.
50+
func TestPipeOperatorPrecedence(test *testing.T) {
51+
root, parserErrors := parsePHP85(`<?php $matches = $a . $b |> strtoupper(...) === 'AB';`)
52+
if len(parserErrors) != 0 {
53+
test.Fatalf("unexpected parser errors: %v", parserErrors)
54+
}
55+
56+
assign := root.(*ast.Root).Stmts[0].(*ast.StmtExpression).Expr.(*ast.ExprAssign)
57+
identical, ok := assign.Expr.(*ast.ExprBinaryIdentical)
58+
if !ok {
59+
test.Fatalf("expected the comparison at the top, got %T", assign.Expr)
60+
}
61+
pipe, ok := identical.Left.(*ast.ExprBinaryPipe)
62+
if !ok {
63+
test.Fatalf("expected the pipe under the comparison, got %T", identical.Left)
64+
}
65+
if _, ok := pipe.Left.(*ast.ExprBinaryConcat); !ok {
66+
test.Errorf("expected the concatenation under the pipe, got %T", pipe.Left)
67+
}
68+
}
69+
70+
func TestPipeOperatorPrintsBackUnchanged(test *testing.T) {
71+
source := `<?php
72+
$slug = $title
73+
|> trim(...)
74+
|> strtolower(...);
75+
`
76+
77+
root, parserErrors := parsePHP85(source)
78+
if len(parserErrors) != 0 {
79+
test.Fatalf("unexpected parser errors: %v", parserErrors)
80+
}
81+
82+
output := bytes.NewBufferString("")
83+
root.Accept(printer.NewPrinter(output))
84+
if output.String() != source {
85+
test.Errorf("printed output differs from source:\n%s", output.String())
86+
}
87+
}

0 commit comments

Comments
 (0)