Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/syntax.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                               syntax.cpp
00003                              ------------
00004     begin                : Sat Mar 11 2006
00005     copyright            : (C) 2006 by Michael Margraf
00006     email                : michael.margraf@alumni.tu-berlin.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 // *****************************************************************
00019 // *********                                              **********
00020 // *********  The class that does the syntax highlighting **********
00021 // *********                                              **********
00022 // *****************************************************************
00023 
00024 #include "textdoc.h"
00025 #include "syntax.h"
00026 
00027 
00028 SyntaxHighlighter::SyntaxHighlighter(TextDoc *textEdit) : QSyntaxHighlighter(textEdit)
00029 {
00030   Doc = textEdit;
00031   language = LANG_NONE;
00032 
00033   reservedWordFormat.setForeground(Qt::darkBlue);
00034   reservedWordFormat.setFontWeight(QFont::Bold);
00035 
00036   unitFormat.setForeground(Qt::darkRed);;
00037   unitFormat.setFontWeight(QFont::StyleItalic);;
00038 
00039   datatypeFormat.setForeground(Qt::darkBlue);;
00040   datatypeFormat.setFontWeight(QFont::Bold);;
00041 
00042   directiveFormat.setForeground(Qt::darkBlue);;
00043   directiveFormat.setFontWeight(QFont::Bold);;
00044 
00045   functionFormat.setForeground(Qt::darkGreen);;
00046   functionFormat.setFontWeight(QFont::Bold);;
00047 
00048   commentFormat.setForeground(Qt::gray);;
00049   commentFormat.setFontWeight(QFont::StyleItalic);;
00050 
00051 }
00052 
00053 SyntaxHighlighter::~SyntaxHighlighter()
00054 {
00055 }
00056 
00057 // ---------------------------------------------------
00058 void SyntaxHighlighter::setLanguage(int lang)
00059 {
00060   language = lang;
00061 
00062   HighlightingRule rule;
00063 
00064   QStringList reservedWordPattern;
00065   QStringList unitPattern;
00066   QStringList datatypePattern;
00067   QStringList directivePattern;
00068   QStringList functionPattern;
00069   QStringList commentPattern;
00070 
00071   switch (language) {
00072     case LANG_VHDL:
00073         reservedWordPattern << "\\babs\\b" << "\\baccess\\b" << "\\bafter\\b" << "\\balias\\b" << "\\ball\\b" << "\\band\\b" <<
00074                         "\\barchitecture\\b\\b"<< "\\barray\\b"<< "\\bassert\\b" << "\\battribute\\b"<<
00075                         "\\bbegin\\b\\b"<< "\\bblock\\b"<< "\\bbody\\b"<< "\\bbuffer\\b"<< "\\bbus\\b"<<
00076                         "\\bcase\\b"<< "\\bcomponent\\b"<< "\\bconfiguration\\b"<< "\\bconstant\\b"<<
00077                         "\\bdisconnect\\b"<< "\\bdownto\\b"<<
00078                         "\\belse\\b"<< "\\belsif\\b"<< "\\bend\\b"<< "\\bentity\\b"<< "\\bexit\\b"<<
00079                         "\\bfile\\b"<< "\\bfor\\b"<< "\\bfunction"
00080                         "\\bgenerate\\b"<< "\\bgeneric\\b"<< "\\bgroup\\b"<< "\\bguarded"
00081                         "\\bif\\b"<< "\\bimpure\\b"<< "\\bin\\b"<< "\\binertial\\b"<< "\\binout\\b"<< "\\bis\\b"<<
00082                         "\\blabel\\b"<< "\\blibrary\\b"<< "\\blinkage\\b"<< "\\bliteral\\b"<< "\\bloop\\b"<<
00083                         "\\bmap\\b"<< "\\bmod\\b"<< 
00084                         "\\bnand\\b"<< "\\bnew\\b"<< "\\bnext\\b"<< "\\bnor\\b"<< "\\bnot\\b"<< "\\bnull"
00085                         "\\bof\\b"<< "\\bon\\b"<< "\\bopen\\b"<< "\\bor\\b"<< "\\bothers\\b"<< "\\bout\\b"<<
00086                         "\\bpackage\\b"<< "\\bport\\b"<< "\\bpostponed\\b"<< "\\bprocedure\\b"<< "\\bprocess\\b"<< "\\bpure\\b"<<
00087                         "\\brange\\b"<< "\\brecord\\b"<< "\\bregister\\b"<< "\\breject\\b"<< "\\brem\\b"<< "\\breport\\b"<< "\\breturn\\b"<< "\\brol\\b"<< "\\bror\\b"<<
00088                         "\\bselect\\b"<< "\\bseverity\\b"<< "\\bshared\\b"<< "\\bsignal\\b"<< "\\bsla\\b"<< "\\bsll\\b"<< "\\bsra\\b"<< "\\bsrl\\b"<< "\\bsubtype\\b"<<
00089                         "\\bthen\\b"<< "\\bto\\b"<< "\\btransport\\b"<< "\\btype\\b"<<
00090                         "\\bunaffected\\b"<< "\\bunits\\b"<< "\\buntil\\b"<< "\\buse\\b"<<
00091                         "\\bvariable\\b"<<
00092                         "\\bwait\\b"<< "\\bwhen\\b"<< "\\bwhile\\b"<< "\\bwith\\b"<<
00093                         "\\bxnor\\b"<< "\\bxor\\b";
00094 
00095           unitPattern <<  "\\bfs\\b"<< "\\bps\\b"<< "\\bns\\b"<< "\\bus\\b"<< "\\bms\\b"<< "\\bsec\\b"<< "\\bmin\\b"<< "\\bhr\\b";
00096 
00097           datatypePattern << "\\bbit\\b"<< "\\bbit_vector\\b"<< "\\bboolean\\b"<< "\\bstd_logic\\b"<< "\\bstd_logic_vector\\b"<< "\\bstd_ulogic\\b"<< "\\bstd_ulogic_vector\\b"<< "\\bsigned\\b"<< "\\bunsigned\\b"<< "\\binteger\\b"<< "\\breal\\b"<< "\\btime\\b"<< "\\bcharacter\\b"<< "\\bnatural\\b";
00098 
00099           directivePattern << "\\bactive\\b"<< "\\bascending\\b" <<
00100                         "\\bbase\\b" <<
00101                         "\\bdelayed\\b" <<
00102                         "\\bevent\\b" <<
00103                         "\\bhigh\\b" <<
00104                         "\\bimage\\b" <<
00105                         "\\blast_active\\b"<< "\\blast_event\\b"<< "\\blast_value\\b"<< "\\bleft\\b"<< "\\bleftof\\b"<< "\\blength\\b"<< "\\blow\\b" <<
00106                         "\\bpos\\b"<< "\\bpred\\b"<<
00107                         "\\bquiet\\b"<<
00108                         "\\brange\\b"<< "\\breverse_range\\b"<< "\\bright\\b"<< "\\brightof\\b"<<
00109                         "\\bstable\\b"<< "\\bsucc\\b"<<
00110                         "\\btransaction\\b"<<
00111                         "\\bval\\b"<< "\\bvalue\\b";
00112 
00113           commentPattern << "--[^\n]*";
00114 
00115         break;
00116 
00117   case LANG_VERILOG:
00118         reservedWordPattern << "\\balways\\b"<< "\\band\\b"<< "\\bassign\\b"<< "\\battribute\\b"<<
00119                         "\\bbegin\\b"<< "\\bbuf\\b"<< "\\bbufif0\\b"<< "\\bbufif1\\b"<<
00120                         "\\bcase\\b"<< "\\bcasex\\b"<< "\\bcasez\\b" <<  "\\bcmos\\b"<< 
00121                         "\\bdeassign\\b"<< "\\bdefault\\b"<< "\\bdefparam\\b"<< "\\bdisable\\b"<<
00122                         "\\bedge\\b"<< "\\belse\\b"<< "\\bend\\b"<< "\\bendattribute\\b"<< "\\bendcase\\b"<< "\\bendfunction\\b"<< "\\bendmodule\\b"<< "\\bendprimitive\\b"<< "\\bendspecify\\b"<< "\\bendtable\\b"<< "\\bendtask\\b"<< "\\bevent\\b"<<
00123                         "\\bfor\\b"<< "\\bforce\\b"<< "\\bforever\\b"<< "\\bfork\\b"<< "\\bfunction\\b"<<
00124                         "\\bhighz0\\b"<< "\\bhighz1\\b"<<
00125                         "\\bif\\b"<< "\\bifnone\\b"<< "\\binitial\\b"<< "\\binout\\b"<< "\\binput\\b"<<
00126                         "\\bjoin\\b"<<
00127                         "\\blarge\\b"<<
00128                         "\\bmedium\\b"<< "\\bmodule\\b"<< "\\bmacromodule\\b"<<
00129                         "\\bnand\\b"<< "\\bnegedge\\b"<< "\\bnmos\\b"<< "\\bnor\\b"<< "\\bnot\\b"<< "\\bnotif0\\b"<< "\\bnotif1\\b"<<
00130                         "\\bor\\b"<< "\\boutput\\b"<<
00131                         "\\bpmos\\b"<< "\\bposedge\\b"<< "\\bprimitive\\b"<< "\\bpull0\\b"<< "\\bpull1\\b"<< "\\bpulldown\\b"<< "\\bpullup\\b"<<
00132                         "\\brcmos\\b"<< "\\brelease\\b"<< "\\brepeat\\b"<< "\\brnmos\\b"<< "\\brpmos\\b"<< "\\brtran\\b"<< "\\brtranif0\\b"<< "\\brtranif1\\b"<<
00133                         "\\bscalared\\b"<< "\\bsigned\\b"<< "\\bsmall\\b"<< "\\bspecify\\b"<< "\\bstrength\\b"<< "\\bstrong0\\b"<< "\\bstrong1\\b"<<
00134                         "\\btable\\b"<< "\\btask\\b"<< "\\btran\\b"<< "\\btranif0\\b"<< "\\btranif1\\b"<<
00135                         "\\bunsigned\\b"<<
00136                         "\\bvectored\\b"<<
00137                         "\\bwait\\b"<< "\\bweak0\\b"<< "\\bweak1\\b"<< "\\bwhile\\b"<<
00138                         "\\bxnor\\b"<< "\\bxor\\b";
00139 
00140         directivePattern << "\\breset_all\\b"<< "\\btimescale\\b"<< "\\bdefine\\b"<< "\\binclude\\b"<< "\\bifdef\\b"<< "\\belse\\b"<< "\\bendif\\b"<<
00141                           "\\bcelldefine\\b"<< "\\bendcelldefine\\b"<< "\\bdefault_nettype\\b"<< "\\bunconnected_drive\\b"<<
00142                           "\\bnounconnected_drive\\b"<< "\\bdelay_mode_zero\\b"<< "\\bdelay_mode_unit\\b"<<
00143                           "\\bdelay_mode_path\\b"<< "\\bdelay_mode_distributed\\b"<< "\\buselib\\b";
00144 
00145         datatypePattern << "\\breg\\b"<< "\\binteger\\b"<< "\\btime\\b"<< "\\breal\\b"<< "\\brealtime\\b"<< "\\bwire\\b"<< "\\btri\\b"<< "\\bwor\\b"<<
00146                           "\\btrior\\b"<< "\\bwand\\b"<< "\\btriand\\b"<< "\\btri0\\b"<< "\\btri1\\b"<< "\\bsupply0\\b"<< "\\bsupply1\\b"<< "\\btrireg\\b"<<
00147                           "\\bparameter\\b"<< "\\bspecparam\\b"<< "\\bevent\\b";
00148 
00149         functionPattern <<  "\\bsetup\\b"<< "\\bhold\\b"<< "\\bsetuphold\\b"<< "\\bskew\\b"<< "\\brecovery\\b"<< "\\bperiod\\b"<< "\\bwidth\\b"<<
00150                           "\\bmonitor\\b"<< "\\bdisplay\\b"<< "\\bwrite\\b"<< "\\bstrobe\\b"<< "\\bfopen\\b"<< "\\bfclose\\b"<< "\\btime\\b"<<
00151                           "\\bstime\\b"<< "\\brealtime\\b"<< "\\btimeformat\\b"<< "\\bprinttimescale\\b"<< "\\brandom\\b"<< "\\breadmemb\\b"<<
00152                           "\\breadmemh\\b"<< "\\bfinish\\b"<< "\\bstop\\b";
00153 
00154         commentPattern << "//[^\n]*";
00155 
00156     break;
00157 
00158   case LANG_VERILOGA:
00159 
00160         reservedWordPattern << "\\babstol\\b"<< "\\baccess\\b"<< "\\banalog\\b"<< "\\bac_stim\\b"<< "\\banalysis\\b"<<
00161                           "\\bbegin\\b"<< "\\bbranch\\b"<< "\\bbound_step\\b"<<
00162                           "\\bcase\\b"<<
00163                           "\\bdiscipline\\b"<< "\\bddt_nature\\b"<< "\\bddt\\b"<< "\\bdelay\\b"<< "\\bdiscontinuity\\b"<< "\\bdefault\\b"<< 
00164                           "\\benddiscipline\\b"<< "\\belse\\b"<< "\\bend\\b"<< "\\bendnature\\b"<< "\\bexclude\\b"<< "\\bendfunction\\b"<< "\\bendmodule\\b"<< "\\belectrical\\b"<< "\\bendcase\\b"<<
00165                           "\\bfor\\b"<< "\\bflow\\b"<< "\\bfrom\\b"<< "\\bfinal_step\\b"<< "\\bflicker_noise\\b"<< "\\bfunction\\b"<<
00166                           "\\bgenerate\\b"<< "\\bground\\b"<<
00167                           "\\bif\\b"<< "\\bidt_nature\\b"<< "\\binf\\b"<< "\\bidt\\b"<< "\\binitial_step\\b"<< "\\binput\\b"<< "\\binout\\b"<<
00168                           "\\blaplace_nd\\b"<< "\\blaplace_np\\b"<< "\\blaplace_zd\\b"<< "\\blaplace_zp\\b"<< "\\blast_crossing\\b"<<
00169                           "\\bmodule\\b"<<
00170                           "\\bnature\\b"<< "\\bnoise_table\\b"<<
00171                           "\\bpotential\\b"<< "\\bparameter\\b"<<
00172                           "\\bslew\\b"<<
00173                           "\\btimer\\b"<< "\\btransition\\b"<<
00174                           "\\bunits\\b"<<
00175                           "\\bwhite_noise\\b"<< "\\bwhile\\b"<<
00176                           "\\bzi_nd\\b"<< "\\bzi_np\\b"<< "\\bzi_zd\\b"<< "\\bzi_zp\\b";
00177 
00178         unitPattern << "\\bT\\b"<< "\\bG\\b"<< "\\bM\\b"<< "\\bK\\b"<< "\\bm\\b"<< "\\bu\\b"<< "\\bn\\b"<< "\\bp\\b"<< "\\bf\\b"<< "\\ba\\b";
00179 
00180         datatypePattern << "\\binteger\\b"<< "\\breal\\b";
00181 
00182         directivePattern << "\\bdefine\\b"<< "\\belse\\b"<< "\\bundef\\b"<< "\\bifdef\\b"<< "\\bendif\\b"<< "\\binclude\\b"<< "\\bresetall\\b";
00183 
00184         functionPattern << "\\brealtime\\b"<< "\\btemperature\\b"<< "\\bvt\\b"<< "\\bdisplay\\b"<< "\\bstrobe\\b";
00185 
00186         commentPattern << "//[^\n]*";
00187 
00188     break;
00189   case LANG_OCTAVE:
00190       
00191         reservedWordPattern << "\\bcase\\b"<< "\\bcatch\\b"<<
00192                           "\\belse\\b"<< "\\belseif\\b"<< "\\bend\\b"<< "\\bendfor\\b"<< "\\bendfunction\\b"<< "\\bendif\\b"<< "\\bendswitch\\b"<< "\\bend_try_catch\\b"<< "\\bendwhile\\b"<< "\\bend_unwind_protect\\b"
00193                           "\\bfor\\b" << "\\bfunction\\b" <<
00194                           "\\bif\\b" <<
00195                           "\\botherwise\\b" <<
00196                           "\\bswitch\\b" <<
00197                           "\\btry\\b" <<
00198                           "\\bunwind_protect\\b"<< "\\bunwind_protect_cleanup\\b" <<
00199                           "\\bwhile\\b";
00200 
00201         datatypePattern << "\\binf\\b"<< "\\bnan\\b"<< "\\bpi\\b";
00202 
00203         functionPattern << "\\bplot\\b";
00204 
00205         commentPattern << "//[^\n]*";
00206 
00207     break;
00208     }
00209 
00210   foreach (const QString &pattern, reservedWordPattern) {
00211     rule.pattern = QRegExp(pattern);
00212     rule.format = reservedWordFormat;
00213     highlightingRules.append(rule);
00214   }
00215 
00216   foreach (const QString &pattern, unitPattern) {
00217     rule.pattern = QRegExp(pattern);
00218     rule.format = unitFormat;
00219     highlightingRules.append(rule);
00220   }
00221 
00222   foreach (const QString &pattern, datatypePattern) {
00223     rule.pattern = QRegExp(pattern);
00224     rule.format = datatypeFormat;
00225     highlightingRules.append(rule);
00226   }
00227 
00228   foreach (const QString &pattern, directivePattern) {
00229     rule.pattern = QRegExp(pattern);
00230     rule.format = directiveFormat;
00231     highlightingRules.append(rule);
00232   }
00233 
00234   foreach (const QString &pattern, functionPattern) {
00235     rule.pattern = QRegExp(pattern);
00236     rule.format = functionFormat;
00237     highlightingRules.append(rule);
00238   }
00239 
00240   foreach (const QString &pattern, commentPattern) {
00241     rule.pattern = QRegExp(pattern);
00242     rule.format = commentFormat;
00243     highlightingRules.append(rule);
00244   }
00245 }
00246 
00247 // ---------------------------------------------------
00248 void SyntaxHighlighter::highlightBlock(const QString& text) {
00249 
00250 
00251 foreach (const HighlightingRule &rule, highlightingRules) {
00252          QRegExp expression(rule.pattern);
00253          int index = expression.indexIn(text);
00254          while (index >= 0) {
00255              int length = expression.matchedLength();
00256              setFormat(index, length, rule.format);
00257              index = expression.indexIn(text, index + length);
00258          }
00259   }
00260 
00261 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines