cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.1.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/parser/read.go

134lines · modecode

1package parser
2
3import (
4 "bufio"
5 "io"
6 "strings"
7)
8
9type skipMode int
10
11const (
12 skipNone skipMode = iota
13 skipNextLine
14 skipBegin
15 skipEnd
16 skipCurrentLine
17 skipFile
18)
19
20func removeRedundantSpaces(line string) string {
21 return strings.Join(strings.Fields(line), " ")
22}
23
24func emptyLine(line string) (emptied string) {
25 preComment := strings.TrimSuffix(line, "\n")
26 var comment string
27 if commentStart := strings.IndexRune(line, '#'); commentStart >= 0 {
28 comment = preComment[commentStart:]
29 preComment = preComment[:commentStart]
30 }
31
32 emptied = strings.Repeat(" ", len(preComment)) + comment
33
34 if strings.HasSuffix(line, "\n") {
35 emptied += "\n"
36 }
37
38 return
39}
40
41func hasComment(line, comment string) bool {
42 if !strings.Contains(line, "#") {
43 return false
44 }
45
46 elems := strings.Split(strings.TrimSuffix(line, "\n"), "#")
47 lastComment := elems[len(elems)-1]
48 parts := strings.SplitN(removeRedundantSpaces(lastComment), " ", 2)
49 if len(parts) < 2 {
50 return false
51 }
52
53 return parts[0] == "pint" && parts[1] == comment
54}
55
56func parseSkipComment(line string) (skipMode, bool) {
57 if hasComment(line, "ignore/file") {
58 return skipFile, true
59 } else if hasComment(line, "ignore/line") {
60 return skipCurrentLine, true
61 } else if hasComment(line, "ignore/next-line") {
62 return skipNextLine, true
63 } else if hasComment(line, "ignore/begin") {
64 return skipBegin, true
65 } else if hasComment(line, "ignore/end") {
66 return skipEnd, true
67 }
68 return skipNone, false
69}
70
71func ReadContent(r io.Reader) (out []byte, err error) {
72 reader := bufio.NewReader(r)
73 var line string
74 var found bool
75 var skip skipMode
76
77 var skipNext bool
78 var autoReset bool
79 var skipAll bool
80 for {
81 line, err = reader.ReadString('\n')
82 if err != nil && err != io.EOF {
83 break
84 }
85
86 if skipAll {
87 out = append(out, []byte(emptyLine(line))...)
88 } else {
89 skip, found = parseSkipComment(line)
90 if found {
91 switch skip {
92 case skipFile:
93 out = append(out, []byte(emptyLine(line))...)
94 skipNext = true
95 autoReset = false
96 skipAll = true
97 case skipCurrentLine:
98 out = append(out, []byte(emptyLine(line))...)
99 skipNext = false
100 autoReset = true
101 case skipNextLine:
102 out = append(out, []byte(line)...)
103 skipNext = true
104 autoReset = true
105 case skipBegin:
106 out = append(out, []byte(line)...)
107 skipNext = true
108 autoReset = false
109 case skipEnd:
110 out = append(out, []byte(line)...)
111 skipNext = false
112 autoReset = true
113 }
114 } else if skipNext {
115 out = append(out, []byte(emptyLine(line))...)
116 if autoReset {
117 skipNext = false
118 }
119 } else {
120 out = append(out, []byte(line)...)
121 }
122 }
123
124 if err != nil {
125 break
126 }
127 }
128
129 if err != io.EOF {
130 return nil, err
131 }
132
133 return out, nil
134}
135