forked from ruiaylin/pgparser
/
annotation.go
54 lines (44 loc) · 1.65 KB
/
annotation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package ast
// AnnotationIdx is the 1-based index of an annotation. AST nodes that can
// be annotated store such an index (unique within that AST).
type AnnotationIdx int32
// NoAnnotation is the uninitialized annotation index.
const NoAnnotation AnnotationIdx = 0
// AnnotatedNode is embedded in AST nodes that have an annotation.
type AnnotatedNode struct {
AnnIdx AnnotationIdx
}
// GetAnnotation retrieves the annotation associated with this node.
func (n AnnotatedNode) GetAnnotation(ann *Annotations) interface{} {
if n.AnnIdx == NoAnnotation {
return nil
}
return ann.Get(n.AnnIdx)
}
// SetAnnotation sets the annotation associated with this node.
func (n AnnotatedNode) SetAnnotation(ann *Annotations, annotation interface{}) {
ann.Set(n.AnnIdx, annotation)
}
// Annotations is a container for AST annotations.
type Annotations []interface{}
// MakeAnnotations allocates an annotations container of the given size.
func MakeAnnotations(numAnnotations AnnotationIdx) Annotations {
return make(Annotations, numAnnotations)
}
// Set an annotation in the container.
func (a *Annotations) Set(idx AnnotationIdx, annotation interface{}) {
(*a)[idx-1] = annotation
}
// Get an annotation from the container.
func (a *Annotations) Get(idx AnnotationIdx) interface{} {
return (*a)[idx-1]
}