qscilexerperl.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. // This module implements the QsciLexerPerl 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/qscilexerperl.h"
  20. #include <qcolor.h>
  21. #include <qfont.h>
  22. #include <qsettings.h>
  23. // The ctor.
  24. QsciLexerPerl::QsciLexerPerl(QObject *parent)
  25. : QsciLexer(parent),
  26. fold_atelse(false), fold_comments(false), fold_compact(true),
  27. fold_packages(true), fold_pod_blocks(true)
  28. {
  29. }
  30. // The dtor.
  31. QsciLexerPerl::~QsciLexerPerl()
  32. {
  33. }
  34. // Returns the language name.
  35. const char *QsciLexerPerl::language() const
  36. {
  37. return "Perl";
  38. }
  39. // Returns the lexer name.
  40. const char *QsciLexerPerl::lexer() const
  41. {
  42. return "perl";
  43. }
  44. // Return the set of character sequences that can separate auto-completion
  45. // words.
  46. QStringList QsciLexerPerl::autoCompletionWordSeparators() const
  47. {
  48. QStringList wl;
  49. wl << "::" << "->";
  50. return wl;
  51. }
  52. // Return the list of characters that can start a block.
  53. const char *QsciLexerPerl::blockStart(int *style) const
  54. {
  55. if (style)
  56. *style = Operator;
  57. return "{";
  58. }
  59. // Return the list of characters that can end a block.
  60. const char *QsciLexerPerl::blockEnd(int *style) const
  61. {
  62. if (style)
  63. *style = Operator;
  64. return "}";
  65. }
  66. // Return the style used for braces.
  67. int QsciLexerPerl::braceStyle() const
  68. {
  69. return Operator;
  70. }
  71. // Return the string of characters that comprise a word.
  72. const char *QsciLexerPerl::wordCharacters() const
  73. {
  74. return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$@%&";
  75. }
  76. // Returns the foreground colour of the text for a style.
  77. QColor QsciLexerPerl::defaultColor(int style) const
  78. {
  79. switch (style)
  80. {
  81. case Default:
  82. return QColor(0x80,0x80,0x80);
  83. case Error:
  84. case Backticks:
  85. case QuotedStringQX:
  86. return QColor(0xff,0xff,0x00);
  87. case Comment:
  88. return QColor(0x00,0x7f,0x00);
  89. case POD:
  90. case PODVerbatim:
  91. return QColor(0x00,0x40,0x00);
  92. case Number:
  93. return QColor(0x00,0x7f,0x7f);
  94. case Keyword:
  95. return QColor(0x00,0x00,0x7f);
  96. case DoubleQuotedString:
  97. case SingleQuotedString:
  98. case SingleQuotedHereDocument:
  99. case DoubleQuotedHereDocument:
  100. case BacktickHereDocument:
  101. case QuotedStringQ:
  102. case QuotedStringQQ:
  103. return QColor(0x7f,0x00,0x7f);
  104. case Operator:
  105. case Identifier:
  106. case Scalar:
  107. case Array:
  108. case Hash:
  109. case SymbolTable:
  110. case Regex:
  111. case Substitution:
  112. case HereDocumentDelimiter:
  113. case QuotedStringQR:
  114. case QuotedStringQW:
  115. case SubroutinePrototype:
  116. case Translation:
  117. return QColor(0x00,0x00,0x00);
  118. case DataSection:
  119. return QColor(0x60,0x00,0x00);
  120. case FormatIdentifier:
  121. case FormatBody:
  122. return QColor(0xc0,0x00,0xc0);
  123. case DoubleQuotedStringVar:
  124. case RegexVar:
  125. case SubstitutionVar:
  126. case BackticksVar:
  127. case DoubleQuotedHereDocumentVar:
  128. case BacktickHereDocumentVar:
  129. case QuotedStringQQVar:
  130. case QuotedStringQXVar:
  131. case QuotedStringQRVar:
  132. return QColor(0xd0, 0x00, 0x00);
  133. }
  134. return QsciLexer::defaultColor(style);
  135. }
  136. // Returns the end-of-line fill for a style.
  137. bool QsciLexerPerl::defaultEolFill(int style) const
  138. {
  139. switch (style)
  140. {
  141. case POD:
  142. case DataSection:
  143. case SingleQuotedHereDocument:
  144. case DoubleQuotedHereDocument:
  145. case BacktickHereDocument:
  146. case PODVerbatim:
  147. case FormatBody:
  148. case DoubleQuotedHereDocumentVar:
  149. case BacktickHereDocumentVar:
  150. return true;
  151. }
  152. return QsciLexer::defaultEolFill(style);
  153. }
  154. // Returns the font of the text for a style.
  155. QFont QsciLexerPerl::defaultFont(int style) const
  156. {
  157. QFont f;
  158. switch (style)
  159. {
  160. case Comment:
  161. #if defined(Q_OS_WIN)
  162. f = QFont("Comic Sans MS",9);
  163. #elif defined(Q_OS_MAC)
  164. f = QFont("Comic Sans MS", 12);
  165. #else
  166. f = QFont("Bitstream Vera Serif",9);
  167. #endif
  168. break;
  169. case POD:
  170. #if defined(Q_OS_WIN)
  171. f = QFont("Times New Roman",11);
  172. #elif defined(Q_OS_MAC)
  173. f = QFont("Times New Roman", 12);
  174. #else
  175. f = QFont("Bitstream Charter",10);
  176. #endif
  177. break;
  178. case Keyword:
  179. case Operator:
  180. case DoubleQuotedHereDocument:
  181. case FormatIdentifier:
  182. case RegexVar:
  183. case SubstitutionVar:
  184. case BackticksVar:
  185. case DoubleQuotedHereDocumentVar:
  186. case BacktickHereDocumentVar:
  187. case QuotedStringQXVar:
  188. case QuotedStringQRVar:
  189. f = QsciLexer::defaultFont(style);
  190. f.setBold(true);
  191. break;
  192. case DoubleQuotedString:
  193. case SingleQuotedString:
  194. case QuotedStringQQ:
  195. case PODVerbatim:
  196. #if defined(Q_OS_WIN)
  197. f = QFont("Courier New",10);
  198. #elif defined(Q_OS_MAC)
  199. f = QFont("Courier", 12);
  200. #else
  201. f = QFont("Bitstream Vera Sans Mono",9);
  202. #endif
  203. break;
  204. case BacktickHereDocument:
  205. case SubroutinePrototype:
  206. f = QsciLexer::defaultFont(style);
  207. f.setItalic(true);
  208. break;
  209. case DoubleQuotedStringVar:
  210. case QuotedStringQQVar:
  211. #if defined(Q_OS_WIN)
  212. f = QFont("Courier New",10);
  213. #elif defined(Q_OS_MAC)
  214. f = QFont("Courier", 12);
  215. #else
  216. f = QFont("Bitstream Vera Sans Mono",9);
  217. #endif
  218. f.setBold(true);
  219. break;
  220. default:
  221. f = QsciLexer::defaultFont(style);
  222. }
  223. return f;
  224. }
  225. // Returns the set of keywords.
  226. const char *QsciLexerPerl::keywords(int set) const
  227. {
  228. if (set == 1)
  229. return
  230. "NULL __FILE__ __LINE__ __PACKAGE__ __DATA__ __END__ "
  231. "AUTOLOAD BEGIN CORE DESTROY END EQ GE GT INIT LE LT "
  232. "NE CHECK abs accept alarm and atan2 bind binmode "
  233. "bless caller chdir chmod chomp chop chown chr chroot "
  234. "close closedir cmp connect continue cos crypt "
  235. "dbmclose dbmopen defined delete die do dump each "
  236. "else elsif endgrent endhostent endnetent endprotoent "
  237. "endpwent endservent eof eq eval exec exists exit exp "
  238. "fcntl fileno flock for foreach fork format formline "
  239. "ge getc getgrent getgrgid getgrnam gethostbyaddr "
  240. "gethostbyname gethostent getlogin getnetbyaddr "
  241. "getnetbyname getnetent getpeername getpgrp getppid "
  242. "getpriority getprotobyname getprotobynumber "
  243. "getprotoent getpwent getpwnam getpwuid getservbyname "
  244. "getservbyport getservent getsockname getsockopt glob "
  245. "gmtime goto grep gt hex if index int ioctl join keys "
  246. "kill last lc lcfirst le length link listen local "
  247. "localtime lock log lstat lt m map mkdir msgctl "
  248. "msgget msgrcv msgsnd my ne next no not oct open "
  249. "opendir or ord our pack package pipe pop pos print "
  250. "printf prototype push q qq qr quotemeta qu qw qx "
  251. "rand read readdir readline readlink readpipe recv "
  252. "redo ref rename require reset return reverse "
  253. "rewinddir rindex rmdir s scalar seek seekdir select "
  254. "semctl semget semop send setgrent sethostent "
  255. "setnetent setpgrp setpriority setprotoent setpwent "
  256. "setservent setsockopt shift shmctl shmget shmread "
  257. "shmwrite shutdown sin sleep socket socketpair sort "
  258. "splice split sprintf sqrt srand stat study sub "
  259. "substr symlink syscall sysopen sysread sysseek "
  260. "system syswrite tell telldir tie tied time times tr "
  261. "truncate uc ucfirst umask undef unless unlink unpack "
  262. "unshift untie until use utime values vec wait "
  263. "waitpid wantarray warn while write x xor y";
  264. return 0;
  265. }
  266. // Returns the user name of a style.
  267. QString QsciLexerPerl::description(int style) const
  268. {
  269. switch (style)
  270. {
  271. case Default:
  272. return tr("Default");
  273. case Error:
  274. return tr("Error");
  275. case Comment:
  276. return tr("Comment");
  277. case POD:
  278. return tr("POD");
  279. case Number:
  280. return tr("Number");
  281. case Keyword:
  282. return tr("Keyword");
  283. case DoubleQuotedString:
  284. return tr("Double-quoted string");
  285. case SingleQuotedString:
  286. return tr("Single-quoted string");
  287. case Operator:
  288. return tr("Operator");
  289. case Identifier:
  290. return tr("Identifier");
  291. case Scalar:
  292. return tr("Scalar");
  293. case Array:
  294. return tr("Array");
  295. case Hash:
  296. return tr("Hash");
  297. case SymbolTable:
  298. return tr("Symbol table");
  299. case Regex:
  300. return tr("Regular expression");
  301. case Substitution:
  302. return tr("Substitution");
  303. case Backticks:
  304. return tr("Backticks");
  305. case DataSection:
  306. return tr("Data section");
  307. case HereDocumentDelimiter:
  308. return tr("Here document delimiter");
  309. case SingleQuotedHereDocument:
  310. return tr("Single-quoted here document");
  311. case DoubleQuotedHereDocument:
  312. return tr("Double-quoted here document");
  313. case BacktickHereDocument:
  314. return tr("Backtick here document");
  315. case QuotedStringQ:
  316. return tr("Quoted string (q)");
  317. case QuotedStringQQ:
  318. return tr("Quoted string (qq)");
  319. case QuotedStringQX:
  320. return tr("Quoted string (qx)");
  321. case QuotedStringQR:
  322. return tr("Quoted string (qr)");
  323. case QuotedStringQW:
  324. return tr("Quoted string (qw)");
  325. case PODVerbatim:
  326. return tr("POD verbatim");
  327. case SubroutinePrototype:
  328. return tr("Subroutine prototype");
  329. case FormatIdentifier:
  330. return tr("Format identifier");
  331. case FormatBody:
  332. return tr("Format body");
  333. case DoubleQuotedStringVar:
  334. return tr("Double-quoted string (interpolated variable)");
  335. case Translation:
  336. return tr("Translation");
  337. case RegexVar:
  338. return tr("Regular expression (interpolated variable)");
  339. case SubstitutionVar:
  340. return tr("Substitution (interpolated variable)");
  341. case BackticksVar:
  342. return tr("Backticks (interpolated variable)");
  343. case DoubleQuotedHereDocumentVar:
  344. return tr("Double-quoted here document (interpolated variable)");
  345. case BacktickHereDocumentVar:
  346. return tr("Backtick here document (interpolated variable)");
  347. case QuotedStringQQVar:
  348. return tr("Quoted string (qq, interpolated variable)");
  349. case QuotedStringQXVar:
  350. return tr("Quoted string (qx, interpolated variable)");
  351. case QuotedStringQRVar:
  352. return tr("Quoted string (qr, interpolated variable)");
  353. }
  354. return QString();
  355. }
  356. // Returns the background colour of the text for a style.
  357. QColor QsciLexerPerl::defaultPaper(int style) const
  358. {
  359. switch (style)
  360. {
  361. case Error:
  362. return QColor(0xff,0x00,0x00);
  363. case POD:
  364. return QColor(0xe0,0xff,0xe0);
  365. case Scalar:
  366. return QColor(0xff,0xe0,0xe0);
  367. case Array:
  368. return QColor(0xff,0xff,0xe0);
  369. case Hash:
  370. return QColor(0xff,0xe0,0xff);
  371. case SymbolTable:
  372. return QColor(0xe0,0xe0,0xe0);
  373. case Regex:
  374. return QColor(0xa0,0xff,0xa0);
  375. case Substitution:
  376. case Translation:
  377. return QColor(0xf0,0xe0,0x80);
  378. case Backticks:
  379. case BackticksVar:
  380. case QuotedStringQXVar:
  381. return QColor(0xa0,0x80,0x80);
  382. case DataSection:
  383. return QColor(0xff,0xf0,0xd8);
  384. case HereDocumentDelimiter:
  385. case SingleQuotedHereDocument:
  386. case DoubleQuotedHereDocument:
  387. case BacktickHereDocument:
  388. case DoubleQuotedHereDocumentVar:
  389. case BacktickHereDocumentVar:
  390. return QColor(0xdd,0xd0,0xdd);
  391. case PODVerbatim:
  392. return QColor(0xc0,0xff,0xc0);
  393. case FormatBody:
  394. return QColor(0xff,0xf0,0xff);
  395. }
  396. return QsciLexer::defaultPaper(style);
  397. }
  398. // Refresh all properties.
  399. void QsciLexerPerl::refreshProperties()
  400. {
  401. setAtElseProp();
  402. setCommentProp();
  403. setCompactProp();
  404. setPackagesProp();
  405. setPODBlocksProp();
  406. }
  407. // Read properties from the settings.
  408. bool QsciLexerPerl::readProperties(QSettings &qs,const QString &prefix)
  409. {
  410. int rc = true;
  411. fold_atelse = qs.value(prefix + "foldatelse", false).toBool();
  412. fold_comments = qs.value(prefix + "foldcomments", false).toBool();
  413. fold_compact = qs.value(prefix + "foldcompact", true).toBool();
  414. fold_packages = qs.value(prefix + "foldpackages", true).toBool();
  415. fold_pod_blocks = qs.value(prefix + "foldpodblocks", true).toBool();
  416. return rc;
  417. }
  418. // Write properties to the settings.
  419. bool QsciLexerPerl::writeProperties(QSettings &qs,const QString &prefix) const
  420. {
  421. int rc = true;
  422. qs.setValue(prefix + "foldatelse", fold_atelse);
  423. qs.setValue(prefix + "foldcomments", fold_comments);
  424. qs.setValue(prefix + "foldcompact", fold_compact);
  425. qs.setValue(prefix + "foldpackages", fold_packages);
  426. qs.setValue(prefix + "foldpodblocks", fold_pod_blocks);
  427. return rc;
  428. }
  429. // Return true if comments can be folded.
  430. bool QsciLexerPerl::foldComments() const
  431. {
  432. return fold_comments;
  433. }
  434. // Set if comments can be folded.
  435. void QsciLexerPerl::setFoldComments(bool fold)
  436. {
  437. fold_comments = fold;
  438. setCommentProp();
  439. }
  440. // Set the "fold.comment" property.
  441. void QsciLexerPerl::setCommentProp()
  442. {
  443. emit propertyChanged("fold.comment",(fold_comments ? "1" : "0"));
  444. }
  445. // Return true if folds are compact.
  446. bool QsciLexerPerl::foldCompact() const
  447. {
  448. return fold_compact;
  449. }
  450. // Set if folds are compact
  451. void QsciLexerPerl::setFoldCompact(bool fold)
  452. {
  453. fold_compact = fold;
  454. setCompactProp();
  455. }
  456. // Set the "fold.compact" property.
  457. void QsciLexerPerl::setCompactProp()
  458. {
  459. emit propertyChanged("fold.compact",(fold_compact ? "1" : "0"));
  460. }
  461. // Return true if packages can be folded.
  462. bool QsciLexerPerl::foldPackages() const
  463. {
  464. return fold_packages;
  465. }
  466. // Set if packages can be folded.
  467. void QsciLexerPerl::setFoldPackages(bool fold)
  468. {
  469. fold_packages = fold;
  470. setPackagesProp();
  471. }
  472. // Set the "fold.perl.package" property.
  473. void QsciLexerPerl::setPackagesProp()
  474. {
  475. emit propertyChanged("fold.perl.package",(fold_packages ? "1" : "0"));
  476. }
  477. // Return true if POD blocks can be folded.
  478. bool QsciLexerPerl::foldPODBlocks() const
  479. {
  480. return fold_pod_blocks;
  481. }
  482. // Set if POD blocks can be folded.
  483. void QsciLexerPerl::setFoldPODBlocks(bool fold)
  484. {
  485. fold_pod_blocks = fold;
  486. setPODBlocksProp();
  487. }
  488. // Set the "fold.perl.pod" property.
  489. void QsciLexerPerl::setPODBlocksProp()
  490. {
  491. emit propertyChanged("fold.perl.pod",(fold_pod_blocks ? "1" : "0"));
  492. }
  493. // Set if else can be folded.
  494. void QsciLexerPerl::setFoldAtElse(bool fold)
  495. {
  496. fold_atelse = fold;
  497. setAtElseProp();
  498. }
  499. // Set the "fold.perl.at.else" property.
  500. void QsciLexerPerl::setAtElseProp()
  501. {
  502. emit propertyChanged("fold.perl.at.else",(fold_atelse ? "1" : "0"));
  503. }