| 1 | /* |
| 2 | Copyright (C) 2008, 2009 Andres Cabrera |
| 3 | [email protected] |
| 4 | |
| 5 | This file is part of CsoundQt. |
| 6 | |
| 7 | CsoundQt is free software; you can redistribute it |
| 8 | and/or modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | CsoundQt is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with Csound; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include "highlighter.h" |
| 24 | |
| 25 | #include <QDebug> |
| 26 | #include <QRegularExpression> |
| 27 | |
| 28 | |
| 29 | TextBlockData::TextBlockData() |
| 30 | { |
| 31 | // Nothing to do |
| 32 | section = UnknownSection; |
| 33 | } |
| 34 | |
| 35 | QVector<ParenthesisInfo *> TextBlockData::parentheses() |
| 36 | { |
| 37 | return m_parentheses; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | void TextBlockData::insert(ParenthesisInfo *info) |
| 42 | { |
| 43 | int i = 0; |
| 44 | while (i < m_parentheses.size() && info->position > m_parentheses.at(i)->position) |
| 45 | ++i; |
| 46 | |
| 47 | m_parentheses.insert(i, info); |
| 48 | } |
| 49 | |
| 50 | QColor saturateColor(QColor color, int percent) { |
| 51 | int sat = color.saturation(); |
| 52 | sat = sat * percent / 100; |
| 53 | if(sat > 255) |
| 54 | sat = 255; |
| 55 | return QColor::fromHsl(color.hue(), sat, color.lightness()); |
| 56 | } |
| 57 | |
| 58 | void Highlighter::setTheme(const QString &theme) { |
| 59 | if(theme == "none") { |
| 60 | defaultFormat.setForeground(QColor("black")); |
| 61 | defaultFormat.setBackground(QColor(250, 250, 250)); |
| 62 | } |
| 63 | else if(theme == "classic") { |
| 64 | defaultFormat.setForeground(QColor("black")); |
| 65 | defaultFormat.setBackground(QColor(250, 250, 250)); |
| 66 | |
| 67 | csdtagFormat.setForeground(QColor("brown")); |
| 68 | csdtagFormat.setFontWeight(QFont::Bold); |
| 69 | |
| 70 | instFormat.setForeground(QColor("purple")); |
| 71 | instFormat.setFontWeight(QFont::Bold); |
| 72 | |
| 73 | keywordFormat.setForeground(QColor("#E65100")); |
| 74 | keywordFormat.setFontWeight(QFont::Bold); |
| 75 | |
| 76 | headerFormat.setForeground(QColor("brown")); |
| 77 | headerFormat.setFontWeight(QFont::Bold); |
| 78 | |
| 79 | opcodeFormat.setForeground(QColor("blue")); |
| 80 | opcodeFormat.setFontWeight(QFont::Bold); |
| 81 | deprecatedFormat = opcodeFormat; |
| 82 | |
| 83 | singleLineCommentFormat.setForeground(QColor("green")); |
| 84 | singleLineCommentFormat.setFontItalic(true); |
| 85 | |
| 86 | macroDefineFormat.setForeground(QColor("green")); |
| 87 | macroDefineFormat.setFontWeight(QFont::Bold); |
| 88 | |
| 89 | pfieldFormat.setFontWeight(QFont::Bold); |
| 90 | |
| 91 | irateFormat.setForeground(QColor("darkCyan")); |
| 92 | krateFormat.setForeground(QColor("darkCyan")); |
| 93 | |
| 94 | arateFormat.setForeground(QColor("darkCyan")); |
| 95 | arateFormat.setFontWeight(QFont::Bold); |
| 96 | |
| 97 | stringVarFormat.setForeground(QColor(Qt::darkYellow)); |
| 98 | stringVarFormat.setFontWeight(QFont::Bold); |
| 99 | |
| 100 | fsigFormat.setForeground(QColor(Qt::gray)); |
| 101 | fsigFormat.setFontWeight(QFont::Bold); |
| 102 | |
| 103 | labelFormat.setForeground(QColor(205,92,92)); |
| 104 | labelFormat.setFontWeight(QFont::Bold); |
| 105 | |
| 106 | nameFormat.setForeground(QColor("#880E4F")); |
| 107 | nameFormat.setFontWeight(QFont::Bold); |
| 108 | // nameFormat.setFontUnderline(true); |
| 109 | |
| 110 | quotationFormat.setForeground(Qt::red); |
| 111 | multiLineCommentFormat.setForeground(QColor("green")); |
| 112 | |
| 113 | ioFormat = opcodeFormat; |
| 114 | udoFormat = opcodeFormat; |
| 115 | operatorFormat = defaultFormat; |
| 116 | importantCommentFormat = singleLineCommentFormat; |
| 117 | scoreLetterFormat = opcodeFormat; |
| 118 | } |
| 119 | else if(theme == "light") { |
| 120 | defaultFormat.setForeground(QColor("#030303")); |
| 121 | defaultFormat.setBackground(QColor("#F9F9F9")); |
| 122 | |
| 123 | // csdtagFormat.setFontWeight(QFont::Bold); |
| 124 | |
| 125 | instFormat.setForeground(QColor("#82387B")); |
| 126 | instFormat.setFontWeight(QFont::Bold); |
| 127 | |
| 128 | keywordFormat.setForeground(QColor("#D64100")); |
| 129 | keywordFormat.setFontWeight(QFont::Bold); |
| 130 | |
| 131 | // headerFormat.setForeground(QColor("#C62828")); |
| 132 | // headerFormat.setFontWeight(QFont::Bold); |
| 133 | headerFormat = keywordFormat; |
| 134 | |
| 135 | opcodeFormat.setForeground(QColor("#303F9F")); |
| 136 | opcodeFormat.setFontWeight(QFont::Bold); |
| 137 | |
| 138 | deprecatedFormat.setForeground(QColor("#880000")); |
| 139 | deprecatedFormat.setFontUnderline(true); |
| 140 | |
| 141 | singleLineCommentFormat.setForeground(QColor("#8F8F7F")); |
| 142 | singleLineCommentFormat.setFontItalic(true); |
| 143 | |
| 144 | |
| 145 | importantCommentFormat.setBackground(QColor("#FFFFA0")); |
| 146 | importantCommentFormat.setForeground( |
| 147 | singleLineCommentFormat.foreground().color().darker(150)); |
| 148 | // importantCommentFormat.setFontItalic(true); |
| 149 | |
| 150 | macroDefineFormat.setForeground(QColor("#00695C")); |
| 151 | macroDefineFormat.setFontWeight(QFont::Bold); |
| 152 | |
| 153 | pfieldFormat.setFontWeight(QFont::Bold); |
| 154 | |
| 155 | |
| 156 | krateFormat.setForeground(QColor("#10796C")); |
| 157 | irateFormat.setForeground(QColor("#303F9F")); |
| 158 | |
| 159 | // arateFormat.setForeground(QColor("#C62828")); |
| 160 | arateFormat.setForeground(QColor("#B71C1C")); |
| 161 | arateFormat.setFontWeight(QFont::Bold); |
| 162 | |
| 163 | stringVarFormat.setForeground(QColor(Qt::darkYellow)); |
| 164 | stringVarFormat.setFontWeight(QFont::Normal); |
| 165 | |
| 166 | fsigFormat.setForeground(QColor("#Ad1457")); |
| 167 | fsigFormat.setFontWeight(QFont::Bold); |
| 168 | |
| 169 | labelFormat.setForeground(QColor(205,92,92)); |
| 170 | labelFormat.setFontWeight(QFont::Bold); |
| 171 | labelFormat.setFontUnderline(true); |
| 172 | |
| 173 | nameFormat.setForeground(QColor("#F50057")); |
| 174 | nameFormat.setFontWeight(QFont::Bold); |
| 175 | // nameFormat.setFontUnderline(true); |
| 176 | |
| 177 | // quotationFormat.setForeground(QColor("#4CAf50")); |
| 178 | quotationFormat.setForeground(QColor("#827717")); |
| 179 | multiLineCommentFormat.setForeground(quotationFormat.foreground()); |
| 180 | |
| 181 | ioFormat.setForeground(QColor("#8844BB")); |
| 182 | ioFormat.setFontWeight(QFont::Bold); |
| 183 | // ioFormat.setFontItalic(true); |
| 184 | |
| 185 | udoFormat = opcodeFormat; |
| 186 | udoFormat.setForeground(opcodeFormat.foreground().color().darker(150)); |
| 187 | |
| 188 | csdtagFormat.setForeground(instFormat.foreground()); |
| 189 | |
| 190 | operatorFormat.setForeground(instFormat.foreground().color()); |
| 191 | |
| 192 | scoreLetterFormat.setForeground(opcodeFormat.foreground()); |
| 193 | scoreLetterFormat.setFontWeight(QFont::Bold); |
| 194 | |
| 195 | } |
| 196 | else if(theme == "dark") { |
| 197 | defaultFormat.setForeground(QColor("#FBFBFB")); |
| 198 | defaultFormat.setBackground(QColor("#161616")); |
| 199 | |
| 200 | // csdtagFormat.setFontWeight(QFont::Bold); |
| 201 | |
| 202 | instFormat.setForeground(QColor("#B268AB")); |
| 203 | instFormat.setFontWeight(QFont::Bold); |
| 204 | |
| 205 | keywordFormat.setForeground(QColor("#FF9800")); |
| 206 | keywordFormat.setFontWeight(QFont::Bold); |
| 207 | |
| 208 | // headerFormat.setForeground(QColor("#FFD54F")); |
| 209 | // headerFormat.setFontWeight(QFont::Bold); |
| 210 | headerFormat = keywordFormat; |
| 211 | |
| 212 | opcodeFormat.setForeground(QColor("#4FC3F7")); |
| 213 | opcodeFormat.setFontWeight(QFont::Bold); |
| 214 | |
| 215 | deprecatedFormat.setForeground(saturateColor(opcodeFormat.foreground().color(), 50)); |
| 216 | deprecatedFormat.setUnderlineColor("#880000"); |
| 217 | deprecatedFormat.setFontUnderline(true); |
| 218 | |
| 219 | // singleLineCommentFormat.setForeground(QColor("#9F9F8F")); |
| 220 | singleLineCommentFormat.setForeground(QColor("#856CB0")); |
| 221 | singleLineCommentFormat.setFontItalic(true); |
| 222 | |
| 223 | importantCommentFormat.setBackground(defaultFormat.background().color().lighter(140)); |
| 224 | importantCommentFormat.setForeground( |
| 225 | singleLineCommentFormat.foreground().color().lighter(175)); |
| 226 | |
| 227 | macroDefineFormat.setForeground(QColor("#F06292")); |
| 228 | macroDefineFormat.setFontWeight(QFont::Bold); |
| 229 | |
| 230 | pfieldFormat.setFontWeight(QFont::Bold); |
| 231 | |
| 232 | krateFormat.setForeground(QColor("#66EEBB")); |
| 233 | irateFormat.setForeground(QColor("#FFFFFF")); |
| 234 | |
| 235 | arateFormat.setForeground(QColor("#F75C5C")); |
| 236 | arateFormat.setFontWeight(QFont::Bold); |
| 237 | |
| 238 | |
| 239 | fsigFormat.setForeground(QColor("#BD2467")); |
| 240 | fsigFormat.setFontWeight(QFont::Bold); |
| 241 | |
| 242 | labelFormat.setForeground(instFormat.foreground()); |
| 243 | labelFormat.setFontWeight(QFont::Bold); |
| 244 | labelFormat.setFontUnderline(true); |
| 245 | |
| 246 | nameFormat.setForeground(QColor("#D298FB")); |
| 247 | nameFormat.setFontWeight(QFont::Bold); |
| 248 | |
| 249 | // quotationFormat.setForeground(QColor("#66FFAA")); |
| 250 | quotationFormat.setForeground(QColor("#91FF55")); |
| 251 | stringVarFormat.setForeground(quotationFormat.foreground()); |
| 252 | stringVarFormat.setFontWeight(QFont::Normal); |
| 253 | |
| 254 | multiLineCommentFormat.setForeground(quotationFormat.foreground()); |
| 255 | |
| 256 | ioFormat.setForeground(QColor("#FFD54F")); |
| 257 | ioFormat.setFontWeight(QFont::Bold); |
| 258 | |
| 259 | udoFormat.setForeground(opcodeFormat.foreground()); |
| 260 | udoFormat.setFontWeight(QFont::Normal); |
| 261 | |
| 262 | // ioFormat.setFontItalic(true); |
| 263 | csdtagFormat.setForeground(instFormat.foreground()); |
| 264 | operatorFormat.setForeground(nameFormat.foreground().color()); |
| 265 | |
| 266 | scoreLetterFormat.setForeground(opcodeFormat.foreground()); |
| 267 | scoreLetterFormat.setFontWeight(QFont::Bold); |
| 268 | |
| 269 | } |
| 270 | else { |
| 271 | qDebug() << "Theme" << theme << "not defined, using default"; |
| 272 | this->setTheme("classic"); |
| 273 | return; |
| 274 | } |
| 275 | // -------------------------------------- |
| 276 | girateFormat.setForeground(irateFormat.foreground()); |
| 277 | // girateFormat.setFontWeight(QFont::Bold); |
| 278 | girateFormat.setFontItalic(true); |
| 279 | |
| 280 | gkrateFormat.setForeground(krateFormat.foreground()); |
| 281 | // gkrateFormat.setFontWeight(QFont::Bold); |
| 282 | gkrateFormat.setFontItalic(true); |
| 283 | |
| 284 | garateFormat.setForeground(arateFormat.foreground()); |
| 285 | garateFormat.setFontWeight(arateFormat.fontWeight()); |
| 286 | garateFormat.setFontItalic(true); |
| 287 | |
| 288 | gfsigFormat.setForeground(fsigFormat.foreground()); |
| 289 | gfsigFormat.setFontWeight(fsigFormat.fontWeight()); |
| 290 | gfsigFormat.setFontItalic(true); |
| 291 | |
| 292 | gstringVarFormat.setForeground(stringVarFormat.foreground()); |
| 293 | gstringVarFormat.setFontWeight(QFont::Bold); |
| 294 | gstringVarFormat.setFontItalic(true); |
| 295 | |
| 296 | errorFormat.setForeground(deprecatedFormat.foreground().color()); |
| 297 | errorFormat.setFontStrikeOut(true); |
| 298 | |
| 299 | // ioFormat.setFontItalic(true); |
| 300 | |
| 301 | QColor color = saturateColor(headerFormat.foreground().color().lighter(150), 150); |
| 302 | csoundOptionFormat.setForeground(color); |
| 303 | jsKeywordFormat = keywordFormat; |
| 304 | |
| 305 | m_theme = theme; |
| 306 | // emit this->rehighlight(); |
| 307 | } |
| 308 | |
| 309 | QTextCharFormat Highlighter::getFormat(QString tag) { |
| 310 | if(tag == "" || tag == "default") { |
| 311 | return defaultFormat; |
| 312 | } |
| 313 | if(tag == "csdtag") |
| 314 | return csdtagFormat; |
| 315 | if(tag == "inst") |
| 316 | return instFormat; |
| 317 | if(tag == "keyword") |
| 318 | return keywordFormat; |
| 319 | if(tag == "opcode") |
| 320 | return opcodeFormat; |
| 321 | if(tag == "singleLineComment") |
| 322 | return singleLineCommentFormat; |
| 323 | if(tag == "macroDefine") |
| 324 | return macroDefineFormat; |
| 325 | if(tag == "pfield") |
| 326 | return pfieldFormat; |
| 327 | QTextCharFormat fallback; |
| 328 | fallback.setForeground(QColor("black")); |
| 329 | fallback.setBackground(QColor(250, 250, 250)); |
| 330 | return fallback; |
| 331 | } |
| 332 | |
| 333 | Highlighter::Highlighter(QTextDocument *parent) |
| 334 | : QSyntaxHighlighter(parent) |
| 335 | { |
| 336 | commentStartExpression.setPattern("/\\*"); |
| 337 | commentEndExpression.setPattern("\\*/"); |
| 338 | |
| 339 | // b64encStartExpression = QRegExp("<CsFileB .*>"); |
| 340 | // b64encEndExpression = QRegExp("<CsFileB>"); |
| 341 | colorVariables = true; |
| 342 | m_mode = 0; // default to Csound mode |
| 343 | m_scoreSyntaxHighlighting = true; |
| 344 | |
| 345 | tagPatterns << "<CsoundSynthesizer>" << "</CsoundSynthesizer>" |
| 346 | << "<CsInstruments>" << "</CsInstruments>" |
| 347 | << "<CsOptions>" << "</CsOptions>" |
| 348 | << "<CsScore>" << "</CsScore>" |
| 349 | << "<CsVersion>" << "</CsVersion>" |
| 350 | << "<MacOptions>" << "</MacOptions>" |
| 351 | << "<MacGUI>" << "</MacGUI>" |
| 352 | << "<csLADSPA>" << "</csLADSPA>" |
| 353 | << "<Cabbage>" << "</Cabbage>" |
| 354 | << "<CsHtml5>" << "</CsHtml5>" |
| 355 | << "<CsFileB>" << "</CsFileB>" |
| 356 | << "<CsLicense>" << "</CsLincense>"; |
| 357 | |
| 358 | instPatterns << "instr" << "endin" << "opcode" << "endop"; |
| 359 | headerPatterns << "sr" << "kr" << "ksmps" << "nchnls" << "nchnls_i" << "0dbfs" << "A4" |
| 360 | << "zakinit" << "massign"; |
| 361 | |
| 362 | keywordLiterals << "init" << "if" << "then" << "else" << "endif" << "elseif" |
| 363 | << "while" << "goto" << "igoto" << "kgoto" |
| 364 | << "do" << "od" |
| 365 | << "int" << "turnoff" << "xin" << "xout" |
| 366 | << "passign"; |
| 367 | |
| 368 | ioPatterns << "in" << "ins" << "inch" << "out" << "outs" << "outch" |
| 369 | << "outvalue" << "invalue" << "chnget" << "chngetk" << "chnset" << "chn_k" |
| 370 | << "chn_a" << "chn_S" << "zaw" << "zar" << "zkw" << "zkr" << "zawm" |
| 371 | << "OSCsend" << "OSClisten" << "OSCraw" << "OSCinit"; |
| 372 | |
| 373 | deprecatedOpcodes << "array" |
| 374 | << "balance" << "button" |
| 375 | << "checkbox" << "chani" << "chano" << "cigoto" << "ckgoto" << "cngoto" |
| 376 | << "compress" << "control" |
| 377 | << "dcblock" |
| 378 | << "flashtxt" << "flooper" << "follow" |
| 379 | << "in32" << "inh" << "ino" << "inq" << "inrq" << "inx" |
| 380 | << "loop_ge" << "loop_gt" << "loop_le" << "loop_lt" |
| 381 | << "moogvcf" << "nlfilt" |
| 382 | << "outc" << "outo" << "outq" << "outx" |
| 383 | << "setctrl" |
| 384 | << "s16b14" << "s32b14" << "slider16" << "slider16f" << "slider32f" << "slider64" << "slider64f" |
| 385 | << "slider8" << "slider8f" |
| 386 | << "vaset" << "vaget" |
| 387 | << "vco" << "vincr" << "vbap16" << "vbap4" << "vbap8" |
| 388 | << "vbap4move" << "vbap8move" |
| 389 | ; |
| 390 | |
| 391 | operatorPatterns << "&&" << "||"; |
| 392 | |
| 393 | csoundOptions << "-+rtaudio=" << "-+rtmidi=" << "--nodisplays" << "--nosound" |
| 394 | << "--syntax-check-only" << "--control-rate=" << "--messagelevel=" |
| 395 | << "--env:" << "--dither" << "--sched" << "--omacro:" |
| 396 | << "--smacro:" << "--verbose" << "--sample-accurate" << "--realtime" |
| 397 | << "--nchnls=" << "--nchnls_i=" << "--sinesize" << "--daemon" |
| 398 | << "--port=" << "--use-system-sr" << "--ksmps=" << "-+jack_client=" |
| 399 | << "--limiter" << "--udp-echo" << "--opcode-dir=" |
| 400 | ; |
| 401 | |
| 402 | csoundOptionsRx = QRegularExpression( |
| 403 | "-(-(env|nodisplays|nosound|control-rate|messagelevel=" |
| 404 | "|dither|sched|omacro:|smacro:|verbose|sample-accurate|" |
| 405 | "realtime|nchnls|nchnls_i|sinesize=|daemon|port=|use-system-sr|" |
| 406 | "ksmps|midi-key-cps=|midi-velocity=)|" |
| 407 | "\\+(rtaudio=|rtmidi=|jack_client=))"); |
| 408 | /* |
| 409 | csoundOptionsRx = QRegExp("--(env|nodisplays|nosound|control-rate|messagelevel=|" |
| 410 | "dither|sched|omacro:|smacro:|verbose|sample-accurate|" |
| 411 | "realtime|nchnls|nchnls_i|sinesize=|daemon|port=|" |
| 412 | "use-system-sr|ksmps|midi-key-cps=|midi-velocity=)"); |
| 413 | */ |
| 414 | csoundOptionsRx2 = QRegularExpression("-+(rtaudio=|rtmidi=|jack_client=)"); |
| 415 | |
| 416 | // For Python |
| 417 | pythonKeywords << "and" << "or" << "not" << "is" |
| 418 | << "global" << "with" << "from" << "import" << "as" |
| 419 | << "if" << "else" << "elif" |
| 420 | << "print" << "class" << "del" << "exec" |
| 421 | << "for" << "in" << "while" << "continue" << "pass" << "break" |
| 422 | << "def" << "return" << "lambda" |
| 423 | << "yield" << "assert" << "try" << "except" << "finally" << "raise" |
| 424 | << "True" << "False" << "None"; |
| 425 | |
| 426 | // for html |
| 427 | htmlKeywords << "<\\ba\\b" << "<\\babbr\\b" << "<\\bacronym\\b" << "<\\baddress\\b" |
| 428 | << "<\\bapplet\\b" << "<\\barea\\b" << "<\\barticle\\b" << "<\\baside\\b" |
| 429 | << "<\\baudio\\b" << "<\\bb\\b" << "<\\bbase\\b" << "<\\bbasefont\\b" |
| 430 | << "<\\bbdi\\b" << "<\\bbdo\\b" << "<\\bbgsound\\b" << "<\\bblockquote\\b" |
| 431 | << "<\\bbig\\b" << "<\\bbody\\b" << "<\\bblink\\b" << "<\\bbr\\b" |
| 432 | << "<\\bbutton\\b" << "<\\bcanvas\\b" << "<\\bcaption\\b" << "<\\bcenter\\b" |
| 433 | << "<\\bcite\\b" << "<\\bcode\\b" << "<\\bcol\\b" << "<\\bcolgroup\\b" |
| 434 | << "<\\bcomment\\b" << "<\\bdata\\b" << "<\\bdatalist\\b" << "<\\bdd\\b" |
| 435 | << "<\\bdel\\b" << "<\\bdetails\\b" << "<\\bdfn\\b" << "<\\bdialog\\b" |
| 436 | << "<\\bdir\\b" << "<\\bdiv\\b" << "<\\bdl\\b" << "<\\bdt\\b" |
| 437 | << "<\\bem\\b" << "<\\bembed\\b" |
| 438 | << "<\\bfieldset\\b" << "<\\bfigcaption\\b" << "<\\bfigure\\b" |
| 439 | << "<\\bfont\\b" << "<\\bfooter\\b" << "<\\bform\\b" << "<\\bframe\\b" |
| 440 | << "<\\bframeset\\b" << "<\\bh1\\b" << "<\\bh2\\b" << "<\\bh3\\b" << "<\\bh4\\b" |
| 441 | << "<\\bh5\\b" << "<\\bh6\\b" << "<\\bhead\\b" << "<\\bheader\\b" << "<\\bhgroup\\b" |
| 442 | << "<\\bhr\\b" << "<\\bhtml\\b" << "<\\bi\\b" << "<\\biframe\\b" << "<\\bimg\\b" |
| 443 | << "<\\binput\\b" << "<\\bins\\b" << "<\\bisindex\\b" << "<\\bkbd\\b" |
| 444 | << "<\\bkeygen\\b" << "<\\blabel\\b" << "<\\blegend\\b" << "<\\bli\\b" |
| 445 | << "<\\blink\\b" << "<\\blisting\\b" << "<\\bmain\\b" << "<\\bmap\\b" |
| 446 | << "<\\bmarquee\\b" << "<\\bmark\\b" << "<\\bmenu\\b" |
| 447 | << "<\\bamenuitem\\b" << "<\\bmeta\\b" << "<\\bmeter\\b" << "<\\bmulticol\\b" |
| 448 | << "<\\bnav\\b" << "<\\bnobr\\b" << "<\\bnoembed\\b" << "<\\bnoindex\\b" |
| 449 | << "<\\bnoframes\\b" << "<\\bnoscript\\b" << "<\\bobject\\b" << "<\\bol\\b" |
| 450 | << "<\\boptgroup\\b" << "<\\boption\\b" << "<\\boutput\\b" |
| 451 | << "<\\bp\\b" << "<\\bparam\\b" << "<\\bpicture\\b" << "<\\bplaintext\\b" |
| 452 | << "<\\bpre\\b" << "<\\bprogress\\b" << "<\\bq\\b" << "<\\brp\\b" |
| 453 | << "<\\brt\\b" << "<\\brtc\\b" << "<\\bruby\\b" << "<\\bs\\b" |
| 454 | << "<\\bsamp\\b" << "<\\bscript\\b" << "<\\bsection\\b" << "<\\bselect\\b" |
| 455 | << "<\\bsmall\\b" << "<\\bsource\\b" << "<\\bspacer\\b" << "<\\bspan\\b" |
| 456 | << "<\\bstrike\\b" << "<\\bstrong\\b" << "<\\bstyle\\b" << "<\\bsub\\b" |
| 457 | << "<\\bsummary\\b" << "<\\bsup\\b" << "<\\btable\\b" << "<\\btbody\\b" |
| 458 | << "<\\btd\\b" << "<\\btemplate\\b" << "<\\btextarea\\b" |
| 459 | << "<\\btfoot\\b" << "<\\bth\\b" << "<\\bthead\\b" << "<\\btime\\b" |
| 460 | << "<\\btitle\\b" << "<\\btr\\b" << "<\\btrack\\b" << "<\\btt\\b" |
| 461 | << "<\\bu\\b" << "<\\bul\\b" << "<\\bvar\\b" << "<\\bwbr\\b" << "<\\bxmp\\b" |
| 462 | << "<!\\bDOCTYPE\\b"; |
| 463 | |
| 464 | javascriptKeywords << "function" << "var" << "if" << "===" << "console.log" << "console.warn"; |
| 465 | |
| 466 | rxScoreLetter.setPattern("^\\s*(i|f|e|d|s)"); |
| 467 | rxQuotation.setPattern("\"[^\"]*\""); |
| 468 | |
| 469 | // this->setTheme("classic"); |
| 470 | } |
| 471 | |
| 472 | |
| 473 | Highlighter::~Highlighter() |
| 474 | { |
| 475 | } |
| 476 | |
| 477 | void Highlighter::setOpcodeNameList(QStringList list) |
| 478 | { |
| 479 | m_opcodeList = list; |
| 480 | // m_opcodesSet = list.toSet(); |
| 481 | m_opcodesSet = QSet<QString>(list.begin(), list.end()); // Use the newer form for better future compatibility with qt6 |
| 482 | // setFirstRules(); |
| 483 | setLastRules(); |
| 484 | } |
| 485 | |
| 486 | |
| 487 | void Highlighter::setMode(int mode) |
| 488 | { |
| 489 | m_mode = mode; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | void Highlighter::setColorVariables(bool color) |
| 494 | { |
| 495 | colorVariables = color; |
| 496 | |
| 497 | highlightingRules.clear(); |
| 498 | setLastRules(); |
| 499 | } |
| 500 | |
| 501 | void Highlighter::highlightBlock(const QString &text) |
| 502 | { |
| 503 | if(m_theme == "none") |
| 504 | return; |
| 505 | |
| 506 | // for parenthesis |
| 507 | auto data = static_cast<TextBlockData*>(currentBlockUserData()); |
| 508 | if(data == nullptr) { |
| 509 | data = new TextBlockData; |
| 510 | } |
| 511 | else { |
| 512 | data->clearParenthesis(); |
| 513 | } |
| 514 | |
| 515 | int leftPos = text.indexOf('('); |
| 516 | while (leftPos != -1) { |
| 517 | ParenthesisInfo *info = new ParenthesisInfo; |
| 518 | info->character = '('; |
| 519 | info->position = leftPos; |
| 520 | |
| 521 | data->insert(info); |
| 522 | leftPos = text.indexOf('(', leftPos + 1); |
| 523 | } |
| 524 | |
| 525 | int rightPos = text.indexOf(')'); |
| 526 | while (rightPos != -1) { |
| 527 | ParenthesisInfo *info = new ParenthesisInfo; |
| 528 | info->character = ')'; |
| 529 | info->position = rightPos; |
| 530 | |
| 531 | data->insert(info); |
| 532 | |
| 533 | rightPos = text.indexOf(')', rightPos +1); |
| 534 | } |
| 535 | setCurrentBlockUserData(data); |
| 536 | |
| 537 | if(m_theme != "none") { |
| 538 | // find previous valid block |
| 539 | auto block = currentBlock().previous(); |
| 540 | while (block.isValid()) { |
| 541 | auto userData = static_cast<TextBlockData*>(block.userData()); |
| 542 | if(userData != nullptr) { |
| 543 | data->section = userData->section; |
| 544 | break; |
| 545 | } |
| 546 | block = block.previous(); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | switch (m_mode) { |
| 551 | case 0: // Csound mode |
| 552 | highlightCsoundBlock(text); |
| 553 | break; |
| 554 | case 1: // Python mode |
| 555 | highlightPythonBlock(text); |
| 556 | break; |
| 557 | case 2: // Xml mode |
| 558 | highlightXmlBlock(text); |
| 559 | break; |
| 560 | case 3: // Orc |
| 561 | highlightCsoundBlock(text); |
| 562 | break; |
| 563 | case 4: // Sco |
| 564 | highlightCsoundBlock(text); |
| 565 | break; |
| 566 | case 5: // Inc |
| 567 | highlightCsoundBlock(text); // maybe anything not python or xml should be higlighter as csound? |
| 568 | break; |
| 569 | case 6: // Html |
| 570 | highlightHtmlBlock(text); |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | } |
| 575 | |
| 576 | |
| 577 | void Highlighter::highlightScore(const QString &text, int start, int end) { |
| 578 | // things to highlight: |
| 579 | // 1) score command (i, f, e, etc) |
| 580 | // 2) strings |
| 581 | // 3) macros: $foo |
| 582 | // qDebug() << "highlighting score text: " << text << "\n"; |
| 583 | // return; |
| 584 | // QRegularExpression rx; |
| 585 | // rx.setPattern("^\\s*(i|f|e|d|s)"); |
| 586 | QRegularExpressionMatch match; |
| 587 | match = rxScoreLetter.match(text, start); |
| 588 | if(match.hasMatch()) { |
| 589 | setFormat(match.capturedStart(1), 1, scoreLetterFormat); |
| 590 | } |
| 591 | int pos = start; |
| 592 | while(pos < end) { |
| 593 | match = rxQuotation.match(text, pos); |
| 594 | if(!match.hasMatch()) |
| 595 | break; |
| 596 | setFormat(match.capturedStart(), match.capturedLength(), quotationFormat); |
| 597 | pos = match.capturedEnd() + 1; |
| 598 | } |
| 599 | /* |
| 600 | pos = start; |
| 601 | rx.setPattern("\\$[a-zA-Z_]\\w+"); |
| 602 | while(pos < end) { |
| 603 | match = rx.match(text, pos); |
| 604 | if(!match.hasMatch()) |
| 605 | break; |
| 606 | setFormat(match.capturedStart(), match.capturedLength(), macroDefineFormat); |
| 607 | pos = match.capturedEnd() + 1; |
| 608 | } |
| 609 | */ |
| 610 | } |
| 611 | |
| 612 | |
| 613 | void Highlighter::highlightCsoundBlock(const QString &line) |
| 614 | { |
| 615 | // TODO: rewrite this using QRegularExpression |
| 616 | QRegularExpression rx; |
| 617 | QRegularExpressionMatch rxmatch; |
| 618 | |
| 619 | // text is processed one line at a time |
| 620 | if(m_theme == "none") |
| 621 | return; |
| 622 | |
| 623 | int commentIndex = line.indexOf(';'); // try both comment markings |
| 624 | if (commentIndex < 0) { |
| 625 | commentIndex = line.indexOf("//"); |
| 626 | } |
| 627 | |
| 628 | if(commentIndex >= 0) { |
| 629 | if(line[commentIndex] == ';' && line.size() > commentIndex+1 && line[commentIndex+1] == ';') { |
| 630 | setFormat(commentIndex, line.size() - commentIndex, importantCommentFormat); |
| 631 | return; |
| 632 | } else { |
| 633 | setFormat(commentIndex, line.size() - commentIndex, singleLineCommentFormat); |
| 634 | } |
| 635 | } else { |
| 636 | commentIndex = line.size() + 1; |
| 637 | } |
| 638 | // auto text = line.mid(0, commentIndex); |
| 639 | |
| 640 | int index = 0; |
| 641 | int length = 0; |
| 642 | |
| 643 | auto blockdata = static_cast<TextBlockData*>(currentBlockUserData()); |
| 644 | |
| 645 | rx.setPattern("^\\s*<\\/?(CsInstruments|CsOptions|CsoundSynthesizer|CsScore|CsFileB|CsLicense|html).*>"); |
| 646 | rxmatch = rx.match(line); |
| 647 | if(rxmatch.hasMatch()) { |
| 648 | if(rxmatch.captured(1) == "CsInstruments") { |
| 649 | blockdata->section = OrchestraSection; |
| 650 | } else if(rxmatch.captured(1) == "CsScore") { |
| 651 | blockdata->section = ScoreSection; |
| 652 | } else if(rxmatch.captured(1) == "CsOptions") { |
| 653 | blockdata->section = OptionsSection; |
| 654 | } else { |
| 655 | blockdata->section = UnknownSection; |
| 656 | } |
| 657 | setFormat(rxmatch.capturedStart(), rxmatch.capturedLength(), csdtagFormat); |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | if(blockdata->section == ScoreSection) { |
| 662 | if(m_scoreSyntaxHighlighting) { |
| 663 | highlightScore(line, 0, commentIndex); |
| 664 | } |
| 665 | return; |
| 666 | } |
| 667 | else if(blockdata->section == OptionsSection) { |
| 668 | index = 0; |
| 669 | while((rxmatch = csoundOptionsRx.match(line, index)).hasMatch()) { |
| 670 | length = rxmatch.capturedLength(); |
| 671 | setFormat(rxmatch.capturedStart(), length, csoundOptionFormat); |
| 672 | index += length; |
| 673 | } |
| 674 | return; |
| 675 | } |
| 676 | else if(blockdata->section == UnknownSection) { |
| 677 | return; |
| 678 | } |
| 679 | |
| 680 | auto text = QStringRef(&line, 0, commentIndex); |
| 681 | |
| 682 | // define |
| 683 | rx.setPattern("^\\s*#define\\s+[_\\w\\ \\t]*#.*#"); |
| 684 | rxmatch = rx.match(text); |
| 685 | if(rxmatch.hasMatch()) { |
| 686 | setFormat(rxmatch.capturedStart(), rxmatch.capturedLength(), macroDefineFormat); |
| 687 | return; |
| 688 | } |
| 689 | |
| 690 | rx.setPattern("^\\s*\\b(instr|opcode)\\s+(\\w+)\\b"); |
| 691 | rxmatch = rx.match(text); |
| 692 | if(rxmatch.hasMatch()) { |
| 693 | auto group = rxmatch.captured(2); |
| 694 | setFormat(rxmatch.capturedStart(1), rxmatch.capturedLength(1), instFormat); |
| 695 | setFormat(rxmatch.capturedStart(2), rxmatch.capturedLength(2), nameFormat); |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | rx.setPattern(R"(&&|==|\|\||<|>|<=|>=|!=|\\)"); |
| 700 | index = 0; |
| 701 | while((rxmatch=rx.match(text, index)).hasMatch()) { |
| 702 | length = rxmatch.capturedLength(); |
| 703 | setFormat(rxmatch.capturedStart(), length, operatorFormat); |
| 704 | index = rxmatch.capturedEnd()+1; |
| 705 | } |
| 706 | |
| 707 | QRegularExpression expressionRx("\\b[\\w:]+\\b"); |
| 708 | QRegularExpression pfieldRx("\\bp[\\d]+\\b"); |
| 709 | index = 0; |
| 710 | while((rxmatch = expressionRx.match(text, index)).hasMatch()) { |
| 711 | int wordStart = rxmatch.capturedStart(); |
| 712 | int wordEnd = rxmatch.capturedEnd(); |
| 713 | index = wordEnd; |
| 714 | |
| 715 | if(wordStart > 0) { |
| 716 | auto prev = text.at(wordStart - 1); |
| 717 | if(prev == '$' || prev == '#') { |
| 718 | // check if macro name - replacement for regexp solution |
| 719 | setFormat(wordStart-1, wordEnd - wordStart + 1, macroDefineFormat); |
| 720 | index = wordEnd; |
| 721 | continue; |
| 722 | } |
| 723 | } |
| 724 | wordEnd = (wordEnd > 0 ? wordEnd : text.size()); |
| 725 | QString word = rxmatch.captured(); |
| 726 | if(pfieldRx.match(word).hasMatch()) { |
| 727 | setFormat(wordStart, wordEnd - wordStart, pfieldFormat); |
| 728 | } |
| 729 | else if(instPatterns.contains(word)) { |
| 730 | setFormat(wordStart, wordEnd - wordStart, instFormat); |
| 731 | break; // was: return. For any case, to go through lines after while loop |
| 732 | } |
| 733 | else if(tagPatterns.contains("<" + word + ">") && wordStart > 0) { |
| 734 | setFormat(wordStart - (text[wordStart - 1] == '/'? 2: 1), |
| 735 | wordEnd - wordStart + (text[wordStart - 1] == '/'? 3: 2), |
| 736 | csdtagFormat); |
| 737 | } |
| 738 | else if(headerPatterns.contains(word)) { |
| 739 | setFormat(wordStart, wordEnd - wordStart, headerFormat); |
| 740 | } |
| 741 | else if(keywordLiterals.contains(word)) { |
| 742 | setFormat(wordStart, wordEnd - wordStart, keywordFormat); |
| 743 | } |
| 744 | else if(ioPatterns.contains(word)) { |
| 745 | setFormat(wordStart, wordEnd - wordStart, ioFormat); |
| 746 | } |
| 747 | else if(word[word.size()-1] != ':' && word.contains(":")) { |
| 748 | // functional style opcode:k(...) |
| 749 | auto parts = word.split(":"); |
| 750 | if (parts.size() == 2) { |
| 751 | if (isOpcode(parts[0])) { |
| 752 | setFormat(wordStart, wordEnd - wordStart, opcodeFormat); |
| 753 | } |
| 754 | } |
| 755 | else { |
| 756 | setFormat(wordStart, wordEnd - wordStart, errorFormat); |
| 757 | } |
| 758 | } |
| 759 | else if(m_parsedUDOs.contains(word)) { |
| 760 | setFormat(wordStart, wordEnd - wordStart, udoFormat); |
| 761 | } |
| 762 | else if(deprecatedOpcodes.contains(word)) { |
| 763 | setFormat(wordStart, wordEnd - wordStart, deprecatedFormat); |
| 764 | } |
| 765 | else if(isOpcode(word)) { |
| 766 | setFormat(wordStart, wordEnd - wordStart, opcodeFormat); |
| 767 | } |
| 768 | else if(colorVariables) { |
| 769 | if(word.startsWith('a')) { |
| 770 | setFormat(wordStart, wordEnd - wordStart, arateFormat); |
| 771 | } |
| 772 | else if(word.startsWith('k')) { |
| 773 | setFormat(wordStart, wordEnd - wordStart, krateFormat); |
| 774 | } |
| 775 | else if(word.startsWith('i')) { |
| 776 | setFormat(wordStart, wordEnd - wordStart, irateFormat); |
| 777 | } |
| 778 | else if(word.startsWith("ga")) { |
| 779 | setFormat(wordStart, wordEnd - wordStart, garateFormat); |
| 780 | } |
| 781 | else if(word.startsWith("gk")) { |
| 782 | setFormat(wordStart, wordEnd - wordStart, gkrateFormat); |
| 783 | } |
| 784 | else if(word.startsWith("gi")) { |
| 785 | setFormat(wordStart, wordEnd - wordStart, girateFormat); |
| 786 | } |
| 787 | else if(word.startsWith("S")) { |
| 788 | setFormat(wordStart, wordEnd - wordStart, stringVarFormat); |
| 789 | } |
| 790 | else if(word.startsWith("gS")) { |
| 791 | setFormat(wordStart, wordEnd - wordStart, gstringVarFormat); |
| 792 | } |
| 793 | else if(word.startsWith("f")) { |
| 794 | setFormat(wordStart, wordEnd - wordStart, fsigFormat); |
| 795 | } |
| 796 | else if(word.startsWith("gf")) { |
| 797 | setFormat(wordStart, wordEnd - wordStart, gfsigFormat); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | } |
| 802 | |
| 803 | // string |
| 804 | rx.setPattern("\"[^\"]*\""); |
| 805 | index = 0; |
| 806 | while ((rxmatch = rx.match(text, index)).hasMatch()) { |
| 807 | setFormat(rxmatch.capturedStart(), rxmatch.capturedLength(), quotationFormat); |
| 808 | index = rxmatch.capturedEnd(); |
| 809 | } |
| 810 | |
| 811 | //last rules |
| 812 | for(auto rule: lastHighlightingRules) { |
| 813 | index = 0; |
| 814 | while((rxmatch = rule.pattern.match(text, index)).hasMatch()) { |
| 815 | int group = rule.group; |
| 816 | qDebug() << "last rule matched: " << rule.pattern.pattern(); |
| 817 | setFormat(rxmatch.capturedLength(group), rxmatch.capturedStart(group), rule.format); |
| 818 | index = rxmatch.capturedEnd(); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | setCurrentBlockState(0); |
| 823 | |
| 824 | int startIndex = 0; |
| 825 | if(previousBlockState() != 1) { |
| 826 | rxmatch = commentStartExpression.match(text, 0); |
| 827 | startIndex = rxmatch.hasMatch() ? rxmatch.capturedStart() : -1; |
| 828 | } |
| 829 | |
| 830 | QRegularExpressionMatch endMatch; |
| 831 | |
| 832 | while (startIndex >= 0 && startIndex < commentIndex) { |
| 833 | endMatch = commentEndExpression.match(text, startIndex); |
| 834 | int endIndex = endMatch.hasMatch() ? endMatch.capturedStart() : -1; |
| 835 | if (format(startIndex) == quotationFormat) { |
| 836 | rxmatch = commentStartExpression.match(text, startIndex+1); |
| 837 | startIndex = rxmatch.hasMatch() ? rxmatch.capturedStart() : -1; |
| 838 | continue; |
| 839 | } |
| 840 | int commentLength; |
| 841 | if (endIndex == -1) { |
| 842 | setCurrentBlockState(1); |
| 843 | commentLength = text.length() - startIndex; |
| 844 | } else { |
| 845 | commentLength = endIndex - startIndex + endMatch.capturedLength(); |
| 846 | } |
| 847 | setFormat(startIndex, commentLength, multiLineCommentFormat); |
| 848 | rxmatch = commentStartExpression.match(text, startIndex + commentLength); |
| 849 | startIndex = rxmatch.hasMatch() ? rxmatch.capturedStart() : -1; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | |
| 854 | void Highlighter::highlightPythonBlock(const QString &text) |
| 855 | { |
| 856 | QRegExp expression("\\b+\\w\\b+"); |
| 857 | int index = text.indexOf(expression, 0); |
| 858 | for (int i = 0; i < pythonKeywords.size(); i++) { |
| 859 | QRegExp expression("\\b+" + pythonKeywords[i] + "\\b+"); |
| 860 | int index = text.indexOf(expression); |
| 861 | while (index >= 0) { |
| 862 | int length = expression.matchedLength(); |
| 863 | setFormat(index, length, keywordFormat); |
| 864 | index = text.indexOf(expression, index + length); |
| 865 | } |
| 866 | } |
| 867 | QRegExp strings( QRegExp("\"[^\"]*\"")); |
| 868 | index = text.indexOf(strings); |
| 869 | while (index >= 0) { |
| 870 | int length = strings.matchedLength(); |
| 871 | setFormat(index, length, quotationFormat); |
| 872 | index = text.indexOf(strings, index + length); |
| 873 | } |
| 874 | strings = QRegExp("'[^'']*'"); |
| 875 | index = text.indexOf(strings); |
| 876 | while (index >= 0) { |
| 877 | int length = strings.matchedLength(); |
| 878 | setFormat(index, length, quotationFormat); |
| 879 | index = text.indexOf(strings, index + length); |
| 880 | } |
| 881 | QRegExp expComment("#.*"); |
| 882 | index = text.indexOf(expComment); |
| 883 | while (index >= 0) { |
| 884 | int length = expComment.matchedLength(); |
| 885 | setFormat(index, length, singleLineCommentFormat); |
| 886 | index = text.indexOf(expComment, index + length); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | void Highlighter::highlightXmlBlock(const QString &/*text*/) |
| 891 | { |
| 892 | } |
| 893 | |
| 894 | // for html |
| 895 | |
| 896 | void Highlighter::highlightHtmlBlock(const QString &text) |
| 897 | { |
| 898 | QRegExp expression("\\b+\\w\\b+"); |
| 899 | int index = text.indexOf(expression, 0); |
| 900 | for (int i = 0; i < htmlKeywords.size(); i++) { |
| 901 | QRegExp expression(htmlKeywords[i]);//expression("\\b+" + htmlKeywords[i] + "\\b+"); |
| 902 | int index = text.indexOf(expression); |
| 903 | while (index >= 0) { |
| 904 | int length = expression.matchedLength(); |
| 905 | setFormat(index, length, keywordFormat); |
| 906 | index = text.indexOf(expression, index + length); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | for (int i=0; i<javascriptKeywords.size(); i++) { |
| 911 | QRegExp expression("\\b+" + javascriptKeywords[i] + "\\b+"); |
| 912 | int index = text.indexOf(expression); |
| 913 | while (index >= 0) { |
| 914 | int length = expression.matchedLength(); |
| 915 | setFormat(index, length, jsKeywordFormat); // TODO javascriptformat |
| 916 | index = text.indexOf(expression, index + length); |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | QRegExp endTag( QRegExp(">$")); |
| 921 | index = text.indexOf(endTag); |
| 922 | while (index >= 0) { |
| 923 | int length = endTag.matchedLength(); |
| 924 | setFormat(index, length, keywordFormat); |
| 925 | index = text.indexOf(endTag, index + length); |
| 926 | } |
| 927 | |
| 928 | QRegExp strings( QRegExp("\"[^\"]*\"")); |
| 929 | index = text.indexOf(strings); |
| 930 | while (index >= 0) { |
| 931 | int length = strings.matchedLength(); |
| 932 | setFormat(index, length, quotationFormat); |
| 933 | index = text.indexOf(strings, index + length); |
| 934 | } |
| 935 | strings = QRegExp("'[^'']*'"); |
| 936 | index = text.indexOf(strings); |
| 937 | while (index >= 0) { |
| 938 | int length = strings.matchedLength(); |
| 939 | setFormat(index, length, quotationFormat); |
| 940 | index = text.indexOf(strings, index + length); |
| 941 | } |
| 942 | int commentIndex = -1; |
| 943 | QRegExp expComment("//.*"); // TODO: avaoid https:// |
| 944 | index = text.indexOf(expComment); |
| 945 | if (index>0 ) { |
| 946 | if (text.at(index-1)!=':') { // clumsy way to avoid addresses like https:// |
| 947 | while (index >= 0) { // did not manage to do it with regular expression |
| 948 | int length = expComment.matchedLength(); |
| 949 | setFormat(index, length, singleLineCommentFormat); |
| 950 | index = text.indexOf(expComment, index + length); |
| 951 | } |
| 952 | commentIndex = index; // better do other way |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | // if (commentIndex >= 0) { |
| 957 | // setFormat(commentIndex, text.size() - commentIndex, singleLineCommentFormat); |
| 958 | //// return; |
| 959 | // } |
| 960 | if (commentIndex < 0) { |
| 961 | commentIndex = text.size() + 1; |
| 962 | } |
| 963 | |
| 964 | // multiline |
| 965 | setCurrentBlockState(0); |
| 966 | QRegExp htmlCommentStartExpression = QRegExp("<!--"); |
| 967 | QRegExp htmlCommentEndExpression = QRegExp("-->"); |
| 968 | |
| 969 | |
| 970 | int startIndex = 0; |
| 971 | if (previousBlockState() != 1) { |
| 972 | startIndex = text.indexOf(htmlCommentStartExpression); |
| 973 | } |
| 974 | |
| 975 | while (startIndex >= 0 && startIndex < commentIndex) { |
| 976 | int endIndex = text.indexOf(htmlCommentEndExpression, startIndex); |
| 977 | if (format(startIndex) == quotationFormat) { |
| 978 | startIndex = text.indexOf(htmlCommentStartExpression, |
| 979 | startIndex + 1); |
| 980 | continue; |
| 981 | } |
| 982 | int commentLength; |
| 983 | if (endIndex == -1) { |
| 984 | setCurrentBlockState(1); |
| 985 | commentLength = text.length() - startIndex; |
| 986 | } else { |
| 987 | commentLength = endIndex - startIndex |
| 988 | + htmlCommentEndExpression.matchedLength(); |
| 989 | } |
| 990 | setFormat(startIndex, commentLength, multiLineCommentFormat); |
| 991 | startIndex = text.indexOf(htmlCommentStartExpression, |
| 992 | startIndex + commentLength); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | // void Highlighter::setFirstRules() |
| 997 | // { |
| 998 | // highlightingRules.clear(); |
| 999 | // } |
| 1000 | |
| 1001 | void Highlighter::setLastRules() |
| 1002 | { |
| 1003 | HighlightingRule rule; |
| 1004 | |
| 1005 | // labels |
| 1006 | rule = {QRegularExpression("^\\s*([a-zA-Z]\\w*):\\s*$"), labelFormat, 1}; |
| 1007 | lastHighlightingRules.append(rule); |
| 1008 | |
| 1009 | // strings |
| 1010 | // lastHighlightingRules.append({QRegularExpression("\"[^\"]*\""), quotationFormat, 0}); |
| 1011 | |
| 1012 | // NB: multiline strings should be implemented like multi-line comments |
| 1013 | // NB2: qt does syntax highlighting on a line-per-line basis |
| 1014 | // multi line strings |
| 1015 | // rule = {QRegularExpression("\\{\\{.*"), quotationFormat, 0}; |
| 1016 | // lastHighlightingRules.append(rule); |
| 1017 | |
| 1018 | // end multi line strings |
| 1019 | // rule = {QRegularExpression(".*\\}\\}"), quotationFormat, 0}; |
| 1020 | // lastHighlightingRules.append(rule); |
| 1021 | } |
| 1022 | |
| 1023 | bool Highlighter::isOpcode(QString name) { |
| 1024 | // Returns true if name is a known opcode |
| 1025 | return m_opcodesSet.contains(name); |
| 1026 | } |
| 1027 | |
| 1028 | int Highlighter::findOpcode(QString opcodeName, int start, int end) |
| 1029 | { |
| 1030 | // fprintf(stderr, "%i - %i\n", start, end); |
| 1031 | Q_ASSERT(m_opcodeList.size() > 0); |
| 1032 | if (end == -1) { |
| 1033 | end = m_opcodeList.size() -1; |
| 1034 | } |
| 1035 | int pos = ((end - start)/2) + start; |
| 1036 | if (opcodeName == m_opcodeList[pos]) |
| 1037 | return pos; |
| 1038 | else if(start == end) |
| 1039 | return -1; |
| 1040 | else if(opcodeName < m_opcodeList[pos]) |
| 1041 | return findOpcode(opcodeName, start, pos); |
| 1042 | else if(opcodeName > m_opcodeList[pos]) |
| 1043 | return findOpcode(opcodeName, pos + 1, end); |
| 1044 | return -1; |
| 1045 | } |
| 1046 | |
| 1047 | void Highlighter::setUDOs(QStringList udos) |
| 1048 | { |
| 1049 | m_parsedUDOs = udos; |
| 1050 | } |