Skip to content

Commit 4bbe046

Browse files
committed
cmd/compile/internal/syntax: add "~" operator
Change-Id: I7991103d97b97260d9615b7f5baf7ec75ad87d1f Reviewed-on: https://go-review.googlesource.com/c/go/+/307370 Trust: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 836356b commit 4bbe046

File tree

5 files changed

+41
-28
lines changed

5 files changed

+41
-28
lines changed

src/cmd/compile/internal/syntax/operator_string.go

Lines changed: 22 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/compile/internal/syntax/scanner.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,11 @@ redo:
343343
s.op, s.prec = Not, 0
344344
s.tok = _Operator
345345

346+
case '~':
347+
s.nextch()
348+
s.op, s.prec = Tilde, 0
349+
s.tok = _Operator
350+
346351
default:
347352
s.errorf("invalid character %#U", s.ch)
348353
s.nextch()

src/cmd/compile/internal/syntax/scanner_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ var sampleTokens = [...]struct {
232232
{_Literal, "`\r`", 0, 0},
233233

234234
// operators
235+
{_Operator, "!", Not, 0},
236+
{_Operator, "~", Tilde, 0},
237+
235238
{_Operator, "||", OrOr, precOrOr},
236239

237240
{_Operator, "&&", AndAnd, precAndAnd},
@@ -601,7 +604,7 @@ func TestScanErrors(t *testing.T) {
601604
{"\U0001d7d8" /* 𝟘 */, "identifier cannot begin with digit U+1D7D8 '𝟘'", 0, 0},
602605
{"foo\U0001d7d8_½" /* foo𝟘_½ */, "invalid character U+00BD '½' in identifier", 0, 8 /* byte offset */},
603606

604-
{"x + ~y", "invalid character U+007E '~'", 0, 4},
607+
{"x + #y", "invalid character U+0023 '#'", 0, 4},
605608
{"foo$bar = 0", "invalid character U+0024 '$'", 0, 3},
606609
{"0123456789", "invalid digit '8' in octal literal", 0, 8},
607610
{"0123456789. /* foobar", "comment not terminated", 0, 12}, // valid float constant

src/cmd/compile/internal/syntax/tokens.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ const (
111111
_ Operator = iota
112112

113113
// Def is the : in :=
114-
Def // :
115-
Not // !
116-
Recv // <-
114+
Def // :
115+
Not // !
116+
Recv // <-
117+
Tilde // ~
117118

118119
// precOrOr
119120
OrOr // ||

test/fixedbugs/issue23587.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
package p
88

9-
func f(x int) {
10-
_ = ~x // ERROR "invalid character"
11-
_ = x ~ x // ERROR "invalid character" "unexpected x at end of statement"
9+
func _(x int) {
10+
_ = ~x // ERROR "unexpected ~"
11+
}
12+
13+
func _(x int) {
14+
_ = x ~ x // ERROR "unexpected ~ at end of statement"
1215
}

0 commit comments

Comments
 (0)