NextBlock can be used as the condition of a for loop to load the next token as long as it opens a block or is already in a block nested more than initialNestingLevel. In other words, a loop over NextBlock() will iterate all tokens in the block assuming the next token is an open curly brace, until th
(initialNestingLevel int)
| 165 | // Next(), as it is usually prudent to skip the initial |
| 166 | // token. |
| 167 | func (d *Dispenser) NextBlock(initialNestingLevel int) bool { |
| 168 | if d.nesting > initialNestingLevel { |
| 169 | if !d.Next() { |
| 170 | return false // should be EOF error |
| 171 | } |
| 172 | if d.Val() == "}" && !d.nextOnSameLine() { |
| 173 | d.nesting-- |
| 174 | } else if d.Val() == "{" && !d.nextOnSameLine() { |
| 175 | d.nesting++ |
| 176 | } |
| 177 | return d.nesting > initialNestingLevel |
| 178 | } |
| 179 | if !d.nextOnSameLine() { // block must open on same line |
| 180 | return false |
| 181 | } |
| 182 | if d.Val() != "{" { |
| 183 | d.cursor-- // roll back if not opening brace |
| 184 | return false |
| 185 | } |
| 186 | d.Next() // consume open curly brace |
| 187 | if d.Val() == "}" { |
| 188 | return false // open and then closed right away |
| 189 | } |
| 190 | d.nesting++ |
| 191 | return true |
| 192 | } |
| 193 | |
| 194 | // Nesting returns the current nesting level. Necessary |
| 195 | // if using NextBlock() |