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