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 |
15 | import java.util.regex.*;\r |
3637381b |
16 | import java.io.*;\r |
fed802b1 |
17 | \r |
eee63a7b |
18 | public final class Critic {\r |
27e0221a |
19 | public static final Pattern PTN_NEW_HEAD_COMMENT = Pattern.compile("^\\/\\*\\*.*?\\*\\*\\/",Pattern.DOTALL);\r |
20 | private static final Pattern ptnheadcomment = Pattern.compile("^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/",Pattern.DOTALL);\r |
21 | 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 |
22 | //private static Pattern ptncommentstructure = Pattern.compile("\\/\\*\\+\\+\\s*Routine Description:\\s*(.*?)\\s*Arguments:\\s*(.*?)\\s*Returns:\\s*(.*?)\\s*\\-\\-\\*\\/",Pattern.DOTALL);\r |
23 | private static final Pattern ptncommentequation = Pattern.compile("([^\\s]*)\\s+-\\s+(.*)\\s*");\r |
24 | private static Matcher mtrcommentequation;\r |
25 | private static final Pattern ptnnewcomment = Pattern.compile("(\\s*@(param|retval)\\s+[^\\s]+)\\s+(.*)");\r |
26 | private static Matcher mtrnewcomment;\r |
27 | \r |
28 | private static final int totallinelength = 82;\r |
29 | \r |
30 | public static final void run(String filepath) throws Exception {\r |
31 | if (MigrationTool.doCritic) { // this is left here to set an example for future structure\r |
32 | critic(filepath);\r |
33 | }\r |
34 | }\r |
35 | \r |
36 | private static final void critic(String filepath) throws Exception {\r |
37 | if (filepath.contains(".c") || filepath.contains(".h")) {\r |
38 | BufferedReader rd = null;\r |
39 | String line = null;\r |
40 | StringBuffer templine = new StringBuffer();\r |
41 | boolean incomment = false;\r |
63406ead |
42 | \r |
27e0221a |
43 | System.out.println("Criticing " + filepath);\r |
44 | String wholeline = Common.file2string(filepath);\r |
63406ead |
45 | \r |
27e0221a |
46 | wholeline = wholeline.replaceAll("\t", " ");\r |
47 | wholeline = Common.replaceAll(wholeline, ptnheadcomment, "/** @file$1**/");\r |
10e4f990 |
48 | wholeline = Common.replaceAll(wholeline, ptnfunccomment, "$1\n/**$3\n**/$4$2");\r |
27e0221a |
49 | //wholeline = Common.replaceAll(wholeline, ptncommentstructure, "/**\n#%\n$1\n%#\n#%%\n$2\n%%#\n#%%%\n$3\n%%%#\n**/");\r |
5ea254f6 |
50 | \r |
27e0221a |
51 | // first scan\r |
52 | boolean description = false;\r |
53 | boolean arguments = false;\r |
54 | boolean returns = false;\r |
55 | boolean inequation = false;\r |
56 | rd = new BufferedReader(new StringReader(wholeline));\r |
57 | while ((line = rd.readLine()) != null) {\r |
58 | if (line.matches("\\/\\*\\*")) {\r |
59 | incomment = true;\r |
60 | description = false;\r |
61 | arguments = false;\r |
62 | returns = false;\r |
63 | templine.append(line + "\n");\r |
64 | } else if (line.matches("\\*\\*\\/")) {\r |
65 | incomment = false;\r |
66 | templine.append("\n" + line + "\n");\r |
67 | } else if (incomment) {\r |
68 | if (line.contains("Routine Description:")) {\r |
69 | description = true;\r |
70 | arguments = false;\r |
71 | returns = false;\r |
72 | } else if (line.contains("Arguments:")) {\r |
73 | description = false;\r |
74 | arguments = true;\r |
75 | returns = false;\r |
76 | templine.append("\n");\r |
77 | } else if (line.contains("Returns:")) {\r |
78 | description = false;\r |
79 | arguments = false;\r |
80 | returns = true;\r |
81 | templine.append("\n");\r |
82 | } else if (description) {\r |
83 | if (line.trim().length() != 0) {\r |
84 | templine.append(" " + line.trim() + "\n");\r |
85 | }\r |
86 | } else if (arguments) {\r |
87 | mtrcommentequation = ptncommentequation.matcher(line);\r |
88 | if (mtrcommentequation.find()) {\r |
89 | inequation = true;\r |
90 | templine.append(" @param " + mtrcommentequation.group(1) + " " + mtrcommentequation.group(2) + "\n");\r |
91 | } else if (inequation && line.trim().length() == 0) {\r |
92 | inequation = false;\r |
93 | } else if (inequation && line.trim().length() != 0) {\r |
94 | templine.append("#%#%" + line + "\n");\r |
95 | } else {\r |
96 | if (line.trim().length() != 0) {\r |
97 | templine.append(" " + line.trim() + "\n");\r |
98 | }\r |
99 | }\r |
100 | } else if (returns) {\r |
101 | mtrcommentequation = ptncommentequation.matcher(line);\r |
102 | if (mtrcommentequation.find()) {\r |
103 | inequation = true;\r |
104 | templine.append(" @retval " + mtrcommentequation.group(1) + " " + mtrcommentequation.group(2) + "\n");\r |
105 | } else if (inequation && line.trim().length() == 0) {\r |
106 | inequation = false;\r |
107 | } else if (inequation && line.trim().length() != 0) {\r |
108 | templine.append("#%#%" + line + "\n");\r |
109 | } else {\r |
110 | if (line.trim().length() != 0) {\r |
111 | templine.append(" @return " + line.trim() + "\n");\r |
112 | }\r |
113 | }\r |
114 | }\r |
115 | } else {\r |
116 | templine.append(line + "\n");\r |
117 | }\r |
118 | }\r |
119 | wholeline = templine.toString();\r |
120 | wholeline = wholeline.replaceAll("\n#%#%\\s*", " ");\r |
121 | //\r |
122 | \r |
123 | // secend scan\r |
124 | int startmax = 0;\r |
125 | rd = new BufferedReader(new StringReader(wholeline));\r |
126 | while ((line = rd.readLine()) != null) {\r |
127 | if (line.matches("\\/\\*\\*")) {\r |
128 | incomment = true;\r |
129 | templine.append(line + "\n");\r |
130 | } else if (line.matches("\\*\\*\\/")) {\r |
131 | incomment = false;\r |
132 | templine.append(line + "\n");\r |
133 | } else if (incomment) {\r |
134 | mtrnewcomment = ptnnewcomment.matcher(line);\r |
135 | if (mtrnewcomment.find()) {\r |
136 | startmax = mtrnewcomment.group(1).length() > startmax ? mtrnewcomment.group(1).length() : startmax;\r |
137 | }\r |
138 | }\r |
139 | }\r |
140 | startmax++;\r |
141 | //\r |
142 | \r |
143 | // third scan\r |
144 | int n = 0;\r |
145 | String temp = null;\r |
146 | String[] tempcont = null;\r |
147 | int count = 0;\r |
148 | templine = new StringBuffer();\r |
149 | rd = new BufferedReader(new StringReader(wholeline));\r |
150 | while ((line = rd.readLine()) != null) {\r |
151 | if (line.matches("\\/\\*\\*")) {\r |
152 | incomment = true;\r |
153 | templine.append(line + "\n");\r |
154 | } else if (line.matches("\\*\\*\\/")) {\r |
155 | incomment = false;\r |
156 | templine.append(line + "\n");\r |
157 | } else if (incomment) {\r |
158 | mtrnewcomment = ptnnewcomment.matcher(line);\r |
159 | if (mtrnewcomment.find()) {\r |
160 | n = startmax - mtrnewcomment.group(1).length();\r |
161 | templine.append(mtrnewcomment.group(1));\r |
162 | while (n-- >= 0) {\r |
163 | templine.append(" ");\r |
164 | }\r |
165 | temp = mtrnewcomment.group(3);\r |
166 | tempcont = temp.split(" "); // use \\s+ ?\r |
167 | \r |
168 | count = 0;\r |
169 | for (int i = 0; i < tempcont.length; i++) {\r |
170 | count += tempcont[i].length();\r |
171 | if (count <= (totallinelength - startmax)) {\r |
172 | templine.append(tempcont[i] + " ");\r |
173 | count += 1;\r |
174 | } else {\r |
175 | templine.append("\n");\r |
176 | n = startmax;\r |
177 | while (n-- >= 0) {\r |
178 | templine.append(" ");\r |
179 | }\r |
180 | templine.append(tempcont[i] + " ");\r |
181 | count = tempcont[i].length() + 1;\r |
182 | }\r |
183 | }\r |
184 | templine.append("\n");\r |
185 | } else {\r |
186 | templine.append(line + "\n");\r |
187 | }\r |
188 | } else {\r |
189 | templine.append(line + "\n");\r |
190 | }\r |
191 | }\r |
192 | wholeline = templine.toString();\r |
193 | //\r |
194 | // Remove trailing blanks.\r |
195 | // \r |
196 | wholeline = wholeline.replaceAll (" +\n", "\n");\r |
197 | Common.string2file(wholeline, filepath);\r |
198 | }\r |
199 | }\r |
200 | \r |
201 | public static final void fireAt(String path) throws Exception {\r |
202 | //Common.toDoAll(Common.dirCopy_(path), Critic.class.getMethod("critic", String.class), null, null, Common.FILE);\r |
203 | Common.toDoAll(path, Critic.class.getMethod("run", String.class), null, null, Common.FILE);\r |
204 | //Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);\r |
205 | System.out.println("Critic Done");\r |
206 | }\r |
6f55be9f |
207 | } |