Debian Codegraph @

common/source.go

raw symbols SCIP
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/
16
17 import (
18 "github.com/google/cel-go/common/runesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/"
19
20 exprpbscip-go gomod google.golang.org/genproto/googleapis/api f6391c0de4c7 `google.golang.org/genproto/googleapis/api/expr/v1alpha1`/ "google.golang.org/genproto/googleapis/api/expr/v1alpha1scip-go gomod google.golang.org/genproto/googleapis/api f6391c0de4c7 `google.golang.org/genproto/googleapis/api/expr/v1alpha1`/"
21 )
22
23 // Source interface for filter source contents.
24 type Sourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source# interface {
25 // Content returns the source content represented as a string.
26 // Examples contents are the single file contents, textbox field,
27 // or url parameter.
28 Contentscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Content.() string
29
30 // Description gives a brief description of the source.
31 // Example descriptions are a file name or ui element.
32 Descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Description.() string
33
34 // LineOffsets gives the character offsets at which lines occur.
35 // The zero-th entry should refer to the break between the first
36 // and second line, or EOF if there is only one line of source.
37 LineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#LineOffsets.() []int32
38
39 // LocationOffset translates a Location to an offset.
40 // Given the line and column of the Location returns the
41 // Location's character offset in the Source, and a bool
42 // indicating whether the Location was found.
43 LocationOffsetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#LocationOffset.(location Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location#) (int32, bool)
44
45 // OffsetLocation translates a character offset to a Location, or
46 // false if the conversion was not feasible.
47 OffsetLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#OffsetLocation.(offset int32) (Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location#, bool)
48
49 // NewLocation takes an input line and column and produces a Location.
50 // The default behavior is to treat the line and column as absolute,
51 // but concrete derivations may use this method to convert a relative
52 // line and column position into an absolute location.
53 NewLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#NewLocation.(line, col int) Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location#
54
55 // Snippet returns a line of content and whether the line was found.
56 Snippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Snippet.(line int) (string, bool)
57 }
58
59 // The sourceImpl type implementation of the Source interface.
60 type sourceImplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl# struct {
61 runesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/.Bufferscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#Buffer.
62 descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#description. string
63 lineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#lineOffsets. []int32
64 }
65
66 var _ runesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/.Bufferscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/Buffer# = &sourceImplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#{}
67
68 // TODO(jimlarson) "Character offsets" should index the code points
69 // within the UTF-8 encoded string. It currently indexes bytes.
70 // Can be accomplished by using rune[] instead of string for contents.
71
72 // NewTextSource creates a new Source from the input text string.
73 func NewTextSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewTextSource().(text string) Sourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source# {
74 return NewStringSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewStringSource().(text, "<input>")
75 }
76
77 // NewStringSource creates a new Source from the given contents and description.
78 func NewStringSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewStringSource().(contents string, description string) Sourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source# {
79 // Compute line offsets up front as they are referred to frequently.
80 buf, offs := runesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/.NewBufferAndLineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/NewBufferAndLineOffsets().(contents)
81 return &sourceImplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#{
82 Bufferscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#Buffer.: buf,
83 descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#description.: description,
84 lineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#lineOffsets.: offs,
85 }
86 }
87
88 // NewInfoSource creates a new Source from a SourceInfo.
89 func NewInfoSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewInfoSource().(info *exprpbscip-go gomod google.golang.org/genproto/googleapis/api f6391c0de4c7 `google.golang.org/genproto/googleapis/api/expr/v1alpha1`/.SourceInfoscip-go gomod google.golang.org/genproto/googleapis/api f6391c0de4c7 `google.golang.org/genproto/googleapis/api/expr/v1alpha1`/SourceInfo#) Sourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source# {
90 return &sourceImplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#{
91 Bufferscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#Buffer.: runesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/.NewBufferscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/NewBuffer().(""),
92 descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#description.: info.GetLocationscip-go gomod google.golang.org/genproto/googleapis/api f6391c0de4c7 `google.golang.org/genproto/googleapis/api/expr/v1alpha1`/SourceInfo#GetLocation().(),
93 lineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#lineOffsets.: info.GetLineOffsetsscip-go gomod google.golang.org/genproto/googleapis/api f6391c0de4c7 `google.golang.org/genproto/googleapis/api/expr/v1alpha1`/SourceInfo#GetLineOffsets().(),
94 }
95 }
96
97 // Content implements the Source interface method.
98 func (s *sourceImplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#) Contentscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#Content().() string {
99 return s.Slicescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/Buffer#Slice.(0, s.Lenscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/Buffer#Len.())
100 }
101
102 // Description implements the Source interface method.
103 func (s *sourceImplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#) Descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#Description().() string {
104 return s.descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#description.
105 }
106
107 // LineOffsets implements the Source interface method.
108 func (s *sourceImplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#) LineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#LineOffsets().() []int32 {
109 return s.lineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#lineOffsets.
110 }
111
112 // LocationOffset implements the Source interface method.
113 func (s *sourceImplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#) LocationOffsetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#LocationOffset().(location Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location#) (int32, bool) {
114 if lineOffset, found := s.findLineOffsetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#findLineOffset().(location.Linescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location#Line.()); found {
115 return lineOffset + int32(location.Columnscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location#Column.()), true
116 }
117 return -1, false
118 }
119
120 // NewLocation implements the Source interface method.
121 func (s *sourceImplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#) NewLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#NewLocation().(line, col int) Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location# {
122 return NewLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewLocation().(line, col)
123 }
124
125 // OffsetLocation implements the Source interface method.
126 func (s *sourceImplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#) OffsetLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#OffsetLocation().(offset int32) (Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location#, bool) {
127 line, lineOffset := s.findLinescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#findLine().(offset)
128 return NewLocationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewLocation().(int(line), int(offset-lineOffset)), true
129 }
130
131 // Snippet implements the Source interface method.
132 func (s *sourceImplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#) Snippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#Snippet().(line int) (string, bool) {
133 charStart, found := s.findLineOffsetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#findLineOffset().(line)
134 if !found || s.Lenscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/Buffer#Len.() == 0 {
135 return "", false
136 }
137 charEnd, found := s.findLineOffsetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#findLineOffset().(line + 1)
138 if found {
139 return s.Slicescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/Buffer#Slice.(int(charStart), int(charEnd-1)), true
140 }
141 return s.Slicescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/Buffer#Slice.(int(charStart), s.Lenscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/runes`/Buffer#Len.()), true
142 }
143
144 // findLineOffset returns the offset where the (1-indexed) line begins,
145 // or false if line doesn't exist.
146 func (s *sourceImplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#) findLineOffsetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#findLineOffset().(line int) (int32, bool) {
147 if line == 1 {
148 return 0, true
149 }
150 if line > 1 && line <= int(len(s.lineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#lineOffsets.)) {
151 offset := s.lineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#lineOffsets.[line-2]
152 return offset, true
153 }
154 return -1, false
155 }
156
157 // findLine finds the line that contains the given character offset and
158 // returns the line number and offset of the beginning of that line.
159 // Note that the last line is treated as if it contains all offsets
160 // beyond the end of the actual source.
161 func (s *sourceImplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#) findLinescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#findLine().(characterOffset int32) (int32, int32) {
162 var line int32 = 1
163 for _, lineOffset := range s.lineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#lineOffsets. {
164 if lineOffset > characterOffset {
165 break
166 }
167 line++
168 }
169 if line == 1 {
170 return line, 0
171 }
172 return line, s.lineOffsetsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/sourceImpl#lineOffsets.[line-2]
173 }