PovrayHighlighter.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <QRegExp>
00027 #endif
00028 
00029 #include "PovrayHighlighter.h"
00030 
00031 using namespace RaytracingGui;
00032 
00033 namespace RaytracingGui {
00034 class PovrayHighlighterP
00035 {
00036 public:
00037     PovrayHighlighterP()
00038     {
00039         keywords << QLatin1String("include") << QLatin1String("if")
00040                  << QLatin1String("ifdef") << QLatin1String("ifndef")
00041                  << QLatin1String("switch") << QLatin1String("while")
00042                  << QLatin1String("macro") << QLatin1String("else")
00043                  << QLatin1String("end") << QLatin1String("declare")
00044                  << QLatin1String("local") << QLatin1String("undef")
00045                  << QLatin1String("fopen") << QLatin1String("fclose")
00046                  << QLatin1String("read") << QLatin1String("write")
00047                  << QLatin1String("default") << QLatin1String("version")
00048                  << QLatin1String("debug") << QLatin1String("case")
00049                  << QLatin1String("range") << QLatin1String("break")
00050                  << QLatin1String("error") << QLatin1String("warning");
00051 ;
00052     }
00053 
00054     QStringList keywords;
00055 };
00056 } // namespace RaytracingGui
00057 
00061 PovrayHighlighter::PovrayHighlighter(QObject* parent)
00062     : SyntaxHighlighter(parent)
00063 {
00064     d = new PovrayHighlighterP;
00065 }
00066 
00068 PovrayHighlighter::~PovrayHighlighter()
00069 {
00070     delete d;
00071 }
00072 
00073 void PovrayHighlighter::highlightBlock(const QString &text)
00074 {
00075     enum { NormalState = -1, InsideCStyleComment };
00076  
00077     int state = previousBlockState();
00078     int start = 0;
00079  
00080     for (int i = 0; i < text.length(); ++i) {
00081  
00082         if (state == InsideCStyleComment) {
00083             if (text.mid(i, 2) == QLatin1String("*/")) {
00084                 state = NormalState;
00085                 setFormat(start, i - start + 2, this->colorByType(SyntaxHighlighter::BlockComment));
00086             }
00087         }
00088         else {
00089             if (text.mid(i, 2) == QLatin1String("//")) {
00090                 setFormat(i, text.length() - i, this->colorByType(SyntaxHighlighter::Comment));
00091                 break;
00092             }
00093             else if (text.mid(i, 2) == QLatin1String("/*")) {
00094                 start = i;
00095                 state = InsideCStyleComment;
00096             }
00097             else if (text.mid(i,1) == QLatin1String("#")) {
00098                 QRegExp rx(QLatin1String("#\\s*(\\w*)"));
00099                 int pos = text.indexOf(rx, i);
00100                 if (pos != -1) {
00101                     if (d->keywords.contains(rx.cap(1)) != 0)
00102                         setFormat(i, rx.matchedLength(), this->colorByType(SyntaxHighlighter::Keyword));
00103                     i += rx.matchedLength();
00104                 }
00105             }
00106             else if (text[i] == QLatin1Char('"')) {
00107                 int j=i;
00108                 for (;j<text.length();j++) {
00109                     if (j > i && text[j] == QLatin1Char('"'))
00110                         break;
00111                 }
00112 
00113                 setFormat(i, j-i+1, this->colorByType(SyntaxHighlighter::String));
00114                 i = j;
00115             }
00116         }
00117     }
00118     if (state == InsideCStyleComment)
00119         setFormat(start, text.length() - start, this->colorByType(SyntaxHighlighter::BlockComment));
00120  
00121     setCurrentBlockState(state);
00122 }

Generated on Wed Nov 23 19:00:28 2011 for FreeCAD by  doxygen 1.6.1