summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/MigrationTools/org/tianocore/migration/Critic.java
blob: e0e48a9671c99c02e6ae6fefe7f5aa689eb24d7d (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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/** @file
 
 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
 
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
 **/
package org.tianocore.migration;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class Critic {
	public static final Pattern PTN_NEW_HEAD_COMMENT = Pattern.compile(
			"^\\/\\*\\*.*?\\*\\*\\/", Pattern.DOTALL);

	private static final Pattern ptnheadcomment = Pattern.compile(
			"^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/", Pattern.DOTALL);

	private static final Pattern ptnfunccomment = Pattern
			.compile(
					"([\\};\\/\">]\\s*)([\\w\\s\\*]*?[_\\w][_\\w\\d]*\\s*\\([^\\)\\(]*\\)\\s*)\\/\\*\\+\\+(.*?)\\-\\-\\*\\/\\s*(.*?)(?=[\\{;])",
					Pattern.DOTALL); // find function with {;">/ , may be
										// unsafe

	// private static Pattern ptncommentstructure =
	// Pattern.compile("\\/\\*\\+\\+\\s*Routine
	// Description:\\s*(.*?)\\s*Arguments:\\s*(.*?)\\s*Returns:\\s*(.*?)\\s*\\-\\-\\*\\/",Pattern.DOTALL);
	private static final Pattern ptncommentequation = Pattern
			.compile("([^\\s]*)\\s+-\\s+(.*)\\s*");

	private static Matcher mtrcommentequation;

	private static final Pattern ptnnewcomment = Pattern
			.compile("(\\s*@(param|retval)\\s+[^\\s]+)\\s+(.*)");

	private static Matcher mtrnewcomment;

	private static final int totallinelength = 82;

	public static final void run(String filepath) throws Exception {
		if (MigrationTool.doCritic) { // this is left here to set an example
										// for future structure
			critic(filepath);
		}
	}

	private static final void critic(String filepath) throws Exception {
		if (filepath.contains(".c") || filepath.contains(".h")) {
			BufferedReader rd = null;
			String line = null;
			StringBuffer templine = new StringBuffer();
			boolean incomment = false;

			System.out.println("Criticing   " + filepath);
			String wholeline = Common.file2string(filepath);

			wholeline = wholeline.replaceAll("\t", "  ");
			wholeline = Common.replaceAll(wholeline, ptnheadcomment,
					"/** @file$1**/");
			wholeline = Common.replaceAll(wholeline, ptnfunccomment,
					"$1\n/**$3\n**/\n$4$2");
			// wholeline = Common.replaceAll(wholeline, ptncommentstructure,
			// "/**\n#%\n$1\n%#\n#%%\n$2\n%%#\n#%%%\n$3\n%%%#\n**/");

			// first scan
			boolean description = false;
			boolean arguments = false;
			boolean returns = false;
			boolean inequation = false;
			rd = new BufferedReader(new StringReader(wholeline));
			while ((line = rd.readLine()) != null) {
				if (line.matches("\\/\\*\\*")) {
					incomment = true;
					description = false;
					arguments = false;
					returns = false;
					templine.append(line + "\n");
				} else if (line.matches("\\*\\*\\/")) {
					incomment = false;
					templine.append("\n" + line + "\n");
				} else if (incomment) {
					if (line.contains("Routine Description:")) {
						description = true;
						arguments = false;
						returns = false;
					} else if (line.contains("Arguments:")) {
						description = false;
						arguments = true;
						returns = false;
						templine.append("\n");
					} else if (line.contains("Returns:")) {
						description = false;
						arguments = false;
						returns = true;
						templine.append("\n");
					} else if (description) {
						if (line.trim().length() != 0) {
							templine.append("  " + line.trim() + "\n");
						}
					} else if (arguments) {
						mtrcommentequation = ptncommentequation.matcher(line);
						if (mtrcommentequation.find()) {
							inequation = true;
							templine.append("  @param  "
									+ mtrcommentequation.group(1) + "     "
									+ mtrcommentequation.group(2) + "\n");
						} else if (inequation && line.trim().length() == 0) {
							inequation = false;
						} else if (inequation && line.trim().length() != 0) {
							templine.append("#%#%" + line + "\n");
						} else {
							if (line.trim().length() != 0) {
								templine.append("  " + line.trim() + "\n");
							}
						}
					} else if (returns) {
						mtrcommentequation = ptncommentequation.matcher(line);
						if (mtrcommentequation.find()) {
							inequation = true;
							templine.append("  @retval "
									+ mtrcommentequation.group(1) + "     "
									+ mtrcommentequation.group(2) + "\n");
						} else if (inequation && line.trim().length() == 0) {
							inequation = false;
						} else if (inequation && line.trim().length() != 0) {
							templine.append("#%#%" + line + "\n");
						} else {
							if (line.trim().length() != 0) {
								templine.append("  @return " + line.trim()
										+ "\n");
							}
						}
					}
				} else {
					templine.append(line + "\n");
				}
			}
			wholeline = templine.toString();
			wholeline = wholeline.replaceAll("\n#%#%\\s*", " ");
			//

			// secend scan
			int startmax = 0;
			rd = new BufferedReader(new StringReader(wholeline));
			while ((line = rd.readLine()) != null) {
				if (line.matches("\\/\\*\\*")) {
					incomment = true;
					templine.append(line + "\n");
				} else if (line.matches("\\*\\*\\/")) {
					incomment = false;
					templine.append(line + "\n");
				} else if (incomment) {
					mtrnewcomment = ptnnewcomment.matcher(line);
					if (mtrnewcomment.find()) {
						startmax = mtrnewcomment.group(1).length() > startmax ? mtrnewcomment
								.group(1).length()
								: startmax;
					}
				}
			}
			startmax++;
			//

			// third scan
			int n = 0;
			String temp = null;
			String[] tempcont = null;
			int count = 0;
			templine = new StringBuffer();
			rd = new BufferedReader(new StringReader(wholeline));
			while ((line = rd.readLine()) != null) {
				if (line.matches("\\/\\*\\*")) {
					incomment = true;
					templine.append(line + "\n");
				} else if (line.matches("\\*\\*\\/")) {
					incomment = false;
					templine.append(line + "\n");
				} else if (incomment) {
					mtrnewcomment = ptnnewcomment.matcher(line);
					if (mtrnewcomment.find()) {
						n = startmax - mtrnewcomment.group(1).length();
						templine.append(mtrnewcomment.group(1));
						while (n-- >= 0) {
							templine.append(" ");
						}
						temp = mtrnewcomment.group(3);
						tempcont = temp.split(" "); // use \\s+ ?

						count = 0;
						for (int i = 0; i < tempcont.length; i++) {
							count += tempcont[i].length();
							if (count <= (totallinelength - startmax)) {
								templine.append(tempcont[i] + " ");
								count += 1;
							} else {
								templine.append("\n");
								n = startmax;
								while (n-- >= 0) {
									templine.append(" ");
								}
								templine.append(tempcont[i] + " ");
								count = tempcont[i].length() + 1;
							}
						}
						templine.append("\n");
					} else {
						templine.append(line + "\n");
					}
				} else {
					templine.append(line + "\n");
				}
			}
			wholeline = templine.toString();
			//
			// Remove trailing blanks.
			// 
			wholeline = wholeline.replaceAll(" +\n", "\n");
			Common.string2file(wholeline, filepath);
		}
	}

	public static final void fireAt(String path) throws Exception {
		// Common.toDoAll(Common.dirCopy_(path),
		// Critic.class.getMethod("critic", String.class), null, null,
		// Common.FILE);
		Common.toDoAll(path, Critic.class.getMethod("run", String.class), null,
				null, Common.FILE);
		// Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);
		System.out.println("Critic Done");
	}
}