LexSML.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Scintilla source code edit control
  2. /** @file LexSML.cxx
  3. ** Lexer for SML.
  4. **/
  5. // Copyright 2009 by James Moffatt and Yuzhou Xin
  6. // Modified from LexCaml.cxx by Robert Roessler <robertr@rftp.com> Copyright 2005
  7. // The License.txt file describes the conditions under which this software may be distributed.
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <stdarg.h>
  12. #include <assert.h>
  13. #include <ctype.h>
  14. #include "ILexer.h"
  15. #include "Scintilla.h"
  16. #include "SciLexer.h"
  17. #include "WordList.h"
  18. #include "LexAccessor.h"
  19. #include "Accessor.h"
  20. #include "StyleContext.h"
  21. #include "CharacterSet.h"
  22. #include "LexerModule.h"
  23. inline int issml(int c) {return isalnum(c) || c == '_';}
  24. inline int issmlf(int c) {return isalpha(c) || c == '_';}
  25. inline int issmld(int c) {return isdigit(c) || c == '_';}
  26. #ifdef SCI_NAMESPACE
  27. using namespace Scintilla;
  28. #endif
  29. static void ColouriseSMLDoc(
  30. Sci_PositionU startPos, Sci_Position length,
  31. int initStyle,
  32. WordList *keywordlists[],
  33. Accessor &styler)
  34. {
  35. StyleContext sc(startPos, length, initStyle, styler);
  36. int nesting = 0;
  37. if (sc.state < SCE_SML_STRING)
  38. sc.state = SCE_SML_DEFAULT;
  39. if (sc.state >= SCE_SML_COMMENT)
  40. nesting = (sc.state & 0x0f) - SCE_SML_COMMENT;
  41. Sci_PositionU chToken = 0;
  42. int chBase = 0, chLit = 0;
  43. WordList& keywords = *keywordlists[0];
  44. WordList& keywords2 = *keywordlists[1];
  45. WordList& keywords3 = *keywordlists[2];
  46. const int useMagic = styler.GetPropertyInt("lexer.caml.magic", 0);
  47. while (sc.More()) {
  48. int state2 = -1;
  49. Sci_Position chColor = sc.currentPos - 1;
  50. bool advance = true;
  51. switch (sc.state & 0x0f) {
  52. case SCE_SML_DEFAULT:
  53. chToken = sc.currentPos;
  54. if (issmlf(sc.ch))
  55. state2 = SCE_SML_IDENTIFIER;
  56. else if (sc.Match('`') && issmlf(sc.chNext))
  57. state2 = SCE_SML_TAGNAME;
  58. else if (sc.Match('#')&&isdigit(sc.chNext))
  59. state2 = SCE_SML_LINENUM;
  60. else if (sc.Match('#','\"')){
  61. state2 = SCE_SML_CHAR,chLit = 0;
  62. sc.Forward();
  63. }
  64. else if (isdigit(sc.ch)) {
  65. state2 = SCE_SML_NUMBER, chBase = 10;
  66. if (sc.Match('0') && strchr("xX", sc.chNext))
  67. chBase = 16, sc.Forward();}
  68. else if (sc.Match('\"')&&sc.chPrev!='#')
  69. state2 = SCE_SML_STRING;
  70. else if (sc.Match('(', '*')){
  71. state2 = SCE_SML_COMMENT,
  72. sc.ch = ' ',
  73. sc.Forward();}
  74. else if (strchr("!~"
  75. "=<>@^+-*/"
  76. "()[];,:.#", sc.ch))
  77. state2 = SCE_SML_OPERATOR;
  78. break;
  79. case SCE_SML_IDENTIFIER:
  80. if (!(issml(sc.ch) || sc.Match('\''))) {
  81. const Sci_Position n = sc.currentPos - chToken;
  82. if (n < 24) {
  83. char t[24];
  84. for (Sci_Position i = -n; i < 0; i++)
  85. t[n + i] = static_cast<char>(sc.GetRelative(i));
  86. t[n] = '\0';
  87. if ((n == 1 && sc.chPrev == '_') || keywords.InList(t))
  88. sc.ChangeState(SCE_SML_KEYWORD);
  89. else if (keywords2.InList(t))
  90. sc.ChangeState(SCE_SML_KEYWORD2);
  91. else if (keywords3.InList(t))
  92. sc.ChangeState(SCE_SML_KEYWORD3);
  93. }
  94. state2 = SCE_SML_DEFAULT, advance = false;
  95. }
  96. break;
  97. case SCE_SML_TAGNAME:
  98. if (!(issml(sc.ch) || sc.Match('\'')))
  99. state2 = SCE_SML_DEFAULT, advance = false;
  100. break;
  101. case SCE_SML_LINENUM:
  102. if (!isdigit(sc.ch))
  103. state2 = SCE_SML_DEFAULT, advance = false;
  104. break;
  105. case SCE_SML_OPERATOR: {
  106. const char* o = 0;
  107. if (issml(sc.ch) || isspace(sc.ch)
  108. || (o = strchr(")]};,\'\"`#", sc.ch),o)
  109. || !strchr("!$%&*+-./:<=>?@^|~", sc.ch)) {
  110. if (o && strchr(")]};,", sc.ch)) {
  111. if ((sc.Match(')') && sc.chPrev == '(')
  112. || (sc.Match(']') && sc.chPrev == '['))
  113. sc.ChangeState(SCE_SML_KEYWORD);
  114. chColor++;
  115. } else
  116. advance = false;
  117. state2 = SCE_SML_DEFAULT;
  118. }
  119. break;
  120. }
  121. case SCE_SML_NUMBER:
  122. if (issmld(sc.ch) || IsADigit(sc.ch, chBase))
  123. break;
  124. if ((sc.Match('l') || sc.Match('L') || sc.Match('n'))
  125. && (issmld(sc.chPrev) || IsADigit(sc.chPrev, chBase)))
  126. break;
  127. if (chBase == 10) {
  128. if (sc.Match('.') && issmld(sc.chPrev))
  129. break;
  130. if ((sc.Match('e') || sc.Match('E'))
  131. && (issmld(sc.chPrev) || sc.chPrev == '.'))
  132. break;
  133. if ((sc.Match('+') || sc.Match('-'))
  134. && (sc.chPrev == 'e' || sc.chPrev == 'E'))
  135. break;
  136. }
  137. state2 = SCE_SML_DEFAULT, advance = false;
  138. break;
  139. case SCE_SML_CHAR:
  140. if (sc.Match('\\')) {
  141. chLit = 1;
  142. if (sc.chPrev == '\\')
  143. sc.ch = ' ';
  144. } else if ((sc.Match('\"') && sc.chPrev != '\\') || sc.atLineEnd) {
  145. state2 = SCE_SML_DEFAULT;
  146. chLit = 1;
  147. if (sc.Match('\"'))
  148. chColor++;
  149. else
  150. sc.ChangeState(SCE_SML_IDENTIFIER);
  151. } else if (chLit < 1 && sc.currentPos - chToken >= 3)
  152. sc.ChangeState(SCE_SML_IDENTIFIER), advance = false;
  153. break;
  154. case SCE_SML_STRING:
  155. if (sc.Match('\\') && sc.chPrev == '\\')
  156. sc.ch = ' ';
  157. else if (sc.Match('\"') && sc.chPrev != '\\')
  158. state2 = SCE_SML_DEFAULT, chColor++;
  159. break;
  160. case SCE_SML_COMMENT:
  161. case SCE_SML_COMMENT1:
  162. case SCE_SML_COMMENT2:
  163. case SCE_SML_COMMENT3:
  164. if (sc.Match('(', '*'))
  165. state2 = sc.state + 1, chToken = sc.currentPos,
  166. sc.ch = ' ',
  167. sc.Forward(), nesting++;
  168. else if (sc.Match(')') && sc.chPrev == '*') {
  169. if (nesting)
  170. state2 = (sc.state & 0x0f) - 1, chToken = 0, nesting--;
  171. else
  172. state2 = SCE_SML_DEFAULT;
  173. chColor++;
  174. } else if (useMagic && sc.currentPos - chToken == 4
  175. && sc.Match('c') && sc.chPrev == 'r' && sc.GetRelative(-2) == '@')
  176. sc.state |= 0x10;
  177. break;
  178. }
  179. if (state2 >= 0)
  180. styler.ColourTo(chColor, sc.state), sc.ChangeState(state2);
  181. if (advance)
  182. sc.Forward();
  183. }
  184. sc.Complete();
  185. }
  186. static void FoldSMLDoc(
  187. Sci_PositionU, Sci_Position,
  188. int,
  189. WordList *[],
  190. Accessor &)
  191. {
  192. }
  193. static const char * const SMLWordListDesc[] = {
  194. "Keywords",
  195. "Keywords2",
  196. "Keywords3",
  197. 0
  198. };
  199. LexerModule lmSML(SCLEX_SML, ColouriseSMLDoc, "SML", FoldSMLDoc, SMLWordListDesc);