?
This document uses PHP Chinese website manual Release
import "text/scanner"
概觀
索引
示例
程序包掃描程序為 UTF-8 編碼的文本提供掃描程序和標記程序。它需要一個提供源的 io.Reader ,然后可以通過重復調用掃描功能對其進行標記。為了與現(xiàn)有工具兼容, NUL 字符是不允許的。如果源中的第一個字符是 UTF-8 編碼的字節(jié)順序標記 (BOM) ,它將被丟棄。
默認情況下,掃描程序會跳過空格并執(zhí)行注釋并識別 Go 語言規(guī)范定義的所有文字。它可以被定制為僅識別這些文字的一個子集并識別不同的標識符和空白字符。
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) 標識符,整數(shù)并跳過注釋,請將掃描儀的模式字段設置為:
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(tok rune) string
TokenString 為標志或 Unicode 字符返回可打印的字符串。
源位置由位置值表示。如果 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 (pos *Position) IsValid() bool
IsValid 報告該位置是否有效。
func (pos Position) String() string
掃描儀實現(xiàn)從 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 (s *Scanner) Init(src io.Reader) *Scanner
Init 用新源初始化掃描儀并返回 s 。錯誤設置為零, ErrorCount 設置為0,模式設置為 GoTokens ,并且空白設置為 GoWhitespace 。
func (s *Scanner) Next() rune
接下來讀取并返回下一個 Unicode 字符。它在源的末尾返回 EOF 。它通過調用 s.Error 來報告讀取錯誤,如果不是零; 否則它會向 os.Stderr 輸出一條錯誤消息。接下來不更新掃描儀的位置字段; 使用 Pos() 來獲取當前位置。
func (s *Scanner) Peek() rune
Peek 將返回源中的下一個 Unicode 字符,而不會推進掃描程序。如果掃描儀的位置在源的最后一個字符處,它會返回 EOF 。
func (s *Scanner) Pos() (pos Position)
Pos 返回最后一次調用 Next 或 Scan 時返回的字符或標記之后的字符位置。將掃描儀的位置字段用于最近掃描的標記的開始位置。
func (s *Scanner) Scan() rune
掃描從源讀取下一個標記或Unicode字符并將其返回。它只識別設置了相應模式位 (1<<-t) 的標志 t 。它在源的末尾返回 EOF 。它通過調用 s.Error 來報告掃描器錯誤(讀取和令牌錯誤),如果不是零; 否則它會向 os.Stder r輸出一條錯誤消息。
func (s *Scanner) TokenText() string
TokenText 返回對應于最近掃描的標記的字符串。調用 Scan() 后有效。