aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/syntax/source.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2016-12-09 11:14:26 -0800
committerRobert Griesemer <gri@golang.org>2016-12-09 23:34:24 +0000
commitf3b56de4d2a9ad5a3ed538455158b8e003b2e25e (patch)
tree9177455f819898393075a7c745173ee6c7b9b8b0 /src/cmd/compile/internal/syntax/source.go
parent48d029fe431f2c19e0ccc62a33de059c7725ee93 (diff)
downloadgo-f3b56de4d2a9ad5a3ed538455158b8e003b2e25e.tar.gz
go-f3b56de4d2a9ad5a3ed538455158b8e003b2e25e.zip
[dev.inline] cmd/compile/internal/syntax: report byte offset rather then rune count for column value
This will only become user-visible if error messages show column information. Per the discussion in #10324. For #10324. Change-Id: I5959c1655aba74bb1a22fdc261cd728ffcfa6912 Reviewed-on: https://go-review.googlesource.com/34244 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> 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.go11
1 files changed, 7 insertions, 4 deletions
diff --git a/src/cmd/compile/internal/syntax/source.go b/src/cmd/compile/internal/syntax/source.go
index 037742d73c..4ce35a3615 100644
--- a/src/cmd/compile/internal/syntax/source.go
+++ b/src/cmd/compile/internal/syntax/source.go
@@ -32,7 +32,7 @@ type source struct {
offs int // source offset of buf
r0, r, w int // previous/current read and write buf positions, excluding sentinel
line0, line uint // previous/current line
- col0, col uint // previous/current column
+ col0, col uint // previous/current column (byte offsets from line start)
ioerr error // pending io error
// literal buffer
@@ -50,7 +50,7 @@ func (s *source) init(src io.Reader, errh func(line, pos uint, msg string)) {
s.offs = 0
s.r0, s.r, s.w = 0, 0, 0
s.line0, s.line = 1, 1
- s.col0, s.col = 1, 1
+ s.col0, s.col = 0, 0
s.ioerr = nil
s.lit = s.lit[:0]
@@ -102,6 +102,9 @@ redo:
// (invariant: s.buf[s.w] == utf8.RuneSelf)
if b := s.buf[s.r]; b < utf8.RuneSelf {
s.r++
+ // TODO(gri) Optimization: Instead of adjusting s.col for each character,
+ // remember the line offset instead and then compute the offset as needed
+ // (which is less often).
s.col++
if b == 0 {
s.error("invalid NUL character")
@@ -109,7 +112,7 @@ redo:
}
if b == '\n' {
s.line++
- s.col = 1
+ s.col = 0
}
return rune(b)
}
@@ -125,7 +128,7 @@ redo:
// uncommon case: not ASCII
r, w := utf8.DecodeRune(s.buf[s.r:s.w])
s.r += w
- s.col++
+ s.col += uint(w)
if r == utf8.RuneError && w == 1 {
s.error("invalid UTF-8 encoding")