LexPascal.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. // Scintilla source code edit control
  2. /** @file LexPascal.cxx
  3. ** Lexer for Pascal.
  4. ** Written by Laurent le Tynevez
  5. ** Updated by Simon Steele <s.steele@pnotepad.org> September 2002
  6. ** Updated by Mathias Rauen <scite@madshi.net> May 2003 (Delphi adjustments)
  7. ** Completely rewritten by Marko Njezic <sf@maxempire.com> October 2008
  8. **/
  9. /*
  10. A few words about features of the new completely rewritten LexPascal...
  11. Generally speaking LexPascal tries to support all available Delphi features (up
  12. to Delphi XE4 at this time).
  13. ~ HIGHLIGHTING:
  14. If you enable "lexer.pascal.smart.highlighting" property, some keywords will
  15. only be highlighted in appropriate context. As implemented those are keywords
  16. related to property and DLL exports declarations (similar to how Delphi IDE
  17. works).
  18. For example, keywords "read" and "write" will only be highlighted if they are in
  19. property declaration:
  20. property MyProperty: boolean read FMyProperty write FMyProperty;
  21. ~ FOLDING:
  22. Folding is supported in the following cases:
  23. - Folding of stream-like comments
  24. - Folding of groups of consecutive line comments
  25. - Folding of preprocessor blocks (the following preprocessor blocks are
  26. supported: IF / IFEND; IFDEF, IFNDEF, IFOPT / ENDIF and REGION / ENDREGION
  27. blocks), including nesting of preprocessor blocks up to 255 levels
  28. - Folding of code blocks on appropriate keywords (the following code blocks are
  29. supported: "begin, asm, record, try, case / end" blocks, class & object
  30. declarations and interface declarations)
  31. Remarks:
  32. - Folding of code blocks tries to handle all special cases in which folding
  33. should not occur. As implemented those are:
  34. 1. Structure "record case / end" (there's only one "end" statement and "case" is
  35. ignored as fold point)
  36. 2. Forward class declarations ("type TMyClass = class;") and object method
  37. declarations ("TNotifyEvent = procedure(Sender: TObject) of object;") are
  38. ignored as fold points
  39. 3. Simplified complete class declarations ("type TMyClass = class(TObject);")
  40. are ignored as fold points
  41. 4. Every other situation when class keyword doesn't actually start class
  42. declaration ("class procedure", "class function", "class of", "class var",
  43. "class property" and "class operator")
  44. 5. Forward (disp)interface declarations ("type IMyInterface = interface;") are
  45. ignored as fold points
  46. - Folding of code blocks inside preprocessor blocks is disabled (any comments
  47. inside them will be folded fine) because there is no guarantee that complete
  48. code block will be contained inside folded preprocessor block in which case
  49. folded code block could end prematurely at the end of preprocessor block if
  50. there is no closing statement inside. This was done in order to properly process
  51. document that may contain something like this:
  52. type
  53. {$IFDEF UNICODE}
  54. TMyClass = class(UnicodeAncestor)
  55. {$ELSE}
  56. TMyClass = class(AnsiAncestor)
  57. {$ENDIF}
  58. private
  59. ...
  60. public
  61. ...
  62. published
  63. ...
  64. end;
  65. If class declarations were folded, then the second class declaration would end
  66. at "$ENDIF" statement, first class statement would end at "end;" statement and
  67. preprocessor "$IFDEF" block would go all the way to the end of document.
  68. However, having in mind all this, if you want to enable folding of code blocks
  69. inside preprocessor blocks, you can disable folding of preprocessor blocks by
  70. changing "fold.preprocessor" property, in which case everything inside them
  71. would be folded.
  72. ~ KEYWORDS:
  73. The list of keywords that can be used in pascal.properties file (up to Delphi
  74. XE4):
  75. - Keywords: absolute abstract and array as asm assembler automated begin case
  76. cdecl class const constructor delayed deprecated destructor dispid dispinterface
  77. div do downto dynamic else end except experimental export exports external far
  78. file final finalization finally for forward function goto helper if
  79. implementation in inherited initialization inline interface is label library
  80. message mod near nil not object of on operator or out overload override packed
  81. pascal platform private procedure program property protected public published
  82. raise record reference register reintroduce repeat resourcestring safecall
  83. sealed set shl shr static stdcall strict string then threadvar to try type unit
  84. unsafe until uses var varargs virtual while winapi with xor
  85. - Keywords related to the "smart highlithing" feature: add default implements
  86. index name nodefault read readonly remove stored write writeonly
  87. - Keywords related to Delphi packages (in addition to all above): package
  88. contains requires
  89. */
  90. #include <stdlib.h>
  91. #include <string.h>
  92. #include <stdio.h>
  93. #include <stdarg.h>
  94. #include <assert.h>
  95. #include <ctype.h>
  96. #include "ILexer.h"
  97. #include "Scintilla.h"
  98. #include "SciLexer.h"
  99. #include "WordList.h"
  100. #include "LexAccessor.h"
  101. #include "Accessor.h"
  102. #include "StyleContext.h"
  103. #include "CharacterSet.h"
  104. #include "LexerModule.h"
  105. #ifdef SCI_NAMESPACE
  106. using namespace Scintilla;
  107. #endif
  108. static void GetRangeLowered(Sci_PositionU start,
  109. Sci_PositionU end,
  110. Accessor &styler,
  111. char *s,
  112. Sci_PositionU len) {
  113. Sci_PositionU i = 0;
  114. while ((i < end - start + 1) && (i < len-1)) {
  115. s[i] = static_cast<char>(tolower(styler[start + i]));
  116. i++;
  117. }
  118. s[i] = '\0';
  119. }
  120. static void GetForwardRangeLowered(Sci_PositionU start,
  121. CharacterSet &charSet,
  122. Accessor &styler,
  123. char *s,
  124. Sci_PositionU len) {
  125. Sci_PositionU i = 0;
  126. while ((i < len-1) && charSet.Contains(styler.SafeGetCharAt(start + i))) {
  127. s[i] = static_cast<char>(tolower(styler.SafeGetCharAt(start + i)));
  128. i++;
  129. }
  130. s[i] = '\0';
  131. }
  132. enum {
  133. stateInAsm = 0x1000,
  134. stateInProperty = 0x2000,
  135. stateInExport = 0x4000,
  136. stateFoldInPreprocessor = 0x0100,
  137. stateFoldInRecord = 0x0200,
  138. stateFoldInPreprocessorLevelMask = 0x00FF,
  139. stateFoldMaskAll = 0x0FFF
  140. };
  141. static void ClassifyPascalWord(WordList *keywordlists[], StyleContext &sc, int &curLineState, bool bSmartHighlighting) {
  142. WordList& keywords = *keywordlists[0];
  143. char s[100];
  144. sc.GetCurrentLowered(s, sizeof(s));
  145. if (keywords.InList(s)) {
  146. if (curLineState & stateInAsm) {
  147. if (strcmp(s, "end") == 0 && sc.GetRelative(-4) != '@') {
  148. curLineState &= ~stateInAsm;
  149. sc.ChangeState(SCE_PAS_WORD);
  150. } else {
  151. sc.ChangeState(SCE_PAS_ASM);
  152. }
  153. } else {
  154. bool ignoreKeyword = false;
  155. if (strcmp(s, "asm") == 0) {
  156. curLineState |= stateInAsm;
  157. } else if (bSmartHighlighting) {
  158. if (strcmp(s, "property") == 0) {
  159. curLineState |= stateInProperty;
  160. } else if (strcmp(s, "exports") == 0) {
  161. curLineState |= stateInExport;
  162. } else if (!(curLineState & (stateInProperty | stateInExport)) && strcmp(s, "index") == 0) {
  163. ignoreKeyword = true;
  164. } else if (!(curLineState & stateInExport) && strcmp(s, "name") == 0) {
  165. ignoreKeyword = true;
  166. } else if (!(curLineState & stateInProperty) &&
  167. (strcmp(s, "read") == 0 || strcmp(s, "write") == 0 ||
  168. strcmp(s, "default") == 0 || strcmp(s, "nodefault") == 0 ||
  169. strcmp(s, "stored") == 0 || strcmp(s, "implements") == 0 ||
  170. strcmp(s, "readonly") == 0 || strcmp(s, "writeonly") == 0 ||
  171. strcmp(s, "add") == 0 || strcmp(s, "remove") == 0)) {
  172. ignoreKeyword = true;
  173. }
  174. }
  175. if (!ignoreKeyword) {
  176. sc.ChangeState(SCE_PAS_WORD);
  177. }
  178. }
  179. } else if (curLineState & stateInAsm) {
  180. sc.ChangeState(SCE_PAS_ASM);
  181. }
  182. sc.SetState(SCE_PAS_DEFAULT);
  183. }
  184. static void ColourisePascalDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
  185. Accessor &styler) {
  186. bool bSmartHighlighting = styler.GetPropertyInt("lexer.pascal.smart.highlighting", 1) != 0;
  187. CharacterSet setWordStart(CharacterSet::setAlpha, "_", 0x80, true);
  188. CharacterSet setWord(CharacterSet::setAlphaNum, "_", 0x80, true);
  189. CharacterSet setNumber(CharacterSet::setDigits, ".-+eE");
  190. CharacterSet setHexNumber(CharacterSet::setDigits, "abcdefABCDEF");
  191. CharacterSet setOperator(CharacterSet::setNone, "#$&'()*+,-./:;<=>@[]^{}");
  192. Sci_Position curLine = styler.GetLine(startPos);
  193. int curLineState = curLine > 0 ? styler.GetLineState(curLine - 1) : 0;
  194. StyleContext sc(startPos, length, initStyle, styler);
  195. for (; sc.More(); sc.Forward()) {
  196. if (sc.atLineEnd) {
  197. // Update the line state, so it can be seen by next line
  198. curLine = styler.GetLine(sc.currentPos);
  199. styler.SetLineState(curLine, curLineState);
  200. }
  201. // Determine if the current state should terminate.
  202. switch (sc.state) {
  203. case SCE_PAS_NUMBER:
  204. if (!setNumber.Contains(sc.ch) || (sc.ch == '.' && sc.chNext == '.')) {
  205. sc.SetState(SCE_PAS_DEFAULT);
  206. } else if (sc.ch == '-' || sc.ch == '+') {
  207. if (sc.chPrev != 'E' && sc.chPrev != 'e') {
  208. sc.SetState(SCE_PAS_DEFAULT);
  209. }
  210. }
  211. break;
  212. case SCE_PAS_IDENTIFIER:
  213. if (!setWord.Contains(sc.ch)) {
  214. ClassifyPascalWord(keywordlists, sc, curLineState, bSmartHighlighting);
  215. }
  216. break;
  217. case SCE_PAS_HEXNUMBER:
  218. if (!setHexNumber.Contains(sc.ch)) {
  219. sc.SetState(SCE_PAS_DEFAULT);
  220. }
  221. break;
  222. case SCE_PAS_COMMENT:
  223. case SCE_PAS_PREPROCESSOR:
  224. if (sc.ch == '}') {
  225. sc.ForwardSetState(SCE_PAS_DEFAULT);
  226. }
  227. break;
  228. case SCE_PAS_COMMENT2:
  229. case SCE_PAS_PREPROCESSOR2:
  230. if (sc.Match('*', ')')) {
  231. sc.Forward();
  232. sc.ForwardSetState(SCE_PAS_DEFAULT);
  233. }
  234. break;
  235. case SCE_PAS_COMMENTLINE:
  236. if (sc.atLineStart) {
  237. sc.SetState(SCE_PAS_DEFAULT);
  238. }
  239. break;
  240. case SCE_PAS_STRING:
  241. if (sc.atLineEnd) {
  242. sc.ChangeState(SCE_PAS_STRINGEOL);
  243. } else if (sc.ch == '\'' && sc.chNext == '\'') {
  244. sc.Forward();
  245. } else if (sc.ch == '\'') {
  246. sc.ForwardSetState(SCE_PAS_DEFAULT);
  247. }
  248. break;
  249. case SCE_PAS_STRINGEOL:
  250. if (sc.atLineStart) {
  251. sc.SetState(SCE_PAS_DEFAULT);
  252. }
  253. break;
  254. case SCE_PAS_CHARACTER:
  255. if (!setHexNumber.Contains(sc.ch) && sc.ch != '$') {
  256. sc.SetState(SCE_PAS_DEFAULT);
  257. }
  258. break;
  259. case SCE_PAS_OPERATOR:
  260. if (bSmartHighlighting && sc.chPrev == ';') {
  261. curLineState &= ~(stateInProperty | stateInExport);
  262. }
  263. sc.SetState(SCE_PAS_DEFAULT);
  264. break;
  265. case SCE_PAS_ASM:
  266. sc.SetState(SCE_PAS_DEFAULT);
  267. break;
  268. }
  269. // Determine if a new state should be entered.
  270. if (sc.state == SCE_PAS_DEFAULT) {
  271. if (IsADigit(sc.ch) && !(curLineState & stateInAsm)) {
  272. sc.SetState(SCE_PAS_NUMBER);
  273. } else if (setWordStart.Contains(sc.ch)) {
  274. sc.SetState(SCE_PAS_IDENTIFIER);
  275. } else if (sc.ch == '$' && !(curLineState & stateInAsm)) {
  276. sc.SetState(SCE_PAS_HEXNUMBER);
  277. } else if (sc.Match('{', '$')) {
  278. sc.SetState(SCE_PAS_PREPROCESSOR);
  279. } else if (sc.ch == '{') {
  280. sc.SetState(SCE_PAS_COMMENT);
  281. } else if (sc.Match("(*$")) {
  282. sc.SetState(SCE_PAS_PREPROCESSOR2);
  283. } else if (sc.Match('(', '*')) {
  284. sc.SetState(SCE_PAS_COMMENT2);
  285. sc.Forward(); // Eat the * so it isn't used for the end of the comment
  286. } else if (sc.Match('/', '/')) {
  287. sc.SetState(SCE_PAS_COMMENTLINE);
  288. } else if (sc.ch == '\'') {
  289. sc.SetState(SCE_PAS_STRING);
  290. } else if (sc.ch == '#') {
  291. sc.SetState(SCE_PAS_CHARACTER);
  292. } else if (setOperator.Contains(sc.ch) && !(curLineState & stateInAsm)) {
  293. sc.SetState(SCE_PAS_OPERATOR);
  294. } else if (curLineState & stateInAsm) {
  295. sc.SetState(SCE_PAS_ASM);
  296. }
  297. }
  298. }
  299. if (sc.state == SCE_PAS_IDENTIFIER && setWord.Contains(sc.chPrev)) {
  300. ClassifyPascalWord(keywordlists, sc, curLineState, bSmartHighlighting);
  301. }
  302. sc.Complete();
  303. }
  304. static bool IsStreamCommentStyle(int style) {
  305. return style == SCE_PAS_COMMENT || style == SCE_PAS_COMMENT2;
  306. }
  307. static bool IsCommentLine(Sci_Position line, Accessor &styler) {
  308. Sci_Position pos = styler.LineStart(line);
  309. Sci_Position eolPos = styler.LineStart(line + 1) - 1;
  310. for (Sci_Position i = pos; i < eolPos; i++) {
  311. char ch = styler[i];
  312. char chNext = styler.SafeGetCharAt(i + 1);
  313. int style = styler.StyleAt(i);
  314. if (ch == '/' && chNext == '/' && style == SCE_PAS_COMMENTLINE) {
  315. return true;
  316. } else if (!IsASpaceOrTab(ch)) {
  317. return false;
  318. }
  319. }
  320. return false;
  321. }
  322. static unsigned int GetFoldInPreprocessorLevelFlag(int lineFoldStateCurrent) {
  323. return lineFoldStateCurrent & stateFoldInPreprocessorLevelMask;
  324. }
  325. static void SetFoldInPreprocessorLevelFlag(int &lineFoldStateCurrent, unsigned int nestLevel) {
  326. lineFoldStateCurrent &= ~stateFoldInPreprocessorLevelMask;
  327. lineFoldStateCurrent |= nestLevel & stateFoldInPreprocessorLevelMask;
  328. }
  329. static void ClassifyPascalPreprocessorFoldPoint(int &levelCurrent, int &lineFoldStateCurrent,
  330. Sci_PositionU startPos, Accessor &styler) {
  331. CharacterSet setWord(CharacterSet::setAlpha);
  332. char s[11]; // Size of the longest possible keyword + one additional character + null
  333. GetForwardRangeLowered(startPos, setWord, styler, s, sizeof(s));
  334. unsigned int nestLevel = GetFoldInPreprocessorLevelFlag(lineFoldStateCurrent);
  335. if (strcmp(s, "if") == 0 ||
  336. strcmp(s, "ifdef") == 0 ||
  337. strcmp(s, "ifndef") == 0 ||
  338. strcmp(s, "ifopt") == 0 ||
  339. strcmp(s, "region") == 0) {
  340. nestLevel++;
  341. SetFoldInPreprocessorLevelFlag(lineFoldStateCurrent, nestLevel);
  342. lineFoldStateCurrent |= stateFoldInPreprocessor;
  343. levelCurrent++;
  344. } else if (strcmp(s, "endif") == 0 ||
  345. strcmp(s, "ifend") == 0 ||
  346. strcmp(s, "endregion") == 0) {
  347. nestLevel--;
  348. SetFoldInPreprocessorLevelFlag(lineFoldStateCurrent, nestLevel);
  349. if (nestLevel == 0) {
  350. lineFoldStateCurrent &= ~stateFoldInPreprocessor;
  351. }
  352. levelCurrent--;
  353. if (levelCurrent < SC_FOLDLEVELBASE) {
  354. levelCurrent = SC_FOLDLEVELBASE;
  355. }
  356. }
  357. }
  358. static Sci_PositionU SkipWhiteSpace(Sci_PositionU currentPos, Sci_PositionU endPos,
  359. Accessor &styler, bool includeChars = false) {
  360. CharacterSet setWord(CharacterSet::setAlphaNum, "_");
  361. Sci_PositionU j = currentPos + 1;
  362. char ch = styler.SafeGetCharAt(j);
  363. while ((j < endPos) && (IsASpaceOrTab(ch) || ch == '\r' || ch == '\n' ||
  364. IsStreamCommentStyle(styler.StyleAt(j)) || (includeChars && setWord.Contains(ch)))) {
  365. j++;
  366. ch = styler.SafeGetCharAt(j);
  367. }
  368. return j;
  369. }
  370. static void ClassifyPascalWordFoldPoint(int &levelCurrent, int &lineFoldStateCurrent,
  371. Sci_Position startPos, Sci_PositionU endPos,
  372. Sci_PositionU lastStart, Sci_PositionU currentPos, Accessor &styler) {
  373. char s[100];
  374. GetRangeLowered(lastStart, currentPos, styler, s, sizeof(s));
  375. if (strcmp(s, "record") == 0) {
  376. lineFoldStateCurrent |= stateFoldInRecord;
  377. levelCurrent++;
  378. } else if (strcmp(s, "begin") == 0 ||
  379. strcmp(s, "asm") == 0 ||
  380. strcmp(s, "try") == 0 ||
  381. (strcmp(s, "case") == 0 && !(lineFoldStateCurrent & stateFoldInRecord))) {
  382. levelCurrent++;
  383. } else if (strcmp(s, "class") == 0 || strcmp(s, "object") == 0) {
  384. // "class" & "object" keywords require special handling...
  385. bool ignoreKeyword = false;
  386. Sci_PositionU j = SkipWhiteSpace(currentPos, endPos, styler);
  387. if (j < endPos) {
  388. CharacterSet setWordStart(CharacterSet::setAlpha, "_");
  389. CharacterSet setWord(CharacterSet::setAlphaNum, "_");
  390. if (styler.SafeGetCharAt(j) == ';') {
  391. // Handle forward class declarations ("type TMyClass = class;")
  392. // and object method declarations ("TNotifyEvent = procedure(Sender: TObject) of object;")
  393. ignoreKeyword = true;
  394. } else if (strcmp(s, "class") == 0) {
  395. // "class" keyword has a few more special cases...
  396. if (styler.SafeGetCharAt(j) == '(') {
  397. // Handle simplified complete class declarations ("type TMyClass = class(TObject);")
  398. j = SkipWhiteSpace(j, endPos, styler, true);
  399. if (j < endPos && styler.SafeGetCharAt(j) == ')') {
  400. j = SkipWhiteSpace(j, endPos, styler);
  401. if (j < endPos && styler.SafeGetCharAt(j) == ';') {
  402. ignoreKeyword = true;
  403. }
  404. }
  405. } else if (setWordStart.Contains(styler.SafeGetCharAt(j))) {
  406. char s2[11]; // Size of the longest possible keyword + one additional character + null
  407. GetForwardRangeLowered(j, setWord, styler, s2, sizeof(s2));
  408. if (strcmp(s2, "procedure") == 0 ||
  409. strcmp(s2, "function") == 0 ||
  410. strcmp(s2, "of") == 0 ||
  411. strcmp(s2, "var") == 0 ||
  412. strcmp(s2, "property") == 0 ||
  413. strcmp(s2, "operator") == 0) {
  414. ignoreKeyword = true;
  415. }
  416. }
  417. }
  418. }
  419. if (!ignoreKeyword) {
  420. levelCurrent++;
  421. }
  422. } else if (strcmp(s, "interface") == 0) {
  423. // "interface" keyword requires special handling...
  424. bool ignoreKeyword = true;
  425. Sci_Position j = lastStart - 1;
  426. char ch = styler.SafeGetCharAt(j);
  427. while ((j >= startPos) && (IsASpaceOrTab(ch) || ch == '\r' || ch == '\n' ||
  428. IsStreamCommentStyle(styler.StyleAt(j)))) {
  429. j--;
  430. ch = styler.SafeGetCharAt(j);
  431. }
  432. if (j >= startPos && styler.SafeGetCharAt(j) == '=') {
  433. ignoreKeyword = false;
  434. }
  435. if (!ignoreKeyword) {
  436. Sci_PositionU k = SkipWhiteSpace(currentPos, endPos, styler);
  437. if (k < endPos && styler.SafeGetCharAt(k) == ';') {
  438. // Handle forward interface declarations ("type IMyInterface = interface;")
  439. ignoreKeyword = true;
  440. }
  441. }
  442. if (!ignoreKeyword) {
  443. levelCurrent++;
  444. }
  445. } else if (strcmp(s, "dispinterface") == 0) {
  446. // "dispinterface" keyword requires special handling...
  447. bool ignoreKeyword = false;
  448. Sci_PositionU j = SkipWhiteSpace(currentPos, endPos, styler);
  449. if (j < endPos && styler.SafeGetCharAt(j) == ';') {
  450. // Handle forward dispinterface declarations ("type IMyInterface = dispinterface;")
  451. ignoreKeyword = true;
  452. }
  453. if (!ignoreKeyword) {
  454. levelCurrent++;
  455. }
  456. } else if (strcmp(s, "end") == 0) {
  457. lineFoldStateCurrent &= ~stateFoldInRecord;
  458. levelCurrent--;
  459. if (levelCurrent < SC_FOLDLEVELBASE) {
  460. levelCurrent = SC_FOLDLEVELBASE;
  461. }
  462. }
  463. }
  464. static void FoldPascalDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *[],
  465. Accessor &styler) {
  466. bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
  467. bool foldPreprocessor = styler.GetPropertyInt("fold.preprocessor") != 0;
  468. bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
  469. Sci_PositionU endPos = startPos + length;
  470. int visibleChars = 0;
  471. Sci_Position lineCurrent = styler.GetLine(startPos);
  472. int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
  473. int levelCurrent = levelPrev;
  474. int lineFoldStateCurrent = lineCurrent > 0 ? styler.GetLineState(lineCurrent - 1) & stateFoldMaskAll : 0;
  475. char chNext = styler[startPos];
  476. int styleNext = styler.StyleAt(startPos);
  477. int style = initStyle;
  478. Sci_Position lastStart = 0;
  479. CharacterSet setWord(CharacterSet::setAlphaNum, "_", 0x80, true);
  480. for (Sci_PositionU i = startPos; i < endPos; i++) {
  481. char ch = chNext;
  482. chNext = styler.SafeGetCharAt(i + 1);
  483. int stylePrev = style;
  484. style = styleNext;
  485. styleNext = styler.StyleAt(i + 1);
  486. bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
  487. if (foldComment && IsStreamCommentStyle(style)) {
  488. if (!IsStreamCommentStyle(stylePrev)) {
  489. levelCurrent++;
  490. } else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
  491. // Comments don't end at end of line and the next character may be unstyled.
  492. levelCurrent--;
  493. }
  494. }
  495. if (foldComment && atEOL && IsCommentLine(lineCurrent, styler))
  496. {
  497. if (!IsCommentLine(lineCurrent - 1, styler)
  498. && IsCommentLine(lineCurrent + 1, styler))
  499. levelCurrent++;
  500. else if (IsCommentLine(lineCurrent - 1, styler)
  501. && !IsCommentLine(lineCurrent+1, styler))
  502. levelCurrent--;
  503. }
  504. if (foldPreprocessor) {
  505. if (style == SCE_PAS_PREPROCESSOR && ch == '{' && chNext == '$') {
  506. ClassifyPascalPreprocessorFoldPoint(levelCurrent, lineFoldStateCurrent, i + 2, styler);
  507. } else if (style == SCE_PAS_PREPROCESSOR2 && ch == '(' && chNext == '*'
  508. && styler.SafeGetCharAt(i + 2) == '$') {
  509. ClassifyPascalPreprocessorFoldPoint(levelCurrent, lineFoldStateCurrent, i + 3, styler);
  510. }
  511. }
  512. if (stylePrev != SCE_PAS_WORD && style == SCE_PAS_WORD)
  513. {
  514. // Store last word start point.
  515. lastStart = i;
  516. }
  517. if (stylePrev == SCE_PAS_WORD && !(lineFoldStateCurrent & stateFoldInPreprocessor)) {
  518. if(setWord.Contains(ch) && !setWord.Contains(chNext)) {
  519. ClassifyPascalWordFoldPoint(levelCurrent, lineFoldStateCurrent, startPos, endPos, lastStart, i, styler);
  520. }
  521. }
  522. if (!IsASpace(ch))
  523. visibleChars++;
  524. if (atEOL) {
  525. int lev = levelPrev;
  526. if (visibleChars == 0 && foldCompact)
  527. lev |= SC_FOLDLEVELWHITEFLAG;
  528. if ((levelCurrent > levelPrev) && (visibleChars > 0))
  529. lev |= SC_FOLDLEVELHEADERFLAG;
  530. if (lev != styler.LevelAt(lineCurrent)) {
  531. styler.SetLevel(lineCurrent, lev);
  532. }
  533. int newLineState = (styler.GetLineState(lineCurrent) & ~stateFoldMaskAll) | lineFoldStateCurrent;
  534. styler.SetLineState(lineCurrent, newLineState);
  535. lineCurrent++;
  536. levelPrev = levelCurrent;
  537. visibleChars = 0;
  538. }
  539. }
  540. // If we didn't reach the EOL in previous loop, store line level and whitespace information.
  541. // The rest will be filled in later...
  542. int lev = levelPrev;
  543. if (visibleChars == 0 && foldCompact)
  544. lev |= SC_FOLDLEVELWHITEFLAG;
  545. styler.SetLevel(lineCurrent, lev);
  546. }
  547. static const char * const pascalWordListDesc[] = {
  548. "Keywords",
  549. 0
  550. };
  551. LexerModule lmPascal(SCLEX_PASCAL, ColourisePascalDoc, "pascal", FoldPascalDoc, pascalWordListDesc);