()
| 71 | } |
| 72 | |
| 73 | func (p *peeker) nextToken() (Token, int) { |
| 74 | for i := p.NextIndex; i < len(p.Tokens); i++ { |
| 75 | tok := p.Tokens[i] |
| 76 | switch tok.Type { |
| 77 | case TokenComment: |
| 78 | if !p.IncludeComments { |
| 79 | // Single-line comment tokens, starting with # or //, absorb |
| 80 | // the trailing newline that terminates them as part of their |
| 81 | // bytes. When we're filtering out comments, we must as a |
| 82 | // special case transform these to newline tokens in order |
| 83 | // to properly parse newline-terminated block items. |
| 84 | |
| 85 | if p.includingNewlines() { |
| 86 | if len(tok.Bytes) > 0 && tok.Bytes[len(tok.Bytes)-1] == '\n' { |
| 87 | fakeNewline := Token{ |
| 88 | Type: TokenNewline, |
| 89 | Bytes: tok.Bytes[len(tok.Bytes)-1 : len(tok.Bytes)], |
| 90 | |
| 91 | // We use the whole token range as the newline |
| 92 | // range, even though that's a little... weird, |
| 93 | // because otherwise we'd need to go count |
| 94 | // characters again in order to figure out the |
| 95 | // column of the newline, and that complexity |
| 96 | // isn't justified when ranges of newlines are |
| 97 | // so rarely printed anyway. |
| 98 | Range: tok.Range, |
| 99 | } |
| 100 | return fakeNewline, i + 1 |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | continue |
| 105 | } |
| 106 | case TokenNewline: |
| 107 | if !p.includingNewlines() { |
| 108 | continue |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return tok, i + 1 |
| 113 | } |
| 114 | |
| 115 | // if we fall out here then we'll return the EOF token, and leave |
| 116 | // our index pointed off the end of the array so we'll keep |
| 117 | // returning EOF in future too. |
| 118 | return p.Tokens[len(p.Tokens)-1], len(p.Tokens) |
| 119 | } |
| 120 | |
| 121 | func (p *peeker) includingNewlines() bool { |
| 122 | return p.IncludeNewlinesStack[len(p.IncludeNewlinesStack)-1] |
no test coverage detected