mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile/internal/syntax: establish principled position information
Until now, the parser set the position for each Node to the position of the first token belonging to that node. For compatibility with the now defunct gc parser, in many places that position information was modified when the gcCompat flag was set (which it was, by default). Furthermore, in some places, position information was not set at all. This change removes the gcCompat flag and all associated code, and sets position information for all nodes in a more principled way, as proposed by mdempsky (see #16943 for details). Specifically, the position of a node may not be at the very beginning of the respective production. For instance for an Operation `a + b`, the position associated with the node is the position of the `+`. Thus, for `a + b + c` we now get different positions for the two additions. This change does not pass toolstash -cmp because position information recorded in export data and pcline tables is different. There are no other functional changes. Added test suite testing the position of all nodes. Fixes #16943. Change-Id: I3fc02bf096bc3b3d7d2fa655dfd4714a1a0eb90c Reviewed-on: https://go-review.googlesource.com/37017 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
6910756f9b
commit
5267ac2732
4 changed files with 466 additions and 172 deletions
|
|
@ -10,9 +10,16 @@ import "cmd/internal/src"
|
|||
// Nodes
|
||||
|
||||
type Node interface {
|
||||
// Pos() returns the position associated with the node as follows:
|
||||
// 1) The position of a node representing a terminal syntax production
|
||||
// (Name, BasicLit, etc.) is the position of the respective production
|
||||
// in the source.
|
||||
// 2) The position of a node representing a non-terminal production
|
||||
// (IndexExpr, IfStmt, etc.) is the position of a token uniquely
|
||||
// associated with that production; usually the left-most one
|
||||
// ('[' for IndexExpr, 'if' for IfStmt, etc.)
|
||||
Pos() src.Pos
|
||||
aNode()
|
||||
init(p *parser)
|
||||
}
|
||||
|
||||
type node struct {
|
||||
|
|
@ -27,11 +34,6 @@ func (n *node) Pos() src.Pos {
|
|||
|
||||
func (*node) aNode() {}
|
||||
|
||||
// TODO(gri) we may be able to get rid of init here and in Node
|
||||
func (n *node) init(p *parser) {
|
||||
n.pos = p.pos()
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Files
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue