Permalink
Browse files

Fix escaping string literals in various cases

Summary:
This fixes two bugs:

Interpolated heredocs need to be escaped using heredoc escaping rules, not doublequotedstring rules, but interpolated heredocs are parsed as a SyntaxList, so there's no way to tell the location without looking at the token kind in the lowerer.

Second, \X42 = \x42 in PHP, so both need to escape to the letter A. This adds the "X" case.

Reviewed By: vladima

Differential Revision: D6951735

fbshipit-source-id: fbae7c3006fdc0b832f444625d972a46ed9880f6
  • Loading branch information...
jamesjwu authored and hhvm-bot committed Feb 15, 2018
1 parent bff7829 commit 66c241df5731038523876563538cde05b607db86
@@ -1050,13 +1050,17 @@ and pExpr ?location:(location=TopLevel) : expr parser = fun node env ->
(pExpr binary_right_operand env)
| Token t ->
(match location with
| MemberSelect when Token.kind t = TK.Variable -> Lvar (pos_name node env)
| InDoubleQuotedString -> String (pos, unesc_dbl (text node))
| InBacktickedString -> String (pos, Php_escaping.unescape_backtick (text node))
| MemberSelect
| InGlobalVar
| TopLevel -> Id (pos_name node env)
(match location, Token.kind t with
| MemberSelect, TK.Variable -> Lvar (pos_name node env)
| InDoubleQuotedString, TK.HeredocStringLiteral
| InDoubleQuotedString, TK.HeredocStringLiteralHead
| InDoubleQuotedString, TK.HeredocStringLiteralTail ->
String (pos, Php_escaping.unescape_heredoc (text node))
| InDoubleQuotedString, _ -> String (pos, unesc_dbl (text node))
| InBacktickedString, _ -> String (pos, Php_escaping.unescape_backtick (text node))
| MemberSelect, _
| InGlobalVar, _
| TopLevel, _ -> Id (pos_name node env)
)
| YieldExpression { yield_operand; _ } when text yield_operand = "break" ->
@@ -125,7 +125,7 @@ let unescape_literal literal_kind s =
idx := !idx + unicode_count;
if next () <> '}' then
raise (Invalid_string "Invalid UTF-8 escape sequence")
| 'x' ->
| 'x' | 'X' ->
let hex_count = count_f is_hex ~max:2 0 in
if hex_count = 0 then
Buffer.add_string buf "\\x"
@@ -1,6 +1,4 @@
spec/tests/constants/core_predefined_constants2.php
spec/tests/lexical_structure/tokens/heredoc_string_literals.php
spec/tests/lexical_structure/tokens/string_literals.php
spec/tests/statements/declare/declare.php
spec/tests/statements/selection/if.php
spec/tests/statements/selection/switch.php
spec/tests/statements/selection/switch.php

0 comments on commit 66c241d

Please sign in to comment.