Skip to content

Commit cf3755c

Browse files
PHP: Fixed string interpolation (#2864)
1 parent fe98d53 commit cf3755c

File tree

3 files changed

+150
-34
lines changed

3 files changed

+150
-34
lines changed

components/prism-php.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@
230230
};
231231

232232
var string_interpolation = {
233-
pattern: /{\$(?:{(?:{[^{}]+}|[^{}]+)}|[^{}])+}|(^|[^\\{])\$+(?:\w+(?:\[[^\r\n\[\]]+\]|->\w+)*)/,
233+
pattern: /{\$(?:{(?:{[^{}]+}|[^{}]+)}|[^{}])+}|(^|[^\\{])\$+(?:\w+(?:\[[^\r\n\[\]]+\]|->\w+)?)/,
234234
lookbehind: true,
235235
inside: Prism.languages.php
236236
};

components/prism-php.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/php/string-interpolation_feature.test

Lines changed: 148 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,127 +14,243 @@ FOO;
1414
{${$name}}, but not {\${\$name}}
1515
FOO_BAR;
1616

17+
$value = "$this->property->property";
18+
$value = "$foo[0][1]";
19+
1720
----------------------------------------------------
1821

1922
[
2023
["string", [
2124
"\"This ",
22-
["interpolation", [["variable", "$variable"]]],
25+
["interpolation", [
26+
["variable", "$variable"]
27+
]],
2328
" is interpolated\""
2429
]],
30+
2531
["string", [
2632
"\"",
27-
["interpolation", [["variable", "$foo"], ["punctuation", "["], ["number", "2"], ["punctuation", "]"]]],
33+
["interpolation", [
34+
["variable", "$foo"],
35+
["punctuation", "["],
36+
["number", "2"],
37+
["punctuation", "]"]
38+
]],
2839
", ",
29-
["interpolation", [["variable", "$bar"], ["punctuation", "["], ["operator", "-"], ["number", "4"], ["punctuation", "]"]]],
40+
["interpolation", [
41+
["variable", "$bar"],
42+
["punctuation", "["],
43+
["operator", "-"],
44+
["number", "4"],
45+
["punctuation", "]"]
46+
]],
3047
", ",
31-
["interpolation", [["variable", "$foo"], ["punctuation", "["], ["variable", "$bar"], ["punctuation", "]"]]],
48+
["interpolation", [
49+
["variable", "$foo"],
50+
["punctuation", "["],
51+
["variable", "$bar"],
52+
["punctuation", "]"]
53+
]],
3254
"\""
3355
]],
56+
3457
["string", [
3558
"\"",
36-
["interpolation", [["variable", "$foo"], ["operator", "->"], ["property", "bar"]]],
59+
["interpolation", [
60+
["variable", "$foo"],
61+
["operator", "->"],
62+
["property", "bar"]
63+
]],
3764
"\""
3865
]],
66+
3967
["string", [
4068
"\"More ",
41-
["interpolation", [["punctuation", "{"], ["variable", "$interpolation"], ["punctuation", "}"]]],
69+
["interpolation", [
70+
["punctuation", "{"],
71+
["variable", "$interpolation"],
72+
["punctuation", "}"]
73+
]],
4274
"\""
4375
]],
76+
4477
["string", [
4578
"\"",
4679
["interpolation", [
4780
["punctuation", "{"],
48-
["variable", "$arr"], ["punctuation", "["], ["string", "'key'"], ["punctuation", "]"],
81+
["variable", "$arr"],
82+
["punctuation", "["],
83+
["string", "'key'"],
84+
["punctuation", "]"],
4985
["punctuation", "}"]
5086
]],
5187
", ",
5288
["interpolation", [
5389
["punctuation", "{"],
5490
["variable", "$arr"],
55-
["punctuation", "["], ["string", "'foo'"], ["punctuation", "]"],
56-
["punctuation", "["], ["number", "3"], ["punctuation", "]"],
91+
["punctuation", "["],
92+
["string", "'foo'"],
93+
["punctuation", "]"],
94+
["punctuation", "["],
95+
["number", "3"],
96+
["punctuation", "]"],
5797
["punctuation", "}"]
5898
]],
5999
"\""
60100
]],
101+
61102
["string", [
62103
"\"",
63104
["interpolation", [
64-
["punctuation", "{"], ["variable", "$"], ["punctuation", "{"],
105+
["punctuation", "{"],
106+
["variable", "$"],
107+
["punctuation", "{"],
65108
["variable", "$name"],
66-
["punctuation", "}"], ["punctuation", "}"]
109+
["punctuation", "}"],
110+
["punctuation", "}"]
67111
]],
68112
", but not {\\${\\$name}}\""
69113
]],
114+
70115
["string", [
71116
"\"the return value of getName(): ",
72117
["interpolation", [
73-
["punctuation", "{"], ["variable", "$"], ["punctuation", "{"],
74-
["function", "getName"], ["punctuation", "("], ["punctuation", ")"],
75-
["punctuation", "}"], ["punctuation", "}"]
118+
["punctuation", "{"],
119+
["variable", "$"],
120+
["punctuation", "{"],
121+
["function", "getName"],
122+
["punctuation", "("],
123+
["punctuation", ")"],
124+
["punctuation", "}"],
125+
["punctuation", "}"]
76126
]],
77127
"\""
78128
]],
129+
79130
["string", [
80131
"\"the return value of \\$object->getName(): ",
81132
["interpolation", [
82-
["punctuation", "{"], ["variable", "$"], ["punctuation", "{"],
83-
["variable", "$object"], ["operator", "->"], ["function", "getName"], ["punctuation", "("], ["punctuation", ")"],
84-
["punctuation", "}"], ["punctuation", "}"]
133+
["punctuation", "{"],
134+
["variable", "$"],
135+
["punctuation", "{"],
136+
["variable", "$object"],
137+
["operator", "->"],
138+
["function", "getName"],
139+
["punctuation", "("],
140+
["punctuation", ")"],
141+
["punctuation", "}"],
142+
["punctuation", "}"]
85143
]],
86144
"\""
87145
]],
146+
88147
["string", [
89148
"\"",
90149
["interpolation", [
91150
["punctuation", "{"],
92-
["variable", "$foo"], ["operator", "->"], ["variable", "$bar"],
151+
["variable", "$foo"],
152+
["operator", "->"],
153+
["variable", "$bar"],
93154
["punctuation", "}"]
94155
]],
95156
", ",
96157
["interpolation", [
97158
["punctuation", "{"],
98-
["variable", "$foo"], ["operator", "->"], ["punctuation", "{"],
99-
["variable", "$baz"], ["punctuation", "["], ["number", "1"], ["punctuation", "]"],
159+
["variable", "$foo"],
160+
["operator", "->"],
161+
["punctuation", "{"],
162+
["variable", "$baz"],
163+
["punctuation", "["],
164+
["number", "1"],
165+
["punctuation", "]"],
100166
["punctuation", "}"],
101167
["punctuation", "}"]
102168
]],
103169
"\""
104170
]],
171+
105172
["string", [
106173
["delimiter", [
107-
["punctuation", "<<<"], "FOO"
174+
["punctuation", "<<<"],
175+
"FOO"
108176
]],
177+
109178
"\r\nHeredoc strings ",
110179
["interpolation", [
111-
["variable", "$also"], ["operator", "->"], ["property", "support"]
180+
["variable", "$also"],
181+
["operator", "->"],
182+
["property", "support"]
112183
]],
113184
["interpolation", [
114-
["punctuation", "{"], ["variable", "$"], ["punctuation", "{"],
115-
["variable", "$string"], ["operator", "->"], ["function", "interpolation"], ["punctuation", "("], ["punctuation", ")"],
116-
["punctuation", "}"], ["punctuation", "}"]
185+
["punctuation", "{"],
186+
["variable", "$"],
187+
["punctuation", "{"],
188+
["variable", "$string"],
189+
["operator", "->"],
190+
["function", "interpolation"],
191+
["punctuation", "("],
192+
["punctuation", ")"],
193+
["punctuation", "}"],
194+
["punctuation", "}"]
117195
]],
196+
118197
["delimiter", [
119-
"FOO", ["punctuation", ";"]
198+
"FOO",
199+
["punctuation", ";"]
120200
]]
121201
]],
202+
122203
["string", [
123204
["delimiter", [
124-
["punctuation", "<<<\""], "FOO_BAR", ["punctuation", "\""]
205+
["punctuation", "<<<\""],
206+
"FOO_BAR",
207+
["punctuation", "\""]
125208
]],
209+
126210
["interpolation", [
127-
["punctuation", "{"], ["variable", "$"], ["punctuation", "{"],
211+
["punctuation", "{"],
212+
["variable", "$"],
213+
["punctuation", "{"],
128214
["variable", "$name"],
129-
["punctuation", "}"], ["punctuation", "}"]
215+
["punctuation", "}"],
216+
["punctuation", "}"]
130217
]],
131218
", but not {\\${\\$name}}\r\n",
219+
132220
["delimiter", [
133-
"FOO_BAR", ["punctuation", ";"]
221+
"FOO_BAR",
222+
["punctuation", ";"]
134223
]]
135-
]]
224+
]],
225+
226+
["variable", "$value"],
227+
["operator", "="],
228+
["string", [
229+
"\"",
230+
["interpolation", [
231+
["variable", "$this"],
232+
["operator", "->"],
233+
["property", "property"]
234+
]],
235+
"->property\""
236+
]],
237+
["punctuation", ";"],
238+
239+
["variable", "$value"],
240+
["operator", "="],
241+
["string", [
242+
"\"",
243+
["interpolation", [
244+
["variable", "$foo"],
245+
["punctuation", "["],
246+
["number", "0"],
247+
["punctuation", "]"]
248+
]],
249+
"[1]\""
250+
]],
251+
["punctuation", ";"]
136252
]
137253

138254
----------------------------------------------------
139255

140-
Checks for interpolation inside strings.
256+
Checks for interpolation inside strings.

0 commit comments

Comments
 (0)