go-parsekit/tokenize/api_output.go

142 lines
2.8 KiB
Go

package tokenize
import (
"unicode/utf8"
)
// Output provides output-related functionality for the tokenize API.
type Output struct {
api *API
}
func (o Output) String() string {
a := o.api
f := a.stackFrame
bytes := a.outputData[f.bytesStart:f.bytesEnd]
return string(bytes)
}
func (o Output) Runes() []rune {
return []rune(o.String())
}
func (o Output) Rune(offset int) rune {
a := o.api
r, _ := utf8.DecodeRune(a.outputData[a.stackFrame.bytesStart+offset:])
return r
}
func (o Output) ClearData() {
f := o.api.stackFrame
f.bytesEnd = f.bytesStart
}
func (o Output) SetBytes(bytes ...byte) {
o.ClearData()
o.AddBytes(bytes...)
}
func (o Output) AddByte(b byte) {
a := o.api
f := a.stackFrame
curBytesEnd := f.bytesEnd
a.growOutputData(curBytesEnd + 1)
a.outputData[curBytesEnd] = b
f.bytesEnd++
}
func (o Output) SetRunes(runes ...rune) {
o.ClearData()
o.AddRunes(runes...)
}
func (o Output) AddBytes(bytes ...byte) {
a := o.api
f := a.stackFrame
curBytesEnd := f.bytesEnd
newBytesEnd := curBytesEnd + len(bytes)
a.growOutputData(newBytesEnd)
copy(a.outputData[curBytesEnd:], bytes)
f.bytesEnd = newBytesEnd
}
func (o Output) AddRunes(runes ...rune) {
a := o.api
f := a.stackFrame
runesAsString := string(runes)
newBytesEnd := f.bytesEnd + len(runesAsString)
a.growOutputData(newBytesEnd)
copy(a.outputData[f.bytesEnd:], runesAsString)
f.bytesEnd = newBytesEnd
}
func (o Output) AddString(s string) {
o.AddBytes([]byte(s)...)
}
func (o Output) SetString(s string) {
o.ClearData()
o.AddBytes([]byte(s)...)
}
func (o Output) Tokens() []Token {
a := o.api
f := a.stackFrame
return a.outputTokens[f.tokenStart:f.tokenEnd]
}
func (o Output) Token(offset int) Token {
a := o.api
return a.outputTokens[a.stackFrame.tokenStart+offset]
}
func (o Output) TokenValue(offset int) interface{} {
a := o.api
return a.outputTokens[a.stackFrame.tokenStart+offset].Value
}
func (o Output) ClearTokens() {
f := o.api.stackFrame
f.tokenEnd = f.tokenStart
}
func (o Output) SetTokens(tokens ...Token) {
o.ClearTokens()
o.AddTokens(tokens...)
}
func (o Output) AddToken(token Token) {
a := o.api
f := a.stackFrame
tokenEnd := f.tokenEnd
a.growOutputTokens(tokenEnd + 1)
a.outputTokens[tokenEnd] = token
f.tokenEnd++
}
func (o Output) AddTokens(tokens ...Token) {
a := o.api
f := a.stackFrame
a.growOutputTokens(f.tokenEnd + len(tokens))
for _, t := range tokens {
a.outputTokens[f.tokenEnd] = t
f.tokenEnd++
}
}
func (api *API) growOutputTokens(requiredTokens int) {
if cap(api.outputTokens) < requiredTokens {
newTokens := make([]Token, requiredTokens*2)
copy(newTokens, api.outputTokens)
api.outputTokens = newTokens
}
}
func (api *API) growOutputData(requiredBytes int) {
if cap(api.outputData) < requiredBytes {
newBytes := make([]byte, requiredBytes*2)
copy(newBytes, api.outputData)
api.outputData = newBytes
}
}