PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Tools/MaterialEditor/wxscintilla_1.69.2/src/scintilla/src/LexScriptol.cxx

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