Skip to content

Commit 0448980

Browse files
committed
Initial support for PHP 8.5 Pipe Operator
1 parent 7d134e8 commit 0448980

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

src/lexer/tokens.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ module.exports = {
342342
} else if (nchar === "|") {
343343
this.input();
344344
return this.tok.T_BOOLEAN_OR;
345+
} else if (nchar === ">") {
346+
this.input();
347+
return this.tok.T_PIPE;
345348
}
346349
return "|";
347350
},

src/parser/expr.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ module.exports = {
119119
if (this.token === this.tok.T_COALESCE) {
120120
return result("bin", "??", expr, this.next().read_expr());
121121
}
122+
// extra operations :
123+
// $a = "Hi" |> strtoupper(...);
124+
if (this.token === this.tok.T_PIPE) {
125+
if (this.version < 805) {
126+
this.raiseError("PHP 8.5+ is required to use pipe operator");
127+
}
128+
return result("bin", "|>", expr, this.next().read_expr());
129+
}
122130

123131
// extra operations :
124132
// $username = $_GET['user'] ? true : false;

src/tokens.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ const TokenNames = {
155155
T_NAME_RELATIVE: 241,
156156
T_NAME_QUALIFIED: 242,
157157
T_NAME_FULLY_QUALIFIED: 243,
158+
T_PIPE: 244,
158159
};
159160

160161
/**
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`Parse Pipeline Operator can parse basic pipeline operator task 1`] = `
4+
Program {
5+
"children": [
6+
ExpressionStatement {
7+
"expression": Assign {
8+
"kind": "assign",
9+
"left": Variable {
10+
"curly": false,
11+
"kind": "variable",
12+
"name": "c",
13+
},
14+
"operator": "=",
15+
"right": Bin {
16+
"kind": "bin",
17+
"left": Variable {
18+
"curly": false,
19+
"kind": "variable",
20+
"name": "a",
21+
},
22+
"right": Closure {
23+
"arguments": [
24+
Parameter {
25+
"attrGroups": [],
26+
"byref": false,
27+
"flags": 0,
28+
"kind": "parameter",
29+
"name": Identifier {
30+
"kind": "identifier",
31+
"name": "s",
32+
},
33+
"nullable": false,
34+
"readonly": false,
35+
"type": null,
36+
"value": null,
37+
"variadic": false,
38+
},
39+
],
40+
"attrGroups": [],
41+
"body": Bin {
42+
"kind": "bin",
43+
"left": Call {
44+
"arguments": [
45+
String {
46+
"isDoubleQuote": true,
47+
"kind": "string",
48+
"raw": "","",
49+
"unicode": false,
50+
"value": ",",
51+
},
52+
Variable {
53+
"curly": false,
54+
"kind": "variable",
55+
"name": "s",
56+
},
57+
],
58+
"kind": "call",
59+
"what": Name {
60+
"kind": "name",
61+
"name": "explode",
62+
"resolution": "uqn",
63+
},
64+
},
65+
"right": Bin {
66+
"kind": "bin",
67+
"left": Call {
68+
"arguments": [
69+
VariadicPlaceholder {
70+
"kind": "variadicplaceholder",
71+
},
72+
],
73+
"kind": "call",
74+
"what": Name {
75+
"kind": "name",
76+
"name": "array_filter",
77+
"resolution": "uqn",
78+
},
79+
},
80+
"right": String {
81+
"isDoubleQuote": false,
82+
"kind": "string",
83+
"raw": "'array_last'",
84+
"unicode": false,
85+
"value": "array_last",
86+
},
87+
"type": "|>",
88+
},
89+
"type": "|>",
90+
},
91+
"byref": false,
92+
"isStatic": false,
93+
"kind": "arrowfunc",
94+
"nullable": false,
95+
"type": null,
96+
},
97+
"type": "|>",
98+
},
99+
},
100+
"kind": "expressionstatement",
101+
},
102+
],
103+
"errors": [],
104+
"kind": "program",
105+
}
106+
`;

test/snapshot/pipe.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const parser = require("../main");
2+
3+
describe("Parse Pipeline Operator", () => {
4+
it("can parse basic pipeline operator task", () => {
5+
const parser8_5 = parser.create({
6+
parser: {
7+
version: "8.5",
8+
},
9+
});
10+
expect(
11+
parser8_5.parseEval(`
12+
$c = $a
13+
|> fn ($s) => explode(",", $s)
14+
|> array_filter(...) |> 'array_last' ;
15+
`),
16+
).toMatchSnapshot();
17+
});
18+
});

0 commit comments

Comments
 (0)