diff options
Diffstat (limited to 'libgo/go/go/token/position.go')
-rw-r--r-- | libgo/go/go/token/position.go | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/libgo/go/go/token/position.go b/libgo/go/go/token/position.go index 88d7416..241133f 100644 --- a/libgo/go/go/token/position.go +++ b/libgo/go/go/token/position.go @@ -30,7 +30,9 @@ func (pos *Position) IsValid() bool { return pos.Line > 0 } // String returns a string in one of several forms: // // file:line:column valid position with file name +// file:line valid position with file name but no column (column == 0) // line:column valid position without file name +// line valid position without file name and no column (column == 0) // file invalid position with file name // - invalid position without file name // @@ -40,7 +42,10 @@ func (pos Position) String() string { if s != "" { s += ":" } - s += fmt.Sprintf("%d:%d", pos.Line, pos.Column) + s += fmt.Sprintf("%d", pos.Line) + if pos.Column != 0 { + s += fmt.Sprintf(":%d", pos.Column) + } } if s == "" { s = "-" @@ -204,28 +209,36 @@ func (f *File) SetLinesForContent(content []byte) { f.mutex.Unlock() } -// A lineInfo object describes alternative file and line number -// information (such as provided via a //line comment in a .go -// file) for a given file offset. +// A lineInfo object describes alternative file, line, and column +// number information (such as provided via a //line directive) +// for a given file offset. type lineInfo struct { // fields are exported to make them accessible to gob - Offset int - Filename string - Line int + Offset int + Filename string + Line, Column int } -// AddLineInfo adds alternative file and line number information for -// a given file offset. The offset must be larger than the offset for -// the previously added alternative line info and smaller than the -// file size; otherwise the information is ignored. -// -// AddLineInfo is typically used to register alternative position -// information for //line filename:line comments in source files. +// AddLineInfo is like AddLineColumnInfo with a column = 1 argument. +// It is here for backward-compatibility for code prior to Go 1.11. // func (f *File) AddLineInfo(offset int, filename string, line int) { + f.AddLineColumnInfo(offset, filename, line, 1) +} + +// AddLineColumnInfo adds alternative file, line, and column number +// information for a given file offset. The offset must be larger +// than the offset for the previously added alternative line info +// and smaller than the file size; otherwise the information is +// ignored. +// +// AddLineColumnInfo is typically used to register alternative position +// information for line directives such as //line filename:line:column. +// +func (f *File) AddLineColumnInfo(offset int, filename string, line, column int) { f.mutex.Lock() if i := len(f.infos); i == 0 || f.infos[i-1].Offset < offset && offset < f.size { - f.infos = append(f.infos, lineInfo{offset, filename, line}) + f.infos = append(f.infos, lineInfo{offset, filename, line, column}) } f.mutex.Unlock() } @@ -275,12 +288,25 @@ func (f *File) unpack(offset int, adjusted bool) (filename string, line, column line, column = i+1, offset-f.lines[i]+1 } if adjusted && len(f.infos) > 0 { - // almost no files have extra line infos + // few files have extra line infos if i := searchLineInfos(f.infos, offset); i >= 0 { alt := &f.infos[i] filename = alt.Filename if i := searchInts(f.lines, alt.Offset); i >= 0 { - line += alt.Line - i - 1 + // i+1 is the line at which the alternative position was recorded + d := line - (i + 1) // line distance from alternative position base + line = alt.Line + d + if alt.Column == 0 { + // alternative column is unknown => relative column is unknown + // (the current specification for line directives requires + // this to apply until the next PosBase/line directive, + // not just until the new newline) + column = 0 + } else if d == 0 { + // the alternative position base is on the current line + // => column is relative to alternative column + column = alt.Column + (offset - alt.Offset) + } } } } |