//go:generate re2go $INPUT -o $OUTPUT
package main
import (
"os"
"strings"
)
/*!max:re2c*/
const BUFSIZE uint = 4096
type Input struct {
file *os.File
yyinput []byte
yycursor uint
yylimit uint
token uint
eof bool
}
func fill(in *Input, need uint) int {
if in.eof { return -1 } // unexpected EOF
// Error: lexeme too long. In real life can reallocate a larger buffer.
if in.token < need { return -2 }
// Shift buffer contents (discard everything up to the current token).
copy(in.yyinput[0:], in.yyinput[in.token:in.yylimit])
in.yycursor -= in.token
in.yylimit -= in.token
in.token = 0
// Fill free space at the end of buffer with new data from file.
n, _ := in.file.Read(in.yyinput[in.yylimit:BUFSIZE])
in.yylimit += uint(n)
re2c version: 4.0.1~e5cb9a1eb
Select example "go/fill/02_fill.re"