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
.io
.BufferedReader
;
16 import java
.io
.StringReader
;
17 import java
.util
.regex
.Matcher
;
18 import java
.util
.regex
.Pattern
;
20 public final class Critic
{
21 public static final Pattern PTN_NEW_HEAD_COMMENT
= Pattern
.compile(
22 "^\\/\\*\\*.*?\\*\\*\\/", Pattern
.DOTALL
);
24 private static final Pattern ptnheadcomment
= Pattern
.compile(
25 "^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/", Pattern
.DOTALL
);
27 private static final Pattern ptnfunccomment
= Pattern
29 "([\\};\\/\">]\\s*)([\\w\\s\\*]*?[_\\w][_\\w\\d]*\\s*\\([^\\)\\(]*\\)\\s*)\\/\\*\\+\\+(.*?)\\-\\-\\*\\/\\s*(.*?)(?=[\\{;])",
30 Pattern
.DOTALL
); // find function with {;">/ , may be
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*");
39 private static Matcher mtrcommentequation
;
41 private static final Pattern ptnnewcomment
= Pattern
42 .compile("(\\s*@(param|retval)\\s+[^\\s]+)\\s+(.*)");
44 private static Matcher mtrnewcomment
;
46 private static final int totallinelength
= 82;
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
55 private static final void critic(String filepath
) throws Exception
{
56 if (filepath
.contains(".c") || filepath
.contains(".h")) {
57 BufferedReader rd
= null;
59 StringBuffer templine
= new StringBuffer();
60 boolean incomment
= false;
62 System
.out
.println("Criticing " + filepath
);
63 String wholeline
= Common
.file2string(filepath
);
65 wholeline
= wholeline
.replaceAll("\t", " ");
66 wholeline
= Common
.replaceAll(wholeline
, ptnheadcomment
,
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**/");
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("\\/\\*\\*")) {
85 templine
.append(line
+ "\n");
86 } else if (line
.matches("\\*\\*\\/")) {
88 templine
.append("\n" + line
+ "\n");
89 } else if (incomment
) {
90 if (line
.contains("Routine Description:")) {
94 } else if (line
.contains("Arguments:")) {
98 templine
.append("\n");
99 } else if (line
.contains("Returns:")) {
103 templine
.append("\n");
104 } else if (description
) {
105 if (line
.trim().length() != 0) {
106 templine
.append(" " + line
.trim() + "\n");
108 } else if (arguments
) {
109 mtrcommentequation
= ptncommentequation
.matcher(line
);
110 if (mtrcommentequation
.find()) {
112 templine
.append(" @param "
113 + mtrcommentequation
.group(1) + " "
114 + mtrcommentequation
.group(2) + "\n");
115 } else if (inequation
&& line
.trim().length() == 0) {
117 } else if (inequation
&& line
.trim().length() != 0) {
118 templine
.append("#%#%" + line
+ "\n");
120 if (line
.trim().length() != 0) {
121 templine
.append(" " + line
.trim() + "\n");
124 } else if (returns
) {
125 mtrcommentequation
= ptncommentequation
.matcher(line
);
126 if (mtrcommentequation
.find()) {
128 templine
.append(" @retval "
129 + mtrcommentequation
.group(1) + " "
130 + mtrcommentequation
.group(2) + "\n");
131 } else if (inequation
&& line
.trim().length() == 0) {
133 } else if (inequation
&& line
.trim().length() != 0) {
134 templine
.append("#%#%" + line
+ "\n");
136 if (line
.trim().length() != 0) {
137 templine
.append(" @return " + line
.trim()
143 templine
.append(line
+ "\n");
146 wholeline
= templine
.toString();
147 wholeline
= wholeline
.replaceAll("\n#%#%\\s*", " ");
152 rd
= new BufferedReader(new StringReader(wholeline
));
153 while ((line
= rd
.readLine()) != null) {
154 if (line
.matches("\\/\\*\\*")) {
156 templine
.append(line
+ "\n");
157 } else if (line
.matches("\\*\\*\\/")) {
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
175 String
[] tempcont
= null;
177 templine
= new StringBuffer();
178 rd
= new BufferedReader(new StringReader(wholeline
));
179 while ((line
= rd
.readLine()) != null) {
180 if (line
.matches("\\/\\*\\*")) {
182 templine
.append(line
+ "\n");
183 } else if (line
.matches("\\*\\*\\/")) {
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));
192 templine
.append(" ");
194 temp
= mtrnewcomment
.group(3);
195 tempcont
= temp
.split(" "); // use \\s+ ?
198 for (int i
= 0; i
< tempcont
.length
; i
++) {
199 count
+= tempcont
[i
].length();
200 if (count
<= (totallinelength
- startmax
)) {
201 templine
.append(tempcont
[i
] + " ");
204 templine
.append("\n");
207 templine
.append(" ");
209 templine
.append(tempcont
[i
] + " ");
210 count
= tempcont
[i
].length() + 1;
213 templine
.append("\n");
215 templine
.append(line
+ "\n");
218 templine
.append(line
+ "\n");
221 wholeline
= templine
.toString();
223 // Remove trailing blanks.
225 wholeline
= wholeline
.replaceAll(" +\n", "\n");
226 Common
.string2file(wholeline
, filepath
);
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,
234 Common
.toDoAll(path
, Critic
.class.getMethod("run", String
.class), null,
236 // Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);
237 System
.out
.println("Critic Done");