]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/Critic.java
821305edf0035ae83508ea2ea31460459c375614
[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
17 public class Critic implements Common.ForDoAll {
18 Critic() {
19 filepath = null;
20 }
21 Critic(String path) {
22 filepath = path;
23 }
24
25 private String filepath = null;
26
27 private static Pattern ptnheadcomment = Pattern.compile("^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/",Pattern.DOTALL);
28 private static Matcher mtrheadcomment;
29 private static Pattern ptnfunccomment = Pattern.compile("([\\w\\d]*\\s*[_\\w][_\\w\\d]*\\s*\\([^\\)\\(]*\\)\\s*)(\\/\\*\\+\\+.*?)(\\-\\-\\*\\/\\s*)(.*?)([\\{;])",Pattern.DOTALL);
30 private static Matcher mtrfunccomment;
31 private static Pattern ptncommentstructure = Pattern.compile("Routine Description:\\s*(\\w.*?\\w)\\s*Arguments:(\\s*\\w.*?\\w\\s*)Returns:(\\s*\\w.*?\\w\\s*)&%",Pattern.DOTALL);
32 private static Matcher mtrcommentstructure;
33 private static Pattern ptntempcomment = Pattern.compile("\\/\\*\\+\\+(.*?)\\-\\-\\*\\/\\s*[\\w\\d]*\\s*[_\\w][_\\w\\d]*\\s*\\([^\\)\\(]*\\)",Pattern.DOTALL);
34 private static Matcher mtrtempcomment;
35 private static Pattern ptninfequation = Pattern.compile("([^\\s]*)\\s*-\\s*(\\w.*\\w)");
36 private static Matcher mtrinfequation;
37
38 public void toDo(String filepath) throws Exception {
39 String funccomment = null;
40 if (filepath.contains(".c") || filepath.contains(".h")) {
41 System.out.println("Criticing " + filepath);
42 String wholeline = Common.file2string(filepath);
43
44 // find head comment
45 mtrheadcomment = ptnheadcomment.matcher(wholeline);
46 if (mtrheadcomment.find()) { //as we find only the head comment here, use 'if' not 'while'
47 wholeline = mtrheadcomment.replaceFirst("/** @file$1**/");
48 }
49
50 // find func comment
51 mtrfunccomment = ptnfunccomment.matcher(wholeline);
52 while (mtrfunccomment.find()) {
53 funccomment = mtrfunccomment.group(2) + "&%";
54 mtrcommentstructure = ptncommentstructure.matcher(funccomment);
55 wholeline = mtrfunccomment.replaceAll("$2$4$3$1$5");
56 }
57
58 // edit func comment
59 mtrtempcomment = ptntempcomment.matcher(wholeline);
60 while (mtrtempcomment.find()) {
61 System.out.println("-----------------------------");
62 System.out.println(mtrtempcomment.group());
63 System.out.println("-----------------------------");
64 }
65 Common.string2file(wholeline, filepath);
66 }
67 }
68
69 public static void fireAt(String path) throws Exception {
70 Critic critic = new Critic();
71 Common.toDoAll(Common.dirCopy_(path), critic);
72 System.out.println("Critic Done");
73 }
74 }
75 //analyze func comment
76 /*if (mtrcommentstructure.find()) {
77 newcomment.append("/*++\n\n" + mtrcommentstructure.group(1) + "\n\n");
78
79 //System.out.println("-------1-------");
80 //System.out.println(mtrcommentstructure.group(1));
81
82 // arg
83 //System.out.println("-------2-------");
84 //System.out.println(mtrcommentstructure.group(2));
85 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(2));
86 while (mtrinfequation.find()) {
87 newcomment.append("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
88 //System.out.println("@param " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
89 }
90 newcomment.append("\n");
91 // return
92 //System.out.println("-------3-------");
93 //System.out.println(mtrcommentstructure.group(3));
94 mtrinfequation = ptninfequation.matcher(mtrcommentstructure.group(3));
95 while (mtrinfequation.find()) {
96 newcomment.append("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2) + "\n");
97 //System.out.println("@retval " + mtrinfequation.group(1) + " " + mtrinfequation.group(2));
98 }
99 System.out.println(newcomment);
100 } else {
101 System.out.println("Error: Comment Style Incorrect");
102 }*/