MCPcopy
hub / github.com/hashicorp/hcl / finishParsingForExpr

Method finishParsingForExpr

hclsyntax/parser.go:1590–1809  ·  view source on GitHub ↗
(open Token)

Source from the content-addressed store, hash-verified

1588}
1589
1590func (p *parser) finishParsingForExpr(open Token) (Expression, hcl.Diagnostics) {
1591 p.PushIncludeNewlines(false)
1592 defer p.PopIncludeNewlines()
1593 introducer := p.Read()
1594 if !forKeyword.TokenMatches(introducer) {
1595 // Should never happen if callers are behaving
1596 panic("finishParsingForExpr called without peeker pointing to 'for' identifier")
1597 }
1598
1599 var makeObj bool
1600 var closeType TokenType
1601 switch open.Type {
1602 case TokenOBrace:
1603 makeObj = true
1604 closeType = TokenCBrace
1605 case TokenOBrack:
1606 makeObj = false // making a tuple
1607 closeType = TokenCBrack
1608 default:
1609 // Should never happen if callers are behaving
1610 panic("finishParsingForExpr called with invalid open token")
1611 }
1612
1613 var diags hcl.Diagnostics
1614 var keyName, valName string
1615
1616 if p.Peek().Type != TokenIdent {
1617 if !p.recovery {
1618 diags = append(diags, &hcl.Diagnostic{
1619 Severity: hcl.DiagError,
1620 Summary: "Invalid 'for' expression",
1621 Detail: "For expression requires variable name after 'for'.",
1622 Subject: p.Peek().Range.Ptr(),
1623 Context: hcl.RangeBetween(open.Range, p.Peek().Range).Ptr(),
1624 })
1625 }
1626 close := p.recover(closeType)
1627 return &LiteralValueExpr{
1628 Val: cty.DynamicVal,
1629 SrcRange: hcl.RangeBetween(open.Range, close.Range),
1630 }, diags
1631 }
1632
1633 valName = string(p.Read().Bytes)
1634
1635 if p.Peek().Type == TokenComma {
1636 // What we just read was actually the key, then.
1637 keyName = valName
1638 p.Read() // eat comma
1639
1640 if p.Peek().Type != TokenIdent {
1641 if !p.recovery {
1642 diags = append(diags, &hcl.Diagnostic{
1643 Severity: hcl.DiagError,
1644 Summary: "Invalid 'for' expression",
1645 Detail: "For expression requires value variable name after comma.",
1646 Subject: p.Peek().Range.Ptr(),
1647 Context: hcl.RangeBetween(open.Range, p.Peek().Range).Ptr(),

Callers 2

parseTupleConsMethod · 0.95
parseObjectConsMethod · 0.95

Calls 11

recoverMethod · 0.95
ParseExpressionMethod · 0.95
HasErrorsMethod · 0.95
oppositeBracketMethod · 0.95
RangeMethod · 0.95
PushIncludeNewlinesMethod · 0.80
PopIncludeNewlinesMethod · 0.80
TokenMatchesMethod · 0.80
PtrMethod · 0.80
ReadMethod · 0.45
PeekMethod · 0.45

Tested by

no test coverage detected