aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/syntax/source.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2018-01-17 21:42:51 -0800
committerRobert Griesemer <gri@golang.org>2018-02-12 22:57:57 +0000
commitb8906889861d0efaf5682a7d26417111eaba3480 (patch)
tree7d7870c860fd8f586f958259cc091ad662a71055 /src/cmd/compile/internal/syntax/source.go
parent670494827c42d4ac64a52dfa909cf6048308e133 (diff)
downloadgo-b8906889861d0efaf5682a7d26417111eaba3480.tar.gz
go-b8906889861d0efaf5682a7d26417111eaba3480.zip
cmd/compile/internal/syntax: implement comment reporting in scanner
R=go1.11 In order to collect comments in the AST and for error testing purposes, the scanner needs to not only recognize and skip comments, but also be able to report them if so desired. This change adds a mode flag to the scanner's init function which controls the scanner behavior around comments. In the common case where comments are not needed, there must be no significant overhead. Thus, comments are reported via a handler upcall rather than being returned as a _Comment token (which the parser would have to filter out with every scanner.next() call). Because the handlers for error messages, directives, and comments all look the same (they take a position and text), and because directives look like comments, and errors never start with a '/', this change simplifies the scanner's init call to only take one (error) handler instead of 2 or 3 different handlers with identical signature. It is trivial in the handler to determine if we have an error, directive, or general comment. Finally, because directives are comments, when reporting directives the full comment text is returned now rather than just the directive text. This simplifies the implementation and makes the scanner API more regular. Furthermore, it provides important information about the comment style used by a directive, which may matter eventually when we fully implement /*line file:line:col*/ directives. Change-Id: I2adbfcebecd615e4237ed3a832b6ceb9518bf09c Reviewed-on: https://go-review.googlesource.com/88215 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/cmd/compile/internal/syntax/source.go')
-rw-r--r--src/cmd/compile/internal/syntax/source.go9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/cmd/compile/internal/syntax/source.go b/src/cmd/compile/internal/syntax/source.go
index 4e3551225a..62eb0fdc30 100644
--- a/src/cmd/compile/internal/syntax/source.go
+++ b/src/cmd/compile/internal/syntax/source.go
@@ -124,7 +124,8 @@ redo:
// EOF
if s.r == s.w {
if s.ioerr != io.EOF {
- s.error(s.ioerr.Error())
+ // ensure we never start with a '/' (e.g., rooted path) in the error message
+ s.error("I/O error: " + s.ioerr.Error())
}
return -1
}
@@ -201,6 +202,10 @@ func (s *source) stopLit() []byte {
if len(s.lit) > 0 {
lit = append(s.lit, lit...)
}
- s.suf = -1 // no pending literal
+ s.killLit()
return lit
}
+
+func (s *source) killLit() {
+ s.suf = -1 // no pending literal
+}