github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/src/cmd/compile/internal/syntax/syntax.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package syntax
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  	"os"
    11  )
    12  
    13  type Mode uint
    14  
    15  // A Pragma value is a set of flags that augment a function or
    16  // type declaration. Callers may assign meaning to the flags as
    17  // appropriate.
    18  type Pragma uint16
    19  
    20  type ErrorHandler func(pos, line int, msg string)
    21  
    22  // A PragmaHandler is used to process //line and //go: directives as
    23  // they're scanned. The returned Pragma value will be unioned into the
    24  // next FuncDecl node.
    25  type PragmaHandler func(pos, line int, text string) Pragma
    26  
    27  // TODO(gri) These need a lot more work.
    28  
    29  func ReadFile(filename string, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
    30  	src, err := os.Open(filename)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	defer src.Close()
    35  	return Read(src, errh, pragh, mode)
    36  }
    37  
    38  type bytesReader struct {
    39  	data []byte
    40  }
    41  
    42  func (r *bytesReader) Read(p []byte) (int, error) {
    43  	if len(r.data) > 0 {
    44  		n := copy(p, r.data)
    45  		r.data = r.data[n:]
    46  		return n, nil
    47  	}
    48  	return 0, io.EOF
    49  }
    50  
    51  func ReadBytes(src []byte, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
    52  	return Read(&bytesReader{src}, errh, pragh, mode)
    53  }
    54  
    55  func Read(src io.Reader, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
    56  	var p parser
    57  	p.init(src, errh, pragh)
    58  
    59  	p.next()
    60  	ast := p.file()
    61  
    62  	if errh == nil && p.nerrors > 0 {
    63  		return nil, fmt.Errorf("%d syntax errors", p.nerrors)
    64  	}
    65  
    66  	return ast, nil
    67  }
    68  
    69  func Write(w io.Writer, n *File) error {
    70  	panic("unimplemented")
    71  }