]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/Critic.java
modify critic
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / Critic.java
1 /** @file
2
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
8
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.
11
12 **/
13 package org.tianocore.migration;
14
15 import java.util.regex.*;
16 import java.io.*;
17
18 public class Critic implements Common.ForDoAll {
19 private static Pattern ptnheadcomment = Pattern.compile("^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/",Pattern.DOTALL);
20 private static Pattern ptnfunccomment = Pattern.compile("([\\};\\/]\\s*)([\\w\\s]*?[_\\w][_\\w\\d]*\\s*\\([^\\)\\(]*\\)\\s*)\\/\\*\\+\\+(.*?)\\-\\-\\*\\/(\\s*.*?)([\\{;])",Pattern.DOTALL);
21 //private static Pattern ptncommentstructure = Pattern.compile("\\/\\*\\+\\+\\s*Routine Description:\\s*(.*?)\\s*Arguments:\\s*(.*?)\\s*Returns:\\s*(.*?)\\s*\\-\\-\\*\\/",Pattern.DOTALL);
22 private static Pattern ptninfequation = Pattern.compile("([^\\s]*)\\s*-\\s*(.*)\\s*");
23 private static Matcher mtrinfequation;
24
25 public void toDo(String filepath) throws Exception {
26 if (filepath.contains(".c") || filepath.contains(".h")) {
27 BufferedReader rd = null;
28 String line = null;
29 StringBuffer templine = new StringBuffer();
30 boolean incomment = false;
31 boolean description = false;
32 boolean arguments = false;
33 boolean returns = false;
34 boolean inequation = false;
35
36 System.out.println("Criticing " + filepath);
37 String wholeline = Common.file2string(filepath);
38
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**/");
43
44 rd = new BufferedReader(new StringReader(wholeline));
45 while ((line = rd.readLine()) != null) {
46 if (line.matches("\\/\\*\\*")) {
47 incomment = true;
48 templine.append(line + "\n");
49 } else if (line.matches("\\*\\*\\/")) {
50 incomment = false;
51 templine.append(line + "\n");
52 } else if (incomment && line.contains("Routine Description:")) {
53 description = true;
54 arguments = false;
55 returns = false;
56 } else if (incomment && line.contains("Arguments:")) {
57 description = false;
58 arguments = true;
59 returns = false;
60 } else if (incomment && line.contains("Returns:")) {
61 description = false;
62 arguments = false;
63 returns = true;
64 } else if (incomment && description) {
65 templine.append(line + "\n");
66 } else if (incomment && arguments) {
67 mtrinfequation = ptninfequation.matcher(line);
68 if (mtrinfequation.find()) {
69 inequation = true;
70 templine.append(" @param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
71 } else if (inequation && line.trim().length() == 0) {
72 inequation = false;
73 templine.append(line + "\n");
74 } else if (inequation && line.trim().length() != 0) {
75 templine.append("#%#%" + line + "\n");
76 } else {
77 templine.append(line + "\n");
78 }
79 } else if (incomment && returns) {
80 mtrinfequation = ptninfequation.matcher(line);
81 if (mtrinfequation.find()) {
82 inequation = true;
83 templine.append(" @retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
84 } else if (inequation && line.trim().length() == 0) {
85 inequation = false;
86 templine.append(line + "\n");
87 } else if (inequation && line.trim().length() != 0) {
88 templine.append("#%#%" + line + "\n");
89 } else {
90 templine.append(line + "\n");
91 }
92 } else {
93 templine.append(line + "\n");
94 }
95 }
96 wholeline = templine.toString();
97 wholeline = wholeline.replaceAll("\n#%#%\\s*", " ");
98
99 /* -----slow edition of replacefirst with stringbuffer-----
100 line.append(wholeline);
101 mtrfunccomment = ptnfunccomment.matcher(line);
102 while (mtrfunccomment.find()) {
103 line.replace(0, line.length()-1, mtrfunccomment.replaceFirst("$2$4$3$1$5"));
104 }
105 */
106 /* -----slow edition of replacefirst with string-----
107 while ((mtrfunccomment = ptnfunccomment.matcher(wholeline)).find()) {
108 //funccomment = mtrfunccomment.group(2);
109 //mtrcommentstructure = ptncommentstructure.matcher(funccomment);
110 wholeline = mtrfunccomment.replaceFirst("$2$4$3$1$5");
111 }
112 */
113 /*
114 // edit func comment
115 mtrtempcomment = ptntempcomment.matcher(wholeline);
116 while (mtrtempcomment.find()) {
117 System.out.println("-----------------------------");
118 System.out.println(mtrtempcomment.group());
119 System.out.println("-----------------------------");
120 }
121 */
122 Common.string2file(wholeline, filepath);
123 }
124 }
125
126 public static void fireAt(String path) throws Exception {
127 Critic critic = new Critic();
128 Common.toDoAll(Common.dirCopy_(path), critic);
129 System.out.println("Critic Done");
130 }
131 }
132 //analyze func comment
133 /*if (mtrcommentstructure.find()) {
134 newcomment.append("/*++\n\n" + mtrcommentstructure.group(1) + "\n\n");
135
136 //System.out.println("-------1-------");
137 //System.out.println(mtrcommentstructure.group(1));
138
139 // arg
140 //System.out.println("-------2-------");
141 //System.out.println(mtrcommentstructure.group(2));
142 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(2));
143 while (mtrinfequation.find()) {
144 newcomment.append("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
145 //System.out.println("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
146 }
147 newcomment.append("\n");
148 // return
149 //System.out.println("-------3-------");
150 //System.out.println(mtrcommentstructure.group(3));
151 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(3));
152 while (mtrinfequation.find()) {
153 newcomment.append("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
154 //System.out.println("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
155 }
156 System.out.println(newcomment);
157 } else {
158 System.out.println("Error: Comment Style Incorrect");
159 }*/