scanner.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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.
  4. // Package scanner provides a scanner and tokenizer for UTF-8-encoded text.
  5. // It takes an io.Reader providing the source, which then can be tokenized
  6. // through repeated calls to the Scan function. For compatibility with
  7. // existing tools, the NUL character is not allowed. If the first character
  8. // in the source is a UTF-8 encoded byte order mark (BOM), it is discarded.
  9. //
  10. // By default, a Scanner skips white space and Go comments and recognizes all
  11. // literals as defined by the Go language specification. It may be
  12. // customized to recognize only a subset of those literals and to recognize
  13. // different white space characters.
  14. //
  15. // Basic usage pattern:
  16. //
  17. // var s scanner.Scanner
  18. // s.Init(src)
  19. // tok := s.Scan()
  20. // for tok != scanner.EOF {
  21. // // do something with tok
  22. // tok = s.Scan()
  23. // }
  24. //
  25. package scanner
  26. import (
  27. "bytes"
  28. "fmt"
  29. "io"
  30. "os"
  31. "unicode/utf8"
  32. )
  33. // TODO(gri): Consider changing this to use the new (token) Position package.
  34. // A source position is represented by a Position value.
  35. // A position is valid if Line > 0.
  36. type Position struct {
  37. Filename string // filename, if any
  38. Offset int // byte offset, starting at 0
  39. Line int // line number, starting at 1
  40. Column int // column number, starting at 1 (character count per line)
  41. }
  42. // IsValid returns true if the position is valid.
  43. func (pos *Position) IsValid() bool { return pos.Line > 0 }
  44. func (pos Position) String() string {
  45. s := pos.Filename
  46. if pos.IsValid() {
  47. if s != "" {
  48. s += ":"
  49. }
  50. s += fmt.Sprintf("%d:%d", pos.Line, pos.Column)
  51. }
  52. if s == "" {
  53. s = "???"
  54. }
  55. return s
  56. }
  57. // Predefined mode bits to control recognition of tokens. For instance,
  58. // to configure a Scanner such that it only recognizes (Go) identifiers,
  59. // integers, and skips comments, set the Scanner's Mode field to:
  60. //
  61. // ScanIdents | ScanInts | SkipComments
  62. //
  63. const (
  64. ScanIdents = 1 << -Ident
  65. ScanInts = 1 << -Int
  66. ScanFloats = 1 << -Float // includes Ints
  67. ScanChars = 1 << -Char
  68. ScanStrings = 1 << -String
  69. ScanRawStrings = 1 << -RawString
  70. ScanComments = 1 << -Comment
  71. SkipComments = 1 << -skipComment // if set with ScanComments, comments become white space
  72. GoTokens = ScanIdents | ScanFloats | ScanChars | ScanStrings | ScanRawStrings | ScanComments | SkipComments
  73. )
  74. // The result of Scan is one of the following tokens or a Unicode character.
  75. const (
  76. EOF = -(iota + 1)
  77. Ident
  78. Int
  79. Float
  80. Char
  81. String
  82. RawString
  83. Comment
  84. skipComment
  85. )
  86. var tokenString = map[rune]string{
  87. EOF: "EOF",
  88. Ident: "Ident",
  89. Int: "Int",
  90. Float: "Float",
  91. Char: "Char",
  92. String: "String",
  93. RawString: "RawString",
  94. Comment: "Comment",
  95. }
  96. // TokenString returns a printable string for a token or Unicode character.
  97. func TokenString(tok rune) string {
  98. if s, found := tokenString[tok]; found {
  99. return s
  100. }
  101. return fmt.Sprintf("%q", string(tok))
  102. }
  103. // GoWhitespace is the default value for the Scanner's Whitespace field.
  104. // Its value selects Go's white space characters.
  105. const GoWhitespace = 1<<'\t' | 1<<'\n' | 1<<'\r' | 1<<' '
  106. const bufLen = 1024 // at least utf8.UTFMax
  107. // A Scanner implements reading of Unicode characters and tokens from an io.Reader.
  108. type Scanner struct {
  109. // Input
  110. src io.Reader
  111. // Source buffer
  112. srcBuf [bufLen + 1]byte // +1 for sentinel for common case of s.next()
  113. srcPos int // reading position (srcBuf index)
  114. srcEnd int // source end (srcBuf index)
  115. // Source position
  116. srcBufOffset int // byte offset of srcBuf[0] in source
  117. line int // line count
  118. column int // character count
  119. lastLineLen int // length of last line in characters (for correct column reporting)
  120. lastCharLen int // length of last character in bytes
  121. // Token text buffer
  122. // Typically, token text is stored completely in srcBuf, but in general
  123. // the token text's head may be buffered in tokBuf while the token text's
  124. // tail is stored in srcBuf.
  125. tokBuf bytes.Buffer // token text head that is not in srcBuf anymore
  126. tokPos int // token text tail position (srcBuf index); valid if >= 0
  127. tokEnd int // token text tail end (srcBuf index)
  128. // One character look-ahead
  129. ch rune // character before current srcPos
  130. // Error is called for each error encountered. If no Error
  131. // function is set, the error is reported to os.Stderr.
  132. Error func(s *Scanner, msg string)
  133. // ErrorCount is incremented by one for each error encountered.
  134. ErrorCount int
  135. // The Mode field controls which tokens are recognized. For instance,
  136. // to recognize Ints, set the ScanInts bit in Mode. The field may be
  137. // changed at any time.
  138. Mode uint
  139. // The Whitespace field controls which characters are recognized
  140. // as white space. To recognize a character ch <= ' ' as white space,
  141. // set the ch'th bit in Whitespace (the Scanner's behavior is undefined
  142. // for values ch > ' '). The field may be changed at any time.
  143. Whitespace uint64
  144. // Start position of most recently scanned token; set by Scan.
  145. // Calling Init or Next invalidates the position (Line == 0).
  146. // The Filename field is always left untouched by the Scanner.
  147. // If an error is reported (via Error) and Position is invalid,
  148. // the scanner is not inside a token. Call Pos to obtain an error
  149. // position in that case.
  150. Position
  151. }
  152. // Init initializes a Scanner with a new source and returns s.
  153. // Error is set to nil, ErrorCount is set to 0, Mode is set to GoTokens,
  154. // and Whitespace is set to GoWhitespace.
  155. func (s *Scanner) Init(src io.Reader) *Scanner {
  156. s.src = src
  157. // initialize source buffer
  158. // (the first call to next() will fill it by calling src.Read)
  159. s.srcBuf[0] = utf8.RuneSelf // sentinel
  160. s.srcPos = 0
  161. s.srcEnd = 0
  162. // initialize source position
  163. s.srcBufOffset = 0
  164. s.line = 1
  165. s.column = 0
  166. s.lastLineLen = 0
  167. s.lastCharLen = 0
  168. // initialize token text buffer
  169. // (required for first call to next()).
  170. s.tokPos = -1
  171. // initialize one character look-ahead
  172. s.ch = -1 // no char read yet
  173. // initialize public fields
  174. s.Error = nil
  175. s.ErrorCount = 0
  176. s.Mode = GoTokens
  177. s.Whitespace = GoWhitespace
  178. s.Line = 0 // invalidate token position
  179. return s
  180. }
  181. // next reads and returns the next Unicode character. It is designed such
  182. // that only a minimal amount of work needs to be done in the common ASCII
  183. // case (one test to check for both ASCII and end-of-buffer, and one test
  184. // to check for newlines).
  185. func (s *Scanner) next() rune {
  186. ch, width := rune(s.srcBuf[s.srcPos]), 1
  187. if ch >= utf8.RuneSelf {
  188. // uncommon case: not ASCII or not enough bytes
  189. for s.srcPos+utf8.UTFMax > s.srcEnd && !utf8.FullRune(s.srcBuf[s.srcPos:s.srcEnd]) {
  190. // not enough bytes: read some more, but first
  191. // save away token text if any
  192. if s.tokPos >= 0 {
  193. s.tokBuf.Write(s.srcBuf[s.tokPos:s.srcPos])
  194. s.tokPos = 0
  195. // s.tokEnd is set by Scan()
  196. }
  197. // move unread bytes to beginning of buffer
  198. copy(s.srcBuf[0:], s.srcBuf[s.srcPos:s.srcEnd])
  199. s.srcBufOffset += s.srcPos
  200. // read more bytes
  201. // (an io.Reader must return io.EOF when it reaches
  202. // the end of what it is reading - simply returning
  203. // n == 0 will make this loop retry forever; but the
  204. // error is in the reader implementation in that case)
  205. i := s.srcEnd - s.srcPos
  206. n, err := s.src.Read(s.srcBuf[i:bufLen])
  207. s.srcPos = 0
  208. s.srcEnd = i + n
  209. s.srcBuf[s.srcEnd] = utf8.RuneSelf // sentinel
  210. if err != nil {
  211. if s.srcEnd == 0 {
  212. if s.lastCharLen > 0 {
  213. // previous character was not EOF
  214. s.column++
  215. }
  216. s.lastCharLen = 0
  217. return EOF
  218. }
  219. if err != io.EOF {
  220. s.error(err.Error())
  221. }
  222. // If err == EOF, we won't be getting more
  223. // bytes; break to avoid infinite loop. If
  224. // err is something else, we don't know if
  225. // we can get more bytes; thus also break.
  226. break
  227. }
  228. }
  229. // at least one byte
  230. ch = rune(s.srcBuf[s.srcPos])
  231. if ch >= utf8.RuneSelf {
  232. // uncommon case: not ASCII
  233. ch, width = utf8.DecodeRune(s.srcBuf[s.srcPos:s.srcEnd])
  234. if ch == utf8.RuneError && width == 1 {
  235. // advance for correct error position
  236. s.srcPos += width
  237. s.lastCharLen = width
  238. s.column++
  239. s.error("illegal UTF-8 encoding")
  240. return ch
  241. }
  242. }
  243. }
  244. // advance
  245. s.srcPos += width
  246. s.lastCharLen = width
  247. s.column++
  248. // special situations
  249. switch ch {
  250. case 0:
  251. // for compatibility with other tools
  252. s.error("illegal character NUL")
  253. case '\n':
  254. s.line++
  255. s.lastLineLen = s.column
  256. s.column = 0
  257. }
  258. return ch
  259. }
  260. // Next reads and returns the next Unicode character.
  261. // It returns EOF at the end of the source. It reports
  262. // a read error by calling s.Error, if not nil; otherwise
  263. // it prints an error message to os.Stderr. Next does not
  264. // update the Scanner's Position field; use Pos() to
  265. // get the current position.
  266. func (s *Scanner) Next() rune {
  267. s.tokPos = -1 // don't collect token text
  268. s.Line = 0 // invalidate token position
  269. ch := s.Peek()
  270. s.ch = s.next()
  271. return ch
  272. }
  273. // Peek returns the next Unicode character in the source without advancing
  274. // the scanner. It returns EOF if the scanner's position is at the last
  275. // character of the source.
  276. func (s *Scanner) Peek() rune {
  277. if s.ch < 0 {
  278. // this code is only run for the very first character
  279. s.ch = s.next()
  280. if s.ch == '\uFEFF' {
  281. s.ch = s.next() // ignore BOM
  282. }
  283. }
  284. return s.ch
  285. }
  286. func (s *Scanner) error(msg string) {
  287. s.ErrorCount++
  288. if s.Error != nil {
  289. s.Error(s, msg)
  290. return
  291. }
  292. pos := s.Position
  293. if !pos.IsValid() {
  294. pos = s.Pos()
  295. }
  296. fmt.Fprintf(os.Stderr, "%s: %s\n", pos, msg)
  297. }
  298. func (s *Scanner) scanIdentifier() rune {
  299. ch := s.next() // read character after first '_' or letter
  300. for detectIdent(ch) {
  301. ch = s.next()
  302. }
  303. return ch
  304. }
  305. func digitVal(ch rune) int {
  306. switch {
  307. case '0' <= ch && ch <= '9':
  308. return int(ch - '0')
  309. case 'a' <= ch && ch <= 'f':
  310. return int(ch - 'a' + 10)
  311. case 'A' <= ch && ch <= 'F':
  312. return int(ch - 'A' + 10)
  313. }
  314. return 16 // larger than any legal digit val
  315. }
  316. func isDecimal(ch rune) bool { return '0' <= ch && ch <= '9' }
  317. func (s *Scanner) scanMantissa(ch rune) rune {
  318. for isDecimal(ch) {
  319. ch = s.next()
  320. }
  321. return ch
  322. }
  323. func (s *Scanner) scanFraction(ch rune) rune {
  324. if ch == '.' {
  325. ch = s.scanMantissa(s.next())
  326. }
  327. return ch
  328. }
  329. func (s *Scanner) scanExponent(ch rune) rune {
  330. if ch == 'e' || ch == 'E' {
  331. ch = s.next()
  332. if ch == '-' || ch == '+' {
  333. ch = s.next()
  334. }
  335. ch = s.scanMantissa(ch)
  336. }
  337. return ch
  338. }
  339. func (s *Scanner) scanNumber(ch rune) (rune, rune) {
  340. // isDecimal(ch)
  341. if ch == '0' {
  342. // int or float
  343. ch = s.next()
  344. if ch == 'x' || ch == 'X' {
  345. // hexadecimal int
  346. ch = s.next()
  347. hasMantissa := false
  348. for digitVal(ch) < 16 {
  349. ch = s.next()
  350. hasMantissa = true
  351. }
  352. if !hasMantissa {
  353. s.error("illegal hexadecimal number")
  354. }
  355. } else {
  356. // octal int or float
  357. has8or9 := false
  358. for isDecimal(ch) {
  359. if ch > '7' {
  360. has8or9 = true
  361. }
  362. ch = s.next()
  363. }
  364. if s.Mode&ScanFloats != 0 && (ch == '.' || ch == 'e' || ch == 'E') {
  365. // float
  366. ch = s.scanFraction(ch)
  367. ch = s.scanExponent(ch)
  368. return Float, ch
  369. }
  370. // octal int
  371. if has8or9 {
  372. s.error("illegal octal number")
  373. }
  374. }
  375. return Int, ch
  376. }
  377. // decimal int or float
  378. ch = s.scanMantissa(ch)
  379. if s.Mode&ScanFloats != 0 && (ch == '.' || ch == 'e' || ch == 'E') {
  380. // float
  381. ch = s.scanFraction(ch)
  382. ch = s.scanExponent(ch)
  383. return Float, ch
  384. }
  385. return Int, ch
  386. }
  387. func (s *Scanner) scanDigits(ch rune, base, n int) rune {
  388. for n > 0 && digitVal(ch) < base {
  389. ch = s.next()
  390. n--
  391. }
  392. if n > 0 {
  393. s.error("illegal char escape")
  394. }
  395. return ch
  396. }
  397. func (s *Scanner) scanEscape(quote rune) rune {
  398. ch := s.next() // read character after '/'
  399. switch ch {
  400. case 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', quote:
  401. // nothing to do
  402. ch = s.next()
  403. case '0', '1', '2', '3', '4', '5', '6', '7':
  404. ch = s.scanDigits(ch, 8, 3)
  405. case 'x':
  406. ch = s.scanDigits(s.next(), 16, 2)
  407. case 'u':
  408. ch = s.scanDigits(s.next(), 16, 4)
  409. case 'U':
  410. ch = s.scanDigits(s.next(), 16, 8)
  411. default:
  412. s.error("illegal char escape")
  413. }
  414. return ch
  415. }
  416. func (s *Scanner) scanString(quote rune) (n int) {
  417. ch := s.next() // read character after quote
  418. for ch != quote {
  419. if ch == '\n' || ch < 0 {
  420. s.error("literal not terminated")
  421. return
  422. }
  423. if ch == '\\' {
  424. ch = s.scanEscape(quote)
  425. } else {
  426. ch = s.next()
  427. }
  428. n++
  429. }
  430. return
  431. }
  432. func (s *Scanner) scanRawString() {
  433. ch := s.next() // read character after '`'
  434. for ch != '`' {
  435. if ch < 0 {
  436. s.error("literal not terminated")
  437. return
  438. }
  439. ch = s.next()
  440. }
  441. }
  442. func (s *Scanner) scanChar() {
  443. if s.scanString('\'') != 1 {
  444. s.error("illegal char literal")
  445. }
  446. }
  447. func (s *Scanner) scanComment(ch rune) rune {
  448. // ch == '/' || ch == '*'
  449. if ch == '/' {
  450. // line comment
  451. ch = s.next() // read character after "//"
  452. for ch != '\n' && ch >= 0 {
  453. ch = s.next()
  454. }
  455. return ch
  456. }
  457. // general comment
  458. ch = s.next() // read character after "/*"
  459. for {
  460. if ch < 0 {
  461. s.error("comment not terminated")
  462. break
  463. }
  464. ch0 := ch
  465. ch = s.next()
  466. if ch0 == '*' && ch == '/' {
  467. ch = s.next()
  468. break
  469. }
  470. }
  471. return ch
  472. }
  473. // Scan reads the next token or Unicode character from source and returns it.
  474. // It only recognizes tokens t for which the respective Mode bit (1<<-t) is set.
  475. // It returns EOF at the end of the source. It reports scanner errors (read and
  476. // token errors) by calling s.Error, if not nil; otherwise it prints an error
  477. // message to os.Stderr.
  478. func (s *Scanner) Scan() rune {
  479. ch := s.Peek()
  480. // reset token text position
  481. s.tokPos = -1
  482. s.Line = 0
  483. redo:
  484. // skip white space
  485. for s.Whitespace&(1<<uint(ch)) != 0 {
  486. ch = s.next()
  487. }
  488. // start collecting token text
  489. s.tokBuf.Reset()
  490. s.tokPos = s.srcPos - s.lastCharLen
  491. // set token position
  492. // (this is a slightly optimized version of the code in Pos())
  493. s.Offset = s.srcBufOffset + s.tokPos
  494. if s.column > 0 {
  495. // common case: last character was not a '\n'
  496. s.Line = s.line
  497. s.Column = s.column
  498. } else {
  499. // last character was a '\n'
  500. // (we cannot be at the beginning of the source
  501. // since we have called next() at least once)
  502. s.Line = s.line - 1
  503. s.Column = s.lastLineLen
  504. }
  505. // determine token value
  506. tok := ch
  507. switch {
  508. case detectIdent(ch):
  509. if s.Mode&ScanIdents != 0 {
  510. tok = Ident
  511. ch = s.scanIdentifier()
  512. } else {
  513. ch = s.next()
  514. }
  515. case isDecimal(ch):
  516. if s.Mode&(ScanInts|ScanFloats) != 0 {
  517. tok, ch = s.scanNumber(ch)
  518. } else {
  519. ch = s.next()
  520. }
  521. default:
  522. switch ch {
  523. case '"':
  524. if s.Mode&ScanStrings != 0 {
  525. s.scanString('"')
  526. tok = String
  527. }
  528. ch = s.next()
  529. case '\'':
  530. if s.Mode&ScanChars != 0 {
  531. s.scanChar()
  532. tok = Char
  533. }
  534. ch = s.next()
  535. case '.':
  536. ch = s.next()
  537. if isDecimal(ch) && s.Mode&ScanFloats != 0 {
  538. tok = Float
  539. ch = s.scanMantissa(ch)
  540. ch = s.scanExponent(ch)
  541. }
  542. case '/':
  543. ch = s.next()
  544. if (ch == '/' || ch == '*') && s.Mode&ScanComments != 0 {
  545. if s.Mode&SkipComments != 0 {
  546. s.tokPos = -1 // don't collect token text
  547. ch = s.scanComment(ch)
  548. goto redo
  549. }
  550. ch = s.scanComment(ch)
  551. tok = Comment
  552. }
  553. case '`':
  554. if s.Mode&ScanRawStrings != 0 {
  555. s.scanRawString()
  556. tok = String
  557. }
  558. ch = s.next()
  559. default:
  560. ch = s.next()
  561. }
  562. }
  563. // end of token text
  564. s.tokEnd = s.srcPos - s.lastCharLen
  565. s.ch = ch
  566. return tok
  567. }
  568. // Pos returns the position of the character immediately after
  569. // the character or token returned by the last call to Next or Scan.
  570. func (s *Scanner) Pos() (pos Position) {
  571. pos.Filename = s.Filename
  572. pos.Offset = s.srcBufOffset + s.srcPos - s.lastCharLen
  573. switch {
  574. case s.column > 0:
  575. // common case: last character was not a '\n'
  576. pos.Line = s.line
  577. pos.Column = s.column
  578. case s.lastLineLen > 0:
  579. // last character was a '\n'
  580. pos.Line = s.line - 1
  581. pos.Column = s.lastLineLen
  582. default:
  583. // at the beginning of the source
  584. pos.Line = 1
  585. pos.Column = 1
  586. }
  587. return
  588. }
  589. // TokenText returns the string corresponding to the most recently scanned token.
  590. // Valid after calling Scan().
  591. func (s *Scanner) TokenText() string {
  592. if s.tokPos < 0 {
  593. // no token text
  594. return ""
  595. }
  596. if s.tokEnd < 0 {
  597. // if EOF was reached, s.tokEnd is set to -1 (s.srcPos == 0)
  598. s.tokEnd = s.tokPos
  599. }
  600. if s.tokBuf.Len() == 0 {
  601. // common case: the entire token text is still in srcBuf
  602. return string(s.srcBuf[s.tokPos:s.tokEnd])
  603. }
  604. // part of the token text was saved in tokBuf: save the rest in
  605. // tokBuf as well and return its content
  606. s.tokBuf.Write(s.srcBuf[s.tokPos:s.tokEnd])
  607. s.tokPos = s.tokEnd // ensure idempotency of TokenText() call
  608. return s.tokBuf.String()
  609. }