예배 규칙서 찾다
archive archive/tar archive/zip bufio bufio(缓存) builtin builtin(内置包) bytes bytes(包字节) compress compress/bzip2(压缩/bzip2) compress/flate(压缩/flate) compress/gzip(压缩/gzip) compress/lzw(压缩/lzw) compress/zlib(压缩/zlib) container container/heap(容器数据结构heap) container/list(容器数据结构list) container/ring(容器数据结构ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(数据库/sql) database/sql/driver(数据库/sql/driver) debug debug/dwarf(调试/dwarf) debug/elf(调试/elf) debug/gosym(调试/gosym) debug/macho(调试/macho) debug/pe(调试/pe) debug/plan9obj(调试/plan9obj) encoding encoding(编码) encoding/ascii85(编码/ascii85) encoding/asn1(编码/asn1) encoding/base32(编码/base32) encoding/base64(编码/base64) encoding/binary(编码/binary) encoding/csv(编码/csv) encoding/gob(编码/gob) encoding/hex(编码/hex) encoding/json(编码/json) encoding/pem(编码/pem) encoding/xml(编码/xml) errors errors(错误) expvar expvar flag flag(命令行参数解析flag包) fmt fmt go go/ast(抽象语法树) go/build go/constant(常量) go/doc(文档) go/format(格式) go/importer go/parser go/printer go/scanner(扫描仪) go/token(令牌) go/types(类型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(图像) image/color(颜色) image/color/palette(调色板) image/draw(绘图) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系统) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路径) plugin plugin(插件) reflect reflect(反射) regexp regexp(正则表达式) regexp/syntax runtime runtime(运行时) runtime/debug(调试) runtime/internal/sys runtime/pprof runtime/race(竞争) runtime/trace(执行追踪器) sort sort(排序算法) strconv strconv(转换) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系统调用) testing testing(测试) testing/iotest testing/quick text text/scanner(扫描文本) text/tabwriter text/template(定义模板) text/template/parse time time(时间戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
문자

  • import "go/scanner"

  • Overview

  • Index

  • Examples

Overview

Package scanner为Go源文本实现扫描器。它需要一个[]字节作为源,然后可以通过重复调用Scan方法来进行标记。

索引

  • func PrintError(w io.Writer, err error)

  • type Error

  • func (e Error) Error() string

  • type ErrorHandler

  • type ErrorList

  • func (p *ErrorList) Add(pos token.Position, msg string)

  • func (p ErrorList) Err() error

  • func (p ErrorList) Error() string

  • func (p ErrorList) Len() int

  • func (p ErrorList) Less(i, j int) bool

  • func (p *ErrorList) RemoveMultiples()

  • func (p *ErrorList) Reset()

  • func (p ErrorList) Sort()

  • func (p ErrorList) Swap(i, j int)

  • type Mode

  • type Scanner

  • func (s *Scanner) Init(file *token.File, src []byte, err ErrorHandler, mode Mode)

  • func (s *Scanner) Scan() (pos token.Pos, tok token.Token, lit string)

例子

Scanner.Scan

包文件

errors.go scanner.go

func PrintError

func PrintError(w io.Writer, err error)

PrintError是一个实用程序函数,如果err参数是ErrorList,则会向w打印一个错误列表,每行一个错误。否则,它会打印错误字符串。

type Error

在ErrorList中,错误由* Error表示。Pos位置(如果有效)指向有问题的令牌的开始,错误情况由Msg描述。

type Error struct {
        Pos token.Position
        Msg string}

func (Error) Error

func (e Error) Error() string

Error实现了错误接口。

type ErrorHandler

一个ErrorHandler可以提供给Scanner.Init。如果遇到语法错误并且安装了处理程序,则会使用位置和错误消息调用处理程序。位置指向违规令牌的开始。

type ErrorHandler func(pos token.Position, msg string)

type ErrorList

ErrorList是*错误的列表。ErrorList的零值是一个空的ErrorList,可以使用。

type ErrorList []*Error

func (*ErrorList) Add

func (p *ErrorList) Add(pos token.Position, msg string)

Add添加一个给定位置和错误消息的错误到一个ErrorList。

func (ErrorList) Err

func (p ErrorList) Err() error

Err返回等同于此错误列表的错误。如果列表为空,则Err返回nil。

func (ErrorList) Error

func (p ErrorList) Error() string

ErrorList实现错误接口。

func (ErrorList) Len

func (p ErrorList) Len() int

ErrorList实现排序接口。

func (ErrorList) Less

func (p ErrorList) Less(i, j int) bool

func (*ErrorList) RemoveMultiples

func (p *ErrorList) RemoveMultiples()

RemoveMultiples对一个ErrorList进行排序并删除每行的第一个错误。

func (*ErrorList) Reset

func (p *ErrorList) Reset()

重置将错误列表重置为无错误。

func (ErrorList) Sort

func (p ErrorList) Sort()

Sort排序错误列表。*错误条目按位置排序,其他错误按错误消息排序,并且在任何*错误条目之前。

func (ErrorList) Swap

func (p ErrorList) Swap(i, j int)

type Mode

mode值是一组标志(或0)。他们控制扫描仪行为。

type Mode uint
const (
        ScanComments Mode = 1 << iota // return comments as COMMENT tokens)

type Scanner

Scanner在处理给定文本时保存扫描仪的内部状态。它可以作为另一个数据结构的一部分进行分配,但必须在使用前通过Init进行初始化。

type Scanner struct {        // public state - ok to modify
        ErrorCount int // number of errors encountered        // contains filtered or unexported fields}

func (*Scanner) Init

func (s *Scanner) Init(file *token.File, src []byte, err ErrorHandler, mode Mode)

Init通过将扫描器设置为src的开头来准备扫描器来标记文本src。扫描仪使用文件集文件获取位置信息,并为每行添加行信息。当重新扫描相同的文件时,重新使用同一文件是可以的,因为已经存在的行信息将被忽略。如果文件大小与src大小不匹配,则Init会导致混乱。

如果遇到语法错误并且err不为零,则调用扫描将调用错误处理程序err。此外,对于遇到的每个错误,扫描器字段ErrorCount都会加1。模式参数决定如何处理注释。

请注意,如果文件的第一个字符有错误,Init可能会调用err。

func (*Scanner) Scan

func (s *Scanner) Scan() (pos token.Pos, tok token.Token, lit string)

Scan扫描下一个标记并返回标记位置,标记及其文字字符串(如果适用)。源端由token.EOF指示。

如果返回的标记是文字(token.IDENT,token.INT,token.FLOAT,token.IMAG,token.CHAR,token.STRING)或token.COMMENT,则文字字符串具有相应的值。

如果返回的标记是关键字,则字符串是关键字。

如果返回的令牌是token.SEMICOLON,则相应的文字字符串是“;” 如果分号存在于源中,则“\ n”如果由于换行符或EOF而插入了分号。

如果返回的标记是token.ILLEGAL,则文字字符串是违规字符。

在其他所有情况下,Scan会返回一个空的文字字符串。

对于更宽容的解析,如果可能,即使遇到语法错误,Scan也会返回有效的令牌。因此,即使生成的令牌序列不包含非法令牌,客户端也不能认为没有发生错误。相反,它必须检查扫描器的ErrorCount或错误处理程序的调用次数,如果安装了一个。

Scan 行信息添加到添加到Init文件集的文件中。令牌位置与该文件相关,因此与文件集有关。

例子

package mainimport ("fmt""go/scanner""go/token")func main() {// src is the input that we want to tokenize.
	src := []byte("cos(x) + 1i*sin(x) // Euler")// Initialize the scanner.var s scanner.Scanner
	fset := token.NewFileSet()                      // positions are relative to fset
	file := fset.AddFile("", fset.Base(), len(src)) // register input "file"
	s.Init(file, src, nil /* no error handler */, scanner.ScanComments)// Repeated calls to Scan yield the token sequence found in the input.for {
		pos, tok, lit := s.Scan()if tok == token.EOF {break}
		fmt.Printf("%s\t%s\t%q\n", fset.Position(pos), tok, lit)}}
이전 기사: 다음 기사: