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:
parent
7ac393a3f2
commit
e87f2a1b70
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user