forked from antlr/antlr4
/
file_stream.go
49 lines (35 loc) · 842 Bytes
/
file_stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
// Use of this file is governed by the BSD 3-clause license that
// can be found in the LICENSE.txt file in the project root.
package antlr
import (
"bytes"
"io"
"os"
)
// This is an InputStream that is loaded from a file all at once
// when you construct the object.
type FileStream struct {
*InputStream
filename string
}
func NewFileStream(fileName string) (*FileStream, error) {
buf := bytes.NewBuffer(nil)
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer f.Close()
_, err = io.Copy(buf, f)
if err != nil {
return nil, err
}
fs := new(FileStream)
fs.filename = fileName
s := string(buf.Bytes())
fs.InputStream = NewInputStream(s)
return fs, nil
}
func (f *FileStream) GetSourceName() string {
return f.filename
}