| 137 | } |
| 138 | |
| 139 | func (p *templateParser) parseIf() (Expression, hcl.Diagnostics) { |
| 140 | open := p.Read() |
| 141 | openIf, isIf := open.(*templateIfToken) |
| 142 | if !isIf { |
| 143 | // should never happen if caller is behaving |
| 144 | panic("parseIf called with peeker not pointing at if token") |
| 145 | } |
| 146 | |
| 147 | var ifExprs, elseExprs []Expression |
| 148 | var diags hcl.Diagnostics |
| 149 | var endifRange hcl.Range |
| 150 | |
| 151 | currentExprs := &ifExprs |
| 152 | Token: |
| 153 | for { |
| 154 | next := p.Peek() |
| 155 | if end, isEnd := next.(*templateEndToken); isEnd { |
| 156 | diags = append(diags, &hcl.Diagnostic{ |
| 157 | Severity: hcl.DiagError, |
| 158 | Summary: "Unexpected end of template", |
| 159 | Detail: fmt.Sprintf( |
| 160 | "The if directive at %s is missing its corresponding endif directive.", |
| 161 | openIf.SrcRange, |
| 162 | ), |
| 163 | Subject: &end.SrcRange, |
| 164 | }) |
| 165 | return errPlaceholderExpr(end.SrcRange), diags |
| 166 | } |
| 167 | if end, isCtrlEnd := next.(*templateEndCtrlToken); isCtrlEnd { |
| 168 | p.Read() // eat end directive |
| 169 | |
| 170 | switch end.Type { |
| 171 | |
| 172 | case templateElse: |
| 173 | if currentExprs == &ifExprs { |
| 174 | currentExprs = &elseExprs |
| 175 | continue Token |
| 176 | } |
| 177 | |
| 178 | diags = append(diags, &hcl.Diagnostic{ |
| 179 | Severity: hcl.DiagError, |
| 180 | Summary: "Unexpected else directive", |
| 181 | Detail: fmt.Sprintf( |
| 182 | "Already in the else clause for the if started at %s.", |
| 183 | openIf.SrcRange, |
| 184 | ), |
| 185 | Subject: &end.SrcRange, |
| 186 | }) |
| 187 | |
| 188 | case templateEndIf: |
| 189 | endifRange = end.SrcRange |
| 190 | break Token |
| 191 | |
| 192 | default: |
| 193 | diags = append(diags, &hcl.Diagnostic{ |
| 194 | Severity: hcl.DiagError, |
| 195 | Summary: fmt.Sprintf("Unexpected %s directive", end.Name()), |
| 196 | Detail: fmt.Sprintf( |