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
&& line
.contains("Routine Description:")) {
61 } else if (incomment
&& line
.contains("Arguments:")) {
65 } else if (incomment
&& line
.contains("Returns:")) {
69 } else if (incomment
&& description
) {
70 templine
.append(" " + line
.trim() + "\n");
71 } else if (incomment
&& arguments
) {
72 mtrcommentequation
= ptncommentequation
.matcher(line
);
73 if (mtrcommentequation
.find()) {
75 templine
.append(" @param " + mtrcommentequation
.group(1) + " " + mtrcommentequation
.group(2) + "\n");
76 } else if (inequation
&& line
.trim().length() == 0) {
78 templine
.append(line
+ "\n");
79 } else if (inequation
&& line
.trim().length() != 0) {
80 templine
.append("#%#%" + line
+ "\n");
82 templine
.append(" " + line
.trim() + "\n");
84 } else if (incomment
&& returns
) {
85 mtrcommentequation
= ptncommentequation
.matcher(line
);
86 if (mtrcommentequation
.find()) {
88 templine
.append(" @retval " + mtrcommentequation
.group(1) + " " + mtrcommentequation
.group(2) + "\n");
89 } else if (inequation
&& line
.trim().length() == 0) {
91 templine
.append(line
+ "\n");
92 } else if (inequation
&& line
.trim().length() != 0) {
93 templine
.append("#%#%" + line
+ "\n");
95 templine
.append(" " + line
.trim() + "\n");
98 templine
.append(line
+ "\n");
101 wholeline
= templine
.toString();
102 wholeline
= wholeline
.replaceAll("\n#%#%\\s*", " ");
107 rd
= new BufferedReader(new StringReader(wholeline
));
108 while ((line
= rd
.readLine()) != null) {
109 if (line
.matches("\\/\\*\\*")) {
111 templine
.append(line
+ "\n");
112 } else if (line
.matches("\\*\\*\\/")) {
114 templine
.append(line
+ "\n");
115 } else if (incomment
) {
116 mtrnewcomment
= ptnnewcomment
.matcher(line
);
117 if (mtrnewcomment
.find()) {
118 startmax
= mtrnewcomment
.group(1).length() > startmax ? mtrnewcomment
.group(1).length() : startmax
;
128 String
[] tempcont
= null;
130 templine
= new StringBuffer();
131 rd
= new BufferedReader(new StringReader(wholeline
));
132 while ((line
= rd
.readLine()) != null) {
133 if (line
.matches("\\/\\*\\*")) {
135 templine
.append(line
+ "\n");
136 } else if (line
.matches("\\*\\*\\/")) {
138 templine
.append(line
+ "\n");
139 } else if (incomment
) {
140 mtrnewcomment
= ptnnewcomment
.matcher(line
);
141 if (mtrnewcomment
.find()) {
142 n
= startmax
- mtrnewcomment
.group(1).length();
143 templine
.append(mtrnewcomment
.group(1));
145 templine
.append(" ");
147 temp
= mtrnewcomment
.group(3);
148 tempcont
= temp
.split(" "); // use \\s+ ?
151 for (int i
= 0; i
< tempcont
.length
; i
++) {
152 count
+= tempcont
[i
].length();
153 if (count
<= (totallinelength
- startmax
)) {
154 templine
.append(tempcont
[i
] + " ");
157 templine
.append("\n");
160 templine
.append(" ");
162 templine
.append(tempcont
[i
] + " ");
163 count
= tempcont
[i
].length() + 1;
166 templine
.append("\n");
168 templine
.append(line
+ "\n");
171 templine
.append(line
+ "\n");
174 wholeline
= templine
.toString();
177 /* -----slow edition of replacefirst with stringbuffer-----
178 line.append(wholeline);
179 mtrfunccomment = ptnfunccomment.matcher(line);
180 while (mtrfunccomment.find()) {
181 line.replace(0, line.length()-1, mtrfunccomment.replaceFirst("$2$4$3$1$5"));
184 /* -----slow edition of replacefirst with string-----
185 while ((mtrfunccomment = ptnfunccomment.matcher(wholeline)).find()) {
186 //funccomment = mtrfunccomment.group(2);
187 //mtrcommentstructure = ptncommentstructure.matcher(funccomment);
188 wholeline = mtrfunccomment.replaceFirst("$2$4$3$1$5");
193 mtrtempcomment = ptntempcomment.matcher(wholeline);
194 while (mtrtempcomment.find()) {
195 System.out.println("-----------------------------");
196 System.out.println(mtrtempcomment.group());
197 System.out.println("-----------------------------");
200 Common
.string2file(wholeline
, filepath
);
204 public static final void fireAt(String path
) throws Exception
{
205 //Common.toDoAll(Common.dirCopy_(path), Critic.class.getMethod("critic", String.class), null, null, Common.FILE);
206 Common
.toDoAll(path
, Critic
.class.getMethod("critic", String
.class), null, null, Common
.FILE
);
207 //Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);
208 System
.out
.println("Critic Done");
211 //analyze func comment
212 /*if (mtrcommentstructure.find()) {
213 newcomment.append("/*++\n\n" + mtrcommentstructure.group(1) + "\n\n");
215 //System.out.println("-------1-------");
216 //System.out.println(mtrcommentstructure.group(1));
219 //System.out.println("-------2-------");
220 //System.out.println(mtrcommentstructure.group(2));
221 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(2));
222 while (mtrinfequation.find()) {
223 newcomment.append("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
224 //System.out.println("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
226 newcomment.append("\n");
228 //System.out.println("-------3-------");
229 //System.out.println(mtrcommentstructure.group(3));
230 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(3));
231 while (mtrinfequation.find()) {
232 newcomment.append("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
233 //System.out.println("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
235 System.out.println(newcomment);
237 System.out.println("Error: Comment Style Incorrect");