parse is the top-level parser for a template, essentially the same It runs to EOF.
(cacheAfterParsing bool)
| 246 | // parse is the top-level parser for a template, essentially the same |
| 247 | // It runs to EOF. |
| 248 | func (t *Template) parseTemplate(cacheAfterParsing bool) (next Node) { |
| 249 | t.Root = t.newList(t.peek().pos) |
| 250 | // {{ extends|import stringLiteral }} |
| 251 | for t.peek().typ != itemEOF { |
| 252 | delim := t.next() |
| 253 | if delim.typ == itemText && strings.TrimSpace(delim.val) == "" { |
| 254 | continue //skips empty text nodes |
| 255 | } |
| 256 | if delim.typ == itemLeftDelim { |
| 257 | token := t.nextNonSpace() |
| 258 | if token.typ == itemExtends || token.typ == itemImport { |
| 259 | s := t.expectString("extends|import") |
| 260 | if token.typ == itemExtends { |
| 261 | if t.extends != nil { |
| 262 | t.errorf("Unexpected extends clause: each template can only extend one template") |
| 263 | } else if len(t.imports) > 0 { |
| 264 | t.errorf("Unexpected extends clause: the 'extends' clause should come before all import clauses") |
| 265 | } |
| 266 | var err error |
| 267 | t.extends, err = t.set.getSiblingTemplate(s, t.Name, cacheAfterParsing) |
| 268 | if err != nil { |
| 269 | t.error(err) |
| 270 | } |
| 271 | } else { |
| 272 | tt, err := t.set.getSiblingTemplate(s, t.Name, cacheAfterParsing) |
| 273 | if err != nil { |
| 274 | t.error(err) |
| 275 | } |
| 276 | t.imports = append(t.imports, tt) |
| 277 | } |
| 278 | t.expect(itemRightDelim, "extends|import", "closing delimiter") |
| 279 | } else { |
| 280 | t.backup2(delim) |
| 281 | break |
| 282 | } |
| 283 | } else { |
| 284 | t.backup() |
| 285 | break |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | for t.peek().typ != itemEOF { |
| 290 | switch n := t.textOrAction(); n.Type() { |
| 291 | case nodeEnd, nodeElse, nodeContent: |
| 292 | t.errorf("unexpected %s", n) |
| 293 | default: |
| 294 | t.Root.append(n) |
| 295 | } |
| 296 | } |
| 297 | return nil |
| 298 | } |
| 299 | |
| 300 | // startParse initializes the parser, using the lexer. |
| 301 | func (t *Template) startParse(lex *lexer) { |
no test coverage detected