[go] go/scanner: allow backtick in raw string literal

229 views
Skip to first unread message

Gobot Gobot (Gerrit)

unread,
Dec 23, 2017, 3:42:32 AM12/23/17
to Ryuichi Hayashida, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
Within the next week or so, a maintainer will review your change and provide
feedback. See https://golang.org/doc/contribute.html#review for more info and
tips to get your patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11, it means that this CL will be reviewed as part of the next development
cycle. See https://golang.org/s/release for more details.

View Change

    To view, visit change 85435. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-MessageType: comment
    Gerrit-Change-Id: I5708cb3538fb49ca8669ad56e22b15a36461925a
    Gerrit-Change-Number: 85435
    Gerrit-PatchSet: 1
    Gerrit-Owner: Ryuichi Hayashida <lin9...@yahoo.co.jp>
    Gerrit-CC: Gobot Gobot <go...@golang.org>
    Gerrit-Comment-Date: Sat, 23 Dec 2017 08:42:29 +0000
    Gerrit-HasComments: No
    Gerrit-HasLabels: No

    Giovanni Bajo (Gerrit)

    unread,
    Dec 23, 2017, 4:47:25 AM12/23/17
    to Ryuichi Hayashida, goph...@pubsubhelper.golang.org, Gobot Gobot, golang-co...@googlegroups.com

    View Change

    1 comment:

    To view, visit change 85435. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-MessageType: comment
    Gerrit-Change-Id: I5708cb3538fb49ca8669ad56e22b15a36461925a
    Gerrit-Change-Number: 85435
    Gerrit-PatchSet: 1
    Gerrit-Owner: Ryuichi Hayashida <lin9...@yahoo.co.jp>
    Gerrit-CC: Giovanni Bajo <ra...@develer.com>
    Gerrit-CC: Gobot Gobot <go...@golang.org>
    Gerrit-Comment-Date: Sat, 23 Dec 2017 09:47:21 +0000
    Gerrit-HasComments: Yes
    Gerrit-HasLabels: No

    Yasuhiro MATSUMOTO (Gerrit)

    unread,
    Dec 23, 2017, 5:16:58 AM12/23/17
    to Ryuichi Hayashida, goph...@pubsubhelper.golang.org, Giovanni Bajo, Gobot Gobot, golang-co...@googlegroups.com

    View Change

    1 comment:

    To view, visit change 85435. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-MessageType: comment
    Gerrit-Change-Id: I5708cb3538fb49ca8669ad56e22b15a36461925a
    Gerrit-Change-Number: 85435
    Gerrit-PatchSet: 1
    Gerrit-Owner: Ryuichi Hayashida <lin9...@yahoo.co.jp>
    Gerrit-CC: Giovanni Bajo <ra...@develer.com>
    Gerrit-CC: Gobot Gobot <go...@golang.org>
    Gerrit-CC: Yasuhiro MATSUMOTO <matt...@gmail.com>
    Gerrit-Comment-Date: Sat, 23 Dec 2017 10:16:52 +0000
    Gerrit-HasComments: Yes
    Gerrit-HasLabels: No

    Jan Mercl (Gerrit)

    unread,
    Dec 23, 2017, 5:48:05 AM12/23/17
    to Ryuichi Hayashida, goph...@pubsubhelper.golang.org, Yasuhiro MATSUMOTO, Giovanni Bajo, Gobot Gobot, golang-co...@googlegroups.com

    Patch set 1:Code-Review -1

    View Change

      To view, visit change 85435. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-MessageType: comment
      Gerrit-Change-Id: I5708cb3538fb49ca8669ad56e22b15a36461925a
      Gerrit-Change-Number: 85435
      Gerrit-PatchSet: 1
      Gerrit-Owner: Ryuichi Hayashida <lin9...@yahoo.co.jp>
      Gerrit-Reviewer: Jan Mercl <0xj...@gmail.com>
      Gerrit-CC: Giovanni Bajo <ra...@develer.com>
      Gerrit-CC: Gobot Gobot <go...@golang.org>
      Gerrit-CC: Yasuhiro MATSUMOTO <matt...@gmail.com>
      Gerrit-Comment-Date: Sat, 23 Dec 2017 10:48:01 +0000
      Gerrit-HasComments: No
      Gerrit-HasLabels: Yes

      Ryuichi Hayashida (Gerrit)

      unread,
      Dec 23, 2017, 8:23:08 AM12/23/17
      to Ian Lance Taylor, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

      Ryuichi Hayashida has uploaded this change for review.

      View Change

      go/scanner: allow backtick in raw string literal

      This commit enables to embed ` in raw string literal. Doubled backtick
      `` now means one ` in raw string literal as following.

      // Outputs 'foo `bar'
      fmt.Println(`foo `` bar`)

      // Outputs '`'
      fmt.Println(````)

      // Outputs '``'
      fmt.Println(``````)

      GitHub issue for proposal of this feature:
      https://github.com/golang/go/issues/23228

      Fixes #23228

      Change-Id: I5708cb3538fb49ca8669ad56e22b15a36461925a
      ---
      M doc/go_spec.html
      M src/go/scanner/scanner.go
      M src/go/scanner/scanner_test.go
      M test/string_lit.go
      4 files changed, 40 insertions(+), 1 deletion(-)

      diff --git a/doc/go_spec.html b/doc/go_spec.html
      index ebf1cef..fe36113 100644
      --- a/doc/go_spec.html
      +++ b/doc/go_spec.html
      @@ -456,7 +456,12 @@
      contain newlines.
      Carriage return characters ('\r') inside raw string literals
      are discarded from the raw string value.
      +To contain back quotes in raw string literals, doubled back quotes '``'
      +are available. They means one back quotes in raw string literals.
      </p>
      +<pre>
      +`foo `` bar` // Equivalent to "foo ` bar"
      +</pre>
      <p>
      Interpreted string literals are character sequences between double
      quotes, as in <code>&quot;bar&quot;</code>.
      diff --git a/src/go/scanner/scanner.go b/src/go/scanner/scanner.go
      index a86e4eb..0257b2d 100644
      --- a/src/go/scanner/scanner.go
      +++ b/src/go/scanner/scanner.go
      @@ -492,11 +492,31 @@
      return c[:i]
      }

      +func stripDoubledBackquotes(b []byte) []byte {
      + l := len(b)
      + c := make([]byte, l)
      + i, j := 0, 0
      + for {
      + c[j] = b[i]
      + j++
      + if i == l-1 {
      + break
      + }
      + if b[i] == '`' && b[i+1] == '`' {
      + i += 2
      + } else {
      + i++
      + }
      + }
      + return c[:j]
      +}
      +
      func (s *Scanner) scanRawString() string {
      // '`' opening already consumed
      offs := s.offset - 1

      hasCR := false
      + hasBQ := false
      for {
      ch := s.ch
      if ch < 0 {
      @@ -505,7 +525,12 @@
      }
      s.next()
      if ch == '`' {
      - break
      + if s.ch == '`' {
      + s.next()
      + hasBQ = true
      + } else {
      + break
      + }
      }
      if ch == '\r' {
      hasCR = true
      @@ -516,6 +541,9 @@
      if hasCR {
      lit = stripCR(lit)
      }
      + if hasBQ {
      + lit = stripDoubledBackquotes(lit)
      + }

      return string(lit)
      }
      diff --git a/src/go/scanner/scanner_test.go b/src/go/scanner/scanner_test.go
      index ff41c03..564254d 100644
      --- a/src/go/scanner/scanner_test.go
      +++ b/src/go/scanner/scanner_test.go
      @@ -90,6 +90,9 @@
      },
      {token.STRING, "`\r`", literal},
      {token.STRING, "`foo\r\nbar`", literal},
      + {token.STRING, "`foo``bar`", literal},
      + {token.STRING, "````", literal},
      + {token.STRING, "`foo````bar`", literal},

      // Operators and delimiters
      {token.ADD, "+", operator},
      @@ -285,6 +288,7 @@
      elit = e.lit
      if elit[0] == '`' {
      elit = string(stripCR([]byte(elit)))
      + elit = string(stripDoubledBackquotes([]byte(elit)))
      }
      } else if e.tok.IsKeyword() {
      elit = e.lit
      diff --git a/test/string_lit.go b/test/string_lit.go
      index 4751b82..f92ddad 100644
      --- a/test/string_lit.go
      +++ b/test/string_lit.go
      @@ -93,6 +93,8 @@
      `\000\123\x00\312\xFE\u0123\ubabe\U0000babe`,
      "backslashes 2 (backquote)")
      assert("\\x\\u\\U\\", `\x\u\U\`, "backslash 3 (backquote)")
      + assert(`foo `` bar`, "foo ` bar", "backquote in raw string literal")
      + assert(````, "`", "backquote-only raw string literal")

      // test large and surrogate-half runes. perhaps not the most logical place for these tests.
      var r int32

      To view, visit change 85435. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-MessageType: newchange

      Brad Fitzpatrick (Gerrit)

      unread,
      Dec 23, 2017, 9:45:12 PM12/23/17
      to Ryuichi Hayashida, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Jan Mercl, Yasuhiro MATSUMOTO, Giovanni Bajo, Gobot Gobot, golang-co...@googlegroups.com

      -2 to prevent accidental merge, pending proposal issue's approval.

      Patch set 1:Code-Review -2

      View Change

        To view, visit change 85435. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-MessageType: comment
        Gerrit-Change-Id: I5708cb3538fb49ca8669ad56e22b15a36461925a
        Gerrit-Change-Number: 85435
        Gerrit-PatchSet: 1
        Gerrit-Owner: Ryuichi Hayashida <lin9...@yahoo.co.jp>
        Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
        Gerrit-Reviewer: Jan Mercl <0xj...@gmail.com>
        Gerrit-CC: Giovanni Bajo <ra...@develer.com>
        Gerrit-CC: Gobot Gobot <go...@golang.org>
        Gerrit-CC: Yasuhiro MATSUMOTO <matt...@gmail.com>
        Gerrit-Comment-Date: Sun, 24 Dec 2017 02:45:07 +0000
        Gerrit-HasComments: No
        Gerrit-HasLabels: Yes

        Andrew Bonventre (Gerrit)

        unread,
        Sep 25, 2019, 6:02:36 PM9/25/19
        to Ryuichi Hayashida, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Jan Mercl, Giovanni Bajo, Gobot Gobot, golang-co...@googlegroups.com

        Andrew Bonventre abandoned this change.

        View Change

        Abandoned Proposal was declined

        To view, visit change 85435. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I5708cb3538fb49ca8669ad56e22b15a36461925a
        Gerrit-Change-Number: 85435
        Gerrit-PatchSet: 1
        Gerrit-Owner: Ryuichi Hayashida <lin9...@yahoo.co.jp>
        Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
        Gerrit-Reviewer: Jan Mercl <0xj...@gmail.com>
        Gerrit-CC: Giovanni Bajo <ra...@develer.com>
        Gerrit-CC: Gobot Gobot <go...@golang.org>
        Gerrit-CC: Yasuhiro MATSUMOTO <matt...@gmail.com>
        Gerrit-MessageType: abandon
        Reply all
        Reply to author
        Forward
        0 new messages