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