summaryrefslogtreecommitdiff
path: root/vendor/github.com/jhump/protoreflect/desc/protoparse/ast/print.go
blob: 271200c7323d5b81b141c7b5d9dc68c7e35982f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package ast

import "io"

// Print prints the given AST node to the given output. This operation
// basically walks the AST and, for each TerminalNode, prints the node's
// leading comments, leading whitespace, the node's raw text, and then
// any trailing comments. If the given node is a *FileNode, it will then
// also print the file's FinalComments and FinalWhitespace.
func Print(w io.Writer, node Node) error {
	sw, ok := w.(stringWriter)
	if !ok {
		sw = &strWriter{w}
	}
	var err error
	Walk(node, func(n Node) (bool, VisitFunc) {
		if err != nil {
			return false, nil
		}
		token, ok := n.(TerminalNode)
		if !ok {
			return true, nil
		}

		err = printComments(sw, token.LeadingComments())
		if err != nil {
			return false, nil
		}

		_, err = sw.WriteString(token.LeadingWhitespace())
		if err != nil {
			return false, nil
		}

		_, err = sw.WriteString(token.RawText())
		if err != nil {
			return false, nil
		}

		err = printComments(sw, token.TrailingComments())
		return false, nil
	})
	if err != nil {
		return err
	}

	if file, ok := node.(*FileNode); ok {
		err = printComments(sw, file.FinalComments)
		if err != nil {
			return err
		}
		_, err = sw.WriteString(file.FinalWhitespace)
		return err
	}

	return nil
}

func printComments(sw stringWriter, comments []Comment) error {
	for _, comment := range comments {
		if _, err := sw.WriteString(comment.LeadingWhitespace); err != nil {
			return err
		}
		if _, err := sw.WriteString(comment.Text); err != nil {
			return err
		}
	}
	return nil
}

// many io.Writer impls also provide a string-based method
type stringWriter interface {
	WriteString(s string) (n int, err error)
}

// adapter, in case the given writer does NOT provide a string-based method
type strWriter struct {
	io.Writer
}

func (s *strWriter) WriteString(str string) (int, error) {
	if str == "" {
		return 0, nil
	}
	return s.Write([]byte(str))
}