]>
Commit | Line | Data |
---|---|---|
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 | 13 | package org.tianocore.migration;\r |
14 | \r | |
15 | import java.util.regex.*;\r | |
3637381b | 16 | import java.io.*;\r |
fed802b1 | 17 | \r |
18 | public 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 |
7934aee7 | 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 |
fed802b1 | 24 | \r |
25 | public void toDo(String filepath) throws Exception {\r | |
26 | if (filepath.contains(".c") || filepath.contains(".h")) {\r | |
3637381b | 27 | BufferedReader rd = null;\r |
28 | String line = null;\r | |
29 | StringBuffer templine = new StringBuffer();\r | |
1af2e90c | 30 | boolean incomment = false;\r |
3637381b | 31 | boolean description = false;\r |
32 | boolean arguments = false;\r | |
33 | boolean returns = false;\r | |
7934aee7 | 34 | boolean inequation = false;\r |
3637381b | 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 | |
7934aee7 | 68 | inequation = true;\r |
3637381b | 69 | templine.append(" @param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");\r |
7934aee7 | 70 | } else if (inequation && line.trim().length() == 0) {\r |
71 | inequation = false;\r | |
72 | templine.append(line + "\n");\r | |
73 | } else if (inequation && line.trim().length() != 0) {\r | |
74 | templine.append("#%#%" + line + "\n");\r | |
3637381b | 75 | } else {\r |
76 | templine.append(line + "\n");\r | |
77 | }\r | |
1af2e90c | 78 | } else if (incomment && returns) {\r |
3637381b | 79 | mtrinfequation = ptninfequation.matcher(line);\r |
80 | if (mtrinfequation.find()) {\r | |
7934aee7 | 81 | inequation = true;\r |
3637381b | 82 | templine.append(" @retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");\r |
7934aee7 | 83 | } else if (inequation && line.trim().length() == 0) {\r |
84 | inequation = false;\r | |
85 | templine.append(line + "\n");\r | |
86 | } else if (inequation && line.trim().length() != 0) {\r | |
87 | templine.append("#%#%" + line + "\n");\r | |
3637381b | 88 | } else {\r |
89 | templine.append(line + "\n");\r | |
90 | }\r | |
3637381b | 91 | } else {\r |
92 | templine.append(line + "\n");\r | |
93 | }\r | |
94 | }\r | |
1af2e90c | 95 | wholeline = templine.toString();\r |
7934aee7 | 96 | wholeline = wholeline.replaceAll("\n#%#%\\s*", " ");\r |
1af2e90c | 97 | \r |
a756211f | 98 | /* -----slow edition of replacefirst with stringbuffer-----\r |
99 | line.append(wholeline);\r | |
100 | mtrfunccomment = ptnfunccomment.matcher(line);\r | |
8c4eeeb6 | 101 | while (mtrfunccomment.find()) {\r |
a756211f | 102 | line.replace(0, line.length()-1, mtrfunccomment.replaceFirst("$2$4$3$1$5"));\r |
8c4eeeb6 | 103 | }\r |
a756211f | 104 | */\r |
1af2e90c | 105 | /* -----slow edition of replacefirst with string-----\r |
a756211f | 106 | while ((mtrfunccomment = ptnfunccomment.matcher(wholeline)).find()) {\r |
107 | //funccomment = mtrfunccomment.group(2);\r | |
108 | //mtrcommentstructure = ptncommentstructure.matcher(funccomment);\r | |
109 | wholeline = mtrfunccomment.replaceFirst("$2$4$3$1$5");\r | |
110 | }\r | |
1af2e90c | 111 | */\r |
a756211f | 112 | /*\r |
8c4eeeb6 | 113 | // edit func comment\r |
114 | mtrtempcomment = ptntempcomment.matcher(wholeline);\r | |
115 | while (mtrtempcomment.find()) {\r | |
116 | System.out.println("-----------------------------");\r | |
117 | System.out.println(mtrtempcomment.group());\r | |
118 | System.out.println("-----------------------------");\r | |
119 | }\r | |
a756211f | 120 | */\r |
8c4eeeb6 | 121 | Common.string2file(wholeline, filepath);\r |
fed802b1 | 122 | }\r |
123 | }\r | |
124 | \r | |
125 | public static void fireAt(String path) throws Exception {\r | |
8c4eeeb6 | 126 | Critic critic = new Critic();\r |
127 | Common.toDoAll(Common.dirCopy_(path), critic);\r | |
fed802b1 | 128 | System.out.println("Critic Done");\r |
129 | }\r | |
130 | }\r | |
8c4eeeb6 | 131 | //analyze func comment\r |
132 | /*if (mtrcommentstructure.find()) {\r | |
133 | newcomment.append("/*++\n\n" + mtrcommentstructure.group(1) + "\n\n");\r | |
134 | \r | |
135 | //System.out.println("-------1-------");\r | |
136 | //System.out.println(mtrcommentstructure.group(1));\r | |
137 | \r | |
138 | // arg\r | |
139 | //System.out.println("-------2-------");\r | |
140 | //System.out.println(mtrcommentstructure.group(2));\r | |
141 | mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(2));\r | |
142 | while (mtrinfequation.find()) {\r | |
143 | newcomment.append("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");\r | |
144 | //System.out.println("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));\r | |
145 | }\r | |
146 | newcomment.append("\n");\r | |
147 | // return\r | |
148 | //System.out.println("-------3-------");\r | |
149 | //System.out.println(mtrcommentstructure.group(3));\r | |
150 | mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(3));\r | |
151 | while (mtrinfequation.find()) {\r | |
152 | newcomment.append("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");\r | |
153 | //System.out.println("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));\r | |
154 | }\r | |
155 | System.out.println(newcomment);\r | |
156 | } else {\r | |
157 | System.out.println("Error: Comment Style Incorrect");\r | |
158 | }*/ |