]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/MigrationTools/org/tianocore/migration/Critic.java
fast edition with 'read line' implement
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / Critic.java
CommitLineData
8c4eeeb6 1/** @file\r
2 \r
3 Copyright (c) 2006, Intel Corporation\r
4 All rights reserved. This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php\r
8 \r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11 \r
12 **/\r
fed802b1 13package org.tianocore.migration;\r
14\r
15import java.util.regex.*;\r
3637381b 16import java.io.*;\r
fed802b1 17\r
18public class Critic implements Common.ForDoAll {\r
fed802b1 19 private static Pattern ptnheadcomment = Pattern.compile("^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/",Pattern.DOTALL);\r
1af2e90c 20 private static Pattern ptnfunccomment = Pattern.compile("([\\w\\d]*\\s*[_\\w][_\\w\\d]*\\s*\\([^\\)\\(]*\\)\\s*)\\/\\*\\+\\+(.*?)\\-\\-\\*\\/(\\s*.*?)([\\{;])",Pattern.DOTALL);\r
a756211f 21 private static Pattern ptncommentstructure = Pattern.compile("\\/\\*\\+\\+\\s*Routine Description:\\s*(.*?)\\s*Arguments:\\s*(.*?)\\s*Returns:\\s*(.*?)\\s*\\-\\-\\*\\/",Pattern.DOTALL);\r
3637381b 22 private static Pattern ptninfequation = Pattern.compile("([^\\s]*)\\s*-\\s*(.*)\\s*");\r
8c4eeeb6 23 private static Matcher mtrinfequation;\r
3637381b 24 private static Matcher mtrfunccomment;\r
fed802b1 25 \r
26 public void toDo(String filepath) throws Exception {\r
27 if (filepath.contains(".c") || filepath.contains(".h")) {\r
3637381b 28 BufferedReader rd = null;\r
29 String line = null;\r
30 StringBuffer templine = new StringBuffer();\r
1af2e90c 31 boolean incomment = false;\r
3637381b 32 boolean description = false;\r
33 boolean arguments = false;\r
34 boolean returns = false;\r
35 \r
8c4eeeb6 36 System.out.println("Criticing " + filepath);\r
fed802b1 37 String wholeline = Common.file2string(filepath);\r
8c4eeeb6 38 \r
a756211f 39 wholeline = Common.replaceAll(wholeline, ptnheadcomment, "/** @file$1**/");\r
1af2e90c 40 wholeline = Common.replaceAll(wholeline, ptnfunccomment, "/**$2**/$3$1$4");\r
3637381b 41 //wholeline = Common.replaceAll(wholeline, ptncommentstructure, "/**\n#%\n$1\n%#\n#%%\n$2\n%%#\n#%%%\n$3\n%%%#\n**/");\r
1af2e90c 42 \r
3637381b 43 rd = new BufferedReader(new StringReader(wholeline));\r
44 while ((line = rd.readLine()) != null) {\r
1af2e90c 45 if (line.matches("\\/\\*\\*")) {\r
46 incomment = true;\r
3637381b 47 templine.append(line + "\n");\r
1af2e90c 48 } else if (line.matches("\\*\\*\\/")) {\r
49 incomment = false;\r
50 templine.append(line + "\n");\r
51 } else if (incomment && line.contains("Routine Description:")) {\r
3637381b 52 description = true;\r
53 arguments = false;\r
54 returns = false;\r
1af2e90c 55 } else if (incomment && line.contains("Arguments:")) {\r
3637381b 56 description = false;\r
57 arguments = true;\r
58 returns = false;\r
1af2e90c 59 } else if (incomment && line.contains("Returns:")) {\r
3637381b 60 description = false;\r
61 arguments = false;\r
62 returns = true;\r
1af2e90c 63 } else if (incomment && description) {\r
3637381b 64 templine.append(line + "\n");\r
1af2e90c 65 } else if (incomment && arguments) {\r
3637381b 66 mtrinfequation = ptninfequation.matcher(line);\r
67 if (mtrinfequation.find()) {\r
68 templine.append(" @param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");\r
69 } else {\r
70 templine.append(line + "\n");\r
71 }\r
1af2e90c 72 } else if (incomment && returns) {\r
3637381b 73 mtrinfequation = ptninfequation.matcher(line);\r
74 if (mtrinfequation.find()) {\r
75 templine.append(" @retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");\r
76 } else {\r
77 templine.append(line + "\n");\r
78 }\r
3637381b 79 } else {\r
80 templine.append(line + "\n");\r
81 }\r
82 }\r
1af2e90c 83 wholeline = templine.toString();\r
84 \r
a756211f 85 /* -----slow edition of replacefirst with stringbuffer-----\r
86 line.append(wholeline);\r
87 mtrfunccomment = ptnfunccomment.matcher(line);\r
8c4eeeb6 88 while (mtrfunccomment.find()) {\r
a756211f 89 line.replace(0, line.length()-1, mtrfunccomment.replaceFirst("$2$4$3$1$5"));\r
8c4eeeb6 90 }\r
a756211f 91 */\r
1af2e90c 92 /* -----slow edition of replacefirst with string-----\r
a756211f 93 while ((mtrfunccomment = ptnfunccomment.matcher(wholeline)).find()) {\r
94 //funccomment = mtrfunccomment.group(2);\r
95 //mtrcommentstructure = ptncommentstructure.matcher(funccomment);\r
96 wholeline = mtrfunccomment.replaceFirst("$2$4$3$1$5");\r
97 }\r
1af2e90c 98 */\r
a756211f 99 /*\r
8c4eeeb6 100 // edit func comment\r
101 mtrtempcomment = ptntempcomment.matcher(wholeline);\r
102 while (mtrtempcomment.find()) {\r
103 System.out.println("-----------------------------");\r
104 System.out.println(mtrtempcomment.group());\r
105 System.out.println("-----------------------------");\r
106 }\r
a756211f 107 */\r
8c4eeeb6 108 Common.string2file(wholeline, filepath);\r
fed802b1 109 }\r
110 }\r
111 \r
112 public static void fireAt(String path) throws Exception {\r
8c4eeeb6 113 Critic critic = new Critic();\r
114 Common.toDoAll(Common.dirCopy_(path), critic);\r
fed802b1 115 System.out.println("Critic Done");\r
116 }\r
117}\r
8c4eeeb6 118//analyze func comment\r
119/*if (mtrcommentstructure.find()) {\r
120 newcomment.append("/*++\n\n" + mtrcommentstructure.group(1) + "\n\n");\r
121 \r
122 //System.out.println("-------1-------");\r
123 //System.out.println(mtrcommentstructure.group(1));\r
124 \r
125 // arg\r
126 //System.out.println("-------2-------");\r
127 //System.out.println(mtrcommentstructure.group(2));\r
128 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(2));\r
129 while (mtrinfequation.find()) {\r
130 newcomment.append("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");\r
131 //System.out.println("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));\r
132 }\r
133 newcomment.append("\n");\r
134 // return\r
135 //System.out.println("-------3-------");\r
136 //System.out.println(mtrcommentstructure.group(3));\r
137 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(3));\r
138 while (mtrinfequation.find()) {\r
139 newcomment.append("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");\r
140 //System.out.println("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));\r
141 }\r
142 System.out.println(newcomment);\r
143} else {\r
144 System.out.println("Error: Comment Style Incorrect");\r
145}*/