github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/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 "github.com/shogo82148/std/io" 9 ) 10 11 // Mode describes the parser mode. 12 type Mode uint 13 14 // Modes supported by the parser. 15 const ( 16 CheckBranches Mode = 1 << iota 17 ) 18 19 // Error describes a syntax error. Error implements the error interface. 20 type Error struct { 21 Pos Pos 22 Msg string 23 } 24 25 func (err Error) Error() string 26 27 var _ error = Error{} 28 29 // An ErrorHandler is called for each error encountered reading a .go file. 30 type ErrorHandler func(err error) 31 32 // A Pragma value augments a package, import, const, func, type, or var declaration. 33 // Its meaning is entirely up to the PragmaHandler, 34 // except that nil is used to mean “no pragma seen.” 35 type Pragma interface{} 36 37 // A PragmaHandler is used to process //go: directives while scanning. 38 // It is passed the current pragma value, which starts out being nil, 39 // and it returns an updated pragma value. 40 // The text is the directive, with the "//" prefix stripped. 41 // The current pragma is saved at each package, import, const, func, type, or var 42 // declaration, into the File, ImportDecl, ConstDecl, FuncDecl, TypeDecl, or VarDecl node. 43 // 44 // If text is the empty string, the pragma is being returned 45 // to the handler unused, meaning it appeared before a non-declaration. 46 // The handler may wish to report an error. In this case, pos is the 47 // current parser position, not the position of the pragma itself. 48 // Blank specifies whether the line is blank before the pragma. 49 type PragmaHandler func(pos Pos, blank bool, text string, current Pragma) Pragma 50 51 // Parse parses a single Go source file from src and returns the corresponding 52 // syntax tree. If there are errors, Parse will return the first error found, 53 // and a possibly partially constructed syntax tree, or nil. 54 // 55 // If errh != nil, it is called with each error encountered, and Parse will 56 // process as much source as possible. In this case, the returned syntax tree 57 // is only nil if no correct package clause was found. 58 // If errh is nil, Parse will terminate immediately upon encountering the first 59 // error, and the returned syntax tree is nil. 60 // 61 // If pragh != nil, it is called with each pragma encountered. 62 func Parse(base *PosBase, src io.Reader, errh ErrorHandler, pragh PragmaHandler, mode Mode) (_ *File, first error) 63 64 // ParseFile behaves like Parse but it reads the source from the named file. 65 func ParseFile(filename string, errh ErrorHandler, pragh PragmaHandler, mode Mode) (*File, error)