github.com/sanprasirt/go@v0.0.0-20170607001320-a027466e4b6d/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  	"cmd/internal/src"
     9  	"fmt"
    10  	"io"
    11  	"os"
    12  )
    13  
    14  // Mode describes the parser mode.
    15  type Mode uint
    16  
    17  // Modes supported by the parser.
    18  const (
    19  	CheckBranches Mode = 1 << iota // check correct use of labels, break, continue, and goto statements
    20  )
    21  
    22  // Error describes a syntax error. Error implements the error interface.
    23  type Error struct {
    24  	Pos src.Pos
    25  	Msg string
    26  }
    27  
    28  func (err Error) Error() string {
    29  	return fmt.Sprintf("%s: %s", err.Pos, err.Msg)
    30  }
    31  
    32  var _ error = Error{} // verify that Error implements error
    33  
    34  // An ErrorHandler is called for each error encountered reading a .go file.
    35  type ErrorHandler func(err error)
    36  
    37  // A Pragma value is a set of flags that augment a function or
    38  // type declaration. Callers may assign meaning to the flags as
    39  // appropriate.
    40  type Pragma uint16
    41  
    42  // A PragmaHandler is used to process //line and //go: directives as
    43  // they're scanned. The returned Pragma value will be unioned into the
    44  // next FuncDecl node.
    45  type PragmaHandler func(pos src.Pos, text string) Pragma
    46  
    47  // Parse parses a single Go source file from src and returns the corresponding
    48  // syntax tree. If there are errors, Parse will return the first error found,
    49  // and a possibly partially constructed syntax tree, or nil if no correct package
    50  // clause was found. The base argument is only used for position information.
    51  //
    52  // If errh != nil, it is called with each error encountered, and Parse will
    53  // process as much source as possible. If errh is nil, Parse will terminate
    54  // immediately upon encountering an error.
    55  //
    56  // If a PragmaHandler is provided, it is called with each pragma encountered.
    57  //
    58  // The Mode argument is currently ignored.
    59  func Parse(base *src.PosBase, src io.Reader, errh ErrorHandler, pragh PragmaHandler, mode Mode) (_ *File, first error) {
    60  	defer func() {
    61  		if p := recover(); p != nil {
    62  			if err, ok := p.(Error); ok {
    63  				first = err
    64  				return
    65  			}
    66  			panic(p)
    67  		}
    68  	}()
    69  
    70  	var p parser
    71  	p.init(base, src, errh, pragh, mode)
    72  	p.next()
    73  	return p.fileOrNil(), p.first
    74  }
    75  
    76  // ParseBytes behaves like Parse but it reads the source from the []byte slice provided.
    77  func ParseBytes(base *src.PosBase, src []byte, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
    78  	return Parse(base, &bytesReader{src}, errh, pragh, mode)
    79  }
    80  
    81  type bytesReader struct {
    82  	data []byte
    83  }
    84  
    85  func (r *bytesReader) Read(p []byte) (int, error) {
    86  	if len(r.data) > 0 {
    87  		n := copy(p, r.data)
    88  		r.data = r.data[n:]
    89  		return n, nil
    90  	}
    91  	return 0, io.EOF
    92  }
    93  
    94  // ParseFile behaves like Parse but it reads the source from the named file.
    95  func ParseFile(filename string, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error) {
    96  	f, err := os.Open(filename)
    97  	if err != nil {
    98  		if errh != nil {
    99  			errh(err)
   100  		}
   101  		return nil, err
   102  	}
   103  	defer f.Close()
   104  	return Parse(src.NewFileBase(filename, filename), f, errh, pragh, mode)
   105  }