[dev.inline] cmd/compile/internal/syntax: use syntax.Pos for all external positions

- use syntax.Pos in syntax.Error (rather than line, col)
- use syntax.Pos in syntax.PragmaHandler (rather than just line)
- update uses
- better documentation in various places

Also:
- make Pos methods use Pos receiver (rather than *Pos)

Reviewed in and cherry-picked from https://go-review.googlesource.com/#/c/33891/.
With minor adjustments to noder.go to make merge compile.

Change-Id: I5507cea6c2be46a7677087c1aeb69382d31033eb
Reviewed-on: https://go-review.googlesource.com/34236
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Robert Griesemer 2016-12-02 10:44:34 -08:00
parent 54ef0447fe
commit 3d5df64b3f
8 changed files with 93 additions and 64 deletions

View file

@ -15,13 +15,12 @@ type Mode uint
// Error describes a syntax error. Error implements the error interface.
type Error struct {
// TODO(gri) Line, Col should be replaced with Pos, eventually.
Line, Col uint
Msg string
Pos Pos
Msg string
}
func (err Error) Error() string {
return fmt.Sprintf("%d: %s", err.Line, err.Msg)
return fmt.Sprintf("%s: %s", err.Pos, err.Msg)
}
var _ error = Error{} // verify that Error implements error
@ -37,11 +36,11 @@ type Pragma uint16
// A PragmaHandler is used to process //line and //go: directives as
// they're scanned. The returned Pragma value will be unioned into the
// next FuncDecl node.
type PragmaHandler func(line uint, text string) Pragma
type PragmaHandler func(pos Pos, text string) Pragma
// Parse parses a single Go source file from src and returns the corresponding
// syntax tree. If there are syntax errors, Parse will return the first error
// encountered. The filename is only used for position information.
// syntax tree. If there are errors, Parse will return the first error found.
// The filename is only used for position information.
//
// If errh != nil, it is called with each error encountered, and Parse will
// process as much source as possible. If errh is nil, Parse will terminate
@ -50,11 +49,11 @@ type PragmaHandler func(line uint, text string) Pragma
// If a PragmaHandler is provided, it is called with each pragma encountered.
//
// The Mode argument is currently ignored.
func Parse(filename string, src io.Reader, errh ErrorHandler, pragh PragmaHandler, mode Mode) (_ *File, err error) {
func Parse(filename string, src io.Reader, errh ErrorHandler, pragh PragmaHandler, mode Mode) (_ *File, first error) {
defer func() {
if p := recover(); p != nil {
var ok bool
if err, ok = p.(Error); ok {
if err, ok := p.(Error); ok {
first = err
return
}
panic(p)