From f635033e3e953e74d67b76a520c9760786330af5 Mon Sep 17 00:00:00 2001 From: Niall Sheridan Date: Tue, 24 Jan 2017 23:43:28 +0000 Subject: Switch to scl, an extension of hcl --- vendor/github.com/homemade/scl/scanner.go | 121 ++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 vendor/github.com/homemade/scl/scanner.go (limited to 'vendor/github.com/homemade/scl/scanner.go') diff --git a/vendor/github.com/homemade/scl/scanner.go b/vendor/github.com/homemade/scl/scanner.go new file mode 100644 index 0000000..7dddf59 --- /dev/null +++ b/vendor/github.com/homemade/scl/scanner.go @@ -0,0 +1,121 @@ +package scl + +import ( + "bufio" + "fmt" + "io" + "strings" +) + +type scannerTree []*scannerLine + +type scanner struct { + file string + reader io.Reader + lines scannerTree +} + +func newScanner(reader io.Reader, filename ...string) *scanner { + + file := "" + + if len(filename) > 0 { + file = filename[0] + } + + s := scanner{ + file: file, + reader: reader, + lines: make(scannerTree, 0), + } + + return &s +} + +func (s *scanner) scan() (lines scannerTree, err error) { + + // Split to lines + scanner := bufio.NewScanner(s.reader) + scanner.Split(bufio.ScanLines) + + lineNumber := 0 + rawLines := make(scannerTree, 0) + + heredoc := "" + heredocContent := "" + heredocLine := 0 + + for scanner.Scan() { + lineNumber++ + + if heredoc != "" { + heredocContent += "\n" + scanner.Text() + + if strings.TrimSpace(scanner.Text()) == heredoc { + // HCL requires heredocs to be terminated with a newline + rawLines = append(rawLines, newLine(s.file, lineNumber, 0, heredocContent+"\n")) + heredoc = "" + heredocContent = "" + } + + continue + } + + text := strings.TrimRight(scanner.Text(), " \t{}") + + if text == "" { + continue + } + + if matches := heredocMatcher.FindAllStringSubmatch(text, -1); matches != nil { + heredoc = matches[0][1] + heredocContent = text + heredocLine = lineNumber + continue + } + + rawLines = append(rawLines, newLine(s.file, lineNumber, 0, text)) + } + + if heredoc != "" { + return lines, fmt.Errorf("Heredoc '%s' (started line %d) not terminated", heredoc, heredocLine) + } + + // Make sure the first line has no indent + if len(rawLines) > 0 { + index := 0 + s.indentLines(&index, rawLines, &lines, rawLines[0].content.indent()) + } + + return +} + +func (s *scanner) indentLines(index *int, input scannerTree, output *scannerTree, indent int) { + + // Ends when there are no more lines + if *index >= len(input) { + return + } + + var lineToAdd *scannerLine + + for ; *index < len(input); *index++ { + + lineIndent := input[*index].content.indent() + + if lineIndent == indent { + lineToAdd = input[*index].branch() + *output = append(*output, lineToAdd) + + } else if lineIndent > indent { + s.indentLines(index, input, &lineToAdd.children, lineIndent) + + } else if lineIndent < indent { + *index-- + return + } + + } + + return +} -- cgit v1.2.3