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 run(String filepath
) throws Exception
{
30 if (MigrationTool
.doCritic
) {
35 private static final void critic(String filepath
) throws Exception
{
36 if (filepath
.contains(".c") || filepath
.contains(".h")) {
37 BufferedReader rd
= null;
39 StringBuffer templine
= new StringBuffer();
40 boolean incomment
= false;
42 System
.out
.println("Criticing " + filepath
);
43 String wholeline
= Common
.file2string(filepath
);
45 wholeline
= wholeline
.replaceAll("\t", " ");
46 wholeline
= Common
.replaceAll(wholeline
, ptnheadcomment
, "/** @file$1**/");
47 wholeline
= Common
.replaceAll(wholeline
, ptnfunccomment
, "$1/**$3**/$4$2$5");
48 //wholeline = Common.replaceAll(wholeline, ptncommentstructure, "/**\n#%\n$1\n%#\n#%%\n$2\n%%#\n#%%%\n$3\n%%%#\n**/");
51 boolean description
= false;
52 boolean arguments
= false;
53 boolean returns
= false;
54 boolean inequation
= false;
55 rd
= new BufferedReader(new StringReader(wholeline
));
56 while ((line
= rd
.readLine()) != null) {
57 if (line
.matches("\\/\\*\\*")) {
62 templine
.append(line
+ "\n");
63 } else if (line
.matches("\\*\\*\\/")) {
65 templine
.append(line
+ "\n");
66 } else if (incomment
) {
67 if (line
.contains("Routine Description:")) {
71 } else if (line
.contains("Arguments:")) {
75 } else if (line
.contains("Returns:")) {
79 } else if (description
) {
80 if (line
.trim().length() != 0) {
81 templine
.append(" " + line
.trim() + "\n");
83 } else if (arguments
) {
84 mtrcommentequation
= ptncommentequation
.matcher(line
);
85 if (mtrcommentequation
.find()) {
87 templine
.append(" @param " + mtrcommentequation
.group(1) + " " + mtrcommentequation
.group(2) + "\n");
88 } else if (inequation
&& line
.trim().length() == 0) {
90 templine
.append(line
+ "\n");
91 } else if (inequation
&& line
.trim().length() != 0) {
92 templine
.append("#%#%" + line
+ "\n");
94 templine
.append(" " + line
.trim() + "\n");
97 mtrcommentequation
= ptncommentequation
.matcher(line
);
98 if (mtrcommentequation
.find()) {
100 templine
.append(" @retval " + mtrcommentequation
.group(1) + " " + mtrcommentequation
.group(2) + "\n");
101 } else if (inequation
&& line
.trim().length() == 0) {
103 templine
.append(line
+ "\n");
104 } else if (inequation
&& line
.trim().length() != 0) {
105 templine
.append("#%#%" + line
+ "\n");
107 templine
.append(" " + line
.trim() + "\n");
111 templine
.append(line
+ "\n");
114 wholeline
= templine
.toString();
115 wholeline
= wholeline
.replaceAll("\n#%#%\\s*", " ");
120 rd
= new BufferedReader(new StringReader(wholeline
));
121 while ((line
= rd
.readLine()) != null) {
122 if (line
.matches("\\/\\*\\*")) {
124 templine
.append(line
+ "\n");
125 } else if (line
.matches("\\*\\*\\/")) {
127 templine
.append(line
+ "\n");
128 } else if (incomment
) {
129 mtrnewcomment
= ptnnewcomment
.matcher(line
);
130 if (mtrnewcomment
.find()) {
131 startmax
= mtrnewcomment
.group(1).length() > startmax ? mtrnewcomment
.group(1).length() : startmax
;
141 String
[] tempcont
= null;
143 templine
= new StringBuffer();
144 rd
= new BufferedReader(new StringReader(wholeline
));
145 while ((line
= rd
.readLine()) != null) {
146 if (line
.matches("\\/\\*\\*")) {
148 templine
.append(line
+ "\n");
149 } else if (line
.matches("\\*\\*\\/")) {
151 templine
.append(line
+ "\n");
152 } else if (incomment
) {
153 mtrnewcomment
= ptnnewcomment
.matcher(line
);
154 if (mtrnewcomment
.find()) {
155 n
= startmax
- mtrnewcomment
.group(1).length();
156 templine
.append(mtrnewcomment
.group(1));
158 templine
.append(" ");
160 temp
= mtrnewcomment
.group(3);
161 tempcont
= temp
.split(" "); // use \\s+ ?
164 for (int i
= 0; i
< tempcont
.length
; i
++) {
165 count
+= tempcont
[i
].length();
166 if (count
<= (totallinelength
- startmax
)) {
167 templine
.append(tempcont
[i
] + " ");
170 templine
.append("\n");
173 templine
.append(" ");
175 templine
.append(tempcont
[i
] + " ");
176 count
= tempcont
[i
].length() + 1;
179 templine
.append("\n");
181 templine
.append(line
+ "\n");
184 templine
.append(line
+ "\n");
187 wholeline
= templine
.toString();
190 Common
.string2file(wholeline
, filepath
);
194 public static final void fireAt(String path
) throws Exception
{
195 //Common.toDoAll(Common.dirCopy_(path), Critic.class.getMethod("critic", String.class), null, null, Common.FILE);
196 Common
.toDoAll(path
, Critic
.class.getMethod("run", String
.class), null, null, Common
.FILE
);
197 //Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);
198 System
.out
.println("Critic Done");