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

Method parseTemplateParts

hclsyntax/parser_template.go:365–689  ·  view source on GitHub ↗

parseTemplateParts produces a flat sequence of "template tokens", which are either literal values (with any "trimming" already applied), interpolation sequences, or control flow markers. A further pass is required on the result to turn it into an AST.

(end TokenType)

Source from the content-addressed store, hash-verified

363//
364// A further pass is required on the result to turn it into an AST.
365func (p *parser) parseTemplateParts(end TokenType) (*templateParts, hcl.Diagnostics) {
366 var parts []templateToken
367 var diags hcl.Diagnostics
368
369 startRange := p.NextRange()
370 ltrimNext := false
371 nextCanTrimPrev := false
372 var endRange hcl.Range
373
374Token:
375 for {
376 next := p.Read()
377 if next.Type == end {
378 // all done!
379 endRange = next.Range
380 break
381 }
382
383 ltrim := ltrimNext
384 ltrimNext = false
385 canTrimPrev := nextCanTrimPrev
386 nextCanTrimPrev = false
387
388 switch next.Type {
389 case TokenStringLit, TokenQuotedLit:
390 str, strDiags := ParseStringLiteralToken(next)
391 diags = append(diags, strDiags...)
392
393 if ltrim {
394 str = strings.TrimLeftFunc(str, unicode.IsSpace)
395 }
396
397 parts = append(parts, &templateLiteralToken{
398 Val: str,
399 SrcRange: next.Range,
400 })
401 nextCanTrimPrev = true
402
403 case TokenTemplateInterp:
404 // if the opener is ${~ then we want to eat any trailing whitespace
405 // in the preceding literal token, assuming it is indeed a literal
406 // token.
407 if canTrimPrev && len(next.Bytes) == 3 && next.Bytes[2] == '~' && len(parts) > 0 {
408 prevExpr := parts[len(parts)-1]
409 if lexpr, ok := prevExpr.(*templateLiteralToken); ok {
410 lexpr.Val = strings.TrimRightFunc(lexpr.Val, unicode.IsSpace)
411 }
412 }
413
414 p.PushIncludeNewlines(false)
415 expr, exprDiags := p.ParseExpression()
416 diags = append(diags, exprDiags...)
417 close := p.Peek()
418 if close.Type != TokenTemplateSeqEnd {
419 if !p.recovery {
420 switch close.Type {
421 case TokenEOF:
422 diags = append(diags, &hcl.Diagnostic{

Callers 1

parseTemplateInnerMethod · 0.95

Calls 11

ParseExpressionMethod · 0.95
recoverMethod · 0.95
ParseStringLiteralTokenFunction · 0.85
NextRangeMethod · 0.80
PushIncludeNewlinesMethod · 0.80
PtrMethod · 0.80
PopIncludeNewlinesMethod · 0.80
TokenMatchesMethod · 0.80
nameSuggestionFunction · 0.70
ReadMethod · 0.45
PeekMethod · 0.45

Tested by

no test coverage detected