Skip to content

Commit

Permalink
PHP: Add support for string interpolation inside double-quoted strings.
Browse files Browse the repository at this point in the history
Fix #1146
  • Loading branch information
Golmote committed Mar 3, 2018
1 parent e0cd47f commit 9f1f8d6
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 12 deletions.
29 changes: 24 additions & 5 deletions components/prism-php.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
*/

Prism.languages.php = Prism.languages.extend('clike', {
'string': {
pattern: /(["'])(?:\\[\s\S]|(?!\1)[^\\])*\1/,
greedy: true
},
'keyword': /\b(?:and|or|xor|array|as|break|case|cfunction|class|const|continue|declare|default|die|do|else|elseif|enddeclare|endfor|endforeach|endif|endswitch|endwhile|extends|for|foreach|function|include|include_once|global|if|new|return|static|switch|use|require|require_once|var|while|abstract|interface|public|implements|private|protected|parent|throw|null|echo|print|trait|namespace|final|yield|goto|instanceof|finally|try|catch)\b/i,
'constant': /\b[A-Z0-9_]{2,}\b/,
'comment': {
Expand All @@ -39,7 +35,7 @@ Prism.languages.insertBefore('php', 'keyword', {
pattern: /\?>|<\?(?:php|=)?/i,
alias: 'important'
},
'variable': /\$\w+\b/i,
'variable': /\$+(?:\w+\b|(?={))/i,
'package': {
pattern: /(\\|namespace\s+|use\s+)[\w\\]+/,
lookbehind: true,
Expand All @@ -57,6 +53,29 @@ Prism.languages.insertBefore('php', 'operator', {
}
});

Prism.languages.insertBefore('php', 'string', {
'single-quoted-string': {
pattern: /'(?:\\[\s\S]|[^\\'])*'/,
greedy: true,
alias: 'string'
},
'double-quoted-string': {
pattern: /"(?:\\[\s\S]|[^\\"])*"/,
greedy: true,
alias: 'string',
inside: {
interpolation: {
pattern: /{\$(?:{(?:{[^{}]+}|[^{}]+)}|[^{}])+}|(^|[^\\{])\$+(?:\w+(?:\[.+?]|->\w+)*)/,
lookbehind: true,
inside: {
rest: Prism.languages.php
}
}
}
}
});
delete Prism.languages.php['string'];

// Add HTML support if the markup language exists
if (Prism.languages.markup) {

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.

103 changes: 103 additions & 0 deletions tests/languages/php/string-interpolation_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"This $variable is interpolated"
"$foo[2], $bar[-4], $foo[$bar]"
"$foo->bar"
"More {$interpolation}"
"{$arr['key']}, {$arr['foo'][3]}"
"{${$name}}, but not {\${\$name}}"
"the return value of getName(): {${getName()}}"
"the return value of \$object->getName(): {${$object->getName()}}"
"{$foo->$bar}, {$foo->{$baz[1]}}"

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

[
["double-quoted-string", [
"\"This ",
["interpolation", [["variable", "$variable"]]],
" is interpolated\""
]],
["double-quoted-string", [
"\"",
["interpolation", [["variable", "$foo"], ["punctuation", "["], ["number", "2"], ["punctuation", "]"]]],
", ",
["interpolation", [["variable", "$bar"], ["punctuation", "["], ["operator", "-"], ["number", "4"], ["punctuation", "]"]]],
", ",
["interpolation", [["variable", "$foo"], ["punctuation", "["], ["variable", "$bar"], ["punctuation", "]"]]],
"\""
]],
["double-quoted-string", [
"\"",
["interpolation", [["variable", "$foo"], ["operator", "-"], ["operator", ">"], ["property", "bar"]]],
"\""
]],
["double-quoted-string", [
"\"More ",
["interpolation", [["punctuation", "{"], ["variable", "$interpolation"], ["punctuation", "}"]]],
"\""
]],
["double-quoted-string", [
"\"",
["interpolation", [
["punctuation", "{"],
["variable", "$arr"], ["punctuation", "["], ["single-quoted-string", "'key'"], ["punctuation", "]"],
["punctuation", "}"]
]],
", ",
["interpolation", [
["punctuation", "{"],
["variable", "$arr"],
["punctuation", "["], ["single-quoted-string", "'foo'"], ["punctuation", "]"],
["punctuation", "["], ["number", "3"], ["punctuation", "]"],
["punctuation", "}"]
]],
"\""
]],
["double-quoted-string", [
"\"",
["interpolation", [
["punctuation", "{"], ["variable", "$"], ["punctuation", "{"],
["variable", "$name"],
["punctuation", "}"], ["punctuation", "}"]
]],
", but not {\\${\\$name}}\""
]],
["double-quoted-string", [
"\"the return value of getName(): ",
["interpolation", [
["punctuation", "{"], ["variable", "$"], ["punctuation", "{"],
["function", "getName"], ["punctuation", "("], ["punctuation", ")"],
["punctuation", "}"], ["punctuation", "}"]
]],
"\""
]],
["double-quoted-string", [
"\"the return value of \\$object->getName(): ",
["interpolation", [
["punctuation", "{"], ["variable", "$"], ["punctuation", "{"],
["variable", "$object"], ["operator", "-"], ["operator", ">"], ["function", "getName"], ["punctuation", "("], ["punctuation", ")"],
["punctuation", "}"], ["punctuation", "}"]
]],
"\""
]],
["double-quoted-string", [
"\"",
["interpolation", [
["punctuation", "{"],
["variable", "$foo"], ["operator", "-"], ["operator", ">"], ["variable", "$bar"],
["punctuation", "}"]
]],
", ",
["interpolation", [
["punctuation", "{"],
["variable", "$foo"], ["operator", "-"], ["operator", ">"], ["punctuation", "{"],
["variable", "$baz"], ["punctuation", "["], ["number", "1"], ["punctuation", "]"],
["punctuation", "}"],
["punctuation", "}"]
]],
"\""
]]
]

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

Checks for interpolation inside strings.
10 changes: 5 additions & 5 deletions tests/languages/php/string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ string'
----------------------------------------------------

[
["string", "\"https://example.com\""],
["string", "\" /* not a comment */ \""],
["string", "\"multi-line\r\nstring\""],
["string", "'multi-line\r\nstring'"]
["double-quoted-string", ["\"https://example.com\""]],
["double-quoted-string", ["\" /* not a comment */ \""]],
["double-quoted-string", ["\"multi-line\r\nstring\""]],
["single-quoted-string", "'multi-line\r\nstring'"]
]

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

Checks for strings with comments.
Checks for strings.
6 changes: 5 additions & 1 deletion tests/languages/php/variable_feature.test
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
$f
$foo
$foobar_42
$$bar
${$foo}

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

[
["variable", "$f"],
["variable", "$foo"],
["variable", "$foobar_42"]
["variable", "$foobar_42"],
["variable", "$$bar"],
["variable", "$"], ["punctuation", "{"], ["variable", "$foo"], ["punctuation", "}"]
]

----------------------------------------------------
Expand Down

0 comments on commit 9f1f8d6

Please sign in to comment.