...

Source file src/cmd/compile/internal/syntax/syntax.go

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

View as plain text