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
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.
13 package org
.tianocore
.migration
;
15 import java
.util
.regex
.*;
18 public final class Critic
{
19 private static final Pattern ptnheadcomment
= Pattern
.compile("^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/",Pattern
.DOTALL
);
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
21 //private static Pattern ptncommentstructure = Pattern.compile("\\/\\*\\+\\+\\s*Routine Description:\\s*(.*?)\\s*Arguments:\\s*(.*?)\\s*Returns:\\s*(.*?)\\s*\\-\\-\\*\\/",Pattern.DOTALL);
22 private static final Pattern ptncommentequation
= Pattern
.compile("([^\\s]*)\\s+-\\s+(.*)\\s*");
23 private static Matcher mtrcommentequation
;
24 private static final Pattern ptnnewcomment
= Pattern
.compile("(\\s*@(param|retval)\\s+[^\\s]+)\\s+(.*)");
25 private static Matcher mtrnewcomment
;
27 private static final int totallinelength
= 82;
29 public static final void critic(String filepath
) throws Exception
{
30 if (filepath
.contains(".c") || filepath
.contains(".h")) {
31 BufferedReader rd
= null;
33 StringBuffer templine
= new StringBuffer();
34 boolean incomment
= false;
36 System
.out
.println("Criticing " + filepath
);
37 String wholeline
= Common
.file2string(filepath
);
39 wholeline
= wholeline
.replaceAll("\t", " ");
40 wholeline
= Common
.replaceAll(wholeline
, ptnheadcomment
, "/** @file$1**/");
41 wholeline
= Common
.replaceAll(wholeline
, ptnfunccomment
, "$1/**$3**/$4$2$5");
42 //wholeline = Common.replaceAll(wholeline, ptncommentstructure, "/**\n#%\n$1\n%#\n#%%\n$2\n%%#\n#%%%\n$3\n%%%#\n**/");
45 boolean description
= false;
46 boolean arguments
= false;
47 boolean returns
= false;
48 boolean inequation
= false;
49 rd
= new BufferedReader(new StringReader(wholeline
));
50 while ((line
= rd
.readLine()) != null) {
51 if (line
.matches("\\/\\*\\*")) {
56 templine
.append(line
+ "\n");
57 } else if (line
.matches("\\*\\*\\/")) {
59 templine
.append(line
+ "\n");
60 } else if (incomment
) {
61 if (line
.contains("Routine Description:")) {
65 } else if (line
.contains("Arguments:")) {
69 } else if (line
.contains("Returns:")) {
73 } else if (description
) {
74 if (line
.trim().length() != 0) {
75 templine
.append(" " + line
.trim() + "\n");
77 } else if (arguments
) {
78 mtrcommentequation
= ptncommentequation
.matcher(line
);
79 if (mtrcommentequation
.find()) {
81 templine
.append(" @param " + mtrcommentequation
.group(1) + " " + mtrcommentequation
.group(2) + "\n");
82 } else if (inequation
&& line
.trim().length() == 0) {
84 templine
.append(line
+ "\n");
85 } else if (inequation
&& line
.trim().length() != 0) {
86 templine
.append("#%#%" + line
+ "\n");
88 templine
.append(" " + line
.trim() + "\n");
91 mtrcommentequation
= ptncommentequation
.matcher(line
);
92 if (mtrcommentequation
.find()) {
94 templine
.append(" @retval " + mtrcommentequation
.group(1) + " " + mtrcommentequation
.group(2) + "\n");
95 } else if (inequation
&& line
.trim().length() == 0) {
97 templine
.append(line
+ "\n");
98 } else if (inequation
&& line
.trim().length() != 0) {
99 templine
.append("#%#%" + line
+ "\n");
101 templine
.append(" " + line
.trim() + "\n");
105 templine
.append(line
+ "\n");
108 wholeline
= templine
.toString();
109 wholeline
= wholeline
.replaceAll("\n#%#%\\s*", " ");
114 rd
= new BufferedReader(new StringReader(wholeline
));
115 while ((line
= rd
.readLine()) != null) {
116 if (line
.matches("\\/\\*\\*")) {
118 templine
.append(line
+ "\n");
119 } else if (line
.matches("\\*\\*\\/")) {
121 templine
.append(line
+ "\n");
122 } else if (incomment
) {
123 mtrnewcomment
= ptnnewcomment
.matcher(line
);
124 if (mtrnewcomment
.find()) {
125 startmax
= mtrnewcomment
.group(1).length() > startmax ? mtrnewcomment
.group(1).length() : startmax
;
135 String
[] tempcont
= null;
137 templine
= new StringBuffer();
138 rd
= new BufferedReader(new StringReader(wholeline
));
139 while ((line
= rd
.readLine()) != null) {
140 if (line
.matches("\\/\\*\\*")) {
142 templine
.append(line
+ "\n");
143 } else if (line
.matches("\\*\\*\\/")) {
145 templine
.append(line
+ "\n");
146 } else if (incomment
) {
147 mtrnewcomment
= ptnnewcomment
.matcher(line
);
148 if (mtrnewcomment
.find()) {
149 n
= startmax
- mtrnewcomment
.group(1).length();
150 templine
.append(mtrnewcomment
.group(1));
152 templine
.append(" ");
154 temp
= mtrnewcomment
.group(3);
155 tempcont
= temp
.split(" "); // use \\s+ ?
158 for (int i
= 0; i
< tempcont
.length
; i
++) {
159 count
+= tempcont
[i
].length();
160 if (count
<= (totallinelength
- startmax
)) {
161 templine
.append(tempcont
[i
] + " ");
164 templine
.append("\n");
167 templine
.append(" ");
169 templine
.append(tempcont
[i
] + " ");
170 count
= tempcont
[i
].length() + 1;
173 templine
.append("\n");
175 templine
.append(line
+ "\n");
178 templine
.append(line
+ "\n");
181 wholeline
= templine
.toString();
184 /* -----slow edition of replacefirst with stringbuffer-----
185 line.append(wholeline);
186 mtrfunccomment = ptnfunccomment.matcher(line);
187 while (mtrfunccomment.find()) {
188 line.replace(0, line.length()-1, mtrfunccomment.replaceFirst("$2$4$3$1$5"));
191 /* -----slow edition of replacefirst with string-----
192 while ((mtrfunccomment = ptnfunccomment.matcher(wholeline)).find()) {
193 //funccomment = mtrfunccomment.group(2);
194 //mtrcommentstructure = ptncommentstructure.matcher(funccomment);
195 wholeline = mtrfunccomment.replaceFirst("$2$4$3$1$5");
200 mtrtempcomment = ptntempcomment.matcher(wholeline);
201 while (mtrtempcomment.find()) {
202 System.out.println("-----------------------------");
203 System.out.println(mtrtempcomment.group());
204 System.out.println("-----------------------------");
207 Common
.string2file(wholeline
, filepath
);
211 public static final void fireAt(String path
) throws Exception
{
212 //Common.toDoAll(Common.dirCopy_(path), Critic.class.getMethod("critic", String.class), null, null, Common.FILE);
213 Common
.toDoAll(path
, Critic
.class.getMethod("critic", String
.class), null, null, Common
.FILE
);
214 //Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);
215 System
.out
.println("Critic Done");
218 //analyze func comment
219 /*if (mtrcommentstructure.find()) {
220 newcomment.append("/*++\n\n" + mtrcommentstructure.group(1) + "\n\n");
222 //System.out.println("-------1-------");
223 //System.out.println(mtrcommentstructure.group(1));
226 //System.out.println("-------2-------");
227 //System.out.println(mtrcommentstructure.group(2));
228 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(2));
229 while (mtrinfequation.find()) {
230 newcomment.append("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
231 //System.out.println("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
233 newcomment.append("\n");
235 //System.out.println("-------3-------");
236 //System.out.println(mtrcommentstructure.group(3));
237 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(3));
238 while (mtrinfequation.find()) {
239 newcomment.append("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
240 //System.out.println("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
242 System.out.println(newcomment);
244 System.out.println("Error: Comment Style Incorrect");