aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2011-01-05 10:08:39 -0800
committerRobert Griesemer <gri@golang.org>2011-01-05 10:08:39 -0800
commit4a7d1f2061e9211532c443713872db0859cd70d4 (patch)
tree1dbf0b1b71db5fb258563aef904f9a093eb658e3
parentd84317ba586d1a6cfe121d3dd4fcd6ae3db6b225 (diff)
downloadgo-4a7d1f2061e9211532c443713872db0859cd70d4.tar.gz
go-4a7d1f2061e9211532c443713872db0859cd70d4.zip
go/ast: correct Pos/End ranges for field lists
R=rsc CC=golang-dev https://golang.org/cl/3797045
-rw-r--r--src/pkg/go/ast/ast.go30
1 files changed, 26 insertions, 4 deletions
diff --git a/src/pkg/go/ast/ast.go b/src/pkg/go/ast/ast.go
index e5f2190d13..15fef44565 100644
--- a/src/pkg/go/ast/ast.go
+++ b/src/pkg/go/ast/ast.go
@@ -120,14 +120,36 @@ func (f *Field) End() token.Pos {
// A FieldList represents a list of Fields, enclosed by parentheses or braces.
type FieldList struct {
- Opening token.Pos // position of opening parenthesis/brace
+ Opening token.Pos // position of opening parenthesis/brace, if any
List []*Field // field list
- Closing token.Pos // position of closing parenthesis/brace
+ Closing token.Pos // position of closing parenthesis/brace, if any
}
-func (list *FieldList) Pos() token.Pos { return list.Opening }
-func (list *FieldList) End() token.Pos { return list.Closing + 1 }
+func (f *FieldList) Pos() token.Pos {
+ if f.Opening.IsValid() {
+ return f.Opening
+ }
+ // the list should not be empty in this case;
+ // be conservative and guard against bad ASTs
+ if len(f.List) > 0 {
+ return f.List[0].Pos()
+ }
+ return token.NoPos
+}
+
+
+func (f *FieldList) End() token.Pos {
+ if f.Closing.IsValid() {
+ return f.Closing + 1
+ }
+ // the list should not be empty in this case;
+ // be conservative and guard against bad ASTs
+ if n := len(f.List); n > 0 {
+ return f.List[n-1].End()
+ }
+ return token.NoPos
+}
// NumFields returns the number of (named and anonymous fields) in a FieldList.