qscilexerpascal.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. // This module implements the QsciLexerPascal 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/qscilexerpascal.h"
  20. #include <qcolor.h>
  21. #include <qfont.h>
  22. #include <qsettings.h>
  23. // The ctor.
  24. QsciLexerPascal::QsciLexerPascal(QObject *parent)
  25. : QsciLexer(parent),
  26. fold_comments(false), fold_compact(true), fold_preproc(false),
  27. smart_highlight(true)
  28. {
  29. }
  30. // The dtor.
  31. QsciLexerPascal::~QsciLexerPascal()
  32. {
  33. }
  34. // Returns the language name.
  35. const char *QsciLexerPascal::language() const
  36. {
  37. return "Pascal";
  38. }
  39. // Returns the lexer name.
  40. const char *QsciLexerPascal::lexer() const
  41. {
  42. return "pascal";
  43. }
  44. // Return the set of character sequences that can separate auto-completion
  45. // words.
  46. QStringList QsciLexerPascal::autoCompletionWordSeparators() const
  47. {
  48. QStringList wl;
  49. wl << "." << "^";
  50. return wl;
  51. }
  52. // Return the list of keywords that can start a block.
  53. const char *QsciLexerPascal::blockStartKeyword(int *style) const
  54. {
  55. if (style)
  56. *style = Keyword;
  57. return
  58. "case class do else for then private protected public published "
  59. "repeat try while type";
  60. }
  61. // Return the list of characters that can start a block.
  62. const char *QsciLexerPascal::blockStart(int *style) const
  63. {
  64. if (style)
  65. *style = Operator;
  66. return "begin";
  67. }
  68. // Return the list of characters that can end a block.
  69. const char *QsciLexerPascal::blockEnd(int *style) const
  70. {
  71. if (style)
  72. *style = Operator;
  73. return "end";
  74. }
  75. // Return the style used for braces.
  76. int QsciLexerPascal::braceStyle() const
  77. {
  78. return Operator;
  79. }
  80. // Returns the foreground colour of the text for a style.
  81. QColor QsciLexerPascal::defaultColor(int style) const
  82. {
  83. switch (style)
  84. {
  85. case Default:
  86. return QColor(0x80,0x80,0x80);
  87. case Identifier:
  88. break;
  89. case Comment:
  90. case CommentParenthesis:
  91. case CommentLine:
  92. return QColor(0x00,0x7f,0x00);
  93. case PreProcessor:
  94. case PreProcessorParenthesis:
  95. return QColor(0x7f,0x7f,0x00);
  96. case Number:
  97. case HexNumber:
  98. return QColor(0x00,0x7f,0x7f);
  99. case Keyword:
  100. return QColor(0x00,0x00,0x7f);
  101. case SingleQuotedString:
  102. case Character:
  103. return QColor(0x7f,0x00,0x7f);
  104. case UnclosedString:
  105. case Operator:
  106. return QColor(0x00,0x00,0x00);
  107. case Asm:
  108. return QColor(0x80,0x40,0x80);
  109. }
  110. return QsciLexer::defaultColor(style);
  111. }
  112. // Returns the end-of-line fill for a style.
  113. bool QsciLexerPascal::defaultEolFill(int style) const
  114. {
  115. if (style == UnclosedString)
  116. return true;
  117. return QsciLexer::defaultEolFill(style);
  118. }
  119. // Returns the font of the text for a style.
  120. QFont QsciLexerPascal::defaultFont(int style) const
  121. {
  122. QFont f;
  123. switch (style)
  124. {
  125. case Comment:
  126. case CommentParenthesis:
  127. case CommentLine:
  128. #if defined(Q_OS_WIN)
  129. f = QFont("Comic Sans MS",9);
  130. #elif defined(Q_OS_MAC)
  131. f = QFont("Comic Sans MS", 12);
  132. #else
  133. f = QFont("Bitstream Vera Serif",9);
  134. #endif
  135. break;
  136. case Keyword:
  137. case Operator:
  138. f = QsciLexer::defaultFont(style);
  139. f.setBold(true);
  140. break;
  141. case SingleQuotedString:
  142. #if defined(Q_OS_WIN)
  143. f = QFont("Times New Roman", 11);
  144. #elif defined(Q_OS_MAC)
  145. f = QFont("Times New Roman", 12);
  146. #else
  147. f = QFont("Bitstream Charter", 10);
  148. #endif
  149. f.setItalic(true);
  150. break;
  151. case UnclosedString:
  152. #if defined(Q_OS_WIN)
  153. f = QFont("Courier New", 10);
  154. #elif defined(Q_OS_MAC)
  155. f = QFont("Courier", 12);
  156. #else
  157. f = QFont("Bitstream Vera Sans Mono", 9);
  158. #endif
  159. break;
  160. default:
  161. f = QsciLexer::defaultFont(style);
  162. }
  163. return f;
  164. }
  165. // Returns the background colour of the text for a style.
  166. QColor QsciLexerPascal::defaultPaper(int style) const
  167. {
  168. if (style == UnclosedString)
  169. return QColor(0xe0,0xc0,0xe0);
  170. return QsciLexer::defaultPaper(style);
  171. }
  172. // Returns the set of keywords.
  173. const char *QsciLexerPascal::keywords(int set) const
  174. {
  175. if (set == 1)
  176. return
  177. "absolute abstract and array as asm assembler automated begin "
  178. "case cdecl class const constructor delayed deprecated destructor "
  179. "dispid dispinterface div do downto dynamic else end except "
  180. "experimental export exports external far file final finalization "
  181. "finally for forward function goto helper if implementation in "
  182. "inherited initialization inline interface is label library "
  183. "message mod near nil not object of on operator or out overload "
  184. "override packed pascal platform private procedure program "
  185. "property protected public published raise record reference "
  186. "register reintroduce repeat resourcestring safecall sealed set "
  187. "shl shr static stdcall strict string then threadvar to try type "
  188. "unit unsafe until uses var varargs virtual while winapi with xor"
  189. "add default implements index name nodefault read readonly remove "
  190. "stored write writeonly"
  191. "package contains requires";
  192. return 0;
  193. }
  194. // Returns the user name of a style.
  195. QString QsciLexerPascal::description(int style) const
  196. {
  197. switch (style)
  198. {
  199. case Default:
  200. return tr("Default");
  201. case Identifier:
  202. return tr("Identifier");
  203. case Comment:
  204. return tr("'{ ... }' style comment");
  205. case CommentParenthesis:
  206. return tr("'(* ... *)' style comment");
  207. case CommentLine:
  208. return tr("Line comment");
  209. case PreProcessor:
  210. return tr("'{$ ... }' style pre-processor block");
  211. case PreProcessorParenthesis:
  212. return tr("'(*$ ... *)' style pre-processor block");
  213. case Number:
  214. return tr("Number");
  215. case HexNumber:
  216. return tr("Hexadecimal number");
  217. case Keyword:
  218. return tr("Keyword");
  219. case SingleQuotedString:
  220. return tr("Single-quoted string");
  221. case UnclosedString:
  222. return tr("Unclosed string");
  223. case Character:
  224. return tr("Character");
  225. case Operator:
  226. return tr("Operator");
  227. case Asm:
  228. return tr("Inline asm");
  229. }
  230. return QString();
  231. }
  232. // Refresh all properties.
  233. void QsciLexerPascal::refreshProperties()
  234. {
  235. setCommentProp();
  236. setCompactProp();
  237. setPreprocProp();
  238. setSmartHighlightProp();
  239. }
  240. // Read properties from the settings.
  241. bool QsciLexerPascal::readProperties(QSettings &qs,const QString &prefix)
  242. {
  243. int rc = true;
  244. fold_comments = qs.value(prefix + "foldcomments", false).toBool();
  245. fold_compact = qs.value(prefix + "foldcompact", true).toBool();
  246. fold_preproc = qs.value(prefix + "foldpreprocessor", true).toBool();
  247. smart_highlight = qs.value(prefix + "smarthighlight", true).toBool();
  248. return rc;
  249. }
  250. // Write properties to the settings.
  251. bool QsciLexerPascal::writeProperties(QSettings &qs,const QString &prefix) const
  252. {
  253. int rc = true;
  254. qs.setValue(prefix + "foldcomments", fold_comments);
  255. qs.setValue(prefix + "foldcompact", fold_compact);
  256. qs.setValue(prefix + "foldpreprocessor", fold_preproc);
  257. qs.setValue(prefix + "smarthighlight", smart_highlight);
  258. return rc;
  259. }
  260. // Return true if comments can be folded.
  261. bool QsciLexerPascal::foldComments() const
  262. {
  263. return fold_comments;
  264. }
  265. // Set if comments can be folded.
  266. void QsciLexerPascal::setFoldComments(bool fold)
  267. {
  268. fold_comments = fold;
  269. setCommentProp();
  270. }
  271. // Set the "fold.comment" property.
  272. void QsciLexerPascal::setCommentProp()
  273. {
  274. emit propertyChanged("fold.comment",(fold_comments ? "1" : "0"));
  275. }
  276. // Return true if folds are compact.
  277. bool QsciLexerPascal::foldCompact() const
  278. {
  279. return fold_compact;
  280. }
  281. // Set if folds are compact
  282. void QsciLexerPascal::setFoldCompact(bool fold)
  283. {
  284. fold_compact = fold;
  285. setCompactProp();
  286. }
  287. // Set the "fold.compact" property.
  288. void QsciLexerPascal::setCompactProp()
  289. {
  290. emit propertyChanged("fold.compact",(fold_compact ? "1" : "0"));
  291. }
  292. // Return true if preprocessor blocks can be folded.
  293. bool QsciLexerPascal::foldPreprocessor() const
  294. {
  295. return fold_preproc;
  296. }
  297. // Set if preprocessor blocks can be folded.
  298. void QsciLexerPascal::setFoldPreprocessor(bool fold)
  299. {
  300. fold_preproc = fold;
  301. setPreprocProp();
  302. }
  303. // Set the "fold.preprocessor" property.
  304. void QsciLexerPascal::setPreprocProp()
  305. {
  306. emit propertyChanged("fold.preprocessor",(fold_preproc ? "1" : "0"));
  307. }
  308. // Return true if smart highlighting is enabled.
  309. bool QsciLexerPascal::smartHighlighting() const
  310. {
  311. return smart_highlight;
  312. }
  313. // Set if smart highlighting is enabled.
  314. void QsciLexerPascal::setSmartHighlighting(bool enabled)
  315. {
  316. smart_highlight = enabled;
  317. setSmartHighlightProp();
  318. }
  319. // Set the "lexer.pascal.smart.highlighting" property.
  320. void QsciLexerPascal::setSmartHighlightProp()
  321. {
  322. emit propertyChanged("lexer.pascal.smart.highlighting", (smart_highlight ? "1" : "0"));
  323. }