continue
}
- // build syntaxt tree
- file, err := ParseBytes(nil, []byte(src), nil, nil, nil, 0)
+ // build syntax tree
+ file, err := Parse(nil, strings.NewReader(src), nil, nil, nil, 0)
if err != nil {
t.Errorf("parse error: %s: %v (%s)", src, err, test.nodetyp)
continue
panic(err)
}
- ast2, err := ParseBytes(src.NewFileBase(filename, filename), buf1.Bytes(), nil, nil, nil, 0)
+ ast2, err := Parse(src.NewFileBase(filename, filename), &buf1, nil, nil, nil, 0)
if err != nil {
panic(err)
}
}
func TestIssue17697(t *testing.T) {
- _, err := ParseBytes(nil, nil, nil, nil, nil, 0) // return with parser error, don't panic
+ _, err := Parse(nil, bytes.NewReader(nil), nil, nil, nil, 0) // return with parser error, don't panic
if err == nil {
t.Errorf("no error reported")
}
}
return name
}
- _, err := ParseBytes(nil, []byte(test.src), nil, nil, fileh, 0)
+ _, err := Parse(nil, strings.NewReader(test.src), nil, nil, fileh, 0)
if err == nil {
t.Errorf("%s: no error reported", test.src)
continue
import (
"fmt"
"os"
+ "strings"
"testing"
)
"package p; type _ = int; type T1 = struct{}; type ( _ = *struct{}; T2 = float32 )",
// TODO(gri) expand
} {
- ast, err := ParseBytes(nil, []byte(want), nil, nil, nil, 0)
+ ast, err := Parse(nil, strings.NewReader(want), nil, nil, nil, 0)
if err != nil {
t.Error(err)
continue
package syntax
import (
+ "bytes"
"fmt"
"os"
"strings"
func TestTokens(t *testing.T) {
// make source
- var buf []byte
+ var buf bytes.Buffer
for i, s := range sampleTokens {
- buf = append(buf, "\t\t\t\t"[:i&3]...) // leading indentation
- buf = append(buf, s.src...) // token
- buf = append(buf, " "[:i&7]...) // trailing spaces
- buf = append(buf, "/* foo */ // bar\n"...) // comments
+ buf.WriteString("\t\t\t\t"[:i&3]) // leading indentation
+ buf.WriteString(s.src) // token
+ buf.WriteString(" "[:i&7]) // trailing spaces
+ buf.WriteString("/* foo */ // bar\n") // comments
}
// scan source
var got scanner
- got.init(&bytesReader{buf}, nil, nil)
+ got.init(&buf, nil, nil)
got.next()
for i, want := range sampleTokens {
nlsemi := false
} {
var s scanner
nerrors := 0
- s.init(&bytesReader{[]byte(test.src)}, func(line, col uint, msg string) {
+ s.init(strings.NewReader(test.src), func(line, col uint, msg string) {
nerrors++
// only check the first error
if nerrors == 1 {
// process as much source as possible. If errh is nil, Parse will terminate
// immediately upon encountering an error.
//
-// If a PragmaHandler is provided, it is called with each pragma encountered.
+// If pragh != nil, it is called with each pragma encountered.
//
-// If a FilenameHandler is provided, it is called to process each filename
+// If fileh != nil, it is called to process each filename
// encountered in //line directives.
//
-// The Mode argument is currently ignored.
func Parse(base *src.PosBase, src io.Reader, errh ErrorHandler, pragh PragmaHandler, fileh FilenameHandler, mode Mode) (_ *File, first error) {
defer func() {
if p := recover(); p != nil {
return p.fileOrNil(), p.first
}
-// ParseBytes behaves like Parse but it reads the source from the []byte slice provided.
-func ParseBytes(base *src.PosBase, src []byte, errh ErrorHandler, pragh PragmaHandler, fileh FilenameHandler, mode Mode) (*File, error) {
- return Parse(base, &bytesReader{src}, errh, pragh, fileh, mode)
-}
-
-type bytesReader struct {
- data []byte
-}
-
-func (r *bytesReader) Read(p []byte) (int, error) {
- if len(r.data) > 0 {
- n := copy(p, r.data)
- r.data = r.data[n:]
- return n, nil
- }
- return 0, io.EOF
-}
-
// ParseFile behaves like Parse but it reads the source from the named file.
func ParseFile(filename string, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
f, err := os.Open(filename)