| 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 common |
| 16 | |
| 17 | import ( |
| 18 | "github.com/google/cel-go/common/runes" |
| 19 | |
| 20 | exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 21 | ) |
| 22 | |
| 23 | // Source interface for filter source contents. |
| 24 | type 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 | Content() string |
| 29 | |
| 30 | // Description gives a brief description of the source. |
| 31 | // Example descriptions are a file name or ui element. |
| 32 | 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 | 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 | LocationOffset(location Location) (int32, bool) |
| 44 | |
| 45 | // OffsetLocation translates a character offset to a Location, or |
| 46 | // false if the conversion was not feasible. |
| 47 | OffsetLocation(offset int32) (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 | NewLocation(line, col int) Location |
| 54 | |
| 55 | // Snippet returns a line of content and whether the line was found. |
| 56 | Snippet(line int) (string, bool) |
| 57 | } |
| 58 | |
| 59 | // The sourceImpl type implementation of the Source interface. |
| 60 | type sourceImpl struct { |
| 61 | runes.Buffer |
| 62 | description string |
| 63 | lineOffsets []int32 |
| 64 | } |
| 65 | |
| 66 | var _ runes.Buffer = &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 NewTextSource(text string) Source { |
| 74 | return NewStringSource(text, "<input>") |
| 75 | } |
| 76 | |
| 77 | // NewStringSource creates a new Source from the given contents and description. |
| 78 | func NewStringSource(contents string, description string) Source { |
| 79 | // Compute line offsets up front as they are referred to frequently. |
| 80 | buf, offs := runes.NewBufferAndLineOffsets(contents) |
| 81 | return &sourceImpl{ |
| 82 | Buffer: buf, |
| 83 | description: description, |
| 84 | lineOffsets: offs, |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // NewInfoSource creates a new Source from a SourceInfo. |
| 89 | func NewInfoSource(info *exprpb.SourceInfo) Source { |
| 90 | return &sourceImpl{ |
| 91 | Buffer: runes.NewBuffer(""), |
| 92 | description: info.GetLocation(), |
| 93 | lineOffsets: info.GetLineOffsets(), |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Content implements the Source interface method. |
| 98 | func (s *sourceImpl) Content() string { |
| 99 | return s.Slice(0, s.Len()) |
| 100 | } |
| 101 | |
| 102 | // Description implements the Source interface method. |
| 103 | func (s *sourceImpl) Description() string { |
| 104 | return s.description |
| 105 | } |
| 106 | |
| 107 | // LineOffsets implements the Source interface method. |
| 108 | func (s *sourceImpl) LineOffsets() []int32 { |
| 109 | return s.lineOffsets |
| 110 | } |
| 111 | |
| 112 | // LocationOffset implements the Source interface method. |
| 113 | func (s *sourceImpl) LocationOffset(location Location) (int32, bool) { |
| 114 | if lineOffset, found := s.findLineOffset(location.Line()); found { |
| 115 | return lineOffset + int32(location.Column()), true |
| 116 | } |
| 117 | return -1, false |
| 118 | } |
| 119 | |
| 120 | // NewLocation implements the Source interface method. |
| 121 | func (s *sourceImpl) NewLocation(line, col int) Location { |
| 122 | return NewLocation(line, col) |
| 123 | } |
| 124 | |
| 125 | // OffsetLocation implements the Source interface method. |
| 126 | func (s *sourceImpl) OffsetLocation(offset int32) (Location, bool) { |
| 127 | line, lineOffset := s.findLine(offset) |
| 128 | return NewLocation(int(line), int(offset-lineOffset)), true |
| 129 | } |
| 130 | |
| 131 | // Snippet implements the Source interface method. |
| 132 | func (s *sourceImpl) Snippet(line int) (string, bool) { |
| 133 | charStart, found := s.findLineOffset(line) |
| 134 | if !found || s.Len() == 0 { |
| 135 | return "", false |
| 136 | } |
| 137 | charEnd, found := s.findLineOffset(line + 1) |
| 138 | if found { |
| 139 | return s.Slice(int(charStart), int(charEnd-1)), true |
| 140 | } |
| 141 | return s.Slice(int(charStart), s.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 *sourceImpl) findLineOffset(line int) (int32, bool) { |
| 147 | if line == 1 { |
| 148 | return 0, true |
| 149 | } |
| 150 | if line > 1 && line <= int(len(s.lineOffsets)) { |
| 151 | offset := s.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 *sourceImpl) findLine(characterOffset int32) (int32, int32) { |
| 162 | var line int32 = 1 |
| 163 | for _, lineOffset := range s.lineOffsets { |
| 164 | if lineOffset > characterOffset { |
| 165 | break |
| 166 | } |
| 167 | line++ |
| 168 | } |
| 169 | if line == 1 { |
| 170 | return line, 0 |
| 171 | } |
| 172 | return line, s.lineOffsets[line-2] |
| 173 | } |