-
Notifications
You must be signed in to change notification settings - Fork 8
/
myfmt.go
168 lines (146 loc) · 3.04 KB
/
myfmt.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"os";
"fmt";
"bufio";
"strings";
"regexp";
)
var input = bufio.NewReader(os.Stdin)
var braces = 0
var parens = 0
var comment = false
var backslash = false
var laststar = false
var statements = 0
var wordrx = regexp.MustCompile("(^|[^A-Za-z0-9_])(if|else|while|for|return)($|[^A-Za-z0-9_])")
var stringrx = regexp.MustCompile(`("([^"]|\\")*")|('\\?.')`);
func printIndent(count int) {
if count < 0 {
count = 0
}
}
func removeComments(line string) string {
var rem string;
x := strings.Index(line, "//");
if x > -1 {
line = line[x:len(line)]
}
if !comment {
x = strings.Index(line, "/*");
if x > -1 {
rem = line[x:];
line = line[0:x];
x = strings.Index(rem, "*/");
if x > -1 {
rem = rem[x+2:];
line = strings.Join([]string{line, rem}, " ");
comment = false;
} else {
comment = true
}
}
} else {
x = strings.Index(line, "*/");
if x > -1 {
line = line[x+2:];
comment = false;
} else {
line = ""
}
}
return line;
}
func formatLine(line string) {
// leave macros alone
if strings.HasSuffix(line, "\\\n") {
backslash = true;
fmt.Print(line);
return
}
if backslash {
backslash = false;
fmt.Print(line);
return
}
line = strings.TrimSpace(line);
if strings.HasPrefix(line, "#") {
fmt.Println(line);
return
}
code := line;
code = stringrx.ReplaceAllString(code, "\"X\"");
code = removeComments(code);
obraces := strings.Count(code, "{");
cbraces := strings.Count(code, "}");
oparens := strings.Count(code, "(");
cparens := strings.Count(code, ")");
depth := braces;
if obraces != cbraces && strings.HasPrefix(code, "}") {
depth -= cbraces
}
// indent after statement before block or ;
if strings.Count(code, "{") > 0 || strings.Count(code, "}") > 0 {
statements = 0
}
depth += statements;
if wordrx.MatchString(code) {
statements ++;
}
if strings.Count(code, "{") > 0 || strings.Count(code, "}") > 0 {
statements = 0
}
if strings.HasSuffix(code, ";") {
statements = 0
}
// inside a multiline parenthesis (function params, call, if case)
if parens > 0 && statements == 0 {
depth++
}
// case: default: and other labels
if strings.HasPrefix(code, "case") && strings.Count(code, ":") > 0 {
depth--
} else if strings.HasPrefix(code, "default") && strings.Count(code, ":") > 0 {
depth--
} else if strings.HasSuffix(code, ":") {
depth = 0
}
if depth < 0 {
depth = 0
}
if len(line) > 0 {
for i := 0; i < depth; i++ {
fmt.Print("\t")
}
}
// multi line star comment
if laststar && strings.HasPrefix(line, "*/") {
fmt.Print(" ")
}
if comment && len(line) > 0 && line[0] == '*' {
laststar = true;
fmt.Print(" ")
} else {
laststar = false
}
// print the line!
fmt.Println(line);
braces += obraces - cbraces;
parens += oparens - cparens;
}
func main() {
for {
line, error := input.ReadString('\n');
if error == os.EOF && len(line) == 0 {
os.Exit(0)
}
formatLine(line);
if error == os.EOF {
os.Exit(0)
}
if error != nil {
fmt.Fprintf(os.Stderr, "error: %s", error);
os.Exit(1);
}
}
}