MCPcopy
hub / github.com/valyala/quicktemplate / parseIf

Method parseIf

parser/parser.go:416–487  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

414}
415
416func (p *parser) parseIf() error {
417 s := p.s
418 t, err := expectTagContents(s)
419 if err != nil {
420 return err
421 }
422 if len(t.Value) == 0 {
423 return fmt.Errorf("empty if condition at %s", s.Context())
424 }
425 ifStr := "if " + string(t.Value)
426 if err = validateIfStmt(t.Value); err != nil {
427 return fmt.Errorf("invalid statement %q at %s: %s", ifStr, s.Context(), err)
428 }
429 p.Printf("if %s {", t.Value)
430 p.prefix += "\t"
431 elseUsed := false
432 for s.Next() {
433 t := s.Token()
434 switch t.ID {
435 case text:
436 p.emitText(t.Value)
437 case tagName:
438 ok, err := p.tryParseCommonTags(t.Value)
439 if err != nil {
440 return fmt.Errorf("error in %q: %s", ifStr, err)
441 }
442 if ok {
443 continue
444 }
445 switch string(t.Value) {
446 case "endif":
447 if err = skipTagContents(s); err != nil {
448 return err
449 }
450 p.prefix = p.prefix[1:]
451 p.Printf("}")
452 return nil
453 case "else":
454 if elseUsed {
455 return fmt.Errorf("duplicate else branch found for %q at %s", ifStr, s.Context())
456 }
457 if err = skipTagContents(s); err != nil {
458 return err
459 }
460 p.prefix = p.prefix[1:]
461 p.Printf("} else {")
462 p.prefix += "\t"
463 elseUsed = true
464 case "elseif":
465 if elseUsed {
466 return fmt.Errorf("unexpected elseif branch found after else branch for %q at %s",
467 ifStr, s.Context())
468 }
469 t, err = expectTagContents(s)
470 if err != nil {
471 return err
472 }
473 p.prefix = p.prefix[1:]

Callers 1

tryParseCommonTagsMethod · 0.95

Calls 10

PrintfMethod · 0.95
emitTextMethod · 0.95
tryParseCommonTagsMethod · 0.95
expectTagContentsFunction · 0.85
validateIfStmtFunction · 0.85
skipTagContentsFunction · 0.85
ContextMethod · 0.80
NextMethod · 0.80
TokenMethod · 0.80
LastErrorMethod · 0.80

Tested by

no test coverage detected