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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
package main
import (
"io/ioutil"
"strings"
)
func LoadNonemptyLines(fname string, joinBackslashLines bool) []Line {
lines, err := readLines(fname, joinBackslashLines)
if err != nil {
NewLineWhole(fname).Errorf("Cannot be read.")
return nil
}
if len(lines) == 0 {
NewLineWhole(fname).Errorf("Must not be empty.")
return nil
}
return lines
}
func LoadExistingLines(fname string, joinBackslashLines bool) []Line {
lines, err := readLines(fname, joinBackslashLines)
if err != nil {
NewLineWhole(fname).Fatalf("Cannot be read.")
}
return lines
}
func nextLogicalLine(fname string, rawLines []*RawLine, pindex *int) Line {
{ // Handle the common case efficiently
index := *pindex
rawLine := rawLines[index]
textnl := rawLine.textnl
if hasSuffix(textnl, "\n") && !hasSuffix(textnl, "\\\n") {
*pindex = index + 1
return NewLine(fname, rawLine.Lineno, textnl[:len(textnl)-1], rawLines[index:index+1])
}
}
text := ""
index := *pindex
firstlineno := rawLines[index].Lineno
var lineRawLines []*RawLine
interestingRawLines := rawLines[index:]
for i, rawLine := range interestingRawLines {
indent, rawText, outdent, cont := splitRawLine(rawLine.textnl)
if text == "" {
text += indent
}
text += rawText
lineRawLines = append(lineRawLines, rawLine)
if cont != "" && i != len(interestingRawLines)-1 {
text += " "
index++
} else {
text += outdent + cont
break
}
}
lastlineno := rawLines[index].Lineno
*pindex = index + 1
return NewLineMulti(fname, firstlineno, lastlineno, text, lineRawLines)
}
func splitRawLine(textnl string) (leadingWhitespace, text, trailingWhitespace, cont string) {
end := len(textnl)
if end-1 >= 0 && textnl[end-1] == '\n' {
end--
}
backslashes := 0
for end-1 >= 0 && textnl[end-1] == '\\' {
end--
backslashes++
}
cont = textnl[end : end+backslashes%2]
end += backslashes / 2
trailingEnd := end
for end-1 >= 0 && (textnl[end-1] == ' ' || textnl[end-1] == '\t') {
end--
}
trailingStart := end
trailingWhitespace = textnl[trailingStart:trailingEnd]
i := 0
leadingStart := i
for i < end && (textnl[i] == ' ' || textnl[i] == '\t') {
i++
}
leadingEnd := i
leadingWhitespace = textnl[leadingStart:leadingEnd]
text = textnl[leadingEnd:trailingStart]
return
}
func readLines(fname string, joinBackslashLines bool) ([]Line, error) {
rawText, err := ioutil.ReadFile(fname)
if err != nil {
return nil, err
}
return convertToLogicalLines(fname, string(rawText), joinBackslashLines), nil
}
func convertToLogicalLines(fname string, rawText string, joinBackslashLines bool) []Line {
var rawLines []*RawLine
for lineno, rawLine := range strings.SplitAfter(rawText, "\n") {
if rawLine != "" {
rawLines = append(rawLines, &RawLine{1 + lineno, rawLine, rawLine})
}
}
var loglines []Line
if joinBackslashLines {
for lineno := 0; lineno < len(rawLines); {
loglines = append(loglines, nextLogicalLine(fname, rawLines, &lineno))
}
} else {
for _, rawLine := range rawLines {
text := strings.TrimSuffix(rawLine.textnl, "\n")
logline := NewLine(fname, rawLine.Lineno, text, []*RawLine{rawLine})
loglines = append(loglines, logline)
}
}
if 0 < len(rawLines) && !hasSuffix(rawLines[len(rawLines)-1].textnl, "\n") {
NewLineEOF(fname).Errorf("File must end with a newline.")
}
return loglines
}
|