目錄 搜尋
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 "text/scanner"

  • 概观

  • 索引

  • 示例

概观

程序包扫描程序为 UTF-8 编码的文本提供扫描程序和标记程序。它需要一个提供源的 io.Reader ,然后可以通过重复调用扫描功能对其进行标记。为了与现有工具兼容, NUL 字符是不允许的。如果源中的第一个字符是 UTF-8 编码的字节顺序标记 (BOM) ,它将被丢弃。

默认情况下,扫描程序会跳过空格并执行注释并识别 Go 语言规范定义的所有文字。它可以被定制为仅识别这些文字的一个子集并识别不同的标识符和空白字符。

示例

package mainimport ("fmt""strings""text/scanner")func main() {const src = `
// This is scanned code.
if a > 10 {
	someParsable = text
}`var s scanner.Scanner
	s.Init(strings.NewReader(src))
	s.Filename = "example"for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
		fmt.Printf("%s: %s\n", s.Position, s.TokenText())}}

索引

  • 常量

  • func TokenString(tok rune) string

  • type Position

  • func (pos *Position) IsValid() bool

  • func (pos Position) String() string

  • type Scanner

  • func (s *Scanner) Init(src io.Reader) *Scanner

  • func (s *Scanner) Next() rune

  • func (s *Scanner) Peek() rune

  • func (s *Scanner) Pos() (pos Position)

  • func (s *Scanner) Scan() rune

  • func (s *Scanner) TokenText() string

示例

打包

打包文件

scanner.go

常量

预定义的模式位控制令牌的识别。例如,要配置扫描仪,使其仅识别 (Go) 标识符,整数并跳过注释,请将扫描仪的模式字段设置为:

ScanIdents | ScanInts | SkipComments

除注释外,如果设置了 SkipComments ,将跳过注释,但不会忽略无法识别的令牌。相反,扫描仪只是返回相应的单个字符(或可能是子令牌)。例如,如果模式是 ScanIdents(而不是 ScanStrings ) ,则将字符串“ foo ”作为标记序列'' 'Ident '''进行扫描。

const (
        ScanIdents     = 1 << -Ident
        ScanInts       = 1 << -Int
        ScanFloats     = 1 << -Float // includes Ints
        ScanChars      = 1 << -Char
        ScanStrings    = 1 << -String
        ScanRawStrings = 1 << -RawString
        ScanComments   = 1 << -Comment
        SkipComments   = 1 << -skipComment // if set with ScanComments, comments become white space
        GoTokens       = ScanIdents | ScanFloats | ScanChars | ScanStrings | ScanRawStrings | ScanComments | SkipComments)

Scan 的结果是这些标志或 Unicode 字符之一。

const (
        EOF = -(iota + 1)
        Ident
        Int
        Float
        Char
        String
        RawString
        Comment)

GoWhitespace 是扫描仪空白字段的默认值。它的值选择 Go 的空白字符。

const GoWhitespace = 1<<'\t' | 1<<'\n' | 1<<'\r' | 1<<' '

func TokenString

func TokenString(tok rune) string

TokenString 为标志或 Unicode 字符返回可打印的字符串。

type Position

源位置由位置值表示。如果 Line> 0,则位置有效。

type Position struct {
        Filename string // filename, if any
        Offset   int    // byte offset, starting at 0
        Line     int    // line number, starting at 1
        Column   int    // column number, starting at 1 (character count per line)}

func (*Position) IsValid

func (pos *Position) IsValid() bool

IsValid 报告该位置是否有效。

func (Position) String

func (pos Position) String() string

键入 扫描仪

扫描仪实现从 io.Reader 读取 Unicode 字符和标记。

type Scanner struct {        // Error is called for each error encountered. If no Error        // function is set, the error is reported to os.Stderr.
        Error func(s *Scanner, msg string)        // ErrorCount is incremented by one for each error encountered.
        ErrorCount int        // The Mode field controls which tokens are recognized. For instance,        // to recognize Ints, set the ScanInts bit in Mode. The field may be        // changed at any time.
        Mode uint        // The Whitespace field controls which characters are recognized        // as white space. To recognize a character ch <= ' ' as white space,        // set the ch'th bit in Whitespace (the Scanner's behavior is undefined        // for values ch > ' '). The field may be changed at any time.
        Whitespace uint64        // IsIdentRune is a predicate controlling the characters accepted        // as the ith rune in an identifier. The set of valid characters        // must not intersect with the set of white space characters.        // If no IsIdentRune function is set, regular Go identifiers are        // accepted instead. The field may be changed at any time.
        IsIdentRune func(ch rune, i int) bool        // Start position of most recently scanned token; set by Scan.        // Calling Init or Next invalidates the position (Line == 0).        // The Filename field is always left untouched by the Scanner.        // If an error is reported (via Error) and Position is invalid,        // the scanner is not inside a token. Call Pos to obtain an error        // position in that case, or to obtain the position immediately        // after the most recently scanned token.
        Position        // contains filtered or unexported fields}

func (*Scanner) Init

func (s *Scanner) Init(src io.Reader) *Scanner

Init 用新源初始化扫描仪并返回 s 。错误设置为零, ErrorCount 设置为0,模式设置为 GoTokens ,并且空白设置为 GoWhitespace 。

func (*Scanner) Next

func (s *Scanner) Next() rune

接下来读取并返回下一个 Unicode 字符。它在源的末尾返回 EOF 。它通过调用 s.Error 来报告读取错误,如果不是零; 否则它会向 os.Stderr 输出一条错误消息。接下来不更新扫描仪的位置字段; 使用 Pos() 来获取当前位置。

func (*Scanner) Peek

func (s *Scanner) Peek() rune

Peek 将返回源中的下一个 Unicode 字符,而不会推进扫描程序。如果扫描仪的位置在源的最后一个字符处,它会返回 EOF 。

func (*Scanner) Pos

func (s *Scanner) Pos() (pos Position)

Pos 返回最后一次调用 Next 或 Scan 时返回的字符或标记之后的字符位置。将扫描仪的位置字段用于最近扫描的标记的开始位置。

func (*Scanner) Scan

func (s *Scanner) Scan() rune

扫描从源读取下一个标记或Unicode字符并将其返回。它只识别设置了相应模式位  (1<<-t)  的标志 t 。它在源的末尾返回 EOF 。它通过调用 s.Error 来报告扫描器错误(读取和令牌错误),如果不是零; 否则它会向 os.Stder r输出一条错误消息。

func (*Scanner) TokenText

func (s *Scanner) TokenText() string

TokenText 返回对应于最近扫描的标记的字符串。调用 Scan() 后有效。

上一篇: 下一篇: