node: better handling for unquoting heredoc fragments
This commit is contained in:
parent
fe654d70cc
commit
fd59ff9bb4
11
node.go
11
node.go
@ -1614,10 +1614,13 @@ func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error)
|
|||||||
|
|
||||||
case *scalar.EncapsedStringPart:
|
case *scalar.EncapsedStringPart:
|
||||||
// The n.Value that we receive here does not contain quotes at all
|
// The n.Value that we receive here does not contain quotes at all
|
||||||
// TODO what happens with embedded escape characters? Did the parser
|
// But it is still a fragment of a double-quoted string
|
||||||
// remove them for us already or do we just have to assume we are in
|
rawValue, err := phpUnquote(`"` + strings.Replace(n.Value, `"`, `\"`, -1) + `"`)
|
||||||
// a double-quoted-equivalent situation?
|
if err != nil {
|
||||||
return quoteGoString(n.Value), nil // Go source code quoting format
|
return "", parseErr{n, err}
|
||||||
|
}
|
||||||
|
|
||||||
|
return quoteGoString(rawValue), nil // Go source code quoting format
|
||||||
|
|
||||||
case *scalar.Encapsed:
|
case *scalar.Encapsed:
|
||||||
return this.convertEncapsedString(n.Parts)
|
return this.convertEncapsedString(n.Parts)
|
||||||
|
15
quote.go
15
quote.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -13,10 +14,20 @@ func phpUnquote(s string) (string, error) {
|
|||||||
|
|
||||||
if s[0] == '"' {
|
if s[0] == '"' {
|
||||||
// Quote system is similar enough to Go's that strconv will probably just work
|
// Quote system is similar enough to Go's that strconv will probably just work
|
||||||
// PHP has some lax parsing for unknown escape sequences
|
|
||||||
|
// PROBLEM 1: PHP has some lax parsing for unknown escape sequences
|
||||||
// e.g. regex("foo\.") will be silently parsed as regex("foo.")
|
// e.g. regex("foo\.") will be silently parsed as regex("foo.")
|
||||||
|
|
||||||
return strconv.Unquote(s)
|
// PROBLEM 2: PHP supports multiline string literals, strconv.Unquote does not
|
||||||
|
// Apply fixup
|
||||||
|
s2 := strings.Replace(strings.Replace(s, "\r", `\r`, -1), "\n", `\n`, -1)
|
||||||
|
|
||||||
|
ret, err := strconv.Unquote(s2)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("Parsing string literal `%s`: %v", s, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if s[0] == '\'' {
|
if s[0] == '\'' {
|
||||||
|
Loading…
Reference in New Issue
Block a user