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

@ -15,6 +15,7 @@ import (
type scanner struct {
source
nlsemi bool // if set '\n' and EOF translate to ';'
pragma Pragma
// current token, valid after calling next()
pos, line int
@ -24,12 +25,13 @@ type scanner struct {
op Operator // valid if tok is _Operator, _AssignOp, or _IncOp
prec int // valid if tok is _Operator, _AssignOp, or _IncOp
pragmas []Pragma
pragh PragmaHandler
}
func (s *scanner) init(src io.Reader, errh ErrorHandler) {
func (s *scanner) init(src io.Reader, errh ErrorHandler, pragh PragmaHandler) {
s.source.init(src, errh)
s.nlsemi = false
s.pragh = pragh
}
func (s *scanner) next() {
@ -540,6 +542,10 @@ func (s *scanner) lineComment() {
// recognize pragmas
var prefix string
r := s.getr()
if s.pragh == nil {
goto skip
}
switch r {
case 'g':
prefix = "go:"
@ -565,10 +571,7 @@ func (s *scanner) lineComment() {
}
r = s.getr()
}
s.pragmas = append(s.pragmas, Pragma{
Line: s.line,
Text: strings.TrimSuffix(string(s.stopLit()), "\r"),
})
s.pragma |= s.pragh(0, s.line, strings.TrimSuffix(string(s.stopLit()), "\r"))
return
skip: