package tokenize import ( "unicode/utf8" ) // Input provides input-related functionality for the tokenize API. type Input struct { api *API } // PeekRune returns the UTF8 rune at the provided byte offset, including its byte width. // // The byte width is useful to know what byte offset you'll have to use to peek // the next byte or rune. Some UTF8 runes take up 4 bytes of data, so when the // first rune starts at offset = 0, the second rune might start at offset = 4. // // When an invalid UTF8 rune is encountered on the input, it is replaced with // the utf.RuneError rune. It's up to the caller to handle this as an error // when needed. // // When an error occurs during reading the input, an error will be returned. // When an offset is requested that is beyond the length of the available input // data, then the error will be io.EOF. func (i *Input) PeekRune(offset int) (rune, int, error) { return i.api.peekRune(offset) } func (api *API) peekRune(offset int) (rune, int, error) { return api.reader.RuneAt(api.stackFrame.offset + offset) } // SkipRune is used to skip over a single rune that was read from the input. // This tells the tokenizer: "I've seen this rune. It is of no interest. // I will now continue reading after this rune." // // This will merely update the position of the cursor (which keeps track of what // line and column we are on in APIthe input data). The rune is not added to // the output. // // After the call, byte offset 0 for PeekByte() and PeekRune() will point at // the first byte after the skipped rune. func (i *Input) SkipRune(r rune) { i.api.skipRune(r) } func (api *API) skipRune(r rune) { api.stackFrame.moveCursorByRune(r) api.stackFrame.offset += utf8.RuneLen(r) } // SkipRunes is used to skip over one or more runes that were read from the input. // This tells the tokenizer: "I've seen these runes. They are of no interest. // I will now continue reading after these runes." // // This will merely update the position of the cursor (which keeps track of what // line and column we are on in the input data). The runes are not added to // the output. // // After the call, byte offset 0 for PeekByte() and PeekRune() will point at // the first byte after the skipped runes. func (i *Input) SkipRunes(runes ...rune) { i.api.skipRunes(runes...) } func (api *API) skipRunes(runes ...rune) { for _, r := range runes { api.stackFrame.moveCursorByRune(r) api.stackFrame.offset += utf8.RuneLen(r) } } // AcceptRune is used to accept a single rune that was read from the input. // This tells the tokenizer: "I've seen this rune. I want to make use of it // for the final output, so please remember it for me. I will now continue // reading after this rune." // // This will update the position of the cursor (which keeps track of what line // and column we are on in the input data) and add the rune to the tokenizer // output. // // After the call, byte offset 0 for PeekByte() and PeekRune() will point at // the first byte after the accepted rune. func (i *Input) AcceptRune(r rune) { i.api.acceptRune(r) } func (api *API) acceptRune(r rune) { curBytesEnd := api.stackFrame.bytesEnd maxRequiredBytes := curBytesEnd + utf8.UTFMax api.growOutputData(maxRequiredBytes) w := utf8.EncodeRune(api.outputData[curBytesEnd:], r) api.stackFrame.bytesEnd += w api.stackFrame.offset += w api.stackFrame.moveCursorByRune(r) } // AcceptRunes is used to accept one or more runes that were read from the input. // This tells the tokenizer: "I've seen these runes. I want to make use of them // for the final output, so please remember them for me. I will now continue // reading after these runes." // // This will update the position of the cursor (which keeps track of what line // and column we are on in the input data) and add the runes to the tokenizer // output. // // After the call, byte offset 0 for PeekByte() and PeekRune() will point at // the first byte after the accepted runes. func (i *Input) AcceptRunes(runes ...rune) { i.api.acceptRunes(runes...) } func (api *API) acceptRunes(runes ...rune) { runesAsString := string(runes) byteLen := len(runesAsString) curBytesEnd := api.stackFrame.bytesEnd newBytesEnd := curBytesEnd + byteLen api.growOutputData(newBytesEnd) copy(api.outputData[curBytesEnd:], runesAsString) api.stackFrame.bytesEnd = newBytesEnd api.stackFrame.offset += byteLen for _, r := range runes { api.stackFrame.moveCursorByRune(r) } }