diff options
author | alandonovan <adonovan@google.com> | 2020-03-26 10:23:16 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-26 10:23:16 -0400 |
commit | 16e44b11d94568b6de240a245c9f85c415e69bbc (patch) | |
tree | c91e9af517b59d87d697303f03c001bbd5fb900b /syntax/scan_test.go | |
parent | 8dd3e2ee1dd5d034baada4c7b4fcf231294a1013 (diff) | |
download | starlark-go-16e44b11d94568b6de240a245c9f85c415e69bbc.tar.gz |
syntax: strict string escapes (#265)
This change causes Starlark, like Go, to reject backslashes that
are not part of an escape sequence. Previously they were treated
literally, so \ ( would encode a two-character string.
Many programs rely on this, especially for regular expressions and
shell commands, and will be broken by this change, but the fix is simple:
double each errant backslash.
Python does not yet enforce this behavior, but since 3.6
has emitted a deprecation warning for it.
Also, document string escapes.
Related issues:
- Google issue b/34519173: "bazel: Forbid undefined escape sequences in strings"
- bazelbuild/starlark#38: Starlark spec: String escapes
- bazelbuild/buildtools#688: Bazel: Fix string escapes
- bazelbuild/bazel#8380: Bazel incompatible_restrict_string_escapes: Restrict string escapes
Diffstat (limited to 'syntax/scan_test.go')
-rw-r--r-- | syntax/scan_test.go | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/syntax/scan_test.go b/syntax/scan_test.go index 005b64e..a63ec81 100644 --- a/syntax/scan_test.go +++ b/syntax/scan_test.go @@ -118,7 +118,6 @@ pass`, "pass newline pass EOF"}, // consecutive newlines are consolidated {`x = 1 + \ 2`, `x = 1 + 2 EOF`}, {`x = 'a\nb'`, `x = "a\nb" EOF`}, - {`x = 'a\zb'`, `x = "a\\zb" EOF`}, {`x = r'a\nb'`, `x = "a\\nb" EOF`}, {`x = '\''`, `x = "'" EOF`}, {`x = "\""`, `x = "\"" EOF`}, @@ -192,10 +191,32 @@ pass`, "pass newline pass EOF"}, // consecutive newlines are consolidated {`"\377"`, `"\xff" EOF`}, {`"\378"`, `"\x1f8" EOF`}, // = '\37' + '8' {`"\400"`, `foo.star:1:1: invalid escape sequence \400`}, // unlike Python 2 and 3 - // Backslashes that are not part of escapes are treated literally, - // but this behavior will change; see b/34519173. - {`"\+"`, `"\\+" EOF`}, - {`"\o123"`, `"\\o123" EOF`}, + + // backslash escapes + // As in Go, a backslash must escape something. + // (Python started issuing a deprecation warning in 3.6.) + {`"foo\(bar"`, `foo.star:1:1: invalid escape sequence \(`}, + {`"\+"`, `foo.star:1:1: invalid escape sequence \+`}, + {`"\w"`, `foo.star:1:1: invalid escape sequence \w`}, + {`"\""`, `"\"" EOF`}, + {`"\'"`, `foo.star:1:1: invalid escape sequence \'`}, + {`'\w'`, `foo.star:1:1: invalid escape sequence \w`}, + {`'\''`, `"'" EOF`}, + {`'\"'`, `foo.star:1:1: invalid escape sequence \"`}, + {`"""\w"""`, `foo.star:1:1: invalid escape sequence \w`}, + {`"""\""""`, `"\"" EOF`}, + {`"""\'"""`, `foo.star:1:1: invalid escape sequence \'`}, + {`'''\w'''`, `foo.star:1:1: invalid escape sequence \w`}, + {`'''\''''`, `"'" EOF`}, + {`'''\"'''`, `foo.star:1:1: invalid escape sequence \"`}, // error + {`r"\w"`, `"\\w" EOF`}, + {`r"\""`, `"\\\"" EOF`}, + {`r"\'"`, `"\\'" EOF`}, + {`r'\w'`, `"\\w" EOF`}, + {`r'\''`, `"\\'" EOF`}, + {`r'\"'`, `"\\\"" EOF`}, + {`'a\zb'`, `foo.star:1:1: invalid escape sequence \z`}, + {`"\o123"`, `foo.star:1:1: invalid escape sequence \o`}, // floats starting with octal digits {"012934.", `1.293400e+04 EOF`}, {"012934.1", `1.293410e+04 EOF`}, |