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$5");
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(line
+ "\n");
67 } else if (incomment
) {
68 if (line
.contains("Routine Description:")) {
72 } else if (line
.contains("Arguments:")) {
76 } else if (line
.contains("Returns:")) {
80 } else if (description
) {
81 if (line
.trim().length() != 0) {
82 templine
.append(" " + line
.trim() + "\n");
84 } else if (arguments
) {
85 mtrcommentequation
= ptncommentequation
.matcher(line
);
86 if (mtrcommentequation
.find()) {
88 templine
.append(" @param " + 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 mtrcommentequation
= ptncommentequation
.matcher(line
);
99 if (mtrcommentequation
.find()) {
101 templine
.append(" @retval " + mtrcommentequation
.group(1) + " " + mtrcommentequation
.group(2) + "\n");
102 } else if (inequation
&& line
.trim().length() == 0) {
104 templine
.append(line
+ "\n");
105 } else if (inequation
&& line
.trim().length() != 0) {
106 templine
.append("#%#%" + line
+ "\n");
108 templine
.append(" " + line
.trim() + "\n");
112 templine
.append(line
+ "\n");
115 wholeline
= templine
.toString();
116 wholeline
= wholeline
.replaceAll("\n#%#%\\s*", " ");
121 rd
= new BufferedReader(new StringReader(wholeline
));
122 while ((line
= rd
.readLine()) != null) {
123 if (line
.matches("\\/\\*\\*")) {
125 templine
.append(line
+ "\n");
126 } else if (line
.matches("\\*\\*\\/")) {
128 templine
.append(line
+ "\n");
129 } else if (incomment
) {
130 mtrnewcomment
= ptnnewcomment
.matcher(line
);
131 if (mtrnewcomment
.find()) {
132 startmax
= mtrnewcomment
.group(1).length() > startmax ? mtrnewcomment
.group(1).length() : startmax
;
142 String
[] tempcont
= null;
144 templine
= new StringBuffer();
145 rd
= new BufferedReader(new StringReader(wholeline
));
146 while ((line
= rd
.readLine()) != null) {
147 if (line
.matches("\\/\\*\\*")) {
149 templine
.append(line
+ "\n");
150 } else if (line
.matches("\\*\\*\\/")) {
152 templine
.append(line
+ "\n");
153 } else if (incomment
) {
154 mtrnewcomment
= ptnnewcomment
.matcher(line
);
155 if (mtrnewcomment
.find()) {
156 n
= startmax
- mtrnewcomment
.group(1).length();
157 templine
.append(mtrnewcomment
.group(1));
159 templine
.append(" ");
161 temp
= mtrnewcomment
.group(3);
162 tempcont
= temp
.split(" "); // use \\s+ ?
165 for (int i
= 0; i
< tempcont
.length
; i
++) {
166 count
+= tempcont
[i
].length();
167 if (count
<= (totallinelength
- startmax
)) {
168 templine
.append(tempcont
[i
] + " ");
171 templine
.append("\n");
174 templine
.append(" ");
176 templine
.append(tempcont
[i
] + " ");
177 count
= tempcont
[i
].length() + 1;
180 templine
.append("\n");
182 templine
.append(line
+ "\n");
185 templine
.append(line
+ "\n");
188 wholeline
= templine
.toString();
191 Common
.string2file(wholeline
, filepath
);
195 public static final void fireAt(String path
) throws Exception
{
196 //Common.toDoAll(Common.dirCopy_(path), Critic.class.getMethod("critic", String.class), null, null, Common.FILE);
197 Common
.toDoAll(path
, Critic
.class.getMethod("run", String
.class), null, null, Common
.FILE
);
198 //Common.toDoAll(Common.dirCopy_(path), critic, Common.FILE);
199 System
.out
.println("Critic Done");