]>
Commit | Line | Data |
---|---|---|
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 | |
13 | package org.tianocore.migration;\r | |
14 | \r | |
15 | import java.util.regex.*;\r | |
16 | import java.io.*;\r | |
17 | \r | |
18 | public final class Critic {\r | |
19 | private static final Pattern ptnheadcomment = Pattern.compile("^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/",Pattern.DOTALL);\r | |
20 | private static final Pattern ptnfunccomment = Pattern.compile("([\\};\\/\">]\\s*)([\\w\\s]*?[_\\w][_\\w\\d]*\\s*\\([^\\)\\(]*\\)\\s*)\\/\\*\\+\\+(.*?)\\-\\-\\*\\/(\\s*.*?)([\\{;])",Pattern.DOTALL); // find function with {;">/ , may be unsafe\r | |
21 | //private static Pattern ptncommentstructure = Pattern.compile("\\/\\*\\+\\+\\s*Routine Description:\\s*(.*?)\\s*Arguments:\\s*(.*?)\\s*Returns:\\s*(.*?)\\s*\\-\\-\\*\\/",Pattern.DOTALL);\r | |
22 | private static final Pattern ptncommentequation = Pattern.compile("([^\\s]*)\\s+-\\s+(.*)\\s*");\r | |
23 | private static Matcher mtrcommentequation;\r | |
24 | private static final Pattern ptnnewcomment = Pattern.compile("(\\s*@(param|retval)\\s+[^\\s]+)\\s+(.*)");\r | |
25 | private static Matcher mtrnewcomment;\r | |
26 | \r | |
27 | private static final int totallinelength = 82;\r | |
28 | \r | |
29 | public static final void critic(String filepath) throws Exception {\r | |
30 | if (filepath.contains(".c") || filepath.contains(".h")) {\r | |
31 | BufferedReader rd = null;\r | |
32 | String line = null;\r | |
33 | StringBuffer templine = new StringBuffer();\r | |
34 | boolean incomment = false;\r | |
35 | \r | |
36 | System.out.println("Criticing " + filepath);\r | |
37 | String wholeline = Common.file2string(filepath);\r | |
38 | \r | |
39 | wholeline = wholeline.replaceAll("\t", " ");\r | |
40 | wholeline = Common.replaceAll(wholeline, ptnheadcomment, "/** @file$1**/");\r | |
41 | wholeline = Common.replaceAll(wholeline, ptnfunccomment, "$1/**$3**/$4$2$5");\r | |
42 | //wholeline = Common.replaceAll(wholeline, ptncommentstructure, "/**\n#%\n$1\n%#\n#%%\n$2\n%%#\n#%%%\n$3\n%%%#\n**/");\r | |
43 | \r | |
44 | // first scan\r | |
45 | boolean description = false;\r | |
46 | boolean arguments = false;\r | |
47 | boolean returns = false;\r | |
48 | boolean inequation = false;\r | |
49 | rd = new BufferedReader(new StringReader(wholeline));\r | |
50 | while ((line = rd.readLine()) != null) {\r | |
51 | if (line.matches("\\/\\*\\*")) {\r | |
52 | incomment = true;\r | |
53 | description = false;\r | |
54 | arguments = false;\r | |
55 | returns = false;\r | |
56 | templine.append(line + "\n");\r | |
57 | } else if (line.matches("\\*\\*\\/")) {\r | |
58 | incomment = false;\r | |
59 | templine.append(line + "\n");\r | |
60 | } else if (incomment) {\r | |
61 | if (line.contains("Routine Description:")) {\r | |
62 | description = true;\r | |
63 | arguments = false;\r | |
64 | returns = false;\r | |
65 | } else if (line.contains("Arguments:")) {\r | |
66 | description = false;\r | |
67 | arguments = true;\r | |
68 | returns = false;\r | |
69 | } else if (line.contains("Returns:")) {\r | |
70 | description = false;\r | |
71 | arguments = false;\r | |
72 | returns = true;\r | |
73 | } else if (description) {\r | |
74 | if (line.trim().length() != 0) {\r | |
75 | templine.append(" " + line.trim() + "\n");\r | |
76 | }\r | |
77 | } else if (arguments) {\r | |
78 | mtrcommentequation = ptncommentequation.matcher(line);\r | |
79 | if (mtrcommentequation.find()) {\r | |
80 | inequation = true;\r | |
81 | templine.append(" @param " + mtrcommentequation.group(1) + " " + mtrcommentequation.group(2) + "\n");\r | |
82 | } else if (inequation && line.trim().length() == 0) {\r | |
83 | inequation = false;\r | |
84 | templine.append(line + "\n");\r | |
85 | } else if (inequation && line.trim().length() != 0) {\r | |
86 | templine.append("#%#%" + line + "\n");\r | |
87 | } else {\r | |
88 | templine.append(" " + line.trim() + "\n");\r | |
89 | }\r | |
90 | } else if (returns) {\r | |
91 | mtrcommentequation = ptncommentequation.matcher(line);\r | |
92 | if (mtrcommentequation.find()) {\r | |
93 | inequation = true;\r | |
94 | templine.append(" @retval " + mtrcommentequation.group(1) + " " + mtrcommentequation.group(2) + "\n");\r | |
95 | } else if (inequation && line.trim().length() == 0) {\r | |
96 | inequation = false;\r | |
97 | templine.append(line + "\n");\r | |
98 | } else if (inequation && line.trim().length() != 0) {\r | |
99 | templine.append("#%#%" + line + "\n");\r | |
100 | } else {\r | |
101 | templine.append(" " + line.trim() + "\n");\r | |
102 | }\r | |
103 | }\r | |
104 | } else {\r | |
105 | templine.append(line + "\n");\r | |
106 | }\r | |
107 | }\r | |
108 | wholeline = templine.toString();\r | |
109 | wholeline = wholeline.replaceAll("\n#%#%\\s*", " ");\r | |
110 | //\r | |
111 | \r | |
112 | // secend scan\r | |
113 | int startmax = 0;\r | |
114 | rd = new BufferedReader(new StringReader(wholeline));\r | |
115 | while ((line = rd.readLine()) != null) {\r | |
116 | if (line.matches("\\/\\*\\*")) {\r | |
117 | incomment = true;\r | |
118 | templine.append(line + "\n");\r | |
119 | } else if (line.matches("\\*\\*\\/")) {\r | |
120 | incomment = false;\r | |
121 | templine.append(line + "\n");\r | |
122 | } else if (incomment) {\r | |
123 | mtrnewcomment = ptnnewcomment.matcher(line);\r | |
124 | if (mtrnewcomment.find()) {\r | |
125 | startmax = mtrnewcomment.group(1).length() > startmax ? mtrnewcomment.group(1).length() : startmax;\r | |
126 | }\r | |
127 | }\r | |
128 | }\r | |
129 | startmax++;\r | |
130 | //\r | |
131 | \r | |
132 | // third scan\r | |
133 | int n = 0;\r | |
134 | String temp = null;\r | |
135 | String[] tempcont = null;\r | |
136 | int count = 0;\r | |
137 | templine = new StringBuffer();\r | |
138 | rd = new BufferedReader(new StringReader(wholeline));\r | |
139 | while ((line = rd.readLine()) != null) {\r | |
140 | if (line.matches("\\/\\*\\*")) {\r | |
141 | incomment = true;\r | |
142 | templine.append(line + "\n");\r | |
143 | } else if (line.matches("\\*\\*\\/")) {\r | |
144 | incomment = false;\r | |
145 | templine.append(line + "\n");\r | |
146 | } else if (incomment) {\r | |
147 | mtrnewcomment = ptnnewcomment.matcher(line);\r | |
148 | if (mtrnewcomment.find()) {\r | |
149 | n = startmax - mtrnewcomment.group(1).length();\r | |
150 | templine.append(mtrnewcomment.group(1));\r | |
151 | while (n-- >= 0) {\r | |
152 | templine.append(" ");\r | |
153 | }\r | |
154 | temp = mtrnewcomment.group(3);\r | |
155 | tempcont = temp.split(" "); // use \\s+ ?\r | |
156 | \r | |
157 | count = 0;\r | |
158 | for (int i = 0; i < tempcont.length; i++) {\r | |
159 | count += tempcont[i].length();\r | |
160 | if (count <= (totallinelength - startmax)) {\r | |
161 | templine.append(tempcont[i] + " ");\r | |
162 | count += 1;\r | |
163 | } else {\r | |
164 | templine.append("\n");\r | |
165 | n = startmax;\r | |
166 | while (n-- >= 0) {\r | |
167 | templine.append(" ");\r | |
168 | }\r | |
169 | templine.append(tempcont[i] + " ");\r | |
170 | count = tempcont[i].length() + 1;\r | |
171 | }\r | |
172 | }\r | |
173 | templine.append("\n");\r | |
174 | } else {\r | |
175 | templine.append(line + "\n");\r | |
176 | }\r | |
177 | } else {\r | |
178 | templine.append(line + "\n");\r | |
179 | }\r | |
180 | }\r | |
181 | wholeline = templine.toString();\r | |
182 | //\r | |
183 | \r | |
184 | /* -----slow edition of replacefirst with stringbuffer-----\r | |
185 | line.append(wholeline);\r | |
186 | mtrfunccomment = ptnfunccomment.matcher(line);\r | |
187 | while (mtrfunccomment.find()) {\r | |
188 | line.replace(0, line.length()-1, mtrfunccomment.replaceFirst("$2$4$3$1$5"));\r | |
189 | }\r | |
190 | */\r | |
191 | /* -----slow edition of replacefirst with string-----\r | |
192 | while ((mtrfunccomment = ptnfunccomment.matcher(wholeline)).find()) {\r | |
193 | //funccomment = mtrfunccomment.group(2);\r | |
194 | //mtrcommentstructure = ptncommentstructure.matcher(funccomment);\r | |
195 | wholeline = mtrfunccomment.replaceFirst("$2$4$3$1$5");\r | |
196 | }\r | |
197 | */\r | |
198 | /*\r | |
199 | // edit func comment\r | |
200 | mtrtempcomment = ptntempcomment.matcher(wholeline);\r | |
201 | while (mtrtempcomment.find()) {\r | |
202 | System.out.println("-----------------------------");\r | |
203 | System.out.println(mtrtempcomment.group());\r | |
204 | System.out.println("-----------------------------");\r | |
205 | }\r | |
206 | */\r | |
207 | Common.string2file(wholeline, filepath);\r | |
208 | }\r | |
209 | }\r | |
210 | \r | |
211 | public static final void fireAt(String path) throws Exception {\r | |
212 | //Common.toDoAll(Common.dirCopy_(path), Critic.class.getMethod("critic", String.class), null, null, Common.FILE);\r | |
213 | Common.toDoAll(path, Critic.class.getMethod("critic", String.class), null, null, Common.FILE);\r | |
214 | //Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);\r | |
215 | System.out.println("Critic Done");\r | |
216 | }\r | |
217 | }\r | |
218 | //analyze func comment\r | |
219 | /*if (mtrcommentstructure.find()) {\r | |
220 | newcomment.append("/*++\n\n" + mtrcommentstructure.group(1) + "\n\n");\r | |
221 | \r | |
222 | //System.out.println("-------1-------");\r | |
223 | //System.out.println(mtrcommentstructure.group(1));\r | |
224 | \r | |
225 | // arg\r | |
226 | //System.out.println("-------2-------");\r | |
227 | //System.out.println(mtrcommentstructure.group(2));\r | |
228 | mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(2));\r | |
229 | while (mtrinfequation.find()) {\r | |
230 | newcomment.append("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");\r | |
231 | //System.out.println("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));\r | |
232 | }\r | |
233 | newcomment.append("\n");\r | |
234 | // return\r | |
235 | //System.out.println("-------3-------");\r | |
236 | //System.out.println(mtrcommentstructure.group(3));\r | |
237 | mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(3));\r | |
238 | while (mtrinfequation.find()) {\r | |
239 | newcomment.append("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");\r | |
240 | //System.out.println("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));\r | |
241 | }\r | |
242 | System.out.println(newcomment);\r | |
243 | } else {\r | |
244 | System.out.println("Error: Comment Style Incorrect");\r | |
245 | }*/ |