Skip to content

Commit

Permalink
syntax: newScanner: return os.PathError when using io.Reader (google#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
adonovan authored Dec 7, 2018
1 parent 678bafe commit 2f5aafd
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions syntax/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io/ioutil"
"log"
"math/big"
"os"
"strconv"
"strings"
"unicode"
Expand Down Expand Up @@ -258,23 +259,23 @@ func newScanner(filename string, src interface{}, keepComments bool) (*scanner,
}, nil
}

func readSource(filename string, src interface{}) (data []byte, err error) {
func readSource(filename string, src interface{}) ([]byte, error) {
switch src := src.(type) {
case string:
data = []byte(src)
return []byte(src), nil
case []byte:
data = src
return src, nil
case io.Reader:
data, err = ioutil.ReadAll(src)
data, err := ioutil.ReadAll(src)
if err != nil {
err = &os.PathError{Op: "read", Path: filename, Err: err}
}
return data, nil
case nil:
data, err = ioutil.ReadFile(filename)
return ioutil.ReadFile(filename)
default:
return nil, fmt.Errorf("invalid source: %T", src)
}
if err != nil {
return nil, fmt.Errorf("reading %s: %s", filename, err)
}
return data, nil
}

// An Error describes the nature and position of a scanner or parser error.
Expand Down

0 comments on commit 2f5aafd

Please sign in to comment.