mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
This replaces the src.Pos LineHist-based position tracking with the syntax.Pos implementation and updates all uses. The LineHist table is not used anymore - the respective code is still there but should be removed eventually. CL forthcoming. Passes toolstash -cmp when comparing to the master repo (with the exception of a couple of swapped assembly instructions, likely due to different instruction scheduling because the line-based sorting has changed; though this is won't affect correctness). The sizes of various important compiler data structures have increased significantly (see the various sizes_test.go files); this is probably the reason for an increase of compilation times (to be addressed). Here are the results of compilebench -count 5, run on a "quiet" machine (no apps running besides a terminal): name old time/op new time/op delta Template 256ms ± 1% 280ms ±15% +9.54% (p=0.008 n=5+5) Unicode 132ms ± 1% 132ms ± 1% ~ (p=0.690 n=5+5) GoTypes 891ms ± 1% 917ms ± 2% +2.88% (p=0.008 n=5+5) Compiler 3.84s ± 2% 3.99s ± 2% +3.95% (p=0.016 n=5+5) MakeBash 47.1s ± 1% 47.2s ± 2% ~ (p=0.841 n=5+5) name old user-ns/op new user-ns/op delta Template 309M ± 1% 326M ± 2% +5.18% (p=0.008 n=5+5) Unicode 165M ± 1% 168M ± 4% ~ (p=0.421 n=5+5) GoTypes 1.14G ± 2% 1.18G ± 1% +3.47% (p=0.008 n=5+5) Compiler 5.00G ± 1% 5.16G ± 1% +3.12% (p=0.008 n=5+5) Change-Id: I241c4246cdff627d7ecb95cac23060b38f9775ec Reviewed-on: https://go-review.googlesource.com/34273 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
// Copyright 2016 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package syntax
|
|
|
|
import (
|
|
"cmd/internal/src"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
// Mode describes the parser mode.
|
|
type Mode uint
|
|
|
|
// Error describes a syntax error. Error implements the error interface.
|
|
type Error struct {
|
|
Pos src.Pos
|
|
Msg string
|
|
}
|
|
|
|
func (err Error) Error() string {
|
|
return fmt.Sprintf("%s: %s", err.Pos, err.Msg)
|
|
}
|
|
|
|
var _ error = Error{} // verify that Error implements error
|
|
|
|
// An ErrorHandler is called for each error encountered reading a .go file.
|
|
type ErrorHandler func(err error)
|
|
|
|
// A Pragma value is a set of flags that augment a function or
|
|
// type declaration. Callers may assign meaning to the flags as
|
|
// appropriate.
|
|
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(pos src.Pos, text string) Pragma
|
|
|
|
// Parse parses a single Go source file from src and returns the corresponding
|
|
// syntax tree. If there are errors, Parse will return the first error found.
|
|
// The base argument 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
|
|
// immediately upon encountering an error.
|
|
//
|
|
// If a PragmaHandler is provided, it is called with each pragma encountered.
|
|
//
|
|
// The Mode argument is currently ignored.
|
|
func Parse(base *src.PosBase, src io.Reader, errh ErrorHandler, pragh PragmaHandler, mode Mode) (_ *File, first error) {
|
|
defer func() {
|
|
if p := recover(); p != nil {
|
|
if err, ok := p.(Error); ok {
|
|
first = err
|
|
return
|
|
}
|
|
panic(p)
|
|
}
|
|
}()
|
|
|
|
var p parser
|
|
p.init(base, src, errh, pragh)
|
|
p.next()
|
|
return p.file(), p.first
|
|
}
|
|
|
|
// ParseBytes behaves like Parse but it reads the source from the []byte slice provided.
|
|
func ParseBytes(base *src.PosBase, src []byte, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
|
|
return Parse(base, &bytesReader{src}, errh, pragh, mode)
|
|
}
|
|
|
|
type bytesReader struct {
|
|
data []byte
|
|
}
|
|
|
|
func (r *bytesReader) Read(p []byte) (int, error) {
|
|
if len(r.data) > 0 {
|
|
n := copy(p, r.data)
|
|
r.data = r.data[n:]
|
|
return n, nil
|
|
}
|
|
return 0, io.EOF
|
|
}
|
|
|
|
// ParseFile behaves like Parse but it reads the source from the named file.
|
|
func ParseFile(filename string, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
|
|
f, err := os.Open(filename)
|
|
if err != nil {
|
|
if errh != nil {
|
|
errh(err)
|
|
}
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
return Parse(src.NewFileBase(filename, filename), f, errh, pragh, mode)
|
|
}
|