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

Function meldConsecutiveStringLiterals

hclsyntax/parser_template.go:761–787  ·  view source on GitHub ↗

meldConsecutiveStringLiterals simplifies the AST output by combining a sequence of string literal tokens into a single string literal. This must be performed after any whitespace trimming operations.

(parts *templateParts)

Source from the content-addressed store, hash-verified

759// sequence of string literal tokens into a single string literal. This must be
760// performed after any whitespace trimming operations.
761func meldConsecutiveStringLiterals(parts *templateParts) {
762 if len(parts.Tokens) == 0 {
763 return
764 }
765
766 // Loop over all tokens starting at the second element, as we want to join
767 // pairs of consecutive string literals.
768 i := 1
769 for i < len(parts.Tokens) {
770 if prevLiteral, ok := parts.Tokens[i-1].(*templateLiteralToken); ok {
771 if literal, ok := parts.Tokens[i].(*templateLiteralToken); ok {
772 // The current and previous tokens are both literals: combine
773 prevLiteral.Val = prevLiteral.Val + literal.Val
774 prevLiteral.SrcRange.End = literal.SrcRange.End
775
776 // Remove the current token from the slice
777 parts.Tokens = append(parts.Tokens[:i], parts.Tokens[i+1:]...)
778
779 // Continue without moving forward in the slice
780 continue
781 }
782 }
783
784 // Try the next pair of tokens
785 i++
786 }
787}
788
789type templateParts struct {
790 Tokens []templateToken

Callers 1

parseTemplateInnerMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected