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

Function partitionLineEndTokens

hclwrite/parser.go:603–631  ·  view source on GitHub ↗

partitionLineEndTokens takes a sequence of tokens that is assumed to immediately follow a construct that can have a line comment, and returns first the index where any line comments end and then second the index immediately after the trailing newline. Line comments are defined as comments that appe

(toks hclsyntax.Tokens)

Source from the content-addressed store, hash-verified

601// terminates them, in the presence of these the two returned indices
602// will be the same since the comment itself serves as the line end.
603func partitionLineEndTokens(toks hclsyntax.Tokens) (afterComment, afterNewline int) {
604 for i := 0; i < len(toks); i++ {
605 tok := toks[i]
606 if tok.Type != hclsyntax.TokenComment {
607 switch tok.Type {
608 case hclsyntax.TokenNewline:
609 return i, i + 1
610 case hclsyntax.TokenEOF:
611 // Although this is valid, we mustn't include the EOF
612 // itself as our "newline" or else strange things will
613 // happen when we try to append new items.
614 return i, i
615 default:
616 // If we have well-formed input here then nothing else should be
617 // possible. This path should never happen, because we only try
618 // to extract tokens from the sequence if the parser succeeded,
619 // and it should catch this problem itself.
620 panic("malformed line trailers: expected only comments and newlines")
621 }
622 }
623
624 if len(tok.Bytes) > 0 && tok.Bytes[len(tok.Bytes)-1] == '\n' {
625 // Newline at the end of a single-line comment serves both as
626 // the end of comments *and* the end of the line.
627 return i + 1, i + 1
628 }
629 }
630 return len(toks), len(toks)
631}
632
633// lexConfig uses the hclsyntax scanner to get a token stream and then
634// rewrites it into this package's token model.

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected