| 1 | // Copyright 2021 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 parser |
| 16 | |
| 17 | import ( |
| 18 | antlr "github.com/antlr4-go/antlr/v4" |
| 19 | |
| 20 | "github.com/google/cel-go/common/runes" |
| 21 | ) |
| 22 | |
| 23 | type charStream struct { |
| 24 | buf runes.Buffer |
| 25 | pos int |
| 26 | src string |
| 27 | } |
| 28 | |
| 29 | // Consume implements (antlr.CharStream).Consume. |
| 30 | func (c *charStream) Consume() { |
| 31 | if c.pos >= c.buf.Len() { |
| 32 | panic("cannot consume EOF") |
| 33 | } |
| 34 | c.pos++ |
| 35 | } |
| 36 | |
| 37 | // LA implements (antlr.CharStream).LA. |
| 38 | func (c *charStream) LA(offset int) int { |
| 39 | if offset == 0 { |
| 40 | return 0 |
| 41 | } |
| 42 | if offset < 0 { |
| 43 | offset++ |
| 44 | } |
| 45 | pos := c.pos + offset - 1 |
| 46 | if pos < 0 || pos >= c.buf.Len() { |
| 47 | return antlr.TokenEOF |
| 48 | } |
| 49 | return int(c.buf.Get(pos)) |
| 50 | } |
| 51 | |
| 52 | // LT mimics (*antlr.InputStream).LT. |
| 53 | func (c *charStream) LT(offset int) int { |
| 54 | return c.LA(offset) |
| 55 | } |
| 56 | |
| 57 | // Mark implements (antlr.CharStream).Mark. |
| 58 | func (c *charStream) Mark() int { |
| 59 | return -1 |
| 60 | } |
| 61 | |
| 62 | // Release implements (antlr.CharStream).Release. |
| 63 | func (c *charStream) Release(marker int) {} |
| 64 | |
| 65 | // Index implements (antlr.CharStream).Index. |
| 66 | func (c *charStream) Index() int { |
| 67 | return c.pos |
| 68 | } |
| 69 | |
| 70 | // Seek implements (antlr.CharStream).Seek. |
| 71 | func (c *charStream) Seek(index int) { |
| 72 | if index <= c.pos { |
| 73 | c.pos = index |
| 74 | return |
| 75 | } |
| 76 | if index < c.buf.Len() { |
| 77 | c.pos = index |
| 78 | } else { |
| 79 | c.pos = c.buf.Len() |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Size implements (antlr.CharStream).Size. |
| 84 | func (c *charStream) Size() int { |
| 85 | return c.buf.Len() |
| 86 | } |
| 87 | |
| 88 | // GetSourceName implements (antlr.CharStream).GetSourceName. |
| 89 | func (c *charStream) GetSourceName() string { |
| 90 | return c.src |
| 91 | } |
| 92 | |
| 93 | // GetText implements (antlr.CharStream).GetText. |
| 94 | func (c *charStream) GetText(start, stop int) string { |
| 95 | if stop >= c.buf.Len() { |
| 96 | stop = c.buf.Len() - 1 |
| 97 | } |
| 98 | if start >= c.buf.Len() { |
| 99 | return "" |
| 100 | } |
| 101 | return c.buf.Slice(start, stop+1) |
| 102 | } |
| 103 | |
| 104 | // GetTextFromTokens implements (antlr.CharStream).GetTextFromTokens. |
| 105 | func (c *charStream) GetTextFromTokens(start, stop antlr.Token) string { |
| 106 | if start != nil && stop != nil { |
| 107 | return c.GetText(start.GetTokenIndex(), stop.GetTokenIndex()) |
| 108 | } |
| 109 | return "" |
| 110 | } |
| 111 | |
| 112 | // GetTextFromInterval implements (antlr.CharStream).GetTextFromInterval. |
| 113 | func (c *charStream) GetTextFromInterval(i antlr.Interval) string { |
| 114 | return c.GetText(i.Start, i.Stop) |
| 115 | } |
| 116 | |
| 117 | // String mimics (*antlr.InputStream).String. |
| 118 | func (c *charStream) String() string { |
| 119 | return c.buf.Slice(0, c.buf.Len()) |
| 120 | } |
| 121 | |
| 122 | var _ antlr.CharStream = &charStream{} |
| 123 | |
| 124 | func newCharStream(buf runes.Buffer, desc string) antlr.CharStream { |
| 125 | return &charStream{ |
| 126 | buf: buf, |
| 127 | src: desc, |
| 128 | } |
| 129 | } |