1 // Copyright 2009 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.
5 // Package scanner provides a scanner and tokenizer for UTF-8-encoded text.
6 // It takes an io.Reader providing the source, which then can be tokenized
7 // through repeated calls to the Scan function. For compatibility with
8 // existing tools, the NUL character is not allowed (implementation
11 // By default, a Scanner skips white space and Go comments and recognizes all
12 // literals as defined by the Go language specification. It may be
13 // customized to recognize only a subset of those literals and to recognize
14 // different white space characters.
16 // Basic usage pattern:
18 // var s scanner.Scanner
21 // for tok != scanner.EOF {
22 // // do something with tok
37 // TODO(gri): Consider changing this to use the new (token) Position package.
39 // A source position is represented by a Position value.
40 // A position is valid if Line > 0.
41 type Position struct {
42 Filename string // filename, if any
43 Offset int // byte offset, starting at 0
44 Line int // line number, starting at 1
45 Column int // column number, starting at 1 (character count per line)
48 // IsValid returns true if the position is valid.
49 func (pos *Position) IsValid() bool { return pos.Line > 0 }
51 func (pos Position) String() string {
57 s += fmt.Sprintf("%d:%d", pos.Line, pos.Column)
65 // Predefined mode bits to control recognition of tokens. For instance,
66 // to configure a Scanner such that it only recognizes (Go) identifiers,
67 // integers, and skips comments, set the Scanner's Mode field to:
69 // ScanIdents | ScanInts | SkipComments
72 ScanIdents = 1 << -Ident
74 ScanFloats = 1 << -Float // includes Ints
75 ScanChars = 1 << -Char
76 ScanStrings = 1 << -String
77 ScanRawStrings = 1 << -RawString
78 ScanComments = 1 << -Comment
79 SkipComments = 1 << -skipComment // if set with ScanComments, comments become white space
80 GoTokens = ScanIdents | ScanFloats | ScanChars | ScanStrings | ScanRawStrings | ScanComments | SkipComments
83 // The result of Scan is one of the following tokens or a Unicode character.
96 var tokenString = map[rune]string{
103 RawString: "RawString",
107 // TokenString returns a (visible) string for a token or Unicode character.
108 func TokenString(tok rune) string {
109 if s, found := tokenString[tok]; found {
112 return fmt.Sprintf("%q", string(tok))
115 // GoWhitespace is the default value for the Scanner's Whitespace field.
116 // Its value selects Go's white space characters.
117 const GoWhitespace = 1<<'\t' | 1<<'\n' | 1<<'\r' | 1<<' '
119 const bufLen = 1024 // at least utf8.UTFMax
121 // A Scanner implements reading of Unicode characters and tokens from an io.Reader.
122 type Scanner struct {
127 srcBuf [bufLen + 1]byte // +1 for sentinel for common case of s.next()
128 srcPos int // reading position (srcBuf index)
129 srcEnd int // source end (srcBuf index)
132 srcBufOffset int // byte offset of srcBuf[0] in source
133 line int // line count
134 column int // character count
135 lastLineLen int // length of last line in characters (for correct column reporting)
136 lastCharLen int // length of last character in bytes
139 // Typically, token text is stored completely in srcBuf, but in general
140 // the token text's head may be buffered in tokBuf while the token text's
141 // tail is stored in srcBuf.
142 tokBuf bytes.Buffer // token text head that is not in srcBuf anymore
143 tokPos int // token text tail position (srcBuf index); valid if >= 0
144 tokEnd int // token text tail end (srcBuf index)
146 // One character look-ahead
147 ch rune // character before current srcPos
149 // Error is called for each error encountered. If no Error
150 // function is set, the error is reported to os.Stderr.
151 Error func(s *Scanner, msg string)
153 // ErrorCount is incremented by one for each error encountered.
156 // The Mode field controls which tokens are recognized. For instance,
157 // to recognize Ints, set the ScanInts bit in Mode. The field may be
158 // changed at any time.
161 // The Whitespace field controls which characters are recognized
162 // as white space. To recognize a character ch <= ' ' as white space,
163 // set the ch'th bit in Whitespace (the Scanner's behavior is undefined
164 // for values ch > ' '). The field may be changed at any time.
167 // Start position of most recently scanned token; set by Scan.
168 // Calling Init or Next invalidates the position (Line == 0).
169 // The Filename field is always left untouched by the Scanner.
170 // If an error is reported (via Error) and Position is invalid,
171 // the scanner is not inside a token. Call Pos to obtain an error
172 // position in that case.
176 // Init initializes a Scanner with a new source and returns s.
177 // Error is set to nil, ErrorCount is set to 0, Mode is set to GoTokens,
178 // and Whitespace is set to GoWhitespace.
179 func (s *Scanner) Init(src io.Reader) *Scanner {
182 // initialize source buffer
183 // (the first call to next() will fill it by calling src.Read)
184 s.srcBuf[0] = utf8.RuneSelf // sentinel
188 // initialize source position
195 // initialize token text buffer
196 // (required for first call to next()).
199 // initialize one character look-ahead
200 s.ch = -1 // no char read yet
202 // initialize public fields
206 s.Whitespace = GoWhitespace
207 s.Line = 0 // invalidate token position
212 // TODO(gri): The code for next() and the internal scanner state could benefit
213 // from a rethink. While next() is optimized for the common ASCII
214 // case, the "corrections" needed for proper position tracking undo
215 // some of the attempts for fast-path optimization.
217 // next reads and returns the next Unicode character. It is designed such
218 // that only a minimal amount of work needs to be done in the common ASCII
219 // case (one test to check for both ASCII and end-of-buffer, and one test
220 // to check for newlines).
221 func (s *Scanner) next() rune {
222 ch, width := rune(s.srcBuf[s.srcPos]), 1
224 if ch >= utf8.RuneSelf {
225 // uncommon case: not ASCII or not enough bytes
226 for s.srcPos+utf8.UTFMax > s.srcEnd && !utf8.FullRune(s.srcBuf[s.srcPos:s.srcEnd]) {
227 // not enough bytes: read some more, but first
228 // save away token text if any
230 s.tokBuf.Write(s.srcBuf[s.tokPos:s.srcPos])
232 // s.tokEnd is set by Scan()
234 // move unread bytes to beginning of buffer
235 copy(s.srcBuf[0:], s.srcBuf[s.srcPos:s.srcEnd])
236 s.srcBufOffset += s.srcPos
238 // (an io.Reader must return os.EOF when it reaches
239 // the end of what it is reading - simply returning
240 // n == 0 will make this loop retry forever; but the
241 // error is in the reader implementation in that case)
242 i := s.srcEnd - s.srcPos
243 n, err := s.src.Read(s.srcBuf[i:bufLen])
246 s.srcBuf[s.srcEnd] = utf8.RuneSelf // sentinel
249 if s.lastCharLen > 0 {
250 // previous character was not EOF
259 // If err == EOF, we won't be getting more
260 // bytes; break to avoid infinite loop. If
261 // err is something else, we don't know if
262 // we can get more bytes; thus also break.
267 ch = rune(s.srcBuf[s.srcPos])
268 if ch >= utf8.RuneSelf {
269 // uncommon case: not ASCII
270 ch, width = utf8.DecodeRune(s.srcBuf[s.srcPos:s.srcEnd])
271 if ch == utf8.RuneError && width == 1 {
272 // advance for correct error position
274 s.lastCharLen = width
276 s.error("illegal UTF-8 encoding")
284 s.lastCharLen = width
287 // special situations
290 // implementation restriction for compatibility with other tools
291 s.error("illegal character NUL")
294 s.lastLineLen = s.column
301 // Next reads and returns the next Unicode character.
302 // It returns EOF at the end of the source. It reports
303 // a read error by calling s.Error, if not nil; otherwise
304 // it prints an error message to os.Stderr. Next does not
305 // update the Scanner's Position field; use Pos() to
306 // get the current position.
307 func (s *Scanner) Next() rune {
308 s.tokPos = -1 // don't collect token text
309 s.Line = 0 // invalidate token position
315 // Peek returns the next Unicode character in the source without advancing
316 // the scanner. It returns EOF if the scanner's position is at the last
317 // character of the source.
318 func (s *Scanner) Peek() rune {
325 func (s *Scanner) error(msg string) {
335 fmt.Fprintf(os.Stderr, "%s: %s\n", pos, msg)
338 func (s *Scanner) scanIdentifier() rune {
339 ch := s.next() // read character after first '_' or letter
340 for ch == '_' || unicode.IsLetter(ch) || unicode.IsDigit(ch) {
346 func digitVal(ch rune) int {
348 case '0' <= ch && ch <= '9':
350 case 'a' <= ch && ch <= 'f':
351 return int(ch - 'a' + 10)
352 case 'A' <= ch && ch <= 'F':
353 return int(ch - 'A' + 10)
355 return 16 // larger than any legal digit val
358 func isDecimal(ch rune) bool { return '0' <= ch && ch <= '9' }
360 func (s *Scanner) scanMantissa(ch rune) rune {
367 func (s *Scanner) scanFraction(ch rune) rune {
369 ch = s.scanMantissa(s.next())
374 func (s *Scanner) scanExponent(ch rune) rune {
375 if ch == 'e' || ch == 'E' {
377 if ch == '-' || ch == '+' {
380 ch = s.scanMantissa(ch)
385 func (s *Scanner) scanNumber(ch rune) (rune, rune) {
390 if ch == 'x' || ch == 'X' {
393 for digitVal(ch) < 16 {
397 // octal int or float
398 seenDecimalDigit := false
401 seenDecimalDigit = true
405 if s.Mode&ScanFloats != 0 && (ch == '.' || ch == 'e' || ch == 'E') {
407 ch = s.scanFraction(ch)
408 ch = s.scanExponent(ch)
412 if seenDecimalDigit {
413 s.error("illegal octal number")
418 // decimal int or float
419 ch = s.scanMantissa(ch)
420 if s.Mode&ScanFloats != 0 && (ch == '.' || ch == 'e' || ch == 'E') {
422 ch = s.scanFraction(ch)
423 ch = s.scanExponent(ch)
429 func (s *Scanner) scanDigits(ch rune, base, n int) rune {
430 for n > 0 && digitVal(ch) < base {
435 s.error("illegal char escape")
440 func (s *Scanner) scanEscape(quote rune) rune {
441 ch := s.next() // read character after '/'
443 case 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', quote:
446 case '0', '1', '2', '3', '4', '5', '6', '7':
447 ch = s.scanDigits(ch, 8, 3)
449 ch = s.scanDigits(s.next(), 16, 2)
451 ch = s.scanDigits(s.next(), 16, 4)
453 ch = s.scanDigits(s.next(), 16, 8)
455 s.error("illegal char escape")
460 func (s *Scanner) scanString(quote rune) (n int) {
461 ch := s.next() // read character after quote
463 if ch == '\n' || ch < 0 {
464 s.error("literal not terminated")
468 ch = s.scanEscape(quote)
477 func (s *Scanner) scanRawString() {
478 ch := s.next() // read character after '`'
481 s.error("literal not terminated")
488 func (s *Scanner) scanChar() {
489 if s.scanString('\'') != 1 {
490 s.error("illegal char literal")
494 func (s *Scanner) scanComment(ch rune) rune {
495 // ch == '/' || ch == '*'
498 ch = s.next() // read character after "//"
499 for ch != '\n' && ch >= 0 {
506 ch = s.next() // read character after "/*"
509 s.error("comment not terminated")
514 if ch0 == '*' && ch == '/' {
522 // Scan reads the next token or Unicode character from source and returns it.
523 // It only recognizes tokens t for which the respective Mode bit (1<<-t) is set.
524 // It returns EOF at the end of the source. It reports scanner errors (read and
525 // token errors) by calling s.Error, if not nil; otherwise it prints an error
526 // message to os.Stderr.
527 func (s *Scanner) Scan() rune {
530 // reset token text position
536 for s.Whitespace&(1<<uint(ch)) != 0 {
540 // start collecting token text
542 s.tokPos = s.srcPos - s.lastCharLen
544 // set token position
545 // (this is a slightly optimized version of the code in Pos())
546 s.Offset = s.srcBufOffset + s.tokPos
548 // common case: last character was not a '\n'
552 // last character was a '\n'
553 // (we cannot be at the beginning of the source
554 // since we have called next() at least once)
556 s.Column = s.lastLineLen
559 // determine token value
562 case unicode.IsLetter(ch) || ch == '_':
563 if s.Mode&ScanIdents != 0 {
565 ch = s.scanIdentifier()
570 if s.Mode&(ScanInts|ScanFloats) != 0 {
571 tok, ch = s.scanNumber(ch)
578 if s.Mode&ScanStrings != 0 {
584 if s.Mode&ScanChars != 0 {
591 if isDecimal(ch) && s.Mode&ScanFloats != 0 {
593 ch = s.scanMantissa(ch)
594 ch = s.scanExponent(ch)
598 if (ch == '/' || ch == '*') && s.Mode&ScanComments != 0 {
599 if s.Mode&SkipComments != 0 {
600 s.tokPos = -1 // don't collect token text
601 ch = s.scanComment(ch)
604 ch = s.scanComment(ch)
608 if s.Mode&ScanRawStrings != 0 {
619 s.tokEnd = s.srcPos - s.lastCharLen
625 // Pos returns the position of the character immediately after
626 // the character or token returned by the last call to Next or Scan.
627 func (s *Scanner) Pos() (pos Position) {
628 pos.Filename = s.Filename
629 pos.Offset = s.srcBufOffset + s.srcPos - s.lastCharLen
632 // common case: last character was not a '\n'
634 pos.Column = s.column
635 case s.lastLineLen > 0:
636 // last character was a '\n'
637 pos.Line = s.line - 1
638 pos.Column = s.lastLineLen
640 // at the beginning of the source
647 // TokenText returns the string corresponding to the most recently scanned token.
648 // Valid after calling Scan().
649 func (s *Scanner) TokenText() string {
656 // if EOF was reached, s.tokEnd is set to -1 (s.srcPos == 0)
660 if s.tokBuf.Len() == 0 {
661 // common case: the entire token text is still in srcBuf
662 return string(s.srcBuf[s.tokPos:s.tokEnd])
665 // part of the token text was saved in tokBuf: save the rest in
666 // tokBuf as well and return its content
667 s.tokBuf.Write(s.srcBuf[s.tokPos:s.tokEnd])
668 s.tokPos = s.tokEnd // ensure idempotency of TokenText() call
669 return s.tokBuf.String()