diff --git a/src/cmd/compile/internal/syntax/nodes_test.go b/src/cmd/compile/internal/syntax/nodes_test.go index 1bba9eeacf..433ae30661 100644 --- a/src/cmd/compile/internal/syntax/nodes_test.go +++ b/src/cmd/compile/internal/syntax/nodes_test.go @@ -290,8 +290,8 @@ func testPos(t *testing.T, list []test, prefix, suffix string, extract func(*Fil 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 diff --git a/src/cmd/compile/internal/syntax/parser_test.go b/src/cmd/compile/internal/syntax/parser_test.go index 309f1333f4..684a8429af 100644 --- a/src/cmd/compile/internal/syntax/parser_test.go +++ b/src/cmd/compile/internal/syntax/parser_test.go @@ -131,7 +131,7 @@ func verifyPrint(filename string, ast1 *File) { 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) } @@ -155,7 +155,7 @@ func verifyPrint(filename string, ast1 *File) { } 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") } @@ -208,7 +208,7 @@ func TestLineDirectives(t *testing.T) { } 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 diff --git a/src/cmd/compile/internal/syntax/printer_test.go b/src/cmd/compile/internal/syntax/printer_test.go index bbf75a957d..c218924202 100644 --- a/src/cmd/compile/internal/syntax/printer_test.go +++ b/src/cmd/compile/internal/syntax/printer_test.go @@ -7,6 +7,7 @@ package syntax import ( "fmt" "os" + "strings" "testing" ) @@ -29,7 +30,7 @@ func TestPrintString(t *testing.T) { "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 diff --git a/src/cmd/compile/internal/syntax/scanner_test.go b/src/cmd/compile/internal/syntax/scanner_test.go index 53995e0c79..ba4ba8f69c 100644 --- a/src/cmd/compile/internal/syntax/scanner_test.go +++ b/src/cmd/compile/internal/syntax/scanner_test.go @@ -5,6 +5,7 @@ package syntax import ( + "bytes" "fmt" "os" "strings" @@ -42,17 +43,17 @@ func TestScanner(t *testing.T) { 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 @@ -337,7 +338,7 @@ func TestScanErrors(t *testing.T) { } { 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 { diff --git a/src/cmd/compile/internal/syntax/syntax.go b/src/cmd/compile/internal/syntax/syntax.go index f58d5efd29..f6e9303290 100644 --- a/src/cmd/compile/internal/syntax/syntax.go +++ b/src/cmd/compile/internal/syntax/syntax.go @@ -57,12 +57,11 @@ type FilenameHandler func(name string) string // 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 { @@ -80,24 +79,6 @@ func Parse(base *src.PosBase, src io.Reader, errh ErrorHandler, pragh PragmaHand 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)