Revert broken changes.
[mono.git] / mcs / tools / monostyle.cs
1 //      monostyle.cs
2 //
3 //      Adam Treat (manyoso@yahoo.com)
4 //      (C) 2002 Adam Treat
5 //
6 //      This program is free software; you can redistribute it and/or modify
7 //      it under the terms of the GNU General Public License as published by
8 //      the Free Software Foundation; either version 2 of the License, or
9 //      (at your option) any later version.
10
11 using System;
12 using System.IO;
13 using System.Text.RegularExpressions;
14 using System.Collections.Specialized;
15
16 namespace Mono.Util {
17
18         class MonoStyle {
19
20                 string file;
21                 StringCollection filebuffer;
22                 bool linespace = true;
23
24                 void Usage()
25                 {
26                         Console.Write (
27                                 "monostyle -f file.cs -l <true|false> > output.cs\n\n" +
28                                 "   -f || /-f || --file  file.cs        The csharp source file to parse.\n\n" +
29                                 "   -l || /-l || --line  <true|false>   Specifies wether to use line spacing.\n\n");
30                 }
31
32                 public static void Main (string[] args)
33                 {
34                         MonoStyle style = new MonoStyle(args);
35                 }
36
37                 public MonoStyle (string[] args)
38                 {
39                         int argc = args.Length;
40                         for(int i = 0; i < argc; i++) {
41                                 string arg = args[i];
42                                 // The "/" switch is there for wine users, like me ;-)
43                                 if(arg.StartsWith("-") || arg.StartsWith("/")) {
44                                         switch(arg) {
45                                                 case "-l": case "/-l": case "--line":
46                                                 if((i + 1) >= argc) {
47                                                         Usage();
48                                                         return;
49                                                 }
50                                                 if (args[i++] == "false") {
51                                                         linespace = false;
52                                                 }
53                                                 continue;
54                                                 case "-f": case "/-f": case "--file":
55                                                 if((i + 1) >= argc) {
56                                                         Usage();
57                                                         return;
58                                                 }
59                                                 file = args[++i];
60                                                 continue;
61                                                 default:
62                                                 Usage();
63                                                 return;
64                                         }
65                                 }
66                         }
67                         if(file == null) {
68                                 Usage();
69                                 return;
70                         }
71                         filebuffer = new StringCollection();
72                         StreamReader sr = new StreamReader(file);
73                         FillBuffer(sr);
74                         FixMonoStyle();
75                         PrintToConsole();
76                 }
77
78                 public void FillBuffer(StreamReader sr)
79                 {
80                         sr.BaseStream.Seek(0, SeekOrigin.Begin);
81                         while (sr.Peek() > -1) {
82                                 filebuffer.Add(sr.ReadLine());
83                         }
84                         sr.Close();
85                 }
86
87                 public void FixMonoStyle()
88                 {
89                         for (int i=0; i < filebuffer.Count; i++) {
90                                 IsBadMonoStyle(filebuffer[i]);
91                         }
92                 }
93
94                 public void PrintToConsole()
95                 {
96                         for (int i=0; i < filebuffer.Count; i++) {
97                                 Console.WriteLine(filebuffer[i]);
98                         }
99                 }
100
101                 public void IsBadMonoStyle(String str)
102                 {
103                         if (IsBadMonoType(str)) {
104                                 FixHangingBrace(str);
105                         } else if(IsBadMonoFlow(str)) {
106                                 FixHangingBrace(str);
107                         } else if(IsBadMonoFunction(str)) {
108                                 FixEndBrace(str);
109                         } else if(IsBadMonoProperty(str)) {
110                                 FixHangingBrace(str);
111                         } else {
112                         }
113                 }
114
115                 public void FixHangingBrace(String str)
116                 {
117                         int strloc = filebuffer.IndexOf(str);
118                         int brcloc = FindHangingBrace(strloc);
119                         int diff = brcloc - strloc;
120                         if (brcloc > 0) {
121                                 for (int i = 0; i < diff+1; i++) {
122                                         filebuffer.RemoveAt(strloc);
123                                 }
124                                 filebuffer.Insert(strloc, str + " {");
125                                 if (linespace) {
126                                         filebuffer.Insert(strloc+1, "");
127                                 }
128                         } else {}
129                 }
130
131                 public int FindHangingBrace(int strloc)
132                 {
133                         strloc++;
134                         bool found = false;
135                         while (!found) {
136                                 try {
137                                         string str = filebuffer[strloc++];
138                                         found = IsHangingBrace(str);
139                                         if (!found && !IsBlankLine(str)) {
140                                                 return -1;
141                                         }
142                                  } catch (Exception) {
143                                         return -1;
144                                 }
145                         }
146                         return strloc -1;
147                 }
148
149                 public void FixEndBrace(String str)
150                 {
151                         int strloc = filebuffer.IndexOf(str);
152                         filebuffer.RemoveAt(strloc);
153                         filebuffer.Insert(strloc, RemoveEndBrace(str));
154                         filebuffer.Insert(strloc+1, AddHangingBrace(str));
155                 }
156
157                 public static bool IsBadMonoType(String str)
158                 {
159                         if ( IsType(str) && !EndWithBrace(str)) {
160                                 return true;
161                         } else {
162                                 return false;
163                         }
164                 }
165
166                 public static bool IsBadMonoFlow(String str)
167                 {
168                         if (IsFlow(str) && !EndWithBrace(str)) {
169                                 return true;
170                         } else {
171                                 return false;
172                         }
173                 }
174
175                 public static bool IsBadMonoFunction(String str)
176                 {
177                         if (IsFunction(str) && EndWithBrace(str)) {
178                                 return true;
179                         } else {
180                                 return false;
181                         }
182                 }
183
184                 public static bool IsBadMonoProperty(String str)
185                 {
186                         if (IsProperty(str) && !EndWithBrace(str)) {
187                                 return true;
188                         } else {
189                                 return false;
190                         }
191                 }
192
193                 public static bool IsType(String str)
194                 {
195                         if (    !IsComment(str) && (
196                                         IsNameSpace(str) ||
197                                         IsClass(str) ||
198                                         IsStruct(str) ||
199                                         IsEnum(str) )) {
200                                 return true;
201                         } else {
202                                 return false;
203                         }
204                 }
205
206                 public static bool IsFlow(String str)
207                 {
208                         if (    !IsComment(str) && (
209                                         IsIf(str) ||
210                                         IsElse(str) ||
211                                         IsElseIf(str) ||
212                                         IsTry(str) ||
213                                         IsCatch(str) ||
214                                         IsFinally(str) ||
215                                         IsFor(str) ||
216                                         IsForEach(str) ||
217                                         IsWhile(str) ||
218                                         IsSwitch(str) ||
219                                         IsCase(str) )) {
220                                 return true;
221                         } else {
222                                 return false;
223                         }
224                 }
225
226                 public static bool IsFunction(String str)
227                 {
228                         if (    Regex.IsMatch(str, @"^\s*(\w+)\s+(\w+).*\(+") &&
229                                         !IsDeclaration(str) &&
230                                         !IsComment(str) &&
231                                         !IsType(str) &&
232                                         !IsFlow(str) ) {
233                                 return true;
234                         } else {
235                                 return false;
236                         }
237                 }
238
239                 public static bool IsProperty(String str)
240                 {
241                         if (    Regex.IsMatch(str, @"^\s*(\w+)\s+(\w+).*") &&
242                                         !IsDeclaration(str) &&
243                                         !IsComment(str) &&
244                                         !IsType(str) &&
245                                         !IsFlow(str) &&
246                                         !IsFunction(str) ) {
247                                 return true;
248                         } else {
249                                 return false;
250                         }
251                 }
252
253                 public static string RemoveEndBrace(String str)
254                 {
255                         Regex rg = new Regex(@"\{\s*$");
256                         return rg.Replace(str, "");
257                 }
258
259                 public static string AddHangingBrace(String str)
260                 {
261                         Regex rg = new Regex(@"\S+\s*");
262                         string blank = rg.Replace(str,"");
263                         return blank + "{";
264                 }
265
266                 public static bool IsDeclaration(String str)
267                 {
268                         return Regex.IsMatch(str, @"\;\s*$");
269                 }
270
271                 public static bool IsComment(String str)
272                 {
273                         return Regex.IsMatch(str, @"^(\s*\/+|\s*\*+|\s*\#+)");
274                 }
275
276                 public static bool EndWithBrace(String str)
277                 {
278                         return Regex.IsMatch(str, @"\{\s*$");
279                 }
280
281                 public static bool IsHangingBrace(String str)
282                 {
283                         return Regex.IsMatch(str, @"(^|\s+)\{");
284                 }
285
286                 public static bool IsBlankLine(String str)
287                 {
288                         return Regex.IsMatch(str, @"^\s*$");
289                 }
290
291                 public static bool IsNameSpace(String str)
292                 {
293                         return Regex.IsMatch(str, @"(^|\s+)namespace\s+");
294                 }
295
296                 public static bool IsClass(String str)
297                 {
298                         return Regex.IsMatch(str, @"\s+class\s+");
299                 }
300
301                 public static bool IsStruct(String str)
302                 {
303                         return Regex.IsMatch(str, @"\s+struct\s+");
304                 }
305
306                 public static bool IsEnum(String str)
307                 {
308                         return Regex.IsMatch(str, @"\s+enum\s+");
309                 }
310
311                 public static bool IsIf(String str)
312                 {
313                         return Regex.IsMatch(str, @"(^|\s+|\}+)if(\s+|\(+|$)");
314                 }
315
316                 public static bool IsElse(String str)
317                 {
318                         return Regex.IsMatch(str, @"(^|\s+|\}+)else(\s+|\{+|$)");
319                 }
320
321                 public static bool IsElseIf(String str)
322                 {
323                         return Regex.IsMatch(str, @"(^|\s+|\}+)else if(\s+|\(+|$)");
324                 }
325
326                 public static bool IsTry(String str)
327                 {
328                         return Regex.IsMatch(str, @"(^|\s+|\}+)try(\s+|\(+|$)");
329                 }
330
331                 public static bool IsCatch(String str)
332                 {
333                         return Regex.IsMatch(str, @"(^|\s+|\}+)catch(\s+|\(+|$)");
334                 }
335
336                 public static bool IsFinally(String str)
337                 {
338                         return Regex.IsMatch(str, @"(^|\s+|\}+)finally(\s+|\{+|$)");
339                 }
340
341                 public static bool IsFor(String str)
342                 {
343                         return Regex.IsMatch(str, @"(^|\s+|\}+)for(\s+|\(+|$)");
344                 }
345
346                 public static bool IsForEach(String str)
347                 {
348                         return Regex.IsMatch(str, @"(^|\s+|\}+)foreach(\s+|\(+|$)");
349                 }
350
351                 public static bool IsWhile(String str)
352                 {
353                         return Regex.IsMatch(str, @"(^|\s+|\}+)while(\s+|\(+|$)");
354                 }
355
356                 public static bool IsSwitch(String str)
357                 {
358                         return Regex.IsMatch(str, @"(^|\s+|\}+)switch(\s+|\(+|$)");
359                 }
360
361                 public static bool IsCase(String str)
362                 {
363                         return Regex.IsMatch(str, @"(^|\s+|\}+)case(\s+|\(+|$)");
364                 }
365         }
366 }