Commit 4b8895e2 authored by Robert Griesemer's avatar Robert Griesemer

[dev.inline] cmd/compile/internal/syntax: remove gcCompat uses in scanner

- make the scanner unconditionally gc compatible
- consistently use "invalid" instead "illegal" in errors

Reviewed in and cherry-picked from https://go-review.googlesource.com/#/c/33896/.

Change-Id: I4c4253e7392f3311b0d838bbe503576c9469b203
Reviewed-on: https://go-review.googlesource.com/34237
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarMatthew Dempsky <mdempsky@google.com>
parent 3d5df64b
...@@ -53,7 +53,6 @@ func (p *parser) init(filename string, src io.Reader, errh ErrorHandler, pragh P ...@@ -53,7 +53,6 @@ func (p *parser) init(filename string, src io.Reader, errh ErrorHandler, pragh P
p.pragma |= pragh(p.pos_at(line, col), text) p.pragma |= pragh(p.pos_at(line, col), text)
} }
}, },
gcCompat,
) )
p.first = nil p.first = nil
......
...@@ -21,9 +21,8 @@ import ( ...@@ -21,9 +21,8 @@ import (
type scanner struct { type scanner struct {
source source
pragh func(line, col uint, msg string) pragh func(line, col uint, msg string)
gcCompat bool // TODO(gri) remove this eventually (only here so we can build w/o parser) nlsemi bool // if set '\n' and EOF translate to ';'
nlsemi bool // if set '\n' and EOF translate to ';'
// current token, valid after calling next() // current token, valid after calling next()
line, col uint line, col uint
...@@ -34,10 +33,9 @@ type scanner struct { ...@@ -34,10 +33,9 @@ type scanner struct {
prec int // valid if tok is _Operator, _AssignOp, or _IncOp prec int // valid if tok is _Operator, _AssignOp, or _IncOp
} }
func (s *scanner) init(src io.Reader, errh, pragh func(line, col uint, msg string), gcCompat bool) { func (s *scanner) init(src io.Reader, errh, pragh func(line, col uint, msg string)) {
s.source.init(src, errh) s.source.init(src, errh)
s.pragh = pragh s.pragh = pragh
s.gcCompat = gcCompat
s.nlsemi = false s.nlsemi = false
} }
...@@ -67,7 +65,7 @@ redo: ...@@ -67,7 +65,7 @@ redo:
// token start // token start
s.line, s.col = s.source.line0, s.source.col0 s.line, s.col = s.source.line0, s.source.col0
if isLetter(c) || c >= utf8.RuneSelf && (unicode.IsLetter(c) || s.isCompatRune(c, true)) { if isLetter(c) || c >= utf8.RuneSelf && s.isIdentRune(c, true) {
s.ident() s.ident()
return return
} }
...@@ -290,7 +288,7 @@ redo: ...@@ -290,7 +288,7 @@ redo:
default: default:
s.tok = 0 s.tok = 0
s.error(fmt.Sprintf("illegal character %#U", c)) s.error(fmt.Sprintf("invalid character %#U", c))
goto redo goto redo
} }
...@@ -324,7 +322,7 @@ func (s *scanner) ident() { ...@@ -324,7 +322,7 @@ func (s *scanner) ident() {
// general case // general case
if c >= utf8.RuneSelf { if c >= utf8.RuneSelf {
for unicode.IsLetter(c) || c == '_' || unicode.IsDigit(c) || s.isCompatRune(c, false) { for s.isIdentRune(c, false) {
c = s.getr() c = s.getr()
} }
} }
...@@ -346,14 +344,18 @@ func (s *scanner) ident() { ...@@ -346,14 +344,18 @@ func (s *scanner) ident() {
s.tok = _Name s.tok = _Name
} }
func (s *scanner) isCompatRune(c rune, start bool) bool { func (s *scanner) isIdentRune(c rune, first bool) bool {
if !s.gcCompat || c < utf8.RuneSelf { switch {
return false case unicode.IsLetter(c) || c == '_':
} // ok
if start && unicode.IsNumber(c) { case unicode.IsDigit(c):
s.error(fmt.Sprintf("identifier cannot begin with digit %#U", c)) if first {
} else { s.error(fmt.Sprintf("identifier cannot begin with digit %#U", c))
}
case c >= utf8.RuneSelf:
s.error(fmt.Sprintf("invalid identifier character %#U", c)) s.error(fmt.Sprintf("invalid identifier character %#U", c))
default:
return false
} }
return true return true
} }
...@@ -643,19 +645,11 @@ func (s *scanner) escape(quote rune) bool { ...@@ -643,19 +645,11 @@ func (s *scanner) escape(quote rune) bool {
if c < 0 { if c < 0 {
return true // complain in caller about EOF return true // complain in caller about EOF
} }
if s.gcCompat { kind := "hex"
name := "hex" if base == 8 {
if base == 8 { kind = "octal"
name = "octal"
}
s.error(fmt.Sprintf("non-%s character in escape sequence: %c", name, c))
} else {
if c != quote {
s.error(fmt.Sprintf("illegal character %#U in escape sequence", c))
} else {
s.error("escape sequence incomplete")
}
} }
s.error(fmt.Sprintf("non-%s character in escape sequence: %c", kind, c))
s.ungetr() s.ungetr()
return false return false
} }
......
...@@ -22,7 +22,7 @@ func TestScanner(t *testing.T) { ...@@ -22,7 +22,7 @@ func TestScanner(t *testing.T) {
defer src.Close() defer src.Close()
var s scanner var s scanner
s.init(src, nil, nil, false) s.init(src, nil, nil)
for { for {
s.next() s.next()
if s.tok == _EOF { if s.tok == _EOF {
...@@ -51,7 +51,7 @@ func TestTokens(t *testing.T) { ...@@ -51,7 +51,7 @@ func TestTokens(t *testing.T) {
// scan source // scan source
var got scanner var got scanner
got.init(&bytesReader{buf}, nil, nil, false) got.init(&bytesReader{buf}, nil, nil)
got.next() got.next()
for i, want := range sampleTokens { for i, want := range sampleTokens {
nlsemi := false nlsemi := false
...@@ -269,7 +269,7 @@ func TestScanErrors(t *testing.T) { ...@@ -269,7 +269,7 @@ func TestScanErrors(t *testing.T) {
// token-level errors // token-level errors
{"x + ~y", "bitwise complement operator is ^", 1, 5}, {"x + ~y", "bitwise complement operator is ^", 1, 5},
{"foo$bar = 0", "illegal character U+0024 '$'", 1, 4}, {"foo$bar = 0", "invalid character U+0024 '$'", 1, 4},
{"const x = 0xyz", "malformed hex constant", 1, 13}, {"const x = 0xyz", "malformed hex constant", 1, 13},
{"0123456789", "malformed octal constant", 1, 11}, {"0123456789", "malformed octal constant", 1, 11},
{"0123456789. /* foobar", "comment not terminated", 1, 13}, // valid float constant {"0123456789. /* foobar", "comment not terminated", 1, 13}, // valid float constant
...@@ -348,7 +348,7 @@ func TestScanErrors(t *testing.T) { ...@@ -348,7 +348,7 @@ func TestScanErrors(t *testing.T) {
// TODO(gri) make this use position info // TODO(gri) make this use position info
t.Errorf("%q: got unexpected %q at line = %d", test.src, msg, line) t.Errorf("%q: got unexpected %q at line = %d", test.src, msg, line)
} }
}, nil, true) }, nil)
for { for {
s.next() s.next()
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
package a package a
import"" // ERROR "import path is empty" import"" // ERROR "import path is empty"
var? // ERROR "illegal character U\+003F '\?'" var? // ERROR "invalid character U\+003F '\?'"
var x int // ERROR "unexpected var" var x int // ERROR "unexpected var"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment