Skip to content

Commit

Permalink
PHP: Fixed string interpolation (#2864)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed May 1, 2021
1 parent fe98d53 commit cf3755c
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 34 deletions.
2 changes: 1 addition & 1 deletion components/prism-php.js
Expand Up @@ -230,7 +230,7 @@
};

var string_interpolation = {
pattern: /{\$(?:{(?:{[^{}]+}|[^{}]+)}|[^{}])+}|(^|[^\\{])\$+(?:\w+(?:\[[^\r\n\[\]]+\]|->\w+)*)/,
pattern: /{\$(?:{(?:{[^{}]+}|[^{}]+)}|[^{}])+}|(^|[^\\{])\$+(?:\w+(?:\[[^\r\n\[\]]+\]|->\w+)?)/,
lookbehind: true,
inside: Prism.languages.php
};
Expand Down
2 changes: 1 addition & 1 deletion components/prism-php.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

180 changes: 148 additions & 32 deletions tests/languages/php/string-interpolation_feature.test
Expand Up @@ -14,127 +14,243 @@ FOO;
{${$name}}, but not {\${\$name}}
FOO_BAR;

$value = "$this->property->property";
$value = "$foo[0][1]";

----------------------------------------------------

[
["string", [
"\"This ",
["interpolation", [["variable", "$variable"]]],
["interpolation", [
["variable", "$variable"]
]],
" is interpolated\""
]],

["string", [
"\"",
["interpolation", [["variable", "$foo"], ["punctuation", "["], ["number", "2"], ["punctuation", "]"]]],
["interpolation", [
["variable", "$foo"],
["punctuation", "["],
["number", "2"],
["punctuation", "]"]
]],
", ",
["interpolation", [["variable", "$bar"], ["punctuation", "["], ["operator", "-"], ["number", "4"], ["punctuation", "]"]]],
["interpolation", [
["variable", "$bar"],
["punctuation", "["],
["operator", "-"],
["number", "4"],
["punctuation", "]"]
]],
", ",
["interpolation", [["variable", "$foo"], ["punctuation", "["], ["variable", "$bar"], ["punctuation", "]"]]],
["interpolation", [
["variable", "$foo"],
["punctuation", "["],
["variable", "$bar"],
["punctuation", "]"]
]],
"\""
]],

["string", [
"\"",
["interpolation", [["variable", "$foo"], ["operator", "->"], ["property", "bar"]]],
["interpolation", [
["variable", "$foo"],
["operator", "->"],
["property", "bar"]
]],
"\""
]],

["string", [
"\"More ",
["interpolation", [["punctuation", "{"], ["variable", "$interpolation"], ["punctuation", "}"]]],
["interpolation", [
["punctuation", "{"],
["variable", "$interpolation"],
["punctuation", "}"]
]],
"\""
]],

["string", [
"\"",
["interpolation", [
["punctuation", "{"],
["variable", "$arr"], ["punctuation", "["], ["string", "'key'"], ["punctuation", "]"],
["variable", "$arr"],
["punctuation", "["],
["string", "'key'"],
["punctuation", "]"],
["punctuation", "}"]
]],
", ",
["interpolation", [
["punctuation", "{"],
["variable", "$arr"],
["punctuation", "["], ["string", "'foo'"], ["punctuation", "]"],
["punctuation", "["], ["number", "3"], ["punctuation", "]"],
["punctuation", "["],
["string", "'foo'"],
["punctuation", "]"],
["punctuation", "["],
["number", "3"],
["punctuation", "]"],
["punctuation", "}"]
]],
"\""
]],

["string", [
"\"",
["interpolation", [
["punctuation", "{"], ["variable", "$"], ["punctuation", "{"],
["punctuation", "{"],
["variable", "$"],
["punctuation", "{"],
["variable", "$name"],
["punctuation", "}"], ["punctuation", "}"]
["punctuation", "}"],
["punctuation", "}"]
]],
", but not {\\${\\$name}}\""
]],

["string", [
"\"the return value of getName(): ",
["interpolation", [
["punctuation", "{"], ["variable", "$"], ["punctuation", "{"],
["function", "getName"], ["punctuation", "("], ["punctuation", ")"],
["punctuation", "}"], ["punctuation", "}"]
["punctuation", "{"],
["variable", "$"],
["punctuation", "{"],
["function", "getName"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "}"],
["punctuation", "}"]
]],
"\""
]],

["string", [
"\"the return value of \\$object->getName(): ",
["interpolation", [
["punctuation", "{"], ["variable", "$"], ["punctuation", "{"],
["variable", "$object"], ["operator", "->"], ["function", "getName"], ["punctuation", "("], ["punctuation", ")"],
["punctuation", "}"], ["punctuation", "}"]
["punctuation", "{"],
["variable", "$"],
["punctuation", "{"],
["variable", "$object"],
["operator", "->"],
["function", "getName"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "}"],
["punctuation", "}"]
]],
"\""
]],

["string", [
"\"",
["interpolation", [
["punctuation", "{"],
["variable", "$foo"], ["operator", "->"], ["variable", "$bar"],
["variable", "$foo"],
["operator", "->"],
["variable", "$bar"],
["punctuation", "}"]
]],
", ",
["interpolation", [
["punctuation", "{"],
["variable", "$foo"], ["operator", "->"], ["punctuation", "{"],
["variable", "$baz"], ["punctuation", "["], ["number", "1"], ["punctuation", "]"],
["variable", "$foo"],
["operator", "->"],
["punctuation", "{"],
["variable", "$baz"],
["punctuation", "["],
["number", "1"],
["punctuation", "]"],
["punctuation", "}"],
["punctuation", "}"]
]],
"\""
]],

["string", [
["delimiter", [
["punctuation", "<<<"], "FOO"
["punctuation", "<<<"],
"FOO"
]],

"\r\nHeredoc strings ",
["interpolation", [
["variable", "$also"], ["operator", "->"], ["property", "support"]
["variable", "$also"],
["operator", "->"],
["property", "support"]
]],
["interpolation", [
["punctuation", "{"], ["variable", "$"], ["punctuation", "{"],
["variable", "$string"], ["operator", "->"], ["function", "interpolation"], ["punctuation", "("], ["punctuation", ")"],
["punctuation", "}"], ["punctuation", "}"]
["punctuation", "{"],
["variable", "$"],
["punctuation", "{"],
["variable", "$string"],
["operator", "->"],
["function", "interpolation"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "}"],
["punctuation", "}"]
]],

["delimiter", [
"FOO", ["punctuation", ";"]
"FOO",
["punctuation", ";"]
]]
]],

["string", [
["delimiter", [
["punctuation", "<<<\""], "FOO_BAR", ["punctuation", "\""]
["punctuation", "<<<\""],
"FOO_BAR",
["punctuation", "\""]
]],

["interpolation", [
["punctuation", "{"], ["variable", "$"], ["punctuation", "{"],
["punctuation", "{"],
["variable", "$"],
["punctuation", "{"],
["variable", "$name"],
["punctuation", "}"], ["punctuation", "}"]
["punctuation", "}"],
["punctuation", "}"]
]],
", but not {\\${\\$name}}\r\n",

["delimiter", [
"FOO_BAR", ["punctuation", ";"]
"FOO_BAR",
["punctuation", ";"]
]]
]]
]],

["variable", "$value"],
["operator", "="],
["string", [
"\"",
["interpolation", [
["variable", "$this"],
["operator", "->"],
["property", "property"]
]],
"->property\""
]],
["punctuation", ";"],

["variable", "$value"],
["operator", "="],
["string", [
"\"",
["interpolation", [
["variable", "$foo"],
["punctuation", "["],
["number", "0"],
["punctuation", "]"]
]],
"[1]\""
]],
["punctuation", ";"]
]

----------------------------------------------------

Checks for interpolation inside strings.
Checks for interpolation inside strings.

0 comments on commit cf3755c

Please sign in to comment.