2016-03-04 17:09:08 -08:00
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package syntax
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Mode uint
|
|
|
|
|
|
2016-08-30 16:31:53 -07:00
|
|
|
// A Pragma value is a set of flags that augment a function
|
|
|
|
|
// declaration. Callers may assign meaning to the flags as
|
|
|
|
|
// appropriate.
|
|
|
|
|
type Pragma uint16
|
|
|
|
|
|
2016-03-04 17:09:08 -08:00
|
|
|
type ErrorHandler func(pos, line int, msg string)
|
|
|
|
|
|
2016-08-30 16:31:53 -07:00
|
|
|
// A PragmaHandler is used to process //line and //go: directives as
|
|
|
|
|
// they're scanned. The returned Pragma value will be unioned into the
|
|
|
|
|
// next FuncDecl node.
|
|
|
|
|
type PragmaHandler func(pos, line int, text string) Pragma
|
|
|
|
|
|
2016-03-04 17:09:08 -08:00
|
|
|
// TODO(gri) These need a lot more work.
|
|
|
|
|
|
2016-08-30 16:31:53 -07:00
|
|
|
func ReadFile(filename string, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
|
2016-03-04 17:09:08 -08:00
|
|
|
src, err := os.Open(filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer src.Close()
|
2016-08-30 16:31:53 -07:00
|
|
|
return Read(src, errh, pragh, mode)
|
2016-03-04 17:09:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-30 16:31:53 -07:00
|
|
|
func ReadBytes(src []byte, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
|
|
|
|
|
return Read(&bytesReader{src}, errh, pragh, mode)
|
2016-03-04 17:09:08 -08:00
|
|
|
}
|
|
|
|
|
|
2016-08-30 16:31:53 -07:00
|
|
|
func Read(src io.Reader, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
|
2016-03-04 17:09:08 -08:00
|
|
|
var p parser
|
2016-08-30 16:31:53 -07:00
|
|
|
p.init(src, errh, pragh)
|
2016-03-04 17:09:08 -08:00
|
|
|
|
|
|
|
|
p.next()
|
|
|
|
|
ast := p.file()
|
|
|
|
|
|
|
|
|
|
if errh == nil && p.nerrors > 0 {
|
|
|
|
|
return nil, fmt.Errorf("%d syntax errors", p.nerrors)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ast, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Write(w io.Writer, n *File) error {
|
|
|
|
|
panic("unimplemented")
|
|
|
|
|
}
|