mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile/internal/syntax: fix error handling for Read/Parse calls
- define syntax.Error for cleaner error reporting - abort parsing after first error if no error handler is installed - make sure to always report the first error, if any - document behavior of API calls - while at it: rename ReadXXX -> ParseXXX (clearer) - adjust cmd/compile noder.go accordingly Fixes #17774. Change-Id: I7893eedea454a64acd753e32f7a8bf811ddbb03c Reviewed-on: https://go-review.googlesource.com/32950 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
ad020477f4
commit
60a9bf9f95
8 changed files with 126 additions and 85 deletions
|
|
@ -5,7 +5,6 @@
|
|||
package syntax
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
|
@ -16,8 +15,9 @@ import (
|
|||
// suf r0 r w
|
||||
|
||||
type source struct {
|
||||
src io.Reader
|
||||
errh ErrorHandler
|
||||
src io.Reader
|
||||
errh ErrorHandler
|
||||
first error // first error encountered
|
||||
|
||||
// source buffer
|
||||
buf [4 << 10]byte
|
||||
|
|
@ -34,6 +34,7 @@ type source struct {
|
|||
func (s *source) init(src io.Reader, errh ErrorHandler) {
|
||||
s.src = src
|
||||
s.errh = errh
|
||||
s.first = nil
|
||||
|
||||
s.buf[0] = utf8.RuneSelf // terminate with sentinel
|
||||
s.offs = 0
|
||||
|
|
@ -50,11 +51,14 @@ func (s *source) error(msg string) {
|
|||
}
|
||||
|
||||
func (s *source) error_at(pos, line int, msg string) {
|
||||
if s.errh != nil {
|
||||
s.errh(pos, line, msg)
|
||||
return
|
||||
err := Error{pos, line, msg}
|
||||
if s.first == nil {
|
||||
s.first = err
|
||||
}
|
||||
panic(fmt.Sprintf("%d: %s", line, msg))
|
||||
if s.errh == nil {
|
||||
panic(s.first)
|
||||
}
|
||||
s.errh(err)
|
||||
}
|
||||
|
||||
// pos0 returns the byte position of the last character read.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue