cmd/compile: handle pragmas immediately with -newparser=1

Instead of saving all pragmas and processing them after parsing is
finished, process them immediately during scanning like the current
lexer does.

This is a bit unfortunate because it means we can't use
syntax.ParseFile to concurrently parse files yet, but it fixes how we
report syntax errors in the presence of //line pragmas.

While here, add a bunch more gcCompat entries to syntax/parser.go to
get "go build -toolexec='toolstash -cmp' std cmd" passing. There are
still a few remaining cases only triggered building unit tests, but
this seems like a nice checkpoint.

Change-Id: Iaf3bbcf2849857a460496f31eea228e0c585ce13
Reviewed-on: https://go-review.googlesource.com/28226
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Matthew Dempsky 2016-08-30 16:31:53 -07:00
parent 69e7e8a696
commit ee161e8591
10 changed files with 169 additions and 129 deletions

View file

@ -12,17 +12,27 @@ import (
type Mode uint
// A Pragma value is a set of flags that augment a function
// declaration. Callers may assign meaning to the flags as
// appropriate.
type Pragma uint16
type ErrorHandler func(pos, line int, msg string)
// 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, line int, text string) Pragma
// TODO(gri) These need a lot more work.
func ReadFile(filename string, errh ErrorHandler, mode Mode) (*File, error) {
func ReadFile(filename string, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
src, err := os.Open(filename)
if err != nil {
return nil, err
}
defer src.Close()
return Read(src, errh, mode)
return Read(src, errh, pragh, mode)
}
type bytesReader struct {
@ -38,13 +48,13 @@ func (r *bytesReader) Read(p []byte) (int, error) {
return 0, io.EOF
}
func ReadBytes(src []byte, errh ErrorHandler, mode Mode) (*File, error) {
return Read(&bytesReader{src}, errh, mode)
func ReadBytes(src []byte, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
return Read(&bytesReader{src}, errh, pragh, mode)
}
func Read(src io.Reader, errh ErrorHandler, mode Mode) (*File, error) {
func Read(src io.Reader, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
var p parser
p.init(src, errh)
p.init(src, errh, pragh)
p.next()
ast := p.file()