syntaxhighlighter.cpp 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "syntaxhighlighter.h"
  2. SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent)
  3. : QSyntaxHighlighter(parent)
  4. {
  5. HighlightingRule rule;
  6. rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
  7. QStringList instructionPatterns;
  8. instructionPatterns
  9. << "\\bCREATE\\b" << "\\bMOVE\\b" << "\\bDIE\\b" << "\\bTRANS\\b" << "\\bRTRANS\\b" << "\\bTURN\\b"
  10. << "\\bJUMP\\b" << "\\bAJUMP\\b" << "\\bBJUMP\\b"
  11. << "\\bSCAN\\b" << "\\bFARSCAN\\b"
  12. << "\\bSET\\b" << "\\bADD\\b" << "\\bSUB\\b" << "\\bMUL\\b" << "\\bDIV\\b" << "\\bMOD\\b" << "\\bMIN\\b" << "\\bMAX\\b" << "\\bRANDOM\\b"
  13. << "\\bIF\\b" << "\\bIFN\\b" << "\\bIFG\\b" << "\\bIFL\\b" << "\\bIFGE\\b" << "\\bIFLE\\b"
  14. << "\\bINIT\\b" << "\\bBREAK\\b" << "\\bRESUME\\b" << "\\bSEIZE\\b" << "\\bSLEEP\\b" << "\\bQUIT\\b"
  15. ;
  16. instructionFormat.setForeground(Qt::darkBlue);
  17. instructionFormat.setFontWeight(QFont::Bold);
  18. foreach (const QString &pattern, instructionPatterns) {
  19. rule.pattern = QRegExp(pattern);
  20. rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
  21. rule.format = instructionFormat;
  22. highlightingRules.append(rule);
  23. }
  24. QStringList variablePatterns;
  25. variablePatterns
  26. << "#1\\b" << "#2\\b" << "#3\\b" << "#4\\b" << "#5\\b" << "#6\\b" << "#7\\b" << "#8\\b" << "#9\\b" << "#10\\b"
  27. << "#11\\b" << "#12\\b" << "#13\\b" << "#14\\b" << "#15\\b" << "#16\\b" << "#17\\b" << "#18\\b" << "#19\\b" << "#20\\b"
  28. << "#Active\\b" << "\\\\$Banks\\b" << "\\\\$InstrSet\\b" << "\\\\$Mobile\\b" << "\\\\$Age\\b" << "\\\\$Tasks\\b" << "\\\\$Generation\\b" << "\\\\$ID\\b"
  29. << "%Active\\b" << "%Banks\\b" << "%InstrSet\\b" << "%Mobile\\b" << "%Age\\b" << "%Tasks\\b" << "%Generation\\b"
  30. << "#Pub\\b" << "#Team\\b"
  31. << "\\\\$Own\\b" << "\\\\$Others\\b" << "\\\\$Fields\\b" << "\\\\$Time\\b" << "\\\\$Timeout\\b"
  32. ;
  33. variableFormat.setForeground(Qt::darkRed);
  34. variableFormat.setFontWeight(QFont::Normal);
  35. variableFormat.setFontItalic(true);
  36. foreach (const QString &pattern, variablePatterns) {
  37. rule.pattern = QRegExp(pattern);
  38. rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
  39. rule.format = variableFormat;
  40. highlightingRules.append(rule);
  41. }
  42. bankFormat.setFontWeight(QFont::Bold);
  43. bankFormat.setForeground(Qt::white);
  44. bankFormat.setBackground(Qt::darkGray);
  45. rule.pattern = QRegExp("\\s*BANK.*");
  46. rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
  47. rule.format = bankFormat;
  48. highlightingRules.append(rule);
  49. singleLineCommentFormat.setForeground(Qt::darkGreen);
  50. rule.pattern = QRegExp("//[^\n]*");
  51. rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
  52. rule.format = singleLineCommentFormat;
  53. highlightingRules.append(rule);
  54. multiLineCommentFormat.setForeground(Qt::darkGreen);
  55. commentStartExpression = QRegExp("/\\*");
  56. commentEndExpression = QRegExp("\\*/");
  57. }
  58. void SyntaxHighlighter::highlightBlock(const QString &text)
  59. {
  60. foreach (const HighlightingRule &rule, highlightingRules) {
  61. QRegExp expression(rule.pattern);
  62. int index = expression.indexIn(text);
  63. while (index >= 0) {
  64. int length = expression.matchedLength();
  65. setFormat(index, length, rule.format);
  66. index = expression.indexIn(text, index + length);
  67. }
  68. }
  69. setCurrentBlockState(0);
  70. int startIndex = 0;
  71. if (previousBlockState() != 1)
  72. startIndex = commentStartExpression.indexIn(text);
  73. while (startIndex >= 0) {
  74. int endIndex = commentEndExpression.indexIn(text, startIndex);
  75. int commentLength;
  76. if (endIndex == -1) {
  77. setCurrentBlockState(1);
  78. commentLength = text.length() - startIndex;
  79. } else {
  80. commentLength = endIndex - startIndex
  81. + commentEndExpression.matchedLength();
  82. }
  83. setFormat(startIndex, commentLength, multiLineCommentFormat);
  84. startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
  85. }
  86. }