lex.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. package toml
  2. import (
  3. "fmt"
  4. "unicode/utf8"
  5. )
  6. type itemType int
  7. const (
  8. itemError itemType = iota
  9. itemNIL // used in the parser to indicate no type
  10. itemEOF
  11. itemText
  12. itemString
  13. itemBool
  14. itemInteger
  15. itemFloat
  16. itemDatetime
  17. itemArray // the start of an array
  18. itemArrayEnd
  19. itemTableStart
  20. itemTableEnd
  21. itemArrayTableStart
  22. itemArrayTableEnd
  23. itemKeyStart
  24. itemCommentStart
  25. )
  26. const (
  27. eof = 0
  28. tableStart = '['
  29. tableEnd = ']'
  30. arrayTableStart = '['
  31. arrayTableEnd = ']'
  32. tableSep = '.'
  33. keySep = '='
  34. arrayStart = '['
  35. arrayEnd = ']'
  36. arrayValTerm = ','
  37. commentStart = '#'
  38. stringStart = '"'
  39. stringEnd = '"'
  40. )
  41. type stateFn func(lx *lexer) stateFn
  42. type lexer struct {
  43. input string
  44. start int
  45. pos int
  46. width int
  47. line int
  48. state stateFn
  49. items chan item
  50. // A stack of state functions used to maintain context.
  51. // The idea is to reuse parts of the state machine in various places.
  52. // For example, values can appear at the top level or within arbitrarily
  53. // nested arrays. The last state on the stack is used after a value has
  54. // been lexed. Similarly for comments.
  55. stack []stateFn
  56. }
  57. type item struct {
  58. typ itemType
  59. val string
  60. line int
  61. }
  62. func (lx *lexer) nextItem() item {
  63. for {
  64. select {
  65. case item := <-lx.items:
  66. return item
  67. default:
  68. lx.state = lx.state(lx)
  69. }
  70. }
  71. panic("not reached")
  72. }
  73. func lex(input string) *lexer {
  74. lx := &lexer{
  75. input: input,
  76. state: lexTop,
  77. line: 1,
  78. items: make(chan item, 10),
  79. stack: make([]stateFn, 0, 10),
  80. }
  81. return lx
  82. }
  83. func (lx *lexer) push(state stateFn) {
  84. lx.stack = append(lx.stack, state)
  85. }
  86. func (lx *lexer) pop() stateFn {
  87. if len(lx.stack) == 0 {
  88. return lx.errorf("BUG in lexer: no states to pop.")
  89. }
  90. last := lx.stack[len(lx.stack)-1]
  91. lx.stack = lx.stack[0 : len(lx.stack)-1]
  92. return last
  93. }
  94. func (lx *lexer) current() string {
  95. return lx.input[lx.start:lx.pos]
  96. }
  97. func (lx *lexer) emit(typ itemType) {
  98. lx.items <- item{typ, lx.current(), lx.line}
  99. lx.start = lx.pos
  100. }
  101. func (lx *lexer) next() (r rune) {
  102. if lx.pos >= len(lx.input) {
  103. lx.width = 0
  104. return eof
  105. }
  106. if lx.input[lx.pos] == '\n' {
  107. lx.line++
  108. }
  109. r, lx.width = utf8.DecodeRuneInString(lx.input[lx.pos:])
  110. lx.pos += lx.width
  111. return r
  112. }
  113. // ignore skips over the pending input before this point.
  114. func (lx *lexer) ignore() {
  115. lx.start = lx.pos
  116. }
  117. // backup steps back one rune. Can be called only once per call of next.
  118. func (lx *lexer) backup() {
  119. lx.pos -= lx.width
  120. if lx.pos < len(lx.input) && lx.input[lx.pos] == '\n' {
  121. lx.line--
  122. }
  123. }
  124. // accept consumes the next rune if it's equal to `valid`.
  125. func (lx *lexer) accept(valid rune) bool {
  126. if lx.next() == valid {
  127. return true
  128. }
  129. lx.backup()
  130. return false
  131. }
  132. // peek returns but does not consume the next rune in the input.
  133. func (lx *lexer) peek() rune {
  134. r := lx.next()
  135. lx.backup()
  136. return r
  137. }
  138. // errorf stops all lexing by emitting an error and returning `nil`.
  139. // Note that any value that is a character is escaped if it's a special
  140. // character (new lines, tabs, etc.).
  141. func (lx *lexer) errorf(format string, values ...interface{}) stateFn {
  142. for i, value := range values {
  143. if v, ok := value.(rune); ok {
  144. values[i] = escapeSpecial(v)
  145. }
  146. }
  147. lx.items <- item{
  148. itemError,
  149. fmt.Sprintf(format, values...),
  150. lx.line,
  151. }
  152. return nil
  153. }
  154. // lexTop consumes elements at the top level of TOML data.
  155. func lexTop(lx *lexer) stateFn {
  156. r := lx.next()
  157. if isWhitespace(r) || isNL(r) {
  158. return lexSkip(lx, lexTop)
  159. }
  160. switch r {
  161. case commentStart:
  162. lx.push(lexTop)
  163. return lexCommentStart
  164. case tableStart:
  165. return lexTableStart
  166. case eof:
  167. if lx.pos > lx.start {
  168. return lx.errorf("Unexpected EOF.")
  169. }
  170. lx.emit(itemEOF)
  171. return nil
  172. }
  173. // At this point, the only valid item can be a key, so we back up
  174. // and let the key lexer do the rest.
  175. lx.backup()
  176. lx.push(lexTopEnd)
  177. return lexKeyStart
  178. }
  179. // lexTopEnd is entered whenever a top-level item has been consumed. (A value
  180. // or a table.) It must see only whitespace, and will turn back to lexTop
  181. // upon a new line. If it sees EOF, it will quit the lexer successfully.
  182. func lexTopEnd(lx *lexer) stateFn {
  183. r := lx.next()
  184. switch {
  185. case r == commentStart:
  186. // a comment will read to a new line for us.
  187. lx.push(lexTop)
  188. return lexCommentStart
  189. case isWhitespace(r):
  190. return lexTopEnd
  191. case isNL(r):
  192. lx.ignore()
  193. return lexTop
  194. case r == eof:
  195. lx.ignore()
  196. return lexTop
  197. }
  198. return lx.errorf("Expected a top-level item to end with a new line, "+
  199. "comment or EOF, but got '%s' instead.", r)
  200. }
  201. // lexTable lexes the beginning of a table. Namely, it makes sure that
  202. // it starts with a character other than '.' and ']'.
  203. // It assumes that '[' has already been consumed.
  204. // It also handles the case that this is an item in an array of tables.
  205. // e.g., '[[name]]'.
  206. func lexTableStart(lx *lexer) stateFn {
  207. if lx.peek() == arrayTableStart {
  208. lx.next()
  209. lx.emit(itemArrayTableStart)
  210. lx.push(lexArrayTableEnd)
  211. } else {
  212. lx.emit(itemTableStart)
  213. lx.push(lexTableEnd)
  214. }
  215. return lexTableNameStart
  216. }
  217. func lexTableEnd(lx *lexer) stateFn {
  218. lx.emit(itemTableEnd)
  219. return lexTopEnd
  220. }
  221. func lexArrayTableEnd(lx *lexer) stateFn {
  222. if r := lx.next(); r != arrayTableEnd {
  223. return lx.errorf("Expected end of table array name delimiter '%s', "+
  224. "but got '%s' instead.", arrayTableEnd, r)
  225. }
  226. lx.emit(itemArrayTableEnd)
  227. return lexTopEnd
  228. }
  229. func lexTableNameStart(lx *lexer) stateFn {
  230. switch lx.next() {
  231. case tableEnd:
  232. return lx.errorf("Unexpected end of table. (Tables cannot " +
  233. "be empty.)")
  234. case tableSep:
  235. return lx.errorf("Unexpected table separator. (Tables cannot " +
  236. "be empty.)")
  237. }
  238. return lexTableName
  239. }
  240. // lexTableName lexes the name of a table. It assumes that at least one
  241. // valid character for the table has already been read.
  242. func lexTableName(lx *lexer) stateFn {
  243. switch lx.peek() {
  244. case tableStart:
  245. return lx.errorf("Table names cannot contain '%s' or '%s'.",
  246. tableStart, tableEnd)
  247. case tableEnd:
  248. lx.emit(itemText)
  249. lx.next()
  250. return lx.pop()
  251. case tableSep:
  252. lx.emit(itemText)
  253. lx.next()
  254. lx.ignore()
  255. return lexTableNameStart
  256. }
  257. lx.next()
  258. return lexTableName
  259. }
  260. // lexKeyStart consumes a key name up until the first non-whitespace character.
  261. // lexKeyStart will ignore whitespace.
  262. func lexKeyStart(lx *lexer) stateFn {
  263. r := lx.peek()
  264. switch {
  265. case r == keySep:
  266. return lx.errorf("Unexpected key separator '%s'.", keySep)
  267. case isWhitespace(r) || isNL(r):
  268. lx.next()
  269. return lexSkip(lx, lexKeyStart)
  270. }
  271. lx.ignore()
  272. lx.emit(itemKeyStart)
  273. lx.next()
  274. return lexKey
  275. }
  276. // lexKey consumes the text of a key. Assumes that the first character (which
  277. // is not whitespace) has already been consumed.
  278. func lexKey(lx *lexer) stateFn {
  279. r := lx.peek()
  280. // XXX: Possible divergence from spec?
  281. // "Keys start with the first non-whitespace character and end with the
  282. // last non-whitespace character before the equals sign."
  283. // Note here that whitespace is either a tab or a space.
  284. // But we'll call it quits if we see a new line too.
  285. if isWhitespace(r) || isNL(r) {
  286. lx.emit(itemText)
  287. return lexKeyEnd
  288. }
  289. // Let's also call it quits if we see an equals sign.
  290. if r == keySep {
  291. lx.emit(itemText)
  292. return lexKeyEnd
  293. }
  294. lx.next()
  295. return lexKey
  296. }
  297. // lexKeyEnd consumes the end of a key (up to the key separator).
  298. // Assumes that the first whitespace character after a key (or the '='
  299. // separator) has NOT been consumed.
  300. func lexKeyEnd(lx *lexer) stateFn {
  301. r := lx.next()
  302. switch {
  303. case isWhitespace(r) || isNL(r):
  304. return lexSkip(lx, lexKeyEnd)
  305. case r == keySep:
  306. return lexSkip(lx, lexValue)
  307. }
  308. return lx.errorf("Expected key separator '%s', but got '%s' instead.",
  309. keySep, r)
  310. }
  311. // lexValue starts the consumption of a value anywhere a value is expected.
  312. // lexValue will ignore whitespace.
  313. // After a value is lexed, the last state on the next is popped and returned.
  314. func lexValue(lx *lexer) stateFn {
  315. // We allow whitespace to precede a value, but NOT new lines.
  316. // In array syntax, the array states are responsible for ignoring new lines.
  317. r := lx.next()
  318. if isWhitespace(r) {
  319. return lexSkip(lx, lexValue)
  320. }
  321. switch {
  322. case r == arrayStart:
  323. lx.ignore()
  324. lx.emit(itemArray)
  325. return lexArrayValue
  326. case r == stringStart:
  327. lx.ignore() // ignore the '"'
  328. return lexString
  329. case r == 't':
  330. return lexTrue
  331. case r == 'f':
  332. return lexFalse
  333. case r == '-':
  334. return lexNumberStart
  335. case isDigit(r):
  336. lx.backup() // avoid an extra state and use the same as above
  337. return lexNumberOrDateStart
  338. case r == '.': // special error case, be kind to users
  339. return lx.errorf("Floats must start with a digit, not '.'.")
  340. }
  341. return lx.errorf("Expected value but found '%s' instead.", r)
  342. }
  343. // lexArrayValue consumes one value in an array. It assumes that '[' or ','
  344. // have already been consumed. All whitespace and new lines are ignored.
  345. func lexArrayValue(lx *lexer) stateFn {
  346. r := lx.next()
  347. switch {
  348. case isWhitespace(r) || isNL(r):
  349. return lexSkip(lx, lexArrayValue)
  350. case r == commentStart:
  351. lx.push(lexArrayValue)
  352. return lexCommentStart
  353. case r == arrayValTerm:
  354. return lx.errorf("Unexpected array value terminator '%s'.",
  355. arrayValTerm)
  356. case r == arrayEnd:
  357. return lexArrayEnd
  358. }
  359. lx.backup()
  360. lx.push(lexArrayValueEnd)
  361. return lexValue
  362. }
  363. // lexArrayValueEnd consumes the cruft between values of an array. Namely,
  364. // it ignores whitespace and expects either a ',' or a ']'.
  365. func lexArrayValueEnd(lx *lexer) stateFn {
  366. r := lx.next()
  367. switch {
  368. case isWhitespace(r) || isNL(r):
  369. return lexSkip(lx, lexArrayValueEnd)
  370. case r == commentStart:
  371. lx.push(lexArrayValueEnd)
  372. return lexCommentStart
  373. case r == arrayValTerm:
  374. return lexArrayValue // move on to the next value
  375. case r == arrayEnd:
  376. return lexArrayEnd
  377. }
  378. return lx.errorf("Expected an array value terminator '%s' or an array "+
  379. "terminator '%s', but got '%s' instead.", arrayValTerm, arrayEnd, r)
  380. }
  381. // lexArrayEnd finishes the lexing of an array. It assumes that a ']' has
  382. // just been consumed.
  383. func lexArrayEnd(lx *lexer) stateFn {
  384. lx.ignore()
  385. lx.emit(itemArrayEnd)
  386. return lx.pop()
  387. }
  388. // lexString consumes the inner contents of a string. It assumes that the
  389. // beginning '"' has already been consumed and ignored.
  390. func lexString(lx *lexer) stateFn {
  391. r := lx.next()
  392. switch {
  393. case isNL(r):
  394. return lx.errorf("Strings cannot contain new lines.")
  395. case r == '\\':
  396. return lexStringEscape
  397. case r == stringEnd:
  398. lx.backup()
  399. lx.emit(itemString)
  400. lx.next()
  401. lx.ignore()
  402. return lx.pop()
  403. }
  404. return lexString
  405. }
  406. // lexStringEscape consumes an escaped character. It assumes that the preceding
  407. // '\\' has already been consumed.
  408. func lexStringEscape(lx *lexer) stateFn {
  409. r := lx.next()
  410. switch r {
  411. case 'b':
  412. fallthrough
  413. case 't':
  414. fallthrough
  415. case 'n':
  416. fallthrough
  417. case 'f':
  418. fallthrough
  419. case 'r':
  420. fallthrough
  421. case '"':
  422. fallthrough
  423. case '/':
  424. fallthrough
  425. case '\\':
  426. return lexString
  427. case 'u':
  428. return lexStringUnicode
  429. }
  430. return lx.errorf("Invalid escape character '%s'. Only the following "+
  431. "escape characters are allowed: "+
  432. "\\b, \\t, \\n, \\f, \\r, \\\", \\/, \\\\, and \\uXXXX.", r)
  433. }
  434. // lexStringBinary consumes two hexadecimal digits following '\x'. It assumes
  435. // that the '\x' has already been consumed.
  436. func lexStringUnicode(lx *lexer) stateFn {
  437. var r rune
  438. for i := 0; i < 4; i++ {
  439. r = lx.next()
  440. if !isHexadecimal(r) {
  441. return lx.errorf("Expected four hexadecimal digits after '\\x', "+
  442. "but got '%s' instead.", lx.current())
  443. }
  444. }
  445. return lexString
  446. }
  447. // lexNumberOrDateStart consumes either a (positive) integer, float or datetime.
  448. // It assumes that NO negative sign has been consumed.
  449. func lexNumberOrDateStart(lx *lexer) stateFn {
  450. r := lx.next()
  451. if !isDigit(r) {
  452. if r == '.' {
  453. return lx.errorf("Floats must start with a digit, not '.'.")
  454. } else {
  455. return lx.errorf("Expected a digit but got '%s'.", r)
  456. }
  457. }
  458. return lexNumberOrDate
  459. }
  460. // lexNumberOrDate consumes either a (positive) integer, float or datetime.
  461. func lexNumberOrDate(lx *lexer) stateFn {
  462. r := lx.next()
  463. switch {
  464. case r == '-':
  465. if lx.pos-lx.start != 5 {
  466. return lx.errorf("All ISO8601 dates must be in full Zulu form.")
  467. }
  468. return lexDateAfterYear
  469. case isDigit(r):
  470. return lexNumberOrDate
  471. case r == '.':
  472. return lexFloatStart
  473. }
  474. lx.backup()
  475. lx.emit(itemInteger)
  476. return lx.pop()
  477. }
  478. // lexDateAfterYear consumes a full Zulu Datetime in ISO8601 format.
  479. // It assumes that "YYYY-" has already been consumed.
  480. func lexDateAfterYear(lx *lexer) stateFn {
  481. formats := []rune{
  482. // digits are '0'.
  483. // everything else is direct equality.
  484. '0', '0', '-', '0', '0',
  485. 'T',
  486. '0', '0', ':', '0', '0', ':', '0', '0',
  487. 'Z',
  488. }
  489. for _, f := range formats {
  490. r := lx.next()
  491. if f == '0' {
  492. if !isDigit(r) {
  493. return lx.errorf("Expected digit in ISO8601 datetime, "+
  494. "but found '%s' instead.", r)
  495. }
  496. } else if f != r {
  497. return lx.errorf("Expected '%s' in ISO8601 datetime, "+
  498. "but found '%s' instead.", f, r)
  499. }
  500. }
  501. lx.emit(itemDatetime)
  502. return lx.pop()
  503. }
  504. // lexNumberStart consumes either an integer or a float. It assumes that a
  505. // negative sign has already been read, but that *no* digits have been consumed.
  506. // lexNumberStart will move to the appropriate integer or float states.
  507. func lexNumberStart(lx *lexer) stateFn {
  508. // we MUST see a digit. Even floats have to start with a digit.
  509. r := lx.next()
  510. if !isDigit(r) {
  511. if r == '.' {
  512. return lx.errorf("Floats must start with a digit, not '.'.")
  513. } else {
  514. return lx.errorf("Expected a digit but got '%s'.", r)
  515. }
  516. }
  517. return lexNumber
  518. }
  519. // lexNumber consumes an integer or a float after seeing the first digit.
  520. func lexNumber(lx *lexer) stateFn {
  521. r := lx.next()
  522. switch {
  523. case isDigit(r):
  524. return lexNumber
  525. case r == '.':
  526. return lexFloatStart
  527. }
  528. lx.backup()
  529. lx.emit(itemInteger)
  530. return lx.pop()
  531. }
  532. // lexFloatStart starts the consumption of digits of a float after a '.'.
  533. // Namely, at least one digit is required.
  534. func lexFloatStart(lx *lexer) stateFn {
  535. r := lx.next()
  536. if !isDigit(r) {
  537. return lx.errorf("Floats must have a digit after the '.', but got "+
  538. "'%s' instead.", r)
  539. }
  540. return lexFloat
  541. }
  542. // lexFloat consumes the digits of a float after a '.'.
  543. // Assumes that one digit has been consumed after a '.' already.
  544. func lexFloat(lx *lexer) stateFn {
  545. r := lx.next()
  546. if isDigit(r) {
  547. return lexFloat
  548. }
  549. lx.backup()
  550. lx.emit(itemFloat)
  551. return lx.pop()
  552. }
  553. // lexTrue consumes the "rue" in "true". It assumes that 't' has already
  554. // been consumed.
  555. func lexTrue(lx *lexer) stateFn {
  556. if r := lx.next(); r != 'r' {
  557. return lx.errorf("Expected 'tr', but found 't%s' instead.", r)
  558. }
  559. if r := lx.next(); r != 'u' {
  560. return lx.errorf("Expected 'tru', but found 'tr%s' instead.", r)
  561. }
  562. if r := lx.next(); r != 'e' {
  563. return lx.errorf("Expected 'true', but found 'tru%s' instead.", r)
  564. }
  565. lx.emit(itemBool)
  566. return lx.pop()
  567. }
  568. // lexFalse consumes the "alse" in "false". It assumes that 'f' has already
  569. // been consumed.
  570. func lexFalse(lx *lexer) stateFn {
  571. if r := lx.next(); r != 'a' {
  572. return lx.errorf("Expected 'fa', but found 'f%s' instead.", r)
  573. }
  574. if r := lx.next(); r != 'l' {
  575. return lx.errorf("Expected 'fal', but found 'fa%s' instead.", r)
  576. }
  577. if r := lx.next(); r != 's' {
  578. return lx.errorf("Expected 'fals', but found 'fal%s' instead.", r)
  579. }
  580. if r := lx.next(); r != 'e' {
  581. return lx.errorf("Expected 'false', but found 'fals%s' instead.", r)
  582. }
  583. lx.emit(itemBool)
  584. return lx.pop()
  585. }
  586. // lexCommentStart begins the lexing of a comment. It will emit
  587. // itemCommentStart and consume no characters, passing control to lexComment.
  588. func lexCommentStart(lx *lexer) stateFn {
  589. lx.ignore()
  590. lx.emit(itemCommentStart)
  591. return lexComment
  592. }
  593. // lexComment lexes an entire comment. It assumes that '#' has been consumed.
  594. // It will consume *up to* the first new line character, and pass control
  595. // back to the last state on the stack.
  596. func lexComment(lx *lexer) stateFn {
  597. r := lx.peek()
  598. if isNL(r) || r == eof {
  599. lx.emit(itemText)
  600. return lx.pop()
  601. }
  602. lx.next()
  603. return lexComment
  604. }
  605. // lexSkip ignores all slurped input and moves on to the next state.
  606. func lexSkip(lx *lexer, nextState stateFn) stateFn {
  607. return func(lx *lexer) stateFn {
  608. lx.ignore()
  609. return nextState
  610. }
  611. }
  612. // isWhitespace returns true if `r` is a whitespace character according
  613. // to the spec.
  614. func isWhitespace(r rune) bool {
  615. return r == '\t' || r == ' '
  616. }
  617. func isNL(r rune) bool {
  618. return r == '\n' || r == '\r'
  619. }
  620. func isDigit(r rune) bool {
  621. return r >= '0' && r <= '9'
  622. }
  623. func isHexadecimal(r rune) bool {
  624. return (r >= '0' && r <= '9') ||
  625. (r >= 'a' && r <= 'f') ||
  626. (r >= 'A' && r <= 'F')
  627. }
  628. func (itype itemType) String() string {
  629. switch itype {
  630. case itemError:
  631. return "Error"
  632. case itemNIL:
  633. return "NIL"
  634. case itemEOF:
  635. return "EOF"
  636. case itemText:
  637. return "Text"
  638. case itemString:
  639. return "String"
  640. case itemBool:
  641. return "Bool"
  642. case itemInteger:
  643. return "Integer"
  644. case itemFloat:
  645. return "Float"
  646. case itemDatetime:
  647. return "DateTime"
  648. case itemTableStart:
  649. return "TableStart"
  650. case itemTableEnd:
  651. return "TableEnd"
  652. case itemKeyStart:
  653. return "KeyStart"
  654. case itemArray:
  655. return "Array"
  656. case itemArrayEnd:
  657. return "ArrayEnd"
  658. case itemCommentStart:
  659. return "CommentStart"
  660. }
  661. panic(fmt.Sprintf("BUG: Unknown type '%s'.", itype))
  662. }
  663. func (item item) String() string {
  664. return fmt.Sprintf("(%s, %s)", item.typ.String(), item.val)
  665. }
  666. func escapeSpecial(c rune) string {
  667. switch c {
  668. case '\n':
  669. return "\\n"
  670. }
  671. return string(c)
  672. }