directive collects tokens until the directive's scope closes (either end of line or end of curly brace block). It expects the currently-loaded token to be a directive (or } that ends a server block). The collected tokens are loaded into the current server block for later use by directive setup funct
()
| 634 | // are loaded into the current server block for later use |
| 635 | // by directive setup functions. |
| 636 | func (p *parser) directive() error { |
| 637 | // a segment is a list of tokens associated with this directive |
| 638 | var segment Segment |
| 639 | |
| 640 | // the directive itself is appended as a relevant token |
| 641 | segment = append(segment, p.Token()) |
| 642 | |
| 643 | for p.Next() { |
| 644 | if p.Val() == "{" { |
| 645 | p.nesting++ |
| 646 | if !p.isNextOnNewLine() && p.Token().wasQuoted == 0 { |
| 647 | return p.Err("Unexpected next token after '{' on same line") |
| 648 | } |
| 649 | if p.isNewLine() { |
| 650 | return p.Err("Unexpected '{' on a new line; did you mean to place the '{' on the previous line?") |
| 651 | } |
| 652 | } else if p.Val() == "{}" { |
| 653 | if p.isNextOnNewLine() && p.Token().wasQuoted == 0 { |
| 654 | return p.Err("Unexpected '{}' at end of line") |
| 655 | } |
| 656 | } else if p.isNewLine() && p.nesting == 0 { |
| 657 | p.cursor-- // read too far |
| 658 | break |
| 659 | } else if p.Val() == "}" && p.nesting > 0 { |
| 660 | p.nesting-- |
| 661 | } else if p.Val() == "}" && p.nesting == 0 { |
| 662 | return p.Err("Unexpected '}' because no matching opening brace") |
| 663 | } else if p.Val() == "import" && p.isNewLine() { |
| 664 | if err := p.doImport(1); err != nil { |
| 665 | return err |
| 666 | } |
| 667 | p.cursor-- // cursor is advanced when we continue, so roll back one more |
| 668 | continue |
| 669 | } |
| 670 | |
| 671 | segment = append(segment, p.Token()) |
| 672 | } |
| 673 | |
| 674 | p.block.Segments = append(p.block.Segments, segment) |
| 675 | |
| 676 | if p.nesting > 0 { |
| 677 | return p.EOFErr() |
| 678 | } |
| 679 | |
| 680 | return nil |
| 681 | } |
| 682 | |
| 683 | // openCurlyBrace expects the current token to be an |
| 684 | // opening curly brace. This acts like an assertion |
no test coverage detected