/* * Alabastra Project - C++ Editor writed with QT Library V.4 * Copyright (C) Igor Maculan - geocronos@gmail.com Marco Buoncristiano - marco.buoncristiano@gmail.com * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "AlSyntax.h" #include AlSyntax::AlSyntax(QObject *parent) : QSyntaxHighlighter(parent) {}; AlSyntax::AlSyntax(QTextEdit *parent): QSyntaxHighlighter(parent) {}; AlSyntax::AlSyntax(QTextDocument *parent): QSyntaxHighlighter(parent) {}; void AlSyntax::highlightBlock(const QString& block) { int index,length; //NUMERI INTERI QTextCharFormat numberFormat; numberFormat.setForeground(Qt::blue); QString numberPattern="\\b[0-9]+\\b"; QRegExp expression(numberPattern); index=block.indexOf(expression); while ( index >= 0 ) { length=expression.matchedLength(); setFormat(index,length,numberFormat); index=block.indexOf(expression,index+length); } //NUMERI REALI QTextCharFormat realNumberFormat; realNumberFormat.setForeground(Qt::red); QRegExp exp_realnumber("[0-9]*[.][0-9]*"); index=block.indexOf(exp_realnumber); while (index >=0){ length = exp_realnumber.matchedLength(); setFormat(index,length,realNumberFormat); index=block.indexOf(exp_realnumber,index+length); } //PAROLE CHIAVE QTextCharFormat keywordFormat; keywordFormat.setFontWeight(QFont::Bold); QString key_src="\\bif\\b|\\bwhile\\b|\\bfor\\b|\\bdo\\b|\\bnew\\b|\\breturn\\b|\\belse\\b"; key_src+="|\\bint\\b|\\bbool\\b|\\bfloat\\b|\\breal\\b|\\bQString\\b|\\bchar\\b|\\bstring\\b|\\btrue\\b|\\bfalse\\b|\\bvoid\\b"; key_src+="|\\bpublic\\b|\\bprivate\\b|\\bthis\\b|\\bconst\\b|\\bclass\\b"; QRegExp exp_keyword(key_src); index=block.indexOf(exp_keyword); while ( index >= 0) { length=exp_keyword.matchedLength(); setFormat(index,length,keywordFormat); index=block.indexOf(exp_keyword,index+length); } //PAROLE CHIAVE QT QTextCharFormat keywordFormatQT; keywordFormatQT.setFontWeight(QFont::Bold); keywordFormatQT.setForeground(Qt::blue); QString key_srcQT="\\bconnect\\b|\\bdisconnect\\b|\\bsignals\\b|\\bslots\\b|\\bSLOT\\b|\\bSIGNAL\\b|\\bQ_OBJECT\\b"; QRegExp exp_keywordQT(key_srcQT); index=block.indexOf(exp_keywordQT); while ( index >= 0) { length=exp_keywordQT.matchedLength(); setFormat(index,length,keywordFormatQT); index=block.indexOf(exp_keywordQT,index+length); } //INCLUDE QTextCharFormat includeFormat; includeFormat.setForeground(Qt::green); QRegExp exp_include ("#include\\b|#if\\b|#endif\\b|#define\\b|#ifndef\\b|#ifdef\\b"); index=block.indexOf(exp_include); while (index>=0){ length=exp_include.matchedLength(); setFormat(index,length,includeFormat); index=block.indexOf(exp_include,index+length); } //Commenti su pi righe /* */ QTextCharFormat commentFormat; commentFormat.setForeground(Qt::red); QRegExp startExpression("/\\*"); QRegExp endExpression("\\*/"); setCurrentBlockState(0); int startIndex = 0; int endIndex=-1; if (previousBlockState() != 1) startIndex = block.indexOf(startExpression); while (startIndex >= 0) { endIndex = block.indexOf(endExpression,startIndex); int commentLength; if (endIndex==-1) //se non si trovato il fine commento { setCurrentBlockState(1); commentLength = block.length() - startIndex; } else commentLength = endIndex - startIndex + endExpression.matchedLength(); setFormat(startIndex,commentLength,commentFormat); startIndex = block.indexOf( startExpression,startIndex + commentLength ); } //Commenti monoriga QRegExp exp_comment("//.*"); int commentIndex=block.indexOf(exp_comment); if (commentIndex>=0){ setFormat(commentIndex,block.length()-commentIndex,commentFormat); } if (currentBlockState()==0){ //STRINGHE QTextCharFormat stringFormat; stringFormat.setForeground(Qt::blue); QRegExp exp_string("([^\\\\]|^)\""); setCurrentBlockState(0); int string_start=0; if (previousBlockState()!=2) string_start = block.indexOf(exp_string); while (string_start >=0){ int stringLength=0; if (string_start>endIndex && (commentIndex==-1 || string_start