]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/syntax: remove ParseBytes from API - not needed
authorRobert Griesemer <gri@golang.org>
Tue, 2 Jan 2018 21:36:38 +0000 (13:36 -0800)
committerRobert Griesemer <gri@golang.org>
Mon, 12 Feb 2018 22:57:49 +0000 (22:57 +0000)
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>
src/cmd/compile/internal/syntax/nodes_test.go
src/cmd/compile/internal/syntax/parser_test.go
src/cmd/compile/internal/syntax/printer_test.go
src/cmd/compile/internal/syntax/scanner_test.go
src/cmd/compile/internal/syntax/syntax.go

index 1bba9eeacff314453283b7a4bddd9a031dc2d2ff..433ae30661bcded722f632eb7809a197ed66e693 100644 (file)
@@ -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
index 309f1333f4e782765c0435d682caf74c0f808ec4..684a8429afb4e10ec8388fa5f25124eac46c60b7 100644 (file)
@@ -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
index bbf75a957da2c89ba9152ac2002a63b59f0b8f4a..c21892420299443d78b7fb613b96e355f887be76 100644 (file)
@@ -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
index 53995e0c79992b57c186d6e513b56adc279540a5..ba4ba8f69c870a0a2bf419eaa7cf402ba95f0991 100644 (file)
@@ -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 {
index f58d5efd2923cf71a6e34b29efaecd4a17163eca..f6e9303290124606e5585ea128f5837352c8eab1 100644 (file)
@@ -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)