Qucs-GUI  0.0.18
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
syntax.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  syntax.cpp
3  ------------
4  begin : Sat Mar 11 2006
5  copyright : (C) 2006 by Michael Margraf
6  email : michael.margraf@alumni.tu-berlin.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 // *****************************************************************
19 // ********* **********
20 // ********* The class that does the syntax highlighting **********
21 // ********* **********
22 // *****************************************************************
23 #include <QtGui>
24 
25 #include "textdoc.h"
26 #include "syntax.h"
27 
28 
29 SyntaxHighlighter::SyntaxHighlighter(TextDoc *textEdit) : QSyntaxHighlighter(textEdit)
30 {
31  Doc = textEdit;
33 
34  reservedWordFormat.setForeground(Qt::darkBlue);
35  reservedWordFormat.setFontWeight(QFont::Bold);
36 
37  unitFormat.setForeground(Qt::darkRed);;
38  unitFormat.setFontWeight(QFont::StyleItalic);;
39 
40  datatypeFormat.setForeground(Qt::darkBlue);;
41  datatypeFormat.setFontWeight(QFont::Bold);;
42 
43  directiveFormat.setForeground(Qt::darkBlue);;
44  directiveFormat.setFontWeight(QFont::Bold);;
45 
46  functionFormat.setForeground(Qt::darkGreen);;
47  functionFormat.setFontWeight(QFont::Bold);;
48 
49  commentFormat.setForeground(Qt::gray);;
50  commentFormat.setFontWeight(QFont::StyleItalic);;
51 
52 }
53 
55 {
56 }
57 
58 // ---------------------------------------------------
60 {
61  language = lang;
62 
63  HighlightingRule rule;
64 
65  QStringList reservedWordPattern;
66  QStringList unitPattern;
67  QStringList datatypePattern;
68  QStringList directivePattern;
69  QStringList functionPattern;
70  QStringList commentPattern;
71 
72  switch (language) {
73  case LANG_VHDL:
74  reservedWordPattern << "\\babs\\b" << "\\baccess\\b" << "\\bafter\\b" << "\\balias\\b" << "\\ball\\b" << "\\band\\b" <<
75  "\\barchitecture\\b\\b"<< "\\barray\\b"<< "\\bassert\\b" << "\\battribute\\b"<<
76  "\\bbegin\\b\\b"<< "\\bblock\\b"<< "\\bbody\\b"<< "\\bbuffer\\b"<< "\\bbus\\b"<<
77  "\\bcase\\b"<< "\\bcomponent\\b"<< "\\bconfiguration\\b"<< "\\bconstant\\b"<<
78  "\\bdisconnect\\b"<< "\\bdownto\\b"<<
79  "\\belse\\b"<< "\\belsif\\b"<< "\\bend\\b"<< "\\bentity\\b"<< "\\bexit\\b"<<
80  "\\bfile\\b"<< "\\bfor\\b"<< "\\bfunction"
81  "\\bgenerate\\b"<< "\\bgeneric\\b"<< "\\bgroup\\b"<< "\\bguarded"
82  "\\bif\\b"<< "\\bimpure\\b"<< "\\bin\\b"<< "\\binertial\\b"<< "\\binout\\b"<< "\\bis\\b"<<
83  "\\blabel\\b"<< "\\blibrary\\b"<< "\\blinkage\\b"<< "\\bliteral\\b"<< "\\bloop\\b"<<
84  "\\bmap\\b"<< "\\bmod\\b"<<
85  "\\bnand\\b"<< "\\bnew\\b"<< "\\bnext\\b"<< "\\bnor\\b"<< "\\bnot\\b"<< "\\bnull"
86  "\\bof\\b"<< "\\bon\\b"<< "\\bopen\\b"<< "\\bor\\b"<< "\\bothers\\b"<< "\\bout\\b"<<
87  "\\bpackage\\b"<< "\\bport\\b"<< "\\bpostponed\\b"<< "\\bprocedure\\b"<< "\\bprocess\\b"<< "\\bpure\\b"<<
88  "\\brange\\b"<< "\\brecord\\b"<< "\\bregister\\b"<< "\\breject\\b"<< "\\brem\\b"<< "\\breport\\b"<< "\\breturn\\b"<< "\\brol\\b"<< "\\bror\\b"<<
89  "\\bselect\\b"<< "\\bseverity\\b"<< "\\bshared\\b"<< "\\bsignal\\b"<< "\\bsla\\b"<< "\\bsll\\b"<< "\\bsra\\b"<< "\\bsrl\\b"<< "\\bsubtype\\b"<<
90  "\\bthen\\b"<< "\\bto\\b"<< "\\btransport\\b"<< "\\btype\\b"<<
91  "\\bunaffected\\b"<< "\\bunits\\b"<< "\\buntil\\b"<< "\\buse\\b"<<
92  "\\bvariable\\b"<<
93  "\\bwait\\b"<< "\\bwhen\\b"<< "\\bwhile\\b"<< "\\bwith\\b"<<
94  "\\bxnor\\b"<< "\\bxor\\b";
95 
96  unitPattern << "\\bfs\\b"<< "\\bps\\b"<< "\\bns\\b"<< "\\bus\\b"<< "\\bms\\b"<< "\\bsec\\b"<< "\\bmin\\b"<< "\\bhr\\b";
97 
98  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";
99 
100  directivePattern << "\\bactive\\b"<< "\\bascending\\b" <<
101  "\\bbase\\b" <<
102  "\\bdelayed\\b" <<
103  "\\bevent\\b" <<
104  "\\bhigh\\b" <<
105  "\\bimage\\b" <<
106  "\\blast_active\\b"<< "\\blast_event\\b"<< "\\blast_value\\b"<< "\\bleft\\b"<< "\\bleftof\\b"<< "\\blength\\b"<< "\\blow\\b" <<
107  "\\bpos\\b"<< "\\bpred\\b"<<
108  "\\bquiet\\b"<<
109  "\\brange\\b"<< "\\breverse_range\\b"<< "\\bright\\b"<< "\\brightof\\b"<<
110  "\\bstable\\b"<< "\\bsucc\\b"<<
111  "\\btransaction\\b"<<
112  "\\bval\\b"<< "\\bvalue\\b";
113 
114  commentPattern << "--[^\n]*";
115 
116  break;
117 
118  case LANG_VERILOG:
119  reservedWordPattern << "\\balways\\b"<< "\\band\\b"<< "\\bassign\\b"<< "\\battribute\\b"<<
120  "\\bbegin\\b"<< "\\bbuf\\b"<< "\\bbufif0\\b"<< "\\bbufif1\\b"<<
121  "\\bcase\\b"<< "\\bcasex\\b"<< "\\bcasez\\b" << "\\bcmos\\b"<<
122  "\\bdeassign\\b"<< "\\bdefault\\b"<< "\\bdefparam\\b"<< "\\bdisable\\b"<<
123  "\\bedge\\b"<< "\\belse\\b"<< "\\bend\\b"<< "\\bendattribute\\b"<< "\\bendcase\\b"<< "\\bendfunction\\b"<< "\\bendmodule\\b"<< "\\bendprimitive\\b"<< "\\bendspecify\\b"<< "\\bendtable\\b"<< "\\bendtask\\b"<< "\\bevent\\b"<<
124  "\\bfor\\b"<< "\\bforce\\b"<< "\\bforever\\b"<< "\\bfork\\b"<< "\\bfunction\\b"<<
125  "\\bhighz0\\b"<< "\\bhighz1\\b"<<
126  "\\bif\\b"<< "\\bifnone\\b"<< "\\binitial\\b"<< "\\binout\\b"<< "\\binput\\b"<<
127  "\\bjoin\\b"<<
128  "\\blarge\\b"<<
129  "\\bmedium\\b"<< "\\bmodule\\b"<< "\\bmacromodule\\b"<<
130  "\\bnand\\b"<< "\\bnegedge\\b"<< "\\bnmos\\b"<< "\\bnor\\b"<< "\\bnot\\b"<< "\\bnotif0\\b"<< "\\bnotif1\\b"<<
131  "\\bor\\b"<< "\\boutput\\b"<<
132  "\\bpmos\\b"<< "\\bposedge\\b"<< "\\bprimitive\\b"<< "\\bpull0\\b"<< "\\bpull1\\b"<< "\\bpulldown\\b"<< "\\bpullup\\b"<<
133  "\\brcmos\\b"<< "\\brelease\\b"<< "\\brepeat\\b"<< "\\brnmos\\b"<< "\\brpmos\\b"<< "\\brtran\\b"<< "\\brtranif0\\b"<< "\\brtranif1\\b"<<
134  "\\bscalared\\b"<< "\\bsigned\\b"<< "\\bsmall\\b"<< "\\bspecify\\b"<< "\\bstrength\\b"<< "\\bstrong0\\b"<< "\\bstrong1\\b"<<
135  "\\btable\\b"<< "\\btask\\b"<< "\\btran\\b"<< "\\btranif0\\b"<< "\\btranif1\\b"<<
136  "\\bunsigned\\b"<<
137  "\\bvectored\\b"<<
138  "\\bwait\\b"<< "\\bweak0\\b"<< "\\bweak1\\b"<< "\\bwhile\\b"<<
139  "\\bxnor\\b"<< "\\bxor\\b";
140 
141  directivePattern << "\\breset_all\\b"<< "\\btimescale\\b"<< "\\bdefine\\b"<< "\\binclude\\b"<< "\\bifdef\\b"<< "\\belse\\b"<< "\\bendif\\b"<<
142  "\\bcelldefine\\b"<< "\\bendcelldefine\\b"<< "\\bdefault_nettype\\b"<< "\\bunconnected_drive\\b"<<
143  "\\bnounconnected_drive\\b"<< "\\bdelay_mode_zero\\b"<< "\\bdelay_mode_unit\\b"<<
144  "\\bdelay_mode_path\\b"<< "\\bdelay_mode_distributed\\b"<< "\\buselib\\b";
145 
146  datatypePattern << "\\breg\\b"<< "\\binteger\\b"<< "\\btime\\b"<< "\\breal\\b"<< "\\brealtime\\b"<< "\\bwire\\b"<< "\\btri\\b"<< "\\bwor\\b"<<
147  "\\btrior\\b"<< "\\bwand\\b"<< "\\btriand\\b"<< "\\btri0\\b"<< "\\btri1\\b"<< "\\bsupply0\\b"<< "\\bsupply1\\b"<< "\\btrireg\\b"<<
148  "\\bparameter\\b"<< "\\bspecparam\\b"<< "\\bevent\\b";
149 
150  functionPattern << "\\bsetup\\b"<< "\\bhold\\b"<< "\\bsetuphold\\b"<< "\\bskew\\b"<< "\\brecovery\\b"<< "\\bperiod\\b"<< "\\bwidth\\b"<<
151  "\\bmonitor\\b"<< "\\bdisplay\\b"<< "\\bwrite\\b"<< "\\bstrobe\\b"<< "\\bfopen\\b"<< "\\bfclose\\b"<< "\\btime\\b"<<
152  "\\bstime\\b"<< "\\brealtime\\b"<< "\\btimeformat\\b"<< "\\bprinttimescale\\b"<< "\\brandom\\b"<< "\\breadmemb\\b"<<
153  "\\breadmemh\\b"<< "\\bfinish\\b"<< "\\bstop\\b";
154 
155  commentPattern << "//[^\n]*";
156 
157  break;
158 
159  case LANG_VERILOGA:
160 
161  reservedWordPattern << "\\babstol\\b"<< "\\baccess\\b"<< "\\banalog\\b"<< "\\bac_stim\\b"<< "\\banalysis\\b"<<
162  "\\bbegin\\b"<< "\\bbranch\\b"<< "\\bbound_step\\b"<<
163  "\\bcase\\b"<<
164  "\\bdiscipline\\b"<< "\\bddt_nature\\b"<< "\\bddt\\b"<< "\\bdelay\\b"<< "\\bdiscontinuity\\b"<< "\\bdefault\\b"<<
165  "\\benddiscipline\\b"<< "\\belse\\b"<< "\\bend\\b"<< "\\bendnature\\b"<< "\\bexclude\\b"<< "\\bendfunction\\b"<< "\\bendmodule\\b"<< "\\belectrical\\b"<< "\\bendcase\\b"<<
166  "\\bfor\\b"<< "\\bflow\\b"<< "\\bfrom\\b"<< "\\bfinal_step\\b"<< "\\bflicker_noise\\b"<< "\\bfunction\\b"<<
167  "\\bgenerate\\b"<< "\\bground\\b"<<
168  "\\bif\\b"<< "\\bidt_nature\\b"<< "\\binf\\b"<< "\\bidt\\b"<< "\\binitial_step\\b"<< "\\binput\\b"<< "\\binout\\b"<<
169  "\\blaplace_nd\\b"<< "\\blaplace_np\\b"<< "\\blaplace_zd\\b"<< "\\blaplace_zp\\b"<< "\\blast_crossing\\b"<<
170  "\\bmodule\\b"<<
171  "\\bnature\\b"<< "\\bnoise_table\\b"<<
172  "\\bpotential\\b"<< "\\bparameter\\b"<<
173  "\\bslew\\b"<<
174  "\\btimer\\b"<< "\\btransition\\b"<<
175  "\\bunits\\b"<<
176  "\\bwhite_noise\\b"<< "\\bwhile\\b"<<
177  "\\bzi_nd\\b"<< "\\bzi_np\\b"<< "\\bzi_zd\\b"<< "\\bzi_zp\\b";
178 
179  unitPattern << "\\bT\\b"<< "\\bG\\b"<< "\\bM\\b"<< "\\bK\\b"<< "\\bm\\b"<< "\\bu\\b"<< "\\bn\\b"<< "\\bp\\b"<< "\\bf\\b"<< "\\ba\\b";
180 
181  datatypePattern << "\\binteger\\b"<< "\\breal\\b";
182 
183  directivePattern << "\\bdefine\\b"<< "\\belse\\b"<< "\\bundef\\b"<< "\\bifdef\\b"<< "\\bendif\\b"<< "\\binclude\\b"<< "\\bresetall\\b";
184 
185  functionPattern << "\\brealtime\\b"<< "\\btemperature\\b"<< "\\bvt\\b"<< "\\bdisplay\\b"<< "\\bstrobe\\b";
186 
187  commentPattern << "//[^\n]*";
188 
189  break;
190  case LANG_OCTAVE:
191 
192  reservedWordPattern << "\\bcase\\b"<< "\\bcatch\\b"<<
193  "\\belse\\b"<< "\\belseif\\b"<< "\\bend\\b"<< "\\bendfor\\b"<< "\\bendfunction\\b"<< "\\bendif\\b"<< "\\bendswitch\\b"<< "\\bend_try_catch\\b"<< "\\bendwhile\\b"<< "\\bend_unwind_protect\\b"
194  "\\bfor\\b" << "\\bfunction\\b" <<
195  "\\bif\\b" <<
196  "\\botherwise\\b" <<
197  "\\bswitch\\b" <<
198  "\\btry\\b" <<
199  "\\bunwind_protect\\b"<< "\\bunwind_protect_cleanup\\b" <<
200  "\\bwhile\\b";
201 
202  datatypePattern << "\\binf\\b"<< "\\bnan\\b"<< "\\bpi\\b";
203 
204  functionPattern << "\\bplot\\b";
205 
206  commentPattern << "//[^\n]*";
207 
208  break;
209  }
210 
211  foreach (const QString &pattern, reservedWordPattern) {
212  rule.pattern = QRegExp(pattern);
213  rule.format = reservedWordFormat;
214  highlightingRules.append(rule);
215  }
216 
217  foreach (const QString &pattern, unitPattern) {
218  rule.pattern = QRegExp(pattern);
219  rule.format = unitFormat;
220  highlightingRules.append(rule);
221  }
222 
223  foreach (const QString &pattern, datatypePattern) {
224  rule.pattern = QRegExp(pattern);
225  rule.format = datatypeFormat;
226  highlightingRules.append(rule);
227  }
228 
229  foreach (const QString &pattern, directivePattern) {
230  rule.pattern = QRegExp(pattern);
231  rule.format = directiveFormat;
232  highlightingRules.append(rule);
233  }
234 
235  foreach (const QString &pattern, functionPattern) {
236  rule.pattern = QRegExp(pattern);
237  rule.format = functionFormat;
238  highlightingRules.append(rule);
239  }
240 
241  foreach (const QString &pattern, commentPattern) {
242  rule.pattern = QRegExp(pattern);
243  rule.format = commentFormat;
244  highlightingRules.append(rule);
245  }
246 }
247 
248 // ---------------------------------------------------
249 void SyntaxHighlighter::highlightBlock(const QString& text) {
250 
251 
252 foreach (const HighlightingRule &rule, highlightingRules) {
253  QRegExp expression(rule.pattern);
254  int index = expression.indexIn(text);
255  while (index >= 0) {
256  int length = expression.matchedLength();
257  setFormat(index, length, rule.format);
258  index = expression.indexIn(text, index + length);
259  }
260  }
261 
262 }
Definition of the TextDoc class.
QTextCharFormat unitFormat
Definition: syntax.h:61
TextDoc * Doc
Definition: syntax.h:50
QTextCharFormat functionFormat
Definition: syntax.h:64
QVector< HighlightingRule > highlightingRules
Definition: syntax.h:58
void setLanguage(int)
Definition: syntax.cpp:59
QTextCharFormat commentFormat
Definition: syntax.h:65
The TextDoc class definition.
Definition: textdoc.h:49
void highlightBlock(const QString &)
Definition: syntax.cpp:249
QTextCharFormat reservedWordFormat
Definition: syntax.h:60
QTextCharFormat directiveFormat
Definition: syntax.h:63
SyntaxHighlighter(TextDoc *)
Definition: syntax.cpp:29
virtual ~SyntaxHighlighter()
Definition: syntax.cpp:54
QTextCharFormat datatypeFormat
Definition: syntax.h:62