()
| 144 | } |
| 145 | |
| 146 | func (p *parser) begin() error { |
| 147 | if len(p.tokens) == 0 { |
| 148 | return nil |
| 149 | } |
| 150 | |
| 151 | err := p.addresses() |
| 152 | if err != nil { |
| 153 | return err |
| 154 | } |
| 155 | |
| 156 | if p.eof { |
| 157 | // this happens if the Caddyfile consists of only |
| 158 | // a line of addresses and nothing else |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | if ok, name := p.isNamedRoute(); ok { |
| 163 | // we just need a dummy leading token to ease parsing later |
| 164 | nameToken := p.Token() |
| 165 | nameToken.Text = name |
| 166 | |
| 167 | // named routes only have one key, the route name |
| 168 | p.block.Keys = []Token{nameToken} |
| 169 | p.block.IsNamedRoute = true |
| 170 | |
| 171 | // get all the tokens from the block, including the braces |
| 172 | tokens, err := p.blockTokens(true) |
| 173 | if err != nil { |
| 174 | return err |
| 175 | } |
| 176 | tokens = append([]Token{nameToken}, tokens...) |
| 177 | p.block.Segments = []Segment{tokens} |
| 178 | return nil |
| 179 | } |
| 180 | |
| 181 | if ok, name := p.isSnippet(); ok { |
| 182 | if p.definedSnippets == nil { |
| 183 | p.definedSnippets = map[string][]Token{} |
| 184 | } |
| 185 | if _, found := p.definedSnippets[name]; found { |
| 186 | return p.Errf("redeclaration of previously declared snippet %s", name) |
| 187 | } |
| 188 | // consume all tokens til matched close brace |
| 189 | tokens, err := p.blockTokens(false) |
| 190 | if err != nil { |
| 191 | return err |
| 192 | } |
| 193 | // Just as we need to track which file the token comes from, we need to |
| 194 | // keep track of which snippet the token comes from. This is helpful |
| 195 | // in tracking import cycles across files/snippets by namespacing them. |
| 196 | // Without this, we end up with false-positives in cycle-detection. |
| 197 | for k, v := range tokens { |
| 198 | v.snippetName = name |
| 199 | tokens[k] = v |
| 200 | } |
| 201 | p.definedSnippets[name] = tokens |
| 202 | // empty block keys so we don't save this block as a real server. |
| 203 | p.block.Keys = nil |
no test coverage detected