github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/annotation.go (about)

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