Source File
read.go
Belonging Package
go/build
package build
import (
)
type importReader struct {
b *bufio.Reader
buf []byte
peek byte
err error
eof bool
nerr int
pos token.Position
}
func ( string, io.Reader) *importReader {
return &importReader{
b: bufio.NewReader(),
pos: token.Position{
Filename: ,
Line: 1,
Column: 1,
},
}
}
func ( byte) bool {
return 'A' <= && <= 'Z' || 'a' <= && <= 'z' || '0' <= && <= '9' || == '_' || >= utf8.RuneSelf
}
var (
errSyntax = errors.New("syntax error")
errNUL = errors.New("unexpected NUL in input")
)
func ( *importReader) () {
if .err == nil {
.err = errSyntax
}
}
func ( *importReader) () byte {
var byte
var error
if len(.buf) > 0 {
= .buf[0]
.buf = .buf[1:]
} else {
, = .b.ReadByte()
if == nil && == 0 {
= errNUL
}
}
if != nil {
if == io.EOF {
.eof = true
} else if .err == nil {
.err =
}
return 0
}
.pos.Offset++
if == '\n' {
.pos.Line++
.pos.Column = 1
} else {
.pos.Column++
}
return
}
switch {
case ' ', '\f', '\t', '\r', '\n', ';':
= .readByte()
continue
case '/':
= .readByte()
if == '/' {
for != '\n' && .err == nil && !.eof {
= .readByte()
}
} else if == '*' {
var byte
for ( != '*' || != '/') && .err == nil {
if .eof {
.syntaxError()
}
, = , .readByte()
}
} else {
.syntaxError()
}
= .readByte()
continue
}
}
break
}
.peek =
return .peek
}
case '"':
= false
for .err == nil {
if .eof {
.syntaxError()
}
= .readByteNoBuf()
if == '\\' {
.readByteNoBuf()
if .err != nil {
.syntaxError()
return false
}
continue
}
if == '"' {
= .readByteNoBuf()
goto
}
}
goto
case '`':
= false
for .err == nil {
if .eof {
.syntaxError()
}
= .readByteNoBuf()
if == '`' {
= .readByteNoBuf()
goto
}
}
case '/':
= .readByteNoBuf()
switch {
default:
= false
goto
case '*':
var byte
for ( != '*' || != '/') && .err == nil {
if .eof {
.syntaxError()
}
, = , .readByteNoBuf()
}
= false
case '/':
for := range goEmbed {
= .readByteNoBuf()
if != goEmbed[] {
goto
}
}
= .readByteNoBuf()
func ( *importReader) ( string) {
.peekByte(true)
for := 0; < len(); ++ {
if .nextByte(false) != [] {
.syntaxError()
return
}
}
if isIdent(.peekByte(false)) {
.syntaxError()
}
}
func ( *importReader) () {
:= .peekByte(true)
if !isIdent() {
.syntaxError()
return
}
for isIdent(.peekByte(false)) {
.peek = 0
}
}
func ( *importReader) () {
switch .nextByte(true) {
case '`':
for .err == nil {
if .nextByte(false) == '`' {
break
}
if .eof {
.syntaxError()
}
}
case '"':
for .err == nil {
:= .nextByte(false)
if == '"' {
break
}
if .eof || == '\n' {
.syntaxError()
}
if == '\\' {
.nextByte(false)
}
}
default:
.syntaxError()
}
}
func ( *importReader) () {
:= .peekByte(true)
if == '.' {
.peek = 0
} else if isIdent() {
.readIdent()
}
.readString()
}
func ( io.Reader, *fileInfo) error {
:= newImportReader(.name, )
.readKeyword("package")
.readIdent()
for .peekByte(true) == 'i' {
.readKeyword("import")
if .peekByte(true) == '(' {
.nextByte(false)
for .peekByte(true) != ')' && .err == nil {
.readImport()
}
.nextByte(false)
} else {
.readImport()
}
}
.header = .buf
.parsed, .parseErr = parser.ParseFile(.fset, .name, .header, parser.ImportsOnly|parser.ParseComments)
if .parseErr != nil {
return nil
}
:= false
for , := range .parsed.Decls {
, := .(*ast.GenDecl)
if ! {
continue
}
for , := range .Specs {
, := .(*ast.ImportSpec)
if ! {
continue
}
:= .Path.Value
, := strconv.Unquote()
if != nil {
return fmt.Errorf("parser returned invalid quoted string: <%s>", )
}
if == "embed" {
= true
}
:= .Doc
if == nil && len(.Specs) == 1 {
= .Doc
}
.imports = append(.imports, fileImport{, .Pos(), })
}
}
func ( string, token.Position) ([]fileEmbed, error) {
:= func( int) {
.Offset +=
.Column += utf8.RuneCountInString([:])
= [:]
}
:= func() {
:= strings.TrimLeftFunc(, unicode.IsSpace)
(len() - len())
}
var []fileEmbed
for (); != ""; () {
var string
:=
:
switch [0] {
default:
:= len()
for , := range {
if unicode.IsSpace() {
=
break
}
}
= [:]
()
case '`':
:= strings.Index([1:], "`")
if < 0 {
return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", )
}
= [1 : 1+]
(1 + + 1)
case '"':
:= 1
for ; < len(); ++ {
if [] == '\\' {
++
continue
}
if [] == '"' {
, := strconv.Unquote([:+1])
if != nil {
return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", [:+1])
}
=
( + 1)
break
}
}
if >= len() {
return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", )
}
}
if != "" {
, := utf8.DecodeRuneInString()
if !unicode.IsSpace() {
return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", )
}
}
= append(, fileEmbed{, })
}
return , nil
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |