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. It returns true if a token was loaded, or false when the block's closing curly brace was loaded and thus the block ended. Nested blocks are not supported.
()
| 128 | // was loaded and thus the block ended. Nested blocks are |
| 129 | // not supported. |
| 130 | func (d *Dispenser) NextBlock() bool { |
| 131 | if d.nesting > 0 { |
| 132 | d.Next() |
| 133 | if d.Val() == "}" { |
| 134 | d.nesting-- |
| 135 | return false |
| 136 | } |
| 137 | return true |
| 138 | } |
| 139 | if !d.NextArg() { // block must open on same line |
| 140 | return false |
| 141 | } |
| 142 | if d.Val() != "{" { |
| 143 | d.cursor-- // roll back if not opening brace |
| 144 | return false |
| 145 | } |
| 146 | d.Next() |
| 147 | if d.Val() == "}" { |
| 148 | // Open and then closed right away |
| 149 | return false |
| 150 | } |
| 151 | d.nesting++ |
| 152 | return true |
| 153 | } |
| 154 | |
| 155 | // Val gets the text of the current token. If there is no token |
| 156 | // loaded, it returns empty string. |