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 public static final Pattern PTN_NEW_HEAD_COMMENT
= Pattern
.compile("^\\/\\*\\*.*?\\*\\*\\/",Pattern
.DOTALL
);
20 private static final Pattern ptnheadcomment
= Pattern
.compile("^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/",Pattern
.DOTALL
);
21 private static final Pattern ptnfunccomment
= Pattern
.compile("([\\};\\/\">]\\s*)([\\w\\s\\*]*?[_\\w][_\\w\\d]*\\s*\\([^\\)\\(]*\\)\\s*)\\/\\*\\+\\+(.*?)\\-\\-\\*\\/(\\s*.*?)(?=[\\{;])",Pattern
.DOTALL
); // find function with {;">/ , may be unsafe
22 //private static Pattern ptncommentstructure = Pattern.compile("\\/\\*\\+\\+\\s*Routine Description:\\s*(.*?)\\s*Arguments:\\s*(.*?)\\s*Returns:\\s*(.*?)\\s*\\-\\-\\*\\/",Pattern.DOTALL);
23 private static final Pattern ptncommentequation
= Pattern
.compile("([^\\s]*)\\s+-\\s+(.*)\\s*");
24 private static Matcher mtrcommentequation
;
25 private static final Pattern ptnnewcomment
= Pattern
.compile("(\\s*@(param|retval)\\s+[^\\s]+)\\s+(.*)");
26 private static Matcher mtrnewcomment
;
28 private static final int totallinelength
= 82;
30 public static final void run(String filepath
) throws Exception
{
31 if (MigrationTool
.doCritic
) { // this is left here to set an example for future structure
36 private static final void critic(String filepath
) throws Exception
{
37 if (filepath
.contains(".c") || filepath
.contains(".h")) {
38 BufferedReader rd
= null;
40 StringBuffer templine
= new StringBuffer();
41 boolean incomment
= false;
43 System
.out
.println("Criticing " + filepath
);
44 String wholeline
= Common
.file2string(filepath
);
46 wholeline
= wholeline
.replaceAll("\t", " ");
47 wholeline
= Common
.replaceAll(wholeline
, ptnheadcomment
, "/** @file$1**/");
48 wholeline
= Common
.replaceAll(wholeline
, ptnfunccomment
, "$1/**$3**/$4$2");
49 //wholeline = Common.replaceAll(wholeline, ptncommentstructure, "/**\n#%\n$1\n%#\n#%%\n$2\n%%#\n#%%%\n$3\n%%%#\n**/");
52 boolean description
= false;
53 boolean arguments
= false;
54 boolean returns
= false;
55 boolean inequation
= false;
56 rd
= new BufferedReader(new StringReader(wholeline
));
57 while ((line
= rd
.readLine()) != null) {
58 if (line
.matches("\\/\\*\\*")) {
63 templine
.append(line
+ "\n");
64 } else if (line
.matches("\\*\\*\\/")) {
66 templine
.append("\n" + line
+ "\n");
67 } else if (incomment
) {
68 if (line
.contains("Routine Description:")) {
72 } else if (line
.contains("Arguments:")) {
76 templine
.append("\n");
77 } else if (line
.contains("Returns:")) {
81 templine
.append("\n");
82 } else if (description
) {
83 if (line
.trim().length() != 0) {
84 templine
.append(" " + line
.trim() + "\n");
86 } else if (arguments
) {
87 mtrcommentequation
= ptncommentequation
.matcher(line
);
88 if (mtrcommentequation
.find()) {
90 templine
.append(" @param " + mtrcommentequation
.group(1) + " " + mtrcommentequation
.group(2) + "\n");
91 } else if (inequation
&& line
.trim().length() == 0) {
93 } else if (inequation
&& line
.trim().length() != 0) {
94 templine
.append("#%#%" + line
+ "\n");
96 if (line
.trim().length() != 0) {
97 templine
.append(" " + line
.trim() + "\n");
100 } else if (returns
) {
101 mtrcommentequation
= ptncommentequation
.matcher(line
);
102 if (mtrcommentequation
.find()) {
104 templine
.append(" @retval " + mtrcommentequation
.group(1) + " " + mtrcommentequation
.group(2) + "\n");
105 } else if (inequation
&& line
.trim().length() == 0) {
107 } else if (inequation
&& line
.trim().length() != 0) {
108 templine
.append("#%#%" + line
+ "\n");
110 if (line
.trim().length() != 0) {
111 templine
.append(" @return " + line
.trim() + "\n");
116 templine
.append(line
+ "\n");
119 wholeline
= templine
.toString();
120 wholeline
= wholeline
.replaceAll("\n#%#%\\s*", " ");
125 rd
= new BufferedReader(new StringReader(wholeline
));
126 while ((line
= rd
.readLine()) != null) {
127 if (line
.matches("\\/\\*\\*")) {
129 templine
.append(line
+ "\n");
130 } else if (line
.matches("\\*\\*\\/")) {
132 templine
.append(line
+ "\n");
133 } else if (incomment
) {
134 mtrnewcomment
= ptnnewcomment
.matcher(line
);
135 if (mtrnewcomment
.find()) {
136 startmax
= mtrnewcomment
.group(1).length() > startmax ? mtrnewcomment
.group(1).length() : startmax
;
146 String
[] tempcont
= null;
148 templine
= new StringBuffer();
149 rd
= new BufferedReader(new StringReader(wholeline
));
150 while ((line
= rd
.readLine()) != null) {
151 if (line
.matches("\\/\\*\\*")) {
153 templine
.append(line
+ "\n");
154 } else if (line
.matches("\\*\\*\\/")) {
156 templine
.append(line
+ "\n");
157 } else if (incomment
) {
158 mtrnewcomment
= ptnnewcomment
.matcher(line
);
159 if (mtrnewcomment
.find()) {
160 n
= startmax
- mtrnewcomment
.group(1).length();
161 templine
.append(mtrnewcomment
.group(1));
163 templine
.append(" ");
165 temp
= mtrnewcomment
.group(3);
166 tempcont
= temp
.split(" "); // use \\s+ ?
169 for (int i
= 0; i
< tempcont
.length
; i
++) {
170 count
+= tempcont
[i
].length();
171 if (count
<= (totallinelength
- startmax
)) {
172 templine
.append(tempcont
[i
] + " ");
175 templine
.append("\n");
178 templine
.append(" ");
180 templine
.append(tempcont
[i
] + " ");
181 count
= tempcont
[i
].length() + 1;
184 templine
.append("\n");
186 templine
.append(line
+ "\n");
189 templine
.append(line
+ "\n");
192 wholeline
= templine
.toString();
194 // Remove trailing blanks.
196 wholeline
= wholeline
.replaceAll (" +\n", "\n");
197 Common
.string2file(wholeline
, filepath
);
201 public static final void fireAt(String path
) throws Exception
{
202 //Common.toDoAll(Common.dirCopy_(path), Critic.class.getMethod("critic", String.class), null, null, Common.FILE);
203 Common
.toDoAll(path
, Critic
.class.getMethod("run", String
.class), null, null, Common
.FILE
);
204 //Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);
205 System
.out
.println("Critic Done");