PageRenderTime 70ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/scintilla/lexers/LexFlagship.cxx

https://gitlab.com/cjeight/tortoisegit
C++ | 354 lines | 315 code | 21 blank | 18 comment | 187 complexity | 961a542a7f6731bd173d50fd0eed3f06 MD5 | raw file
  1. // Scintilla source code edit control
  2. /** @file LexFlagShip.cxx
  3. ** Lexer for Harbour and FlagShip.
  4. ** (Syntactically compatible to other xBase dialects, like Clipper, dBase, Clip, FoxPro etc.)
  5. **/
  6. // Copyright 2005 by Randy Butler
  7. // Copyright 2010 by Xavi <jarabal/at/gmail.com> (Harbour)
  8. // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
  9. // The License.txt file describes the conditions under which this software may be distributed.
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <stdarg.h>
  14. #include <assert.h>
  15. #include <ctype.h>
  16. #include "ILexer.h"
  17. #include "Scintilla.h"
  18. #include "SciLexer.h"
  19. #include "WordList.h"
  20. #include "LexAccessor.h"
  21. #include "Accessor.h"
  22. #include "StyleContext.h"
  23. #include "CharacterSet.h"
  24. #include "LexerModule.h"
  25. #ifdef SCI_NAMESPACE
  26. using namespace Scintilla;
  27. #endif
  28. // Extended to accept accented characters
  29. static inline bool IsAWordChar(int ch)
  30. {
  31. return ch >= 0x80 ||
  32. (isalnum(ch) || ch == '_');
  33. }
  34. static void ColouriseFlagShipDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
  35. WordList *keywordlists[], Accessor &styler)
  36. {
  37. WordList &keywords = *keywordlists[0];
  38. WordList &keywords2 = *keywordlists[1];
  39. WordList &keywords3 = *keywordlists[2];
  40. WordList &keywords4 = *keywordlists[3];
  41. WordList &keywords5 = *keywordlists[4];
  42. // property lexer.flagship.styling.within.preprocessor
  43. // For Harbour code, determines whether all preprocessor code is styled in the preprocessor style (0) or only from the
  44. // initial # to the end of the command word(1, the default). It also determines how to present text, dump, and disabled code.
  45. bool stylingWithinPreprocessor = styler.GetPropertyInt("lexer.flagship.styling.within.preprocessor", 1) != 0;
  46. CharacterSet setDoxygen(CharacterSet::setAlpha, "$@\\&<>#{}[]");
  47. int visibleChars = 0;
  48. int closeStringChar = 0;
  49. int styleBeforeDCKeyword = SCE_FS_DEFAULT;
  50. bool bEnableCode = initStyle < SCE_FS_DISABLEDCODE;
  51. StyleContext sc(startPos, length, initStyle, styler);
  52. for (; sc.More(); sc.Forward()) {
  53. // Determine if the current state should terminate.
  54. switch (sc.state) {
  55. case SCE_FS_OPERATOR:
  56. case SCE_FS_OPERATOR_C:
  57. case SCE_FS_WORDOPERATOR:
  58. sc.SetState(bEnableCode ? SCE_FS_DEFAULT : SCE_FS_DEFAULT_C);
  59. break;
  60. case SCE_FS_IDENTIFIER:
  61. case SCE_FS_IDENTIFIER_C:
  62. if (!IsAWordChar(sc.ch)) {
  63. char s[64];
  64. sc.GetCurrentLowered(s, sizeof(s));
  65. if (keywords.InList(s)) {
  66. sc.ChangeState(bEnableCode ? SCE_FS_KEYWORD : SCE_FS_KEYWORD_C);
  67. } else if (keywords2.InList(s)) {
  68. sc.ChangeState(bEnableCode ? SCE_FS_KEYWORD2 : SCE_FS_KEYWORD2_C);
  69. } else if (bEnableCode && keywords3.InList(s)) {
  70. sc.ChangeState(SCE_FS_KEYWORD3);
  71. } else if (bEnableCode && keywords4.InList(s)) {
  72. sc.ChangeState(SCE_FS_KEYWORD4);
  73. }// Else, it is really an identifier...
  74. sc.SetState(bEnableCode ? SCE_FS_DEFAULT : SCE_FS_DEFAULT_C);
  75. }
  76. break;
  77. case SCE_FS_NUMBER:
  78. if (!IsAWordChar(sc.ch) && !(sc.ch == '.' && IsADigit(sc.chNext))) {
  79. sc.SetState(SCE_FS_DEFAULT);
  80. }
  81. break;
  82. case SCE_FS_NUMBER_C:
  83. if (!IsAWordChar(sc.ch) && sc.ch != '.') {
  84. sc.SetState(SCE_FS_DEFAULT_C);
  85. }
  86. break;
  87. case SCE_FS_CONSTANT:
  88. if (!IsAWordChar(sc.ch)) {
  89. sc.SetState(SCE_FS_DEFAULT);
  90. }
  91. break;
  92. case SCE_FS_STRING:
  93. case SCE_FS_STRING_C:
  94. if (sc.ch == closeStringChar) {
  95. sc.ForwardSetState(bEnableCode ? SCE_FS_DEFAULT : SCE_FS_DEFAULT_C);
  96. } else if (sc.atLineEnd) {
  97. sc.ChangeState(bEnableCode ? SCE_FS_STRINGEOL : SCE_FS_STRINGEOL_C);
  98. }
  99. break;
  100. case SCE_FS_STRINGEOL:
  101. case SCE_FS_STRINGEOL_C:
  102. if (sc.atLineStart) {
  103. sc.SetState(bEnableCode ? SCE_FS_DEFAULT : SCE_FS_DEFAULT_C);
  104. }
  105. break;
  106. case SCE_FS_COMMENTDOC:
  107. case SCE_FS_COMMENTDOC_C:
  108. if (sc.Match('*', '/')) {
  109. sc.Forward();
  110. sc.ForwardSetState(bEnableCode ? SCE_FS_DEFAULT : SCE_FS_DEFAULT_C);
  111. } else if (sc.ch == '@' || sc.ch == '\\') { // JavaDoc and Doxygen support
  112. // Verify that we have the conditions to mark a comment-doc-keyword
  113. if ((IsASpace(sc.chPrev) || sc.chPrev == '*') && (!IsASpace(sc.chNext))) {
  114. styleBeforeDCKeyword = bEnableCode ? SCE_FS_COMMENTDOC : SCE_FS_COMMENTDOC_C;
  115. sc.SetState(SCE_FS_COMMENTDOCKEYWORD);
  116. }
  117. }
  118. break;
  119. case SCE_FS_COMMENT:
  120. case SCE_FS_COMMENTLINE:
  121. if (sc.atLineStart) {
  122. sc.SetState(SCE_FS_DEFAULT);
  123. }
  124. break;
  125. case SCE_FS_COMMENTLINEDOC:
  126. case SCE_FS_COMMENTLINEDOC_C:
  127. if (sc.atLineStart) {
  128. sc.SetState(bEnableCode ? SCE_FS_DEFAULT : SCE_FS_DEFAULT_C);
  129. } else if (sc.ch == '@' || sc.ch == '\\') { // JavaDoc and Doxygen support
  130. // Verify that we have the conditions to mark a comment-doc-keyword
  131. if ((IsASpace(sc.chPrev) || sc.chPrev == '/' || sc.chPrev == '!') && (!IsASpace(sc.chNext))) {
  132. styleBeforeDCKeyword = bEnableCode ? SCE_FS_COMMENTLINEDOC : SCE_FS_COMMENTLINEDOC_C;
  133. sc.SetState(SCE_FS_COMMENTDOCKEYWORD);
  134. }
  135. }
  136. break;
  137. case SCE_FS_COMMENTDOCKEYWORD:
  138. if ((styleBeforeDCKeyword == SCE_FS_COMMENTDOC || styleBeforeDCKeyword == SCE_FS_COMMENTDOC_C) &&
  139. sc.Match('*', '/')) {
  140. sc.ChangeState(SCE_FS_COMMENTDOCKEYWORDERROR);
  141. sc.Forward();
  142. sc.ForwardSetState(bEnableCode ? SCE_FS_DEFAULT : SCE_FS_DEFAULT_C);
  143. } else if (!setDoxygen.Contains(sc.ch)) {
  144. char s[64];
  145. sc.GetCurrentLowered(s, sizeof(s));
  146. if (!IsASpace(sc.ch) || !keywords5.InList(s + 1)) {
  147. sc.ChangeState(SCE_FS_COMMENTDOCKEYWORDERROR);
  148. }
  149. sc.SetState(styleBeforeDCKeyword);
  150. }
  151. break;
  152. case SCE_FS_PREPROCESSOR:
  153. case SCE_FS_PREPROCESSOR_C:
  154. if (sc.atLineEnd) {
  155. if (!(sc.chPrev == ';' || sc.GetRelative(-2) == ';')) {
  156. sc.SetState(bEnableCode ? SCE_FS_DEFAULT : SCE_FS_DEFAULT_C);
  157. }
  158. } else if (stylingWithinPreprocessor) {
  159. if (IsASpaceOrTab(sc.ch)) {
  160. sc.SetState(bEnableCode ? SCE_FS_DEFAULT : SCE_FS_DEFAULT_C);
  161. }
  162. } else if (sc.Match('/', '*') || sc.Match('/', '/') || sc.Match('&', '&')) {
  163. sc.SetState(bEnableCode ? SCE_FS_DEFAULT : SCE_FS_DEFAULT_C);
  164. }
  165. break;
  166. case SCE_FS_DISABLEDCODE:
  167. if (sc.ch == '#' && visibleChars == 0) {
  168. sc.SetState(bEnableCode ? SCE_FS_PREPROCESSOR : SCE_FS_PREPROCESSOR_C);
  169. do { // Skip whitespace between # and preprocessor word
  170. sc.Forward();
  171. } while (IsASpaceOrTab(sc.ch) && sc.More());
  172. if (sc.MatchIgnoreCase("pragma")) {
  173. sc.Forward(6);
  174. do { // Skip more whitespace until keyword
  175. sc.Forward();
  176. } while (IsASpaceOrTab(sc.ch) && sc.More());
  177. if (sc.MatchIgnoreCase("enddump") || sc.MatchIgnoreCase("__endtext")) {
  178. bEnableCode = true;
  179. sc.SetState(SCE_FS_DISABLEDCODE);
  180. sc.Forward(sc.ch == '_' ? 8 : 6);
  181. sc.ForwardSetState(SCE_FS_DEFAULT);
  182. } else {
  183. sc.ChangeState(SCE_FS_DISABLEDCODE);
  184. }
  185. } else {
  186. sc.ChangeState(SCE_FS_DISABLEDCODE);
  187. }
  188. }
  189. break;
  190. case SCE_FS_DATE:
  191. if (sc.ch == '}') {
  192. sc.ForwardSetState(SCE_FS_DEFAULT);
  193. } else if (sc.atLineEnd) {
  194. sc.ChangeState(SCE_FS_STRINGEOL);
  195. }
  196. }
  197. // Determine if a new state should be entered.
  198. if (sc.state == SCE_FS_DEFAULT || sc.state == SCE_FS_DEFAULT_C) {
  199. if (bEnableCode &&
  200. (sc.MatchIgnoreCase(".and.") || sc.MatchIgnoreCase(".not."))) {
  201. sc.SetState(SCE_FS_WORDOPERATOR);
  202. sc.Forward(4);
  203. } else if (bEnableCode && sc.MatchIgnoreCase(".or.")) {
  204. sc.SetState(SCE_FS_WORDOPERATOR);
  205. sc.Forward(3);
  206. } else if (bEnableCode &&
  207. (sc.MatchIgnoreCase(".t.") || sc.MatchIgnoreCase(".f.") ||
  208. (!IsAWordChar(sc.GetRelative(3)) && sc.MatchIgnoreCase("nil")))) {
  209. sc.SetState(SCE_FS_CONSTANT);
  210. sc.Forward(2);
  211. } else if (sc.Match('/', '*')) {
  212. sc.SetState(bEnableCode ? SCE_FS_COMMENTDOC : SCE_FS_COMMENTDOC_C);
  213. sc.Forward();
  214. } else if (bEnableCode && sc.Match('&', '&')) {
  215. sc.SetState(SCE_FS_COMMENTLINE);
  216. sc.Forward();
  217. } else if (sc.Match('/', '/')) {
  218. sc.SetState(bEnableCode ? SCE_FS_COMMENTLINEDOC : SCE_FS_COMMENTLINEDOC_C);
  219. sc.Forward();
  220. } else if (bEnableCode && sc.ch == '*' && visibleChars == 0) {
  221. sc.SetState(SCE_FS_COMMENT);
  222. } else if (sc.ch == '\"' || sc.ch == '\'') {
  223. sc.SetState(bEnableCode ? SCE_FS_STRING : SCE_FS_STRING_C);
  224. closeStringChar = sc.ch;
  225. } else if (closeStringChar == '>' && sc.ch == '<') {
  226. sc.SetState(bEnableCode ? SCE_FS_STRING : SCE_FS_STRING_C);
  227. } else if (sc.ch == '#' && visibleChars == 0) {
  228. sc.SetState(bEnableCode ? SCE_FS_PREPROCESSOR : SCE_FS_PREPROCESSOR_C);
  229. do { // Skip whitespace between # and preprocessor word
  230. sc.Forward();
  231. } while (IsASpaceOrTab(sc.ch) && sc.More());
  232. if (sc.atLineEnd) {
  233. sc.SetState(bEnableCode ? SCE_FS_DEFAULT : SCE_FS_DEFAULT_C);
  234. } else if (sc.MatchIgnoreCase("include")) {
  235. if (stylingWithinPreprocessor) {
  236. closeStringChar = '>';
  237. }
  238. } else if (sc.MatchIgnoreCase("pragma")) {
  239. sc.Forward(6);
  240. do { // Skip more whitespace until keyword
  241. sc.Forward();
  242. } while (IsASpaceOrTab(sc.ch) && sc.More());
  243. if (sc.MatchIgnoreCase("begindump") || sc.MatchIgnoreCase("__cstream")) {
  244. bEnableCode = false;
  245. if (stylingWithinPreprocessor) {
  246. sc.SetState(SCE_FS_DISABLEDCODE);
  247. sc.Forward(8);
  248. sc.ForwardSetState(SCE_FS_DEFAULT_C);
  249. } else {
  250. sc.SetState(SCE_FS_DISABLEDCODE);
  251. }
  252. } else if (sc.MatchIgnoreCase("enddump") || sc.MatchIgnoreCase("__endtext")) {
  253. bEnableCode = true;
  254. sc.SetState(SCE_FS_DISABLEDCODE);
  255. sc.Forward(sc.ch == '_' ? 8 : 6);
  256. sc.ForwardSetState(SCE_FS_DEFAULT);
  257. }
  258. }
  259. } else if (bEnableCode && sc.ch == '{') {
  260. Sci_Position p = 0;
  261. int chSeek;
  262. Sci_PositionU endPos(startPos + length);
  263. do { // Skip whitespace
  264. chSeek = sc.GetRelative(++p);
  265. } while (IsASpaceOrTab(chSeek) && (sc.currentPos + p < endPos));
  266. if (chSeek == '^') {
  267. sc.SetState(SCE_FS_DATE);
  268. } else {
  269. sc.SetState(SCE_FS_OPERATOR);
  270. }
  271. } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
  272. sc.SetState(bEnableCode ? SCE_FS_NUMBER : SCE_FS_NUMBER_C);
  273. } else if (IsAWordChar(sc.ch)) {
  274. sc.SetState(bEnableCode ? SCE_FS_IDENTIFIER : SCE_FS_IDENTIFIER_C);
  275. } else if (isoperator(static_cast<char>(sc.ch)) || (bEnableCode && sc.ch == '@')) {
  276. sc.SetState(bEnableCode ? SCE_FS_OPERATOR : SCE_FS_OPERATOR_C);
  277. }
  278. }
  279. if (sc.atLineEnd) {
  280. visibleChars = 0;
  281. closeStringChar = 0;
  282. }
  283. if (!IsASpace(sc.ch)) {
  284. visibleChars++;
  285. }
  286. }
  287. sc.Complete();
  288. }
  289. static void FoldFlagShipDoc(Sci_PositionU startPos, Sci_Position length, int,
  290. WordList *[], Accessor &styler)
  291. {
  292. Sci_Position endPos = startPos + length;
  293. // Backtrack to previous line in case need to fix its fold status
  294. Sci_Position lineCurrent = styler.GetLine(startPos);
  295. if (startPos > 0 && lineCurrent > 0) {
  296. lineCurrent--;
  297. startPos = styler.LineStart(lineCurrent);
  298. }
  299. int spaceFlags = 0;
  300. int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags);
  301. char chNext = styler[startPos];
  302. for (Sci_Position i = startPos; i < endPos; i++) {
  303. char ch = chNext;
  304. chNext = styler.SafeGetCharAt(i + 1);
  305. if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == endPos-1)) {
  306. int lev = indentCurrent;
  307. int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags);
  308. if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
  309. if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK)) {
  310. lev |= SC_FOLDLEVELHEADERFLAG;
  311. } else if (indentNext & SC_FOLDLEVELWHITEFLAG) {
  312. int spaceFlags2 = 0;
  313. int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2);
  314. if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK)) {
  315. lev |= SC_FOLDLEVELHEADERFLAG;
  316. }
  317. }
  318. }
  319. indentCurrent = indentNext;
  320. styler.SetLevel(lineCurrent, lev);
  321. lineCurrent++;
  322. }
  323. }
  324. }
  325. static const char * const FSWordListDesc[] = {
  326. "Keywords Commands",
  327. "Std Library Functions",
  328. "Procedure, return, exit",
  329. "Class (oop)",
  330. "Doxygen keywords",
  331. 0
  332. };
  333. LexerModule lmFlagShip(SCLEX_FLAGSHIP, ColouriseFlagShipDoc, "flagship", FoldFlagShipDoc, FSWordListDesc);