LexEScript.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Scintilla source code edit control
  2. /** @file LexESCRIPT.cxx
  3. ** Lexer for ESCRIPT
  4. **/
  5. // Copyright 2003 by Patrizio Bekerle (patrizio@bekerle.com)
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <stdarg.h>
  10. #include <assert.h>
  11. #include <ctype.h>
  12. #include "ILexer.h"
  13. #include "Scintilla.h"
  14. #include "SciLexer.h"
  15. #include "WordList.h"
  16. #include "LexAccessor.h"
  17. #include "Accessor.h"
  18. #include "StyleContext.h"
  19. #include "CharacterSet.h"
  20. #include "LexerModule.h"
  21. #ifdef SCI_NAMESPACE
  22. using namespace Scintilla;
  23. #endif
  24. static inline bool IsAWordChar(const int ch) {
  25. return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
  26. }
  27. static inline bool IsAWordStart(const int ch) {
  28. return (ch < 0x80) && (isalnum(ch) || ch == '_');
  29. }
  30. static void ColouriseESCRIPTDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
  31. Accessor &styler) {
  32. WordList &keywords = *keywordlists[0];
  33. WordList &keywords2 = *keywordlists[1];
  34. WordList &keywords3 = *keywordlists[2];
  35. // Do not leak onto next line
  36. /*if (initStyle == SCE_ESCRIPT_STRINGEOL)
  37. initStyle = SCE_ESCRIPT_DEFAULT;*/
  38. StyleContext sc(startPos, length, initStyle, styler);
  39. bool caseSensitive = styler.GetPropertyInt("escript.case.sensitive", 0) != 0;
  40. for (; sc.More(); sc.Forward()) {
  41. /*if (sc.atLineStart && (sc.state == SCE_ESCRIPT_STRING)) {
  42. // Prevent SCE_ESCRIPT_STRINGEOL from leaking back to previous line
  43. sc.SetState(SCE_ESCRIPT_STRING);
  44. }*/
  45. // Handle line continuation generically.
  46. if (sc.ch == '\\') {
  47. if (sc.chNext == '\n' || sc.chNext == '\r') {
  48. sc.Forward();
  49. if (sc.ch == '\r' && sc.chNext == '\n') {
  50. sc.Forward();
  51. }
  52. continue;
  53. }
  54. }
  55. // Determine if the current state should terminate.
  56. if (sc.state == SCE_ESCRIPT_OPERATOR || sc.state == SCE_ESCRIPT_BRACE) {
  57. sc.SetState(SCE_ESCRIPT_DEFAULT);
  58. } else if (sc.state == SCE_ESCRIPT_NUMBER) {
  59. if (!IsADigit(sc.ch) || sc.ch != '.') {
  60. sc.SetState(SCE_ESCRIPT_DEFAULT);
  61. }
  62. } else if (sc.state == SCE_ESCRIPT_IDENTIFIER) {
  63. if (!IsAWordChar(sc.ch) || (sc.ch == '.')) {
  64. char s[100];
  65. if (caseSensitive) {
  66. sc.GetCurrent(s, sizeof(s));
  67. } else {
  68. sc.GetCurrentLowered(s, sizeof(s));
  69. }
  70. // sc.GetCurrentLowered(s, sizeof(s));
  71. if (keywords.InList(s)) {
  72. sc.ChangeState(SCE_ESCRIPT_WORD);
  73. } else if (keywords2.InList(s)) {
  74. sc.ChangeState(SCE_ESCRIPT_WORD2);
  75. } else if (keywords3.InList(s)) {
  76. sc.ChangeState(SCE_ESCRIPT_WORD3);
  77. // sc.state = SCE_ESCRIPT_IDENTIFIER;
  78. }
  79. sc.SetState(SCE_ESCRIPT_DEFAULT);
  80. }
  81. } else if (sc.state == SCE_ESCRIPT_COMMENT) {
  82. if (sc.Match('*', '/')) {
  83. sc.Forward();
  84. sc.ForwardSetState(SCE_ESCRIPT_DEFAULT);
  85. }
  86. } else if (sc.state == SCE_ESCRIPT_COMMENTDOC) {
  87. if (sc.Match('*', '/')) {
  88. sc.Forward();
  89. sc.ForwardSetState(SCE_ESCRIPT_DEFAULT);
  90. }
  91. } else if (sc.state == SCE_ESCRIPT_COMMENTLINE) {
  92. if (sc.atLineEnd) {
  93. sc.SetState(SCE_ESCRIPT_DEFAULT);
  94. }
  95. } else if (sc.state == SCE_ESCRIPT_STRING) {
  96. if (sc.ch == '\\') {
  97. if (sc.chNext == '\"' || sc.chNext == '\\') {
  98. sc.Forward();
  99. }
  100. } else if (sc.ch == '\"') {
  101. sc.ForwardSetState(SCE_ESCRIPT_DEFAULT);
  102. }
  103. }
  104. // Determine if a new state should be entered.
  105. if (sc.state == SCE_ESCRIPT_DEFAULT) {
  106. if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
  107. sc.SetState(SCE_ESCRIPT_NUMBER);
  108. } else if (IsAWordStart(sc.ch) || (sc.ch == '#')) {
  109. sc.SetState(SCE_ESCRIPT_IDENTIFIER);
  110. } else if (sc.Match('/', '*')) {
  111. sc.SetState(SCE_ESCRIPT_COMMENT);
  112. sc.Forward(); // Eat the * so it isn't used for the end of the comment
  113. } else if (sc.Match('/', '/')) {
  114. sc.SetState(SCE_ESCRIPT_COMMENTLINE);
  115. } else if (sc.ch == '\"') {
  116. sc.SetState(SCE_ESCRIPT_STRING);
  117. //} else if (isoperator(static_cast<char>(sc.ch))) {
  118. } else if (sc.ch == '+' || sc.ch == '-' || sc.ch == '*' || sc.ch == '/' || sc.ch == '=' || sc.ch == '<' || sc.ch == '>' || sc.ch == '&' || sc.ch == '|' || sc.ch == '!' || sc.ch == '?' || sc.ch == ':') {
  119. sc.SetState(SCE_ESCRIPT_OPERATOR);
  120. } else if (sc.ch == '{' || sc.ch == '}') {
  121. sc.SetState(SCE_ESCRIPT_BRACE);
  122. }
  123. }
  124. }
  125. sc.Complete();
  126. }
  127. static int classifyFoldPointESCRIPT(const char* s, const char* prevWord) {
  128. int lev = 0;
  129. if (strcmp(prevWord, "end") == 0) return lev;
  130. if ((strcmp(prevWord, "else") == 0 && strcmp(s, "if") == 0) || strcmp(s, "elseif") == 0)
  131. return -1;
  132. if (strcmp(s, "for") == 0 || strcmp(s, "foreach") == 0
  133. || strcmp(s, "program") == 0 || strcmp(s, "function") == 0
  134. || strcmp(s, "while") == 0 || strcmp(s, "case") == 0
  135. || strcmp(s, "if") == 0 ) {
  136. lev = 1;
  137. } else if ( strcmp(s, "endfor") == 0 || strcmp(s, "endforeach") == 0
  138. || strcmp(s, "endprogram") == 0 || strcmp(s, "endfunction") == 0
  139. || strcmp(s, "endwhile") == 0 || strcmp(s, "endcase") == 0
  140. || strcmp(s, "endif") == 0 ) {
  141. lev = -1;
  142. }
  143. return lev;
  144. }
  145. static bool IsStreamCommentStyle(int style) {
  146. return style == SCE_ESCRIPT_COMMENT ||
  147. style == SCE_ESCRIPT_COMMENTDOC ||
  148. style == SCE_ESCRIPT_COMMENTLINE;
  149. }
  150. static void FoldESCRIPTDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *[], Accessor &styler) {
  151. //~ bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
  152. // Do not know how to fold the comment at the moment.
  153. bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
  154. bool foldComment = true;
  155. Sci_PositionU endPos = startPos + length;
  156. int visibleChars = 0;
  157. Sci_Position lineCurrent = styler.GetLine(startPos);
  158. int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
  159. int levelCurrent = levelPrev;
  160. char chNext = styler[startPos];
  161. int styleNext = styler.StyleAt(startPos);
  162. int style = initStyle;
  163. Sci_Position lastStart = 0;
  164. char prevWord[32] = "";
  165. for (Sci_PositionU i = startPos; i < endPos; i++) {
  166. char ch = chNext;
  167. chNext = styler.SafeGetCharAt(i + 1);
  168. int stylePrev = style;
  169. style = styleNext;
  170. styleNext = styler.StyleAt(i + 1);
  171. bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
  172. if (foldComment && IsStreamCommentStyle(style)) {
  173. if (!IsStreamCommentStyle(stylePrev)) {
  174. levelCurrent++;
  175. } else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
  176. // Comments don't end at end of line and the next character may be unstyled.
  177. levelCurrent--;
  178. }
  179. }
  180. if (foldComment && (style == SCE_ESCRIPT_COMMENTLINE)) {
  181. if ((ch == '/') && (chNext == '/')) {
  182. char chNext2 = styler.SafeGetCharAt(i + 2);
  183. if (chNext2 == '{') {
  184. levelCurrent++;
  185. } else if (chNext2 == '}') {
  186. levelCurrent--;
  187. }
  188. }
  189. }
  190. if (stylePrev == SCE_ESCRIPT_DEFAULT && style == SCE_ESCRIPT_WORD3)
  191. {
  192. // Store last word start point.
  193. lastStart = i;
  194. }
  195. if (style == SCE_ESCRIPT_WORD3) {
  196. if(iswordchar(ch) && !iswordchar(chNext)) {
  197. char s[32];
  198. Sci_PositionU j;
  199. for(j = 0; ( j < 31 ) && ( j < i-lastStart+1 ); j++) {
  200. s[j] = static_cast<char>(tolower(styler[lastStart + j]));
  201. }
  202. s[j] = '\0';
  203. levelCurrent += classifyFoldPointESCRIPT(s, prevWord);
  204. strcpy(prevWord, s);
  205. }
  206. }
  207. if (atEOL) {
  208. int lev = levelPrev;
  209. if (visibleChars == 0 && foldCompact)
  210. lev |= SC_FOLDLEVELWHITEFLAG;
  211. if ((levelCurrent > levelPrev) && (visibleChars > 0))
  212. lev |= SC_FOLDLEVELHEADERFLAG;
  213. if (lev != styler.LevelAt(lineCurrent)) {
  214. styler.SetLevel(lineCurrent, lev);
  215. }
  216. lineCurrent++;
  217. levelPrev = levelCurrent;
  218. visibleChars = 0;
  219. strcpy(prevWord, "");
  220. }
  221. if (!isspacechar(ch))
  222. visibleChars++;
  223. }
  224. // Fill in the real level of the next line, keeping the current flags as they will be filled in later
  225. int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
  226. styler.SetLevel(lineCurrent, levelPrev | flagsNext);
  227. }
  228. static const char * const ESCRIPTWordLists[] = {
  229. "Primary keywords and identifiers",
  230. "Intrinsic functions",
  231. "Extended and user defined functions",
  232. 0,
  233. };
  234. LexerModule lmESCRIPT(SCLEX_ESCRIPT, ColouriseESCRIPTDoc, "escript", FoldESCRIPTDoc, ESCRIPTWordLists);