Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions runtime/Go/antlr/v4/file_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,61 @@
package antlr

import (
"bytes"
"io"
"bufio"
"os"
)

// This is an InputStream that is loaded from a file all at once
// when you construct the object.

type FileStream struct {
*InputStream

InputStream
filename string
}

//goland:noinspection GoUnusedExportedFunction
func NewFileStream(fileName string) (*FileStream, error) {

buf := bytes.NewBuffer(nil)

f, err := os.Open(fileName)
if err != nil {
return nil, err
}

defer func(f *os.File) {
errF := f.Close()
if errF != nil {
}
}(f)
_, err = io.Copy(buf, f)

reader := bufio.NewReader(f)
fInfo, err := f.Stat()
if err != nil {
return nil, err
}

fs := new(FileStream)
fs := &FileStream{
InputStream: InputStream{
index: 0,
name: fileName,
},
filename: fileName,
}

fs.filename = fileName
s := buf.String()
fs.InputStream = NewInputStream(s)
// Pre-build the buffer and read runes efficiently
//
fs.data = make([]rune, 0, fInfo.Size())
for {
r, _, err := reader.ReadRune()
if err != nil {
break
}
fs.data = append(fs.data, r)
}
fs.size = len(fs.data) // Size in runes

// All done.
//
return fs, nil

}

func (f *FileStream) GetSourceName() string {
Expand Down
44 changes: 38 additions & 6 deletions runtime/Go/antlr/v4/input_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,55 @@

package antlr

import (
"bufio"
"io"
)

type InputStream struct {
name string
index int
data []rune
size int
}

// NewInputStream creates a new input stream from the given string
func NewInputStream(data string) *InputStream {
// NewIoStream creates a new input stream from the given io.Reader reader.
// Note that the reader is read completely into memory and so it must actually
// have a stopping point - you cannot pass in a reader on an open-ended source such
// as a socket for instance.
func NewIoStream(reader io.Reader) *InputStream {

is := new(InputStream)
rReader := bufio.NewReader(reader)

is.name = "<empty>"
is.index = 0
is.data = []rune(data)
is := &InputStream{
name: "<empty>",
index: 0,
}

// Pre-build the buffer and read runes reasonably efficiently given that
// we don't exactly know how big the input is.
//
is.data = make([]rune, 0, 512)
for {
r, _, err := rReader.ReadRune()
if err != nil {
break
}
is.data = append(is.data, r)
}
is.size = len(is.data) // number of runes
return is
}

// NewInputStream creates a new input stream from the given string
func NewInputStream(data string) *InputStream {

is := &InputStream{
name: "<empty>",
index: 0,
data: []rune(data), // This is actually the most efficient way
}
is.size = len(is.data) // number of runes, but we could also use len(data), which is efficient too
return is
}

Expand Down