[dev.inline] cmd/compile/internal/syntax: clean up error and pragma handling

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

- simplify error handling in source.go
  (move handling of first error into parser, where it belongs)

- clean up error handling in scanner.go

- move pragma and position base handling from scanner
  to parser where it belongs

- have separate error methods in parser to avoid confusion
  with handlers from scanner.go and source.go

- (source.go) and (scanner.go, source.go, tokens.go)
  may be stand-alone packages if so desired, which means
  these files are now less entangled and easier to maintain

Change-Id: I81510fc7ef943b78eaa49092c0eab2075a05878c
Reviewed-on: https://go-review.googlesource.com/34235
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Robert Griesemer 2016-12-01 22:04:49 -08:00
parent e97c8a592f
commit 54ef0447fe
4 changed files with 128 additions and 102 deletions

View file

@ -22,7 +22,7 @@ func TestScanner(t *testing.T) {
defer src.Close()
var s scanner
s.init("parser.go", src, nil, nil)
s.init(src, nil, nil, false)
for {
s.next()
if s.tok == _EOF {
@ -51,7 +51,7 @@ func TestTokens(t *testing.T) {
// scan source
var got scanner
got.init("", &bytesReader{buf}, nil, nil)
got.init(&bytesReader{buf}, nil, nil, false)
got.next()
for i, want := range sampleTokens {
nlsemi := false
@ -317,38 +317,38 @@ func TestScanErrors(t *testing.T) {
{`var s string = "\x"`, "non-hex character in escape sequence: \"", 1, 19},
{`return "\Uffffffff"`, "escape sequence is invalid Unicode code point", 1, 19},
{`//line :`, "invalid line number: ", 1, 9},
{`//line :x`, "invalid line number: x", 1, 9},
{`//line foo :`, "invalid line number: ", 1, 13},
{`//line foo:123abc`, "invalid line number: 123abc", 1, 12},
{`/**///line foo:x`, "invalid line number: x", 1, 16},
{`//line foo:0`, "invalid line number: 0", 1, 12},
{fmt.Sprintf(`//line foo:%d`, lineMax+1), fmt.Sprintf("invalid line number: %d", lineMax+1), 1, 12},
// TODO(gri) move these test cases into an appropriate parser test
// {`//line :`, "invalid line number: ", 1, 9},
// {`//line :x`, "invalid line number: x", 1, 9},
// {`//line foo :`, "invalid line number: ", 1, 13},
// {`//line foo:123abc`, "invalid line number: 123abc", 1, 12},
// {`/**///line foo:x`, "invalid line number: x", 1, 16},
// {`//line foo:0`, "invalid line number: 0", 1, 12},
// {fmt.Sprintf(`//line foo:%d`, lineMax+1), fmt.Sprintf("invalid line number: %d", lineMax+1), 1, 12},
// former problem cases
{"package p\n\n\xef", "invalid UTF-8 encoding", 3, 1},
} {
var s scanner
nerrors := 0
s.init("", &bytesReader{[]byte(test.src)}, func(err error) {
s.init(&bytesReader{[]byte(test.src)}, func(line, col uint, msg string) {
nerrors++
// only check the first error
e := err.(Error) // we know it's an Error
if nerrors == 1 {
if e.Msg != test.msg {
t.Errorf("%q: got msg = %q; want %q", test.src, e.Msg, test.msg)
if msg != test.msg {
t.Errorf("%q: got msg = %q; want %q", test.src, msg, test.msg)
}
if e.Line != test.line {
t.Errorf("%q: got line = %d; want %d", test.src, e.Line, test.line)
if line != test.line {
t.Errorf("%q: got line = %d; want %d", test.src, line, test.line)
}
if e.Col != test.col {
t.Errorf("%q: got col = %d; want %d", test.src, e.Col, test.col)
if col != test.col {
t.Errorf("%q: got col = %d; want %d", test.src, col, test.col)
}
} else if nerrors > 1 {
// TODO(gri) make this use position info
t.Errorf("%q: got unexpected %q at line = %d", test.src, e.Msg, e.Line)
t.Errorf("%q: got unexpected %q at line = %d", test.src, msg, line)
}
}, nil)
}, nil, true)
for {
s.next()