diff options
author | Robert Griesemer <gri@golang.org> | 2021-04-05 15:48:32 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2021-04-07 05:19:29 +0000 |
commit | 8f1099b5850747cf61738606f6a3d1386f4458c6 (patch) | |
tree | 15929d236805b05affbb0829e401d3e3bbcb0354 /src/cmd/compile/internal/syntax | |
parent | 1395432f2330498c8e5661c14652996894f0cc7d (diff) | |
download | go-8f1099b5850747cf61738606f6a3d1386f4458c6.tar.gz go-8f1099b5850747cf61738606f6a3d1386f4458c6.zip |
cmd/compile/internal/syntax, types2: move cmpPos to pos.Cmp
Make position comparison generally available.
Change-Id: I94b6f658fa19a15b30574dbb2181879115c131a8
Reviewed-on: https://go-review.googlesource.com/c/go/+/307215
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'src/cmd/compile/internal/syntax')
-rw-r--r-- | src/cmd/compile/internal/syntax/pos.go | 39 | ||||
-rw-r--r-- | src/cmd/compile/internal/syntax/testing.go | 8 |
2 files changed, 43 insertions, 4 deletions
diff --git a/src/cmd/compile/internal/syntax/pos.go b/src/cmd/compile/internal/syntax/pos.go index 99734d42d8..baebcc995c 100644 --- a/src/cmd/compile/internal/syntax/pos.go +++ b/src/cmd/compile/internal/syntax/pos.go @@ -59,6 +59,45 @@ func (pos Pos) RelCol() uint { return pos.Col() } +// Cmp compares the positions p and q and returns a result r as follows: +// +// r < 0: p is before q +// r == 0: p and q are the same position (but may not be identical) +// r > 0: p is after q +// +// If p and q are in different files, p is before q if the filename +// of p sorts lexicographically before the filename of q. +func (p Pos) Cmp(q Pos) int { + pname := p.RelFilename() + qname := q.RelFilename() + switch { + case pname < qname: + return -1 + case pname > qname: + return +1 + } + + pline := p.Line() + qline := q.Line() + switch { + case pline < qline: + return -1 + case pline > qline: + return +1 + } + + pcol := p.Col() + qcol := q.Col() + switch { + case pcol < qcol: + return -1 + case pcol > qcol: + return +1 + } + + return 0 +} + func (pos Pos) String() string { rel := position_{pos.RelFilename(), pos.RelLine(), pos.RelCol()} abs := position_{pos.Base().Pos().RelFilename(), pos.Line(), pos.Col()} diff --git a/src/cmd/compile/internal/syntax/testing.go b/src/cmd/compile/internal/syntax/testing.go index 3e02dc1c5d..6a97dc0c2a 100644 --- a/src/cmd/compile/internal/syntax/testing.go +++ b/src/cmd/compile/internal/syntax/testing.go @@ -33,10 +33,10 @@ var errRx = regexp.MustCompile(`^ *ERROR *"?([^"]*)"?`) // for each Error is the position of the token immediately preceding // the comment, the Error message is the message msg extracted from // the comment, with all errors that are on the same line collected -// in a slice. If there is no preceding token (the `ERROR` comment -// appears in the beginning of the file), then the recorded position -// is unknown (line, col = 0, 0). If there are no ERROR comments, the -// result is nil. +// in a slice, in source order. If there is no preceding token (the +// `ERROR` comment appears in the beginning of the file), then the +// recorded position is unknown (line, col = 0, 0). If there are no +// ERROR comments, the result is nil. func ErrorMap(src io.Reader) (errmap map[uint][]Error) { // position of previous token var base *PosBase |