qscilexerverilog.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // This module implements the QsciLexerVerilog class.
  2. //
  3. // Copyright (c) 2017 Riverbank Computing Limited <info@riverbankcomputing.com>
  4. //
  5. // This file is part of QScintilla.
  6. //
  7. // This file may be used under the terms of the GNU General Public License
  8. // version 3.0 as published by the Free Software Foundation and appearing in
  9. // the file LICENSE included in the packaging of this file. Please review the
  10. // following information to ensure the GNU General Public License version 3.0
  11. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  12. //
  13. // If you do not wish to use this file under the terms of the GPL version 3.0
  14. // then you may purchase a commercial license. For more information contact
  15. // info@riverbankcomputing.com.
  16. //
  17. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  18. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19. #include "Qsci/qscilexerverilog.h"
  20. #include <qcolor.h>
  21. #include <qfont.h>
  22. #include <qsettings.h>
  23. // The ctor.
  24. QsciLexerVerilog::QsciLexerVerilog(QObject *parent)
  25. : QsciLexer(parent),
  26. fold_atelse(false), fold_comments(false), fold_compact(true),
  27. fold_preproc(false), fold_atmodule(false)
  28. {
  29. }
  30. // The dtor.
  31. QsciLexerVerilog::~QsciLexerVerilog()
  32. {
  33. }
  34. // Returns the language name.
  35. const char *QsciLexerVerilog::language() const
  36. {
  37. return "Verilog";
  38. }
  39. // Returns the lexer name.
  40. const char *QsciLexerVerilog::lexer() const
  41. {
  42. return "verilog";
  43. }
  44. // Return the style used for braces.
  45. int QsciLexerVerilog::braceStyle() const
  46. {
  47. return Operator;
  48. }
  49. // Returns the set of keywords.
  50. const char *QsciLexerVerilog::keywords(int set) const
  51. {
  52. if (set == 1)
  53. return
  54. "always and assign automatic begin buf bufif0 bufif1 case casex "
  55. "casez cell cmos config deassign default defparam design disable "
  56. "edge else end endcase endconfig endfunction endgenerate "
  57. "endmodule endprimitiveendspecify endtable endtask event for "
  58. "force forever fork function generate genvar highz0 highz1 if "
  59. "ifnone incdir include initial inout input instance integer join "
  60. "large liblist library localparam macromodule medium module nand "
  61. "negedge nmos nor noshowcancelled not notif0 notif1 or output "
  62. "parameter pmos posedge primitive pull0 pull1 pulldown pullup "
  63. "pulsestyle_ondetect pulsestyle_onevent rcmos real realtime reg "
  64. "release repeat rnmos rpmos rtran rtranif0 rtranif1 scalared "
  65. "showcancelled signed small specify specparam strong0 strong1 "
  66. "supply0 supply1 table task time tran tranif0 tranif1 tri tri0 "
  67. "tri1 triand trior trireg unsigned use vectored wait wand weak0 "
  68. "weak1 while wire wor xnor xor";
  69. if (set == 3)
  70. return
  71. "$async$and$array $async$and$plane $async$nand$array "
  72. "$async$nand$plane $async$nor$array $async$nor$plane "
  73. "$async$or$array $async$or$plane $bitstoreal $countdrivers "
  74. "$display $displayb $displayh $displayo $dist_chi_square "
  75. "$dist_erlang $dist_exponential $dist_normal $dist_poisson "
  76. "$dist_t $dist_uniform $dumpall $dumpfile $dumpflush $dumplimit "
  77. "$dumpoff $dumpon $dumpportsall $dumpportsflush $dumpportslimit "
  78. "$dumpportsoff $dumpportson $dumpvars $fclose $fdisplayh "
  79. "$fdisplay $fdisplayf $fdisplayb $ferror $fflush $fgetc $fgets "
  80. "$finish $fmonitorb $fmonitor $fmonitorf $fmonitorh $fopen "
  81. "$fread $fscanf $fseek $fsscanf $fstrobe $fstrobebb $fstrobef "
  82. "$fstrobeh $ftel $fullskew $fwriteb $fwritef $fwriteh $fwrite "
  83. "$getpattern $history $hold $incsave $input $itor $key $list "
  84. "$log $monitorb $monitorh $monitoroff $monitoron $monitor "
  85. "$monitoro $nochange $nokey $nolog $period $printtimescale "
  86. "$q_add $q_exam $q_full $q_initialize $q_remove $random "
  87. "$readmemb $readmemh $readmemh $realtime $realtobits $recovery "
  88. "$recrem $removal $reset_count $reset $reset_value $restart "
  89. "$rewind $rtoi $save $scale $scope $sdf_annotate $setup "
  90. "$setuphold $sformat $showscopes $showvariables $showvars "
  91. "$signed $skew $sreadmemb $sreadmemh $stime $stop $strobeb "
  92. "$strobe $strobeh $strobeo $swriteb $swriteh $swriteo $swrite "
  93. "$sync$and$array $sync$and$plane $sync$nand$array "
  94. "$sync$nand$plane $sync$nor$array $sync$nor$plane $sync$or$array "
  95. "$sync$or$plane $test$plusargs $time $timeformat $timeskew "
  96. "$ungetc $unsigned $value$plusargs $width $writeb $writeh $write "
  97. "$writeo";
  98. return 0;
  99. }
  100. // Return the string of characters that comprise a word.
  101. const char *QsciLexerVerilog::wordCharacters() const
  102. {
  103. return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$";
  104. }
  105. // Returns the foreground colour of the text for a style.
  106. QColor QsciLexerVerilog::defaultColor(int style) const
  107. {
  108. switch (style)
  109. {
  110. case Default:
  111. case InactiveComment:
  112. case InactiveCommentLine:
  113. case InactiveCommentBang:
  114. case InactiveNumber:
  115. case InactiveKeyword:
  116. case InactiveString:
  117. case InactiveKeywordSet2:
  118. case InactiveSystemTask:
  119. case InactivePreprocessor:
  120. case InactiveOperator:
  121. case InactiveIdentifier:
  122. case InactiveUnclosedString:
  123. case InactiveUserKeywordSet:
  124. case InactiveCommentKeyword:
  125. case InactiveDeclareInputPort:
  126. case InactiveDeclareOutputPort:
  127. case InactiveDeclareInputOutputPort:
  128. case InactivePortConnection:
  129. return QColor(0x80, 0x80, 0x80);
  130. case Comment:
  131. case CommentLine:
  132. return QColor(0x00, 0x7f, 0x00);
  133. case CommentBang:
  134. return QColor(0x3f, 0x7f, 0x3f);
  135. case Number:
  136. case KeywordSet2:
  137. return QColor(0x00, 0x7f, 0x7f);
  138. case Keyword:
  139. case DeclareOutputPort:
  140. return QColor(0x00, 0x00, 0x7f);
  141. case String:
  142. return QColor(0x7f, 0x00, 0x7f);
  143. case SystemTask:
  144. return QColor(0x80, 0x40, 0x20);
  145. case Preprocessor:
  146. return QColor(0x7f, 0x7f, 0x00);
  147. case Operator:
  148. return QColor(0x00, 0x70, 0x70);
  149. case UnclosedString:
  150. return QColor(0x00, 0x00, 0x00);
  151. case UserKeywordSet:
  152. case CommentKeyword:
  153. return QColor(0x2a, 0x00, 0xff);
  154. case DeclareInputPort:
  155. return QColor(0x7f, 0x00, 0x00);
  156. case DeclareInputOutputPort:
  157. return QColor(0x00, 0x00, 0xff);
  158. case PortConnection:
  159. return QColor(0x00, 0x50, 0x32);
  160. }
  161. return QsciLexer::defaultColor(style);
  162. }
  163. // Returns the end-of-line fill for a style.
  164. bool QsciLexerVerilog::defaultEolFill(int style) const
  165. {
  166. switch (style)
  167. {
  168. case CommentBang:
  169. case UnclosedString:
  170. case InactiveDefault:
  171. case InactiveComment:
  172. case InactiveCommentLine:
  173. case InactiveCommentBang:
  174. case InactiveNumber:
  175. case InactiveKeyword:
  176. case InactiveString:
  177. case InactiveKeywordSet2:
  178. case InactiveSystemTask:
  179. case InactivePreprocessor:
  180. case InactiveOperator:
  181. case InactiveIdentifier:
  182. case InactiveUnclosedString:
  183. case InactiveUserKeywordSet:
  184. case InactiveCommentKeyword:
  185. case InactiveDeclareInputPort:
  186. case InactiveDeclareOutputPort:
  187. case InactiveDeclareInputOutputPort:
  188. case InactivePortConnection:
  189. return true;
  190. }
  191. return QsciLexer::defaultEolFill(style);
  192. }
  193. // Returns the font of the text for a style.
  194. QFont QsciLexerVerilog::defaultFont(int style) const
  195. {
  196. QFont f;
  197. switch (style)
  198. {
  199. case Comment:
  200. case CommentLine:
  201. case CommentBang:
  202. case UserKeywordSet:
  203. #if defined(Q_OS_WIN)
  204. f = QFont("Comic Sans MS",9);
  205. #elif defined(Q_OS_MAC)
  206. f = QFont("Comic Sans MS", 12);
  207. #else
  208. f = QFont("Bitstream Vera Serif",9);
  209. #endif
  210. break;
  211. case Keyword:
  212. case PortConnection:
  213. f = QsciLexer::defaultFont(style);
  214. f.setBold(true);
  215. break;
  216. case InactiveDefault:
  217. case InactiveComment:
  218. case InactiveCommentLine:
  219. case InactiveCommentBang:
  220. case InactiveNumber:
  221. case InactiveKeyword:
  222. case InactiveString:
  223. case InactiveKeywordSet2:
  224. case InactiveSystemTask:
  225. case InactivePreprocessor:
  226. case InactiveOperator:
  227. case InactiveIdentifier:
  228. case InactiveUnclosedString:
  229. case InactiveUserKeywordSet:
  230. case InactiveCommentKeyword:
  231. case InactiveDeclareInputPort:
  232. case InactiveDeclareOutputPort:
  233. case InactiveDeclareInputOutputPort:
  234. case InactivePortConnection:
  235. f = QsciLexer::defaultFont(style);
  236. f.setItalic(true);
  237. break;
  238. default:
  239. f = QsciLexer::defaultFont(style);
  240. }
  241. return f;
  242. }
  243. // Returns the user name of a style.
  244. QString QsciLexerVerilog::description(int style) const
  245. {
  246. switch (style)
  247. {
  248. case Default:
  249. return tr("Default");
  250. case Comment:
  251. return tr("Comment");
  252. case CommentLine:
  253. return tr("Line comment");
  254. case CommentBang:
  255. return tr("Bang comment");
  256. case Number:
  257. return tr("Number");
  258. case Keyword:
  259. return tr("Primary keywords and identifiers");
  260. case String:
  261. return tr("String");
  262. case KeywordSet2:
  263. return tr("Secondary keywords and identifiers");
  264. case SystemTask:
  265. return tr("System task");
  266. case Preprocessor:
  267. return tr("Preprocessor block");
  268. case Operator:
  269. return tr("Operator");
  270. case Identifier:
  271. return tr("Identifier");
  272. case UnclosedString:
  273. return tr("Unclosed string");
  274. case UserKeywordSet:
  275. return tr("User defined tasks and identifiers");
  276. case CommentKeyword:
  277. return tr("Keyword comment");
  278. case InactiveCommentKeyword:
  279. return tr("Inactive keyword comment");
  280. case DeclareInputPort:
  281. return tr("Input port declaration");
  282. case InactiveDeclareInputPort:
  283. return tr("Inactive input port declaration");
  284. case DeclareOutputPort:
  285. return tr("Output port declaration");
  286. case InactiveDeclareOutputPort:
  287. return tr("Inactive output port declaration");
  288. case DeclareInputOutputPort:
  289. return tr("Input/output port declaration");
  290. case InactiveDeclareInputOutputPort:
  291. return tr("Inactive input/output port declaration");
  292. case PortConnection:
  293. return tr("Port connection");
  294. case InactivePortConnection:
  295. return tr("Inactive port connection");
  296. }
  297. return QString();
  298. }
  299. // Returns the background colour of the text for a style.
  300. QColor QsciLexerVerilog::defaultPaper(int style) const
  301. {
  302. switch (style)
  303. {
  304. case CommentBang:
  305. return QColor(0xe0, 0xf0, 0xff);
  306. case UnclosedString:
  307. return QColor(0xe0, 0xc0, 0xe0);
  308. case InactiveDefault:
  309. case InactiveComment:
  310. case InactiveCommentLine:
  311. case InactiveCommentBang:
  312. case InactiveNumber:
  313. case InactiveKeyword:
  314. case InactiveString:
  315. case InactiveKeywordSet2:
  316. case InactiveSystemTask:
  317. case InactivePreprocessor:
  318. case InactiveOperator:
  319. case InactiveIdentifier:
  320. case InactiveUnclosedString:
  321. case InactiveUserKeywordSet:
  322. case InactiveCommentKeyword:
  323. case InactiveDeclareInputPort:
  324. case InactiveDeclareOutputPort:
  325. case InactiveDeclareInputOutputPort:
  326. case InactivePortConnection:
  327. return QColor(0xe0, 0xe0, 0xe0);
  328. }
  329. return QsciLexer::defaultPaper(style);
  330. }
  331. // Refresh all properties.
  332. void QsciLexerVerilog::refreshProperties()
  333. {
  334. setAtElseProp();
  335. setCommentProp();
  336. setCompactProp();
  337. setPreprocProp();
  338. // We don't provide options for these as there doesn't seem much point in
  339. // disabling them.
  340. emit propertyChanged("lexer.verilog.track.preprocessor", "1");
  341. emit propertyChanged("lexer.verilog.update.preprocessor", "1");
  342. emit propertyChanged("lexer.verilog.portstyling", "1");
  343. emit propertyChanged("lexer.verilog.allupperkeywords", "1");
  344. }
  345. // Read properties from the settings.
  346. bool QsciLexerVerilog::readProperties(QSettings &qs,const QString &prefix)
  347. {
  348. fold_atelse = qs.value(prefix + "foldatelse", false).toBool();
  349. fold_comments = qs.value(prefix + "foldcomments", false).toBool();
  350. fold_compact = qs.value(prefix + "foldcompact", true).toBool();
  351. fold_preproc = qs.value(prefix + "foldpreprocessor", false).toBool();
  352. fold_atmodule = qs.value(prefix + "foldverilogflags", false).toBool();
  353. return true;
  354. }
  355. // Write properties to the settings.
  356. bool QsciLexerVerilog::writeProperties(QSettings &qs,const QString &prefix) const
  357. {
  358. qs.setValue(prefix + "foldatelse", fold_atelse);
  359. qs.setValue(prefix + "foldcomments", fold_comments);
  360. qs.setValue(prefix + "foldcompact", fold_compact);
  361. qs.setValue(prefix + "foldpreprocessor", fold_preproc);
  362. qs.setValue(prefix + "foldverilogflags", fold_atmodule);
  363. return true;
  364. }
  365. // Set if else can be folded.
  366. void QsciLexerVerilog::setFoldAtElse(bool fold)
  367. {
  368. fold_atelse = fold;
  369. setAtElseProp();
  370. }
  371. // Set the "fold.at.else" property.
  372. void QsciLexerVerilog::setAtElseProp()
  373. {
  374. emit propertyChanged("fold.at.else", (fold_atelse ? "1" : "0"));
  375. }
  376. // Set if comments can be folded.
  377. void QsciLexerVerilog::setFoldComments(bool fold)
  378. {
  379. fold_comments = fold;
  380. setCommentProp();
  381. }
  382. // Set the "fold.comment" property.
  383. void QsciLexerVerilog::setCommentProp()
  384. {
  385. emit propertyChanged("fold.comment", (fold_comments ? "1" : "0"));
  386. }
  387. // Set if folds are compact
  388. void QsciLexerVerilog::setFoldCompact(bool fold)
  389. {
  390. fold_compact = fold;
  391. setCompactProp();
  392. }
  393. // Set the "fold.compact" property.
  394. void QsciLexerVerilog::setCompactProp()
  395. {
  396. emit propertyChanged("fold.compact", (fold_compact ? "1" : "0"));
  397. }
  398. // Set if preprocessor blocks can be folded.
  399. void QsciLexerVerilog::setFoldPreprocessor(bool fold)
  400. {
  401. fold_preproc = fold;
  402. setPreprocProp();
  403. }
  404. // Set the "fold.preprocessor" property.
  405. void QsciLexerVerilog::setPreprocProp()
  406. {
  407. emit propertyChanged("fold.preprocessor", (fold_preproc ? "1" : "0"));
  408. }
  409. // Set if modules can be folded.
  410. void QsciLexerVerilog::setFoldAtModule(bool fold)
  411. {
  412. fold_atmodule = fold;
  413. setAtModuleProp();
  414. }
  415. // Set the "fold.verilog.flags" property.
  416. void QsciLexerVerilog::setAtModuleProp()
  417. {
  418. emit propertyChanged("fold.verilog.flags", (fold_atmodule ? "1" : "0"));
  419. }