ij-16.png   inspectorJ -- JavaTM Profiler
sf project site browse source checkout source
SourceForge.net Logo



src/inspectorj/model/bytecodehighlighter.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   inspectorJ - java profiler                                            *
00003  *   Copyright (C) 2007 by James May
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00027 #include <QtGui>
00028 #include "inspectorj/model/bytecodehighlighter.h"
00029 
00030 namespace inspectorj {
00031 namespace model {
00032 
00033 BytecodeHighlighter::BytecodeHighlighter(QTextDocument *parent)
00034     : QSyntaxHighlighter(parent)
00035 {
00036     HighlightingRule rule;
00037 
00038     keywordFormat.setForeground(Qt::darkBlue);
00039     keywordFormat.setFontWeight(QFont::Bold);
00040     
00041     QStringList keywordPatterns;
00042     keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bfinal\\b"
00043                     << "\\bdouble\\b" << "\\benum\\b" << "\\bextends\\b"
00044                     << "\\bint\\b" << "\\bsynchronized\\b" << "\\blong\\b" 
00045                     << "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
00046                     << "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
00047                     << "\\bstatic\\b" << "\\bstruct\\b" << "\\bimplements\\b"                    
00048                     << "\\bunsigned\\b" << "\\bvirtual\\b" << "\\bsynthetic\\b"
00049                     << "\\babstract\\b" << "\\bvoid\\b" << "\\bvolatile\\b" << "\\binterface\\b";
00050     foreach (QString pattern, keywordPatterns) {
00051         rule.pattern = QRegExp(pattern);
00052         rule.format = keywordFormat;
00053         highlightingRules.append(rule);
00054     }
00055         
00056     headerFormat.setForeground(Qt::darkGreen);
00057     headerFormat.setFontWeight(QFont::Bold);
00058         
00059     QStringList headers;
00060     headers << "\\bCode:" << "\\bSignature:" << "\\bLineNumberTable:"
00061             << "\\bLocalVariableTable:" << "\\bException table:" << "\\bConstant value:";
00062 
00063     foreach (QString pattern, headers) {
00064         rule.pattern = QRegExp(pattern);
00065         rule.format = headerFormat;
00066         highlightingRules.append(rule);
00067     }
00068     
00069     numberFormat.setForeground(Qt::blue);
00070         
00071     QStringList numbers;
00072     numbers << "\\b[0-9]+\\b" << "\\b[0-9]+:" << "Bytecode generation failed."
00073             << "inspectorJ";
00074     foreach (QString pattern, numbers) {
00075         rule.pattern = QRegExp(pattern);
00076         rule.format = numberFormat;
00077         highlightingRules.append(rule);
00078     }    
00079 
00080     errFormat.setForeground(Qt::red);
00081     rule.pattern = QRegExp("ERROR:");
00082     rule.format = errFormat;
00083     highlightingRules.append(rule);
00084     
00085     classFormat.setFontWeight(QFont::Bold);
00086     classFormat.setForeground(Qt::darkMagenta);
00087     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
00088     rule.format = classFormat;
00089     highlightingRules.append(rule);
00090 
00091     singleLineCommentFormat.setForeground(Qt::darkYellow);
00092     rule.pattern = QRegExp("//[^\n]*");
00093     rule.format = singleLineCommentFormat;
00094     highlightingRules.append(rule);
00095 
00096     multiLineCommentFormat.setForeground(Qt::darkYellow);
00097 
00098     quotationFormat.setForeground(Qt::darkGreen);
00099     rule.pattern = QRegExp("\".*\"");
00100     rule.format = quotationFormat;
00101     highlightingRules.append(rule);
00102 
00103     functionFormat.setFontItalic(true);
00104     functionFormat.setForeground(Qt::blue);
00105     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
00106     rule.format = functionFormat;
00107     highlightingRules.append(rule);
00108 
00109     commentStartExpression = QRegExp("/\\*");
00110     commentEndExpression = QRegExp("\\*/");
00111 }
00112 
00113 void BytecodeHighlighter::highlightBlock(const QString &text)
00114 {
00115     foreach (HighlightingRule rule, highlightingRules) {
00116         QRegExp expression(rule.pattern);
00117         int index = text.indexOf(expression);
00118         while (index >= 0) {
00119             int length = expression.matchedLength();
00120             setFormat(index, length, rule.format);
00121             index = text.indexOf(expression, index + length);
00122         }
00123     }
00124     setCurrentBlockState(0);
00125 
00126     int startIndex = 0;
00127     if (previousBlockState() != 1)
00128         startIndex = text.indexOf(commentStartExpression);
00129 
00130     while (startIndex >= 0) {
00131         int endIndex = text.indexOf(commentEndExpression, startIndex);
00132         int commentLength;
00133         if (endIndex == -1) {
00134             setCurrentBlockState(1);
00135             commentLength = text.length() - startIndex;
00136         } else {
00137             commentLength = endIndex - startIndex
00138                             + commentEndExpression.matchedLength();
00139         }
00140         setFormat(startIndex, commentLength, multiLineCommentFormat);
00141         startIndex = text.indexOf(commentStartExpression,
00142                                                 startIndex + commentLength);
00143     }
00144 }
00145 
00146 } // end namespace model
00147 } // end namespace inspectorj

Generated on Sun Aug 19 17:07:53 2007 for inspectorJ by  doxygen 1.5.1