package parser
import (
antlr "github.com/antlr4-go/antlr/v4"
"github.com/google/cel-go/common/runes"
)
type charStream struct {
buf runes.Buffer
pos int
src string
}
func (c *charStream) Consume() {
if c.pos >= c.buf.Len() {
panic("cannot consume EOF")
}
c.pos++
}
func (c *charStream) LA(offset int) int {
if offset == 0 {
return 0
}
if offset < 0 {
offset++
}
pos := c.pos + offset - 1
if pos < 0 || pos >= c.buf.Len() {
return antlr.TokenEOF
}
return int(c.buf.Get(pos))
}
func (c *charStream) LT(offset int) int {
return c.LA(offset)
}
func (c *charStream) Mark() int {
return -1
}
func (c *charStream) Release(marker int) {}
func (c *charStream) Index() int {
return c.pos
}
func (c *charStream) Seek(index int) {
if index <= c.pos {
c.pos = index
return
}
if index < c.buf.Len() {
c.pos = index
} else {
c.pos = c.buf.Len()
}
}
func (c *charStream) Size() int {
return c.buf.Len()
}
func (c *charStream) GetSourceName() string {
return c.src
}
func (c *charStream) GetText(start, stop int) string {
if stop >= c.buf.Len() {
stop = c.buf.Len() - 1
}
if start >= c.buf.Len() {
return ""
}
return c.buf.Slice(start, stop+1)
}
func (c *charStream) GetTextFromTokens(start, stop antlr.Token) string {
if start != nil && stop != nil {
return c.GetText(start.GetTokenIndex(), stop.GetTokenIndex())
}
return ""
}
func (c *charStream) GetTextFromInterval(i antlr.Interval) string {
return c.GetText(i.Start, i.Stop)
}
func (c *charStream) String() string {
return c.buf.Slice(0, c.buf.Len())
}
var _ antlr.CharStream = &charStream{}
func newCharStream(buf runes.Buffer, desc string) antlr.CharStream {
return &charStream{
buf: buf,
src: desc,
}
}