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
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.
13 package org
.tianocore
.migration
;
15 import java
.util
.regex
.*;
17 public class Critic
implements Common
.ForDoAll
{
25 private String filepath
= null;
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
;
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
);
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**/");
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");
59 mtrtempcomment
= ptntempcomment
.matcher(wholeline
);
60 while (mtrtempcomment
.find()) {
61 System
.out
.println("-----------------------------");
62 System
.out
.println(mtrtempcomment
.group());
63 System
.out
.println("-----------------------------");
65 Common
.string2file(wholeline
, filepath
);
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");
75 //analyze func comment
76 /*if (mtrcommentstructure.find()) {
77 newcomment.append("/*++\n\n" + mtrcommentstructure.group(1) + "\n\n");
79 //System.out.println("-------1-------");
80 //System.out.println(mtrcommentstructure.group(1));
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));
90 newcomment.append("\n");
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));
99 System.out.println(newcomment);
101 System.out.println("Error: Comment Style Incorrect");