]>
Commit | Line | Data |
---|---|---|
8c4eeeb6 | 1 | /** @file\r |
2 | \r | |
3 | Copyright (c) 2006, Intel Corporation\r | |
4 | All rights reserved. This program and the accompanying materials\r | |
5 | are licensed and made available under the terms and conditions of the BSD License\r | |
6 | which accompanies this distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | **/\r | |
fed802b1 | 13 | package org.tianocore.migration;\r |
14 | \r | |
71f30e51 | 15 | import java.io.BufferedReader;\r |
16 | import java.io.StringReader;\r | |
17 | import java.util.regex.Matcher;\r | |
18 | import java.util.regex.Pattern;\r | |
fed802b1 | 19 | \r |
eee63a7b | 20 | public final class Critic {\r |
71f30e51 | 21 | public static final Pattern PTN_NEW_HEAD_COMMENT = Pattern.compile(\r |
22 | "^\\/\\*\\*.*?\\*\\*\\/", Pattern.DOTALL);\r | |
23 | \r | |
24 | private static final Pattern ptnheadcomment = Pattern.compile(\r | |
25 | "^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/", Pattern.DOTALL);\r | |
26 | \r | |
27 | private static final Pattern ptnfunccomment = Pattern\r | |
28 | .compile(\r | |
29 | "([\\};\\/\">]\\s*)([\\w\\s\\*]*?[_\\w][_\\w\\d]*\\s*\\([^\\)\\(]*\\)\\s*)\\/\\*\\+\\+(.*?)\\-\\-\\*\\/\\s*(.*?)(?=[\\{;])",\r | |
30 | Pattern.DOTALL); // find function with {;">/ , may be\r | |
31 | // unsafe\r | |
32 | \r | |
33 | // private static Pattern ptncommentstructure =\r | |
34 | // Pattern.compile("\\/\\*\\+\\+\\s*Routine\r | |
35 | // Description:\\s*(.*?)\\s*Arguments:\\s*(.*?)\\s*Returns:\\s*(.*?)\\s*\\-\\-\\*\\/",Pattern.DOTALL);\r | |
36 | private static final Pattern ptncommentequation = Pattern\r | |
37 | .compile("([^\\s]*)\\s+-\\s+(.*)\\s*");\r | |
38 | \r | |
39 | private static Matcher mtrcommentequation;\r | |
40 | \r | |
41 | private static final Pattern ptnnewcomment = Pattern\r | |
42 | .compile("(\\s*@(param|retval)\\s+[^\\s]+)\\s+(.*)");\r | |
43 | \r | |
44 | private static Matcher mtrnewcomment;\r | |
45 | \r | |
46 | private static final int totallinelength = 82;\r | |
47 | \r | |
48 | public static final void run(String filepath) throws Exception {\r | |
49 | if (MigrationTool.doCritic) { // this is left here to set an example\r | |
50 | // for future structure\r | |
51 | critic(filepath);\r | |
52 | }\r | |
53 | }\r | |
54 | \r | |
55 | private static final void critic(String filepath) throws Exception {\r | |
56 | if (filepath.contains(".c") || filepath.contains(".h")) {\r | |
57 | BufferedReader rd = null;\r | |
58 | String line = null;\r | |
59 | StringBuffer templine = new StringBuffer();\r | |
60 | boolean incomment = false;\r | |
61 | \r | |
62 | System.out.println("Criticing " + filepath);\r | |
63 | String wholeline = Common.file2string(filepath);\r | |
64 | \r | |
65 | wholeline = wholeline.replaceAll("\t", " ");\r | |
66 | wholeline = Common.replaceAll(wholeline, ptnheadcomment,\r | |
67 | "/** @file$1**/");\r | |
68 | wholeline = Common.replaceAll(wholeline, ptnfunccomment,\r | |
69 | "$1\n/**$3\n**/\n$4$2");\r | |
70 | // wholeline = Common.replaceAll(wholeline, ptncommentstructure,\r | |
71 | // "/**\n#%\n$1\n%#\n#%%\n$2\n%%#\n#%%%\n$3\n%%%#\n**/");\r | |
72 | \r | |
73 | // first scan\r | |
74 | boolean description = false;\r | |
75 | boolean arguments = false;\r | |
76 | boolean returns = false;\r | |
77 | boolean inequation = false;\r | |
78 | rd = new BufferedReader(new StringReader(wholeline));\r | |
79 | while ((line = rd.readLine()) != null) {\r | |
80 | if (line.matches("\\/\\*\\*")) {\r | |
81 | incomment = true;\r | |
82 | description = false;\r | |
83 | arguments = false;\r | |
84 | returns = false;\r | |
85 | templine.append(line + "\n");\r | |
86 | } else if (line.matches("\\*\\*\\/")) {\r | |
87 | incomment = false;\r | |
88 | templine.append("\n" + line + "\n");\r | |
89 | } else if (incomment) {\r | |
90 | if (line.contains("Routine Description:")) {\r | |
91 | description = true;\r | |
92 | arguments = false;\r | |
93 | returns = false;\r | |
94 | } else if (line.contains("Arguments:")) {\r | |
95 | description = false;\r | |
96 | arguments = true;\r | |
97 | returns = false;\r | |
98 | templine.append("\n");\r | |
99 | } else if (line.contains("Returns:")) {\r | |
100 | description = false;\r | |
101 | arguments = false;\r | |
102 | returns = true;\r | |
103 | templine.append("\n");\r | |
104 | } else if (description) {\r | |
105 | if (line.trim().length() != 0) {\r | |
106 | templine.append(" " + line.trim() + "\n");\r | |
107 | }\r | |
108 | } else if (arguments) {\r | |
109 | mtrcommentequation = ptncommentequation.matcher(line);\r | |
110 | if (mtrcommentequation.find()) {\r | |
111 | inequation = true;\r | |
112 | templine.append(" @param "\r | |
113 | + mtrcommentequation.group(1) + " "\r | |
114 | + mtrcommentequation.group(2) + "\n");\r | |
115 | } else if (inequation && line.trim().length() == 0) {\r | |
116 | inequation = false;\r | |
117 | } else if (inequation && line.trim().length() != 0) {\r | |
118 | templine.append("#%#%" + line + "\n");\r | |
119 | } else {\r | |
120 | if (line.trim().length() != 0) {\r | |
121 | templine.append(" " + line.trim() + "\n");\r | |
122 | }\r | |
123 | }\r | |
124 | } else if (returns) {\r | |
125 | mtrcommentequation = ptncommentequation.matcher(line);\r | |
126 | if (mtrcommentequation.find()) {\r | |
127 | inequation = true;\r | |
128 | templine.append(" @retval "\r | |
129 | + mtrcommentequation.group(1) + " "\r | |
130 | + mtrcommentequation.group(2) + "\n");\r | |
131 | } else if (inequation && line.trim().length() == 0) {\r | |
132 | inequation = false;\r | |
133 | } else if (inequation && line.trim().length() != 0) {\r | |
134 | templine.append("#%#%" + line + "\n");\r | |
135 | } else {\r | |
136 | if (line.trim().length() != 0) {\r | |
137 | templine.append(" @return " + line.trim()\r | |
138 | + "\n");\r | |
139 | }\r | |
140 | }\r | |
141 | }\r | |
142 | } else {\r | |
143 | templine.append(line + "\n");\r | |
144 | }\r | |
145 | }\r | |
146 | wholeline = templine.toString();\r | |
147 | wholeline = wholeline.replaceAll("\n#%#%\\s*", " ");\r | |
148 | //\r | |
149 | \r | |
150 | // secend scan\r | |
151 | int startmax = 0;\r | |
152 | rd = new BufferedReader(new StringReader(wholeline));\r | |
153 | while ((line = rd.readLine()) != null) {\r | |
154 | if (line.matches("\\/\\*\\*")) {\r | |
155 | incomment = true;\r | |
156 | templine.append(line + "\n");\r | |
157 | } else if (line.matches("\\*\\*\\/")) {\r | |
158 | incomment = false;\r | |
159 | templine.append(line + "\n");\r | |
160 | } else if (incomment) {\r | |
161 | mtrnewcomment = ptnnewcomment.matcher(line);\r | |
162 | if (mtrnewcomment.find()) {\r | |
163 | startmax = mtrnewcomment.group(1).length() > startmax ? mtrnewcomment\r | |
164 | .group(1).length()\r | |
165 | : startmax;\r | |
166 | }\r | |
167 | }\r | |
168 | }\r | |
169 | startmax++;\r | |
170 | //\r | |
171 | \r | |
172 | // third scan\r | |
173 | int n = 0;\r | |
174 | String temp = null;\r | |
175 | String[] tempcont = null;\r | |
176 | int count = 0;\r | |
177 | templine = new StringBuffer();\r | |
178 | rd = new BufferedReader(new StringReader(wholeline));\r | |
179 | while ((line = rd.readLine()) != null) {\r | |
180 | if (line.matches("\\/\\*\\*")) {\r | |
181 | incomment = true;\r | |
182 | templine.append(line + "\n");\r | |
183 | } else if (line.matches("\\*\\*\\/")) {\r | |
184 | incomment = false;\r | |
185 | templine.append(line + "\n");\r | |
186 | } else if (incomment) {\r | |
187 | mtrnewcomment = ptnnewcomment.matcher(line);\r | |
188 | if (mtrnewcomment.find()) {\r | |
189 | n = startmax - mtrnewcomment.group(1).length();\r | |
190 | templine.append(mtrnewcomment.group(1));\r | |
191 | while (n-- >= 0) {\r | |
192 | templine.append(" ");\r | |
193 | }\r | |
194 | temp = mtrnewcomment.group(3);\r | |
195 | tempcont = temp.split(" "); // use \\s+ ?\r | |
196 | \r | |
197 | count = 0;\r | |
198 | for (int i = 0; i < tempcont.length; i++) {\r | |
199 | count += tempcont[i].length();\r | |
200 | if (count <= (totallinelength - startmax)) {\r | |
201 | templine.append(tempcont[i] + " ");\r | |
202 | count += 1;\r | |
203 | } else {\r | |
204 | templine.append("\n");\r | |
205 | n = startmax;\r | |
206 | while (n-- >= 0) {\r | |
207 | templine.append(" ");\r | |
208 | }\r | |
209 | templine.append(tempcont[i] + " ");\r | |
210 | count = tempcont[i].length() + 1;\r | |
211 | }\r | |
212 | }\r | |
213 | templine.append("\n");\r | |
214 | } else {\r | |
215 | templine.append(line + "\n");\r | |
216 | }\r | |
217 | } else {\r | |
218 | templine.append(line + "\n");\r | |
219 | }\r | |
220 | }\r | |
221 | wholeline = templine.toString();\r | |
222 | //\r | |
223 | // Remove trailing blanks.\r | |
224 | // \r | |
225 | wholeline = wholeline.replaceAll(" +\n", "\n");\r | |
226 | Common.string2file(wholeline, filepath);\r | |
227 | }\r | |
228 | }\r | |
229 | \r | |
230 | public static final void fireAt(String path) throws Exception {\r | |
231 | // Common.toDoAll(Common.dirCopy_(path),\r | |
232 | // Critic.class.getMethod("critic", String.class), null, null,\r | |
233 | // Common.FILE);\r | |
234 | Common.toDoAll(path, Critic.class.getMethod("run", String.class), null,\r | |
235 | null, Common.FILE);\r | |
236 | // Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);\r | |
237 | System.out.println("Critic Done");\r | |
238 | }\r | |
6f55be9f | 239 | } |