1
0
mirror of https://github.com/golang/go synced 2024-11-19 21:24:40 -07:00

cmd/compile/internal/syntax: remove ParseBytes from API - not needed

R=go1.11

Also: Minor updates to syntax.Parse doc string.

Change-Id: I649965be9670a2f1c3de2cdb350634ed21e36ad9
Reviewed-on: https://go-review.googlesource.com/85663
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Robert Griesemer 2018-01-02 13:36:38 -08:00
parent 7ac393a3f2
commit e87f2a1b70
5 changed files with 17 additions and 34 deletions

View File

@ -290,8 +290,8 @@ func testPos(t *testing.T, list []test, prefix, suffix string, extract func(*Fil
continue continue
} }
// build syntaxt tree // build syntax tree
file, err := ParseBytes(nil, []byte(src), nil, nil, nil, 0) file, err := Parse(nil, strings.NewReader(src), nil, nil, nil, 0)
if err != nil { if err != nil {
t.Errorf("parse error: %s: %v (%s)", src, err, test.nodetyp) t.Errorf("parse error: %s: %v (%s)", src, err, test.nodetyp)
continue continue

View File

@ -131,7 +131,7 @@ func verifyPrint(filename string, ast1 *File) {
panic(err) 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 { if err != nil {
panic(err) panic(err)
} }
@ -155,7 +155,7 @@ func verifyPrint(filename string, ast1 *File) {
} }
func TestIssue17697(t *testing.T) { 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 { if err == nil {
t.Errorf("no error reported") t.Errorf("no error reported")
} }
@ -208,7 +208,7 @@ func TestLineDirectives(t *testing.T) {
} }
return name 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 { if err == nil {
t.Errorf("%s: no error reported", test.src) t.Errorf("%s: no error reported", test.src)
continue continue

View File

@ -7,6 +7,7 @@ package syntax
import ( import (
"fmt" "fmt"
"os" "os"
"strings"
"testing" "testing"
) )
@ -29,7 +30,7 @@ func TestPrintString(t *testing.T) {
"package p; type _ = int; type T1 = struct{}; type ( _ = *struct{}; T2 = float32 )", "package p; type _ = int; type T1 = struct{}; type ( _ = *struct{}; T2 = float32 )",
// TODO(gri) expand // 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 { if err != nil {
t.Error(err) t.Error(err)
continue continue

View File

@ -5,6 +5,7 @@
package syntax package syntax
import ( import (
"bytes"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@ -42,17 +43,17 @@ func TestScanner(t *testing.T) {
func TestTokens(t *testing.T) { func TestTokens(t *testing.T) {
// make source // make source
var buf []byte var buf bytes.Buffer
for i, s := range sampleTokens { for i, s := range sampleTokens {
buf = append(buf, "\t\t\t\t"[:i&3]...) // leading indentation buf.WriteString("\t\t\t\t"[:i&3]) // leading indentation
buf = append(buf, s.src...) // token buf.WriteString(s.src) // token
buf = append(buf, " "[:i&7]...) // trailing spaces buf.WriteString(" "[:i&7]) // trailing spaces
buf = append(buf, "/* foo */ // bar\n"...) // comments buf.WriteString("/* foo */ // bar\n") // comments
} }
// scan source // scan source
var got scanner var got scanner
got.init(&bytesReader{buf}, nil, nil) got.init(&buf, nil, nil)
got.next() got.next()
for i, want := range sampleTokens { for i, want := range sampleTokens {
nlsemi := false nlsemi := false
@ -337,7 +338,7 @@ func TestScanErrors(t *testing.T) {
} { } {
var s scanner var s scanner
nerrors := 0 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++ nerrors++
// only check the first error // only check the first error
if nerrors == 1 { if nerrors == 1 {

View File

@ -57,12 +57,11 @@ type FilenameHandler func(name string) string
// process as much source as possible. If errh is nil, Parse will terminate // process as much source as possible. If errh is nil, Parse will terminate
// immediately upon encountering an error. // 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. // 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) { func Parse(base *src.PosBase, src io.Reader, errh ErrorHandler, pragh PragmaHandler, fileh FilenameHandler, mode Mode) (_ *File, first error) {
defer func() { defer func() {
if p := recover(); p != nil { 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 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. // 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) { func ParseFile(filename string, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
f, err := os.Open(filename) f, err := os.Open(filename)