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

Method parseQuotedStringLiteral

hclsyntax/parser.go:1813–1900  ·  hclsyntax/parser.go::parser.parseQuotedStringLiteral

parseQuotedStringLiteral is a helper for parsing quoted strings that aren't allowed to contain any interpolations, such as block labels.

()

Source from the content-addressed store, hash-verified

1811// parseQuotedStringLiteral is a helper for parsing quoted strings that
1812// aren't allowed to contain any interpolations, such as block labels.
1813func (p *parser) parseQuotedStringLiteral() (string, hcl.Range, hcl.Diagnostics) {
1814 oQuote := p.Read()
1815 if oQuote.Type != TokenOQuote {
1816 return "", oQuote.Range, hcl.Diagnostics{
1817 {
1818 Severity: hcl.DiagError,
1819 Summary: "Invalid string literal",
1820 Detail: "A quoted string is required here.",
1821 Subject: &oQuote.Range,
1822 },
1823 }
1824 }
1825
1826 var diags hcl.Diagnostics
1827 ret := &bytes.Buffer{}
1828 var endRange hcl.Range
1829
1830Token:
1831 for {
1832 tok := p.Read()
1833 switch tok.Type {
1834
1835 case TokenCQuote:
1836 endRange = tok.Range
1837 break Token
1838
1839 case TokenQuotedLit:
1840 s, sDiags := ParseStringLiteralToken(tok)
1841 diags = append(diags, sDiags...)
1842 ret.WriteString(s)
1843
1844 case TokenTemplateControl, TokenTemplateInterp:
1845 which := "$"
1846 if tok.Type == TokenTemplateControl {
1847 which = "%"
1848 }
1849
1850 diags = append(diags, &hcl.Diagnostic{
1851 Severity: hcl.DiagError,
1852 Summary: "Invalid string literal",
1853 Detail: fmt.Sprintf(
1854 "Template sequences are not allowed in this string. To include a literal %q, double it (as \"%s%s\") to escape it.",
1855 which, which, which,
1856 ),
1857 Subject: &tok.Range,
1858 Context: hcl.RangeBetween(oQuote.Range, tok.Range).Ptr(),
1859 })
1860
1861 // Now that we're returning an error callers won't attempt to use
1862 // the result for any real operations, but they might try to use
1863 // the partial AST for other analyses, so we'll leave a marker
1864 // to indicate that there was something invalid in the string to
1865 // help avoid misinterpretation of the partial result
1866 ret.WriteString(which)
1867 ret.WriteString("{ ... }")
1868
1869 p.recover(TokenTemplateSeqEnd) // we'll try to keep parsing after the sequence ends
1870

Callers 2

parseTraversalMethod · 0.95

Calls 5

recoverMethod · 0.95
ParseStringLiteralTokenFunction · 0.85
PtrMethod · 0.80
ReadMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected