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("\\/\\*\\*")) {
53 templine
.append(line
+ "\n");
54 } else if (line
.matches("\\*\\*\\/")) {
56 templine
.append(line
+ "\n");
57 } else if (incomment
) {
58 if (line
.contains("Routine Description:")) {
62 } else if (line
.contains("Arguments:")) {
66 } else if (line
.contains("Returns:")) {
70 } else if (description
) {
71 if (line
.trim().length() != 0) {
72 templine
.append(" " + line
.trim() + "\n");
74 } else if (arguments
) {
75 mtrcommentequation
= ptncommentequation
.matcher(line
);
76 if (mtrcommentequation
.find()) {
78 templine
.append(" @param " + mtrcommentequation
.group(1) + " " + mtrcommentequation
.group(2) + "\n");
79 } else if (inequation
&& line
.trim().length() == 0) {
81 templine
.append(line
+ "\n");
82 } else if (inequation
&& line
.trim().length() != 0) {
83 templine
.append("#%#%" + line
+ "\n");
85 templine
.append(" " + line
.trim() + "\n");
88 mtrcommentequation
= ptncommentequation
.matcher(line
);
89 if (mtrcommentequation
.find()) {
91 templine
.append(" @retval " + mtrcommentequation
.group(1) + " " + mtrcommentequation
.group(2) + "\n");
92 } else if (inequation
&& line
.trim().length() == 0) {
94 templine
.append(line
+ "\n");
95 } else if (inequation
&& line
.trim().length() != 0) {
96 templine
.append("#%#%" + line
+ "\n");
98 templine
.append(" " + line
.trim() + "\n");
102 templine
.append(line
+ "\n");
105 wholeline
= templine
.toString();
106 wholeline
= wholeline
.replaceAll("\n#%#%\\s*", " ");
111 rd
= new BufferedReader(new StringReader(wholeline
));
112 while ((line
= rd
.readLine()) != null) {
113 if (line
.matches("\\/\\*\\*")) {
115 templine
.append(line
+ "\n");
116 } else if (line
.matches("\\*\\*\\/")) {
118 templine
.append(line
+ "\n");
119 } else if (incomment
) {
120 mtrnewcomment
= ptnnewcomment
.matcher(line
);
121 if (mtrnewcomment
.find()) {
122 startmax
= mtrnewcomment
.group(1).length() > startmax ? mtrnewcomment
.group(1).length() : startmax
;
132 String
[] tempcont
= null;
134 templine
= new StringBuffer();
135 rd
= new BufferedReader(new StringReader(wholeline
));
136 while ((line
= rd
.readLine()) != null) {
137 if (line
.matches("\\/\\*\\*")) {
139 templine
.append(line
+ "\n");
140 } else if (line
.matches("\\*\\*\\/")) {
142 templine
.append(line
+ "\n");
143 } else if (incomment
) {
144 mtrnewcomment
= ptnnewcomment
.matcher(line
);
145 if (mtrnewcomment
.find()) {
146 n
= startmax
- mtrnewcomment
.group(1).length();
147 templine
.append(mtrnewcomment
.group(1));
149 templine
.append(" ");
151 temp
= mtrnewcomment
.group(3);
152 tempcont
= temp
.split(" "); // use \\s+ ?
155 for (int i
= 0; i
< tempcont
.length
; i
++) {
156 count
+= tempcont
[i
].length();
157 if (count
<= (totallinelength
- startmax
)) {
158 templine
.append(tempcont
[i
] + " ");
161 templine
.append("\n");
164 templine
.append(" ");
166 templine
.append(tempcont
[i
] + " ");
167 count
= tempcont
[i
].length() + 1;
170 templine
.append("\n");
172 templine
.append(line
+ "\n");
175 templine
.append(line
+ "\n");
178 wholeline
= templine
.toString();
181 /* -----slow edition of replacefirst with stringbuffer-----
182 line.append(wholeline);
183 mtrfunccomment = ptnfunccomment.matcher(line);
184 while (mtrfunccomment.find()) {
185 line.replace(0, line.length()-1, mtrfunccomment.replaceFirst("$2$4$3$1$5"));
188 /* -----slow edition of replacefirst with string-----
189 while ((mtrfunccomment = ptnfunccomment.matcher(wholeline)).find()) {
190 //funccomment = mtrfunccomment.group(2);
191 //mtrcommentstructure = ptncommentstructure.matcher(funccomment);
192 wholeline = mtrfunccomment.replaceFirst("$2$4$3$1$5");
197 mtrtempcomment = ptntempcomment.matcher(wholeline);
198 while (mtrtempcomment.find()) {
199 System.out.println("-----------------------------");
200 System.out.println(mtrtempcomment.group());
201 System.out.println("-----------------------------");
204 Common
.string2file(wholeline
, filepath
);
208 public static final void fireAt(String path
) throws Exception
{
209 //Common.toDoAll(Common.dirCopy_(path), Critic.class.getMethod("critic", String.class), null, null, Common.FILE);
210 Common
.toDoAll(path
, Critic
.class.getMethod("critic", String
.class), null, null, Common
.FILE
);
211 //Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);
212 System
.out
.println("Critic Done");
215 //analyze func comment
216 /*if (mtrcommentstructure.find()) {
217 newcomment.append("/*++\n\n" + mtrcommentstructure.group(1) + "\n\n");
219 //System.out.println("-------1-------");
220 //System.out.println(mtrcommentstructure.group(1));
223 //System.out.println("-------2-------");
224 //System.out.println(mtrcommentstructure.group(2));
225 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(2));
226 while (mtrinfequation.find()) {
227 newcomment.append("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
228 //System.out.println("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
230 newcomment.append("\n");
232 //System.out.println("-------3-------");
233 //System.out.println(mtrcommentstructure.group(3));
234 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(3));
235 while (mtrinfequation.find()) {
236 newcomment.append("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
237 //System.out.println("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
239 System.out.println(newcomment);
241 System.out.println("Error: Comment Style Incorrect");