LexScriptol.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Scintilla source code edit control
  2. /** @file LexScriptol.cxx
  3. ** Lexer for Scriptol.
  4. **/
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdarg.h>
  9. #include <assert.h>
  10. #include <ctype.h>
  11. #include "ILexer.h"
  12. #include "Scintilla.h"
  13. #include "SciLexer.h"
  14. #include "WordList.h"
  15. #include "LexAccessor.h"
  16. #include "Accessor.h"
  17. #include "StyleContext.h"
  18. #include "CharacterSet.h"
  19. #include "LexerModule.h"
  20. #ifdef SCI_NAMESPACE
  21. using namespace Scintilla;
  22. #endif
  23. static void ClassifyWordSol(Sci_PositionU start, Sci_PositionU end, WordList &keywords, Accessor &styler, char *prevWord)
  24. {
  25. char s[100] = "";
  26. bool wordIsNumber = isdigit(styler[start]) != 0;
  27. for (Sci_PositionU i = 0; i < end - start + 1 && i < 30; i++)
  28. {
  29. s[i] = styler[start + i];
  30. s[i + 1] = '\0';
  31. }
  32. char chAttr = SCE_SCRIPTOL_IDENTIFIER;
  33. if (0 == strcmp(prevWord, "class")) chAttr = SCE_SCRIPTOL_CLASSNAME;
  34. else if (wordIsNumber) chAttr = SCE_SCRIPTOL_NUMBER;
  35. else if (keywords.InList(s)) chAttr = SCE_SCRIPTOL_KEYWORD;
  36. else for (Sci_PositionU i = 0; i < end - start + 1; i++) // test dotted idents
  37. {
  38. if (styler[start + i] == '.')
  39. {
  40. styler.ColourTo(start + i - 1, chAttr);
  41. styler.ColourTo(start + i, SCE_SCRIPTOL_OPERATOR);
  42. }
  43. }
  44. styler.ColourTo(end, chAttr);
  45. strcpy(prevWord, s);
  46. }
  47. static bool IsSolComment(Accessor &styler, Sci_Position pos, Sci_Position len)
  48. {
  49. if(len > 0)
  50. {
  51. char c = styler[pos];
  52. if(c == '`') return true;
  53. if(len > 1)
  54. {
  55. if(c == '/')
  56. {
  57. c = styler[pos + 1];
  58. if(c == '/') return true;
  59. if(c == '*') return true;
  60. }
  61. }
  62. }
  63. return false;
  64. }
  65. static bool IsSolStringStart(char ch)
  66. {
  67. if (ch == '\'' || ch == '"') return true;
  68. return false;
  69. }
  70. static bool IsSolWordStart(char ch)
  71. {
  72. return (iswordchar(ch) && !IsSolStringStart(ch));
  73. }
  74. static int GetSolStringState(Accessor &styler, Sci_Position i, Sci_Position *nextIndex)
  75. {
  76. char ch = styler.SafeGetCharAt(i);
  77. char chNext = styler.SafeGetCharAt(i + 1);
  78. if (ch != '\"' && ch != '\'')
  79. {
  80. *nextIndex = i + 1;
  81. return SCE_SCRIPTOL_DEFAULT;
  82. }
  83. // ch is either single or double quotes in string
  84. // code below seem non-sense but is here for future extensions
  85. if (ch == chNext && ch == styler.SafeGetCharAt(i + 2))
  86. {
  87. *nextIndex = i + 3;
  88. if(ch == '\"') return SCE_SCRIPTOL_TRIPLE;
  89. if(ch == '\'') return SCE_SCRIPTOL_TRIPLE;
  90. return SCE_SCRIPTOL_STRING;
  91. }
  92. else
  93. {
  94. *nextIndex = i + 1;
  95. if (ch == '"') return SCE_SCRIPTOL_STRING;
  96. else return SCE_SCRIPTOL_STRING;
  97. }
  98. }
  99. static void ColouriseSolDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
  100. WordList *keywordlists[], Accessor &styler)
  101. {
  102. Sci_Position lengthDoc = startPos + length;
  103. char stringType = '\"';
  104. if (startPos > 0)
  105. {
  106. Sci_Position lineCurrent = styler.GetLine(startPos);
  107. if (lineCurrent > 0)
  108. {
  109. startPos = styler.LineStart(lineCurrent-1);
  110. if (startPos == 0) initStyle = SCE_SCRIPTOL_DEFAULT;
  111. else initStyle = styler.StyleAt(startPos-1);
  112. }
  113. }
  114. styler.StartAt(startPos);
  115. WordList &keywords = *keywordlists[0];
  116. char prevWord[200];
  117. prevWord[0] = '\0';
  118. if (length == 0) return;
  119. int state = initStyle & 31;
  120. Sci_Position nextIndex = 0;
  121. char chPrev = ' ';
  122. char chPrev2 = ' ';
  123. char chNext = styler[startPos];
  124. styler.StartSegment(startPos);
  125. for (Sci_Position i = startPos; i < lengthDoc; i++)
  126. {
  127. char ch = chNext;
  128. chNext = styler.SafeGetCharAt(i + 1);
  129. if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == lengthDoc))
  130. {
  131. if ((state == SCE_SCRIPTOL_DEFAULT) ||
  132. (state == SCE_SCRIPTOL_TRIPLE) ||
  133. (state == SCE_SCRIPTOL_COMMENTBLOCK))
  134. {
  135. styler.ColourTo(i, state);
  136. }
  137. }
  138. if (styler.IsLeadByte(ch))
  139. {
  140. chNext = styler.SafeGetCharAt(i + 2);
  141. chPrev = ' ';
  142. chPrev2 = ' ';
  143. i += 1;
  144. continue;
  145. }
  146. if (state == SCE_SCRIPTOL_STRINGEOL)
  147. {
  148. if (ch != '\r' && ch != '\n')
  149. {
  150. styler.ColourTo(i - 1, state);
  151. state = SCE_SCRIPTOL_DEFAULT;
  152. }
  153. }
  154. if (state == SCE_SCRIPTOL_DEFAULT)
  155. {
  156. if (IsSolWordStart(ch))
  157. {
  158. styler.ColourTo(i - 1, state);
  159. state = SCE_SCRIPTOL_KEYWORD;
  160. }
  161. else if (ch == '`')
  162. {
  163. styler.ColourTo(i - 1, state);
  164. state = SCE_SCRIPTOL_COMMENTLINE;
  165. }
  166. else if (ch == '/')
  167. {
  168. styler.ColourTo(i - 1, state);
  169. if(chNext == '/') state = SCE_SCRIPTOL_CSTYLE;
  170. if(chNext == '*') state = SCE_SCRIPTOL_COMMENTBLOCK;
  171. }
  172. else if (IsSolStringStart(ch))
  173. {
  174. styler.ColourTo(i - 1, state);
  175. state = GetSolStringState(styler, i, &nextIndex);
  176. if(state == SCE_SCRIPTOL_STRING)
  177. {
  178. stringType = ch;
  179. }
  180. if (nextIndex != i + 1)
  181. {
  182. i = nextIndex - 1;
  183. ch = ' ';
  184. chPrev = ' ';
  185. chNext = styler.SafeGetCharAt(i + 1);
  186. }
  187. }
  188. else if (isoperator(ch))
  189. {
  190. styler.ColourTo(i - 1, state);
  191. styler.ColourTo(i, SCE_SCRIPTOL_OPERATOR);
  192. }
  193. }
  194. else if (state == SCE_SCRIPTOL_KEYWORD)
  195. {
  196. if (!iswordchar(ch))
  197. {
  198. ClassifyWordSol(styler.GetStartSegment(), i - 1, keywords, styler, prevWord);
  199. state = SCE_SCRIPTOL_DEFAULT;
  200. if (ch == '`')
  201. {
  202. state = chNext == '`' ? SCE_SCRIPTOL_PERSISTENT : SCE_SCRIPTOL_COMMENTLINE;
  203. }
  204. else if (IsSolStringStart(ch))
  205. {
  206. styler.ColourTo(i - 1, state);
  207. state = GetSolStringState(styler, i, &nextIndex);
  208. if (nextIndex != i + 1)
  209. {
  210. i = nextIndex - 1;
  211. ch = ' ';
  212. chPrev = ' ';
  213. chNext = styler.SafeGetCharAt(i + 1);
  214. }
  215. }
  216. else if (isoperator(ch))
  217. {
  218. styler.ColourTo(i, SCE_SCRIPTOL_OPERATOR);
  219. }
  220. }
  221. }
  222. else
  223. {
  224. if (state == SCE_SCRIPTOL_COMMENTLINE ||
  225. state == SCE_SCRIPTOL_PERSISTENT ||
  226. state == SCE_SCRIPTOL_CSTYLE)
  227. {
  228. if (ch == '\r' || ch == '\n')
  229. {
  230. styler.ColourTo(i - 1, state);
  231. state = SCE_SCRIPTOL_DEFAULT;
  232. }
  233. }
  234. else if(state == SCE_SCRIPTOL_COMMENTBLOCK)
  235. {
  236. if(chPrev == '*' && ch == '/')
  237. {
  238. styler.ColourTo(i, state);
  239. state = SCE_SCRIPTOL_DEFAULT;
  240. }
  241. }
  242. else if ((state == SCE_SCRIPTOL_STRING) ||
  243. (state == SCE_SCRIPTOL_CHARACTER))
  244. {
  245. if ((ch == '\r' || ch == '\n') && (chPrev != '\\'))
  246. {
  247. styler.ColourTo(i - 1, state);
  248. state = SCE_SCRIPTOL_STRINGEOL;
  249. }
  250. else if (ch == '\\')
  251. {
  252. if (chNext == '\"' || chNext == '\'' || chNext == '\\')
  253. {
  254. i++;
  255. ch = chNext;
  256. chNext = styler.SafeGetCharAt(i + 1);
  257. }
  258. }
  259. else if ((ch == '\"') || (ch == '\''))
  260. {
  261. // must match the entered quote type
  262. if(ch == stringType)
  263. {
  264. styler.ColourTo(i, state);
  265. state = SCE_SCRIPTOL_DEFAULT;
  266. }
  267. }
  268. }
  269. else if (state == SCE_SCRIPTOL_TRIPLE)
  270. {
  271. if ((ch == '\'' && chPrev == '\'' && chPrev2 == '\'') ||
  272. (ch == '\"' && chPrev == '\"' && chPrev2 == '\"'))
  273. {
  274. styler.ColourTo(i, state);
  275. state = SCE_SCRIPTOL_DEFAULT;
  276. }
  277. }
  278. }
  279. chPrev2 = chPrev;
  280. chPrev = ch;
  281. }
  282. if (state == SCE_SCRIPTOL_KEYWORD)
  283. {
  284. ClassifyWordSol(styler.GetStartSegment(),
  285. lengthDoc-1, keywords, styler, prevWord);
  286. }
  287. else
  288. {
  289. styler.ColourTo(lengthDoc-1, state);
  290. }
  291. }
  292. static void FoldSolDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
  293. WordList *[], Accessor &styler)
  294. {
  295. Sci_Position lengthDoc = startPos + length;
  296. Sci_Position lineCurrent = styler.GetLine(startPos);
  297. if (startPos > 0)
  298. {
  299. if (lineCurrent > 0)
  300. {
  301. lineCurrent--;
  302. startPos = styler.LineStart(lineCurrent);
  303. if (startPos == 0)
  304. initStyle = SCE_SCRIPTOL_DEFAULT;
  305. else
  306. initStyle = styler.StyleAt(startPos-1);
  307. }
  308. }
  309. int state = initStyle & 31;
  310. int spaceFlags = 0;
  311. int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsSolComment);
  312. if (state == SCE_SCRIPTOL_TRIPLE)
  313. indentCurrent |= SC_FOLDLEVELWHITEFLAG;
  314. char chNext = styler[startPos];
  315. for (Sci_Position i = startPos; i < lengthDoc; i++)
  316. {
  317. char ch = chNext;
  318. chNext = styler.SafeGetCharAt(i + 1);
  319. int style = styler.StyleAt(i) & 31;
  320. if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == lengthDoc))
  321. {
  322. int lev = indentCurrent;
  323. int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsSolComment);
  324. if (style == SCE_SCRIPTOL_TRIPLE)
  325. indentNext |= SC_FOLDLEVELWHITEFLAG;
  326. if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG))
  327. {
  328. // Only non whitespace lines can be headers
  329. if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK))
  330. {
  331. lev |= SC_FOLDLEVELHEADERFLAG;
  332. }
  333. else if (indentNext & SC_FOLDLEVELWHITEFLAG)
  334. {
  335. // Line after is blank so check the next - maybe should continue further?
  336. int spaceFlags2 = 0;
  337. int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2, IsSolComment);
  338. if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK))
  339. {
  340. lev |= SC_FOLDLEVELHEADERFLAG;
  341. }
  342. }
  343. }
  344. indentCurrent = indentNext;
  345. styler.SetLevel(lineCurrent, lev);
  346. lineCurrent++;
  347. }
  348. }
  349. }
  350. LexerModule lmScriptol(SCLEX_SCRIPTOL, ColouriseSolDoc, "scriptol", FoldSolDoc);