LexRebol.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Scintilla source code edit control
  2. /** @file LexRebol.cxx
  3. ** Lexer for REBOL.
  4. ** Written by Pascal Hurni, inspired from LexLua by Paul Winwood & Marcos E. Wurzius & Philippe Lhoste
  5. **
  6. ** History:
  7. ** 2005-04-07 First release.
  8. ** 2005-04-10 Closing parens and brackets go now in default style
  9. ** String and comment nesting should be more safe
  10. **/
  11. // Copyright 2005 by Pascal Hurni <pascal_hurni@fastmail.fm>
  12. // The License.txt file describes the conditions under which this software may be distributed.
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <stdarg.h>
  17. #include <assert.h>
  18. #include <ctype.h>
  19. #include "ILexer.h"
  20. #include "Scintilla.h"
  21. #include "SciLexer.h"
  22. #include "WordList.h"
  23. #include "LexAccessor.h"
  24. #include "Accessor.h"
  25. #include "StyleContext.h"
  26. #include "CharacterSet.h"
  27. #include "LexerModule.h"
  28. #ifdef SCI_NAMESPACE
  29. using namespace Scintilla;
  30. #endif
  31. static inline bool IsAWordChar(const int ch) {
  32. return (isalnum(ch) || ch == '?' || ch == '!' || ch == '.' || ch == '\'' || ch == '+' || ch == '-' || ch == '*' || ch == '&' || ch == '|' || ch == '=' || ch == '_' || ch == '~');
  33. }
  34. static inline bool IsAWordStart(const int ch, const int ch2) {
  35. return ((ch == '+' || ch == '-' || ch == '.') && !isdigit(ch2)) ||
  36. (isalpha(ch) || ch == '?' || ch == '!' || ch == '\'' || ch == '*' || ch == '&' || ch == '|' || ch == '=' || ch == '_' || ch == '~');
  37. }
  38. static inline bool IsAnOperator(const int ch, const int ch2, const int ch3) {
  39. // One char operators
  40. if (IsASpaceOrTab(ch2)) {
  41. return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '<' || ch == '>' || ch == '=' || ch == '?';
  42. }
  43. // Two char operators
  44. if (IsASpaceOrTab(ch3)) {
  45. return (ch == '*' && ch2 == '*') ||
  46. (ch == '/' && ch2 == '/') ||
  47. (ch == '<' && (ch2 == '=' || ch2 == '>')) ||
  48. (ch == '>' && ch2 == '=') ||
  49. (ch == '=' && (ch2 == '=' || ch2 == '?')) ||
  50. (ch == '?' && ch2 == '?');
  51. }
  52. return false;
  53. }
  54. static inline bool IsBinaryStart(const int ch, const int ch2, const int ch3, const int ch4) {
  55. return (ch == '#' && ch2 == '{') ||
  56. (IsADigit(ch) && ch2 == '#' && ch3 == '{' ) ||
  57. (IsADigit(ch) && IsADigit(ch2) && ch3 == '#' && ch4 == '{' );
  58. }
  59. static void ColouriseRebolDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[], Accessor &styler) {
  60. WordList &keywords = *keywordlists[0];
  61. WordList &keywords2 = *keywordlists[1];
  62. WordList &keywords3 = *keywordlists[2];
  63. WordList &keywords4 = *keywordlists[3];
  64. WordList &keywords5 = *keywordlists[4];
  65. WordList &keywords6 = *keywordlists[5];
  66. WordList &keywords7 = *keywordlists[6];
  67. WordList &keywords8 = *keywordlists[7];
  68. Sci_Position currentLine = styler.GetLine(startPos);
  69. // Initialize the braced string {.. { ... } ..} nesting level, if we are inside such a string.
  70. int stringLevel = 0;
  71. if (initStyle == SCE_REBOL_BRACEDSTRING || initStyle == SCE_REBOL_COMMENTBLOCK) {
  72. stringLevel = styler.GetLineState(currentLine - 1);
  73. }
  74. bool blockComment = initStyle == SCE_REBOL_COMMENTBLOCK;
  75. int dotCount = 0;
  76. // Do not leak onto next line
  77. if (initStyle == SCE_REBOL_COMMENTLINE) {
  78. initStyle = SCE_REBOL_DEFAULT;
  79. }
  80. StyleContext sc(startPos, length, initStyle, styler);
  81. if (startPos == 0) {
  82. sc.SetState(SCE_REBOL_PREFACE);
  83. }
  84. for (; sc.More(); sc.Forward()) {
  85. //--- What to do at line end ?
  86. if (sc.atLineEnd) {
  87. // Can be either inside a {} string or simply at eol
  88. if (sc.state != SCE_REBOL_BRACEDSTRING && sc.state != SCE_REBOL_COMMENTBLOCK &&
  89. sc.state != SCE_REBOL_BINARY && sc.state != SCE_REBOL_PREFACE)
  90. sc.SetState(SCE_REBOL_DEFAULT);
  91. // Update the line state, so it can be seen by next line
  92. currentLine = styler.GetLine(sc.currentPos);
  93. switch (sc.state) {
  94. case SCE_REBOL_BRACEDSTRING:
  95. case SCE_REBOL_COMMENTBLOCK:
  96. // Inside a braced string, we set the line state
  97. styler.SetLineState(currentLine, stringLevel);
  98. break;
  99. default:
  100. // Reset the line state
  101. styler.SetLineState(currentLine, 0);
  102. break;
  103. }
  104. // continue with next char
  105. continue;
  106. }
  107. //--- What to do on white-space ?
  108. if (IsASpaceOrTab(sc.ch))
  109. {
  110. // Return to default if any of these states
  111. if (sc.state == SCE_REBOL_OPERATOR || sc.state == SCE_REBOL_CHARACTER ||
  112. sc.state == SCE_REBOL_NUMBER || sc.state == SCE_REBOL_PAIR ||
  113. sc.state == SCE_REBOL_TUPLE || sc.state == SCE_REBOL_FILE ||
  114. sc.state == SCE_REBOL_DATE || sc.state == SCE_REBOL_TIME ||
  115. sc.state == SCE_REBOL_MONEY || sc.state == SCE_REBOL_ISSUE ||
  116. sc.state == SCE_REBOL_URL || sc.state == SCE_REBOL_EMAIL) {
  117. sc.SetState(SCE_REBOL_DEFAULT);
  118. }
  119. }
  120. //--- Specialize state ?
  121. // URL, Email look like identifier
  122. if (sc.state == SCE_REBOL_IDENTIFIER)
  123. {
  124. if (sc.ch == ':' && !IsASpace(sc.chNext)) {
  125. sc.ChangeState(SCE_REBOL_URL);
  126. } else if (sc.ch == '@') {
  127. sc.ChangeState(SCE_REBOL_EMAIL);
  128. } else if (sc.ch == '$') {
  129. sc.ChangeState(SCE_REBOL_MONEY);
  130. }
  131. }
  132. // Words look like identifiers
  133. if (sc.state == SCE_REBOL_IDENTIFIER || (sc.state >= SCE_REBOL_WORD && sc.state <= SCE_REBOL_WORD8)) {
  134. // Keywords ?
  135. if (!IsAWordChar(sc.ch) || sc.Match('/')) {
  136. char s[100];
  137. sc.GetCurrentLowered(s, sizeof(s));
  138. blockComment = strcmp(s, "comment") == 0;
  139. if (keywords8.InList(s)) {
  140. sc.ChangeState(SCE_REBOL_WORD8);
  141. } else if (keywords7.InList(s)) {
  142. sc.ChangeState(SCE_REBOL_WORD7);
  143. } else if (keywords6.InList(s)) {
  144. sc.ChangeState(SCE_REBOL_WORD6);
  145. } else if (keywords5.InList(s)) {
  146. sc.ChangeState(SCE_REBOL_WORD5);
  147. } else if (keywords4.InList(s)) {
  148. sc.ChangeState(SCE_REBOL_WORD4);
  149. } else if (keywords3.InList(s)) {
  150. sc.ChangeState(SCE_REBOL_WORD3);
  151. } else if (keywords2.InList(s)) {
  152. sc.ChangeState(SCE_REBOL_WORD2);
  153. } else if (keywords.InList(s)) {
  154. sc.ChangeState(SCE_REBOL_WORD);
  155. }
  156. // Keep same style if there are refinements
  157. if (!sc.Match('/')) {
  158. sc.SetState(SCE_REBOL_DEFAULT);
  159. }
  160. }
  161. // special numbers
  162. } else if (sc.state == SCE_REBOL_NUMBER) {
  163. switch (sc.ch) {
  164. case 'x': sc.ChangeState(SCE_REBOL_PAIR);
  165. break;
  166. case ':': sc.ChangeState(SCE_REBOL_TIME);
  167. break;
  168. case '-':
  169. case '/': sc.ChangeState(SCE_REBOL_DATE);
  170. break;
  171. case '.': if (++dotCount >= 2) sc.ChangeState(SCE_REBOL_TUPLE);
  172. break;
  173. }
  174. }
  175. //--- Determine if the current state should terminate
  176. if (sc.state == SCE_REBOL_QUOTEDSTRING || sc.state == SCE_REBOL_CHARACTER) {
  177. if (sc.ch == '^' && sc.chNext == '\"') {
  178. sc.Forward();
  179. } else if (sc.ch == '\"') {
  180. sc.ForwardSetState(SCE_REBOL_DEFAULT);
  181. }
  182. } else if (sc.state == SCE_REBOL_BRACEDSTRING || sc.state == SCE_REBOL_COMMENTBLOCK) {
  183. if (sc.ch == '}') {
  184. if (--stringLevel == 0) {
  185. sc.ForwardSetState(SCE_REBOL_DEFAULT);
  186. }
  187. } else if (sc.ch == '{') {
  188. stringLevel++;
  189. }
  190. } else if (sc.state == SCE_REBOL_BINARY) {
  191. if (sc.ch == '}') {
  192. sc.ForwardSetState(SCE_REBOL_DEFAULT);
  193. }
  194. } else if (sc.state == SCE_REBOL_TAG) {
  195. if (sc.ch == '>') {
  196. sc.ForwardSetState(SCE_REBOL_DEFAULT);
  197. }
  198. } else if (sc.state == SCE_REBOL_PREFACE) {
  199. if (sc.MatchIgnoreCase("rebol"))
  200. {
  201. int i;
  202. for (i=5; IsASpaceOrTab(styler.SafeGetCharAt(sc.currentPos+i, 0)); i++);
  203. if (sc.GetRelative(i) == '[')
  204. sc.SetState(SCE_REBOL_DEFAULT);
  205. }
  206. }
  207. //--- Parens and bracket changes to default style when the current is a number
  208. if (sc.state == SCE_REBOL_NUMBER || sc.state == SCE_REBOL_PAIR || sc.state == SCE_REBOL_TUPLE ||
  209. sc.state == SCE_REBOL_MONEY || sc.state == SCE_REBOL_ISSUE || sc.state == SCE_REBOL_EMAIL ||
  210. sc.state == SCE_REBOL_URL || sc.state == SCE_REBOL_DATE || sc.state == SCE_REBOL_TIME) {
  211. if (sc.ch == '(' || sc.ch == '[' || sc.ch == ')' || sc.ch == ']') {
  212. sc.SetState(SCE_REBOL_DEFAULT);
  213. }
  214. }
  215. //--- Determine if a new state should be entered.
  216. if (sc.state == SCE_REBOL_DEFAULT) {
  217. if (IsAnOperator(sc.ch, sc.chNext, sc.GetRelative(2))) {
  218. sc.SetState(SCE_REBOL_OPERATOR);
  219. } else if (IsBinaryStart(sc.ch, sc.chNext, sc.GetRelative(2), sc.GetRelative(3))) {
  220. sc.SetState(SCE_REBOL_BINARY);
  221. } else if (IsAWordStart(sc.ch, sc.chNext)) {
  222. sc.SetState(SCE_REBOL_IDENTIFIER);
  223. } else if (IsADigit(sc.ch) || sc.ch == '+' || sc.ch == '-' || /*Decimal*/ sc.ch == '.' || sc.ch == ',') {
  224. dotCount = 0;
  225. sc.SetState(SCE_REBOL_NUMBER);
  226. } else if (sc.ch == '\"') {
  227. sc.SetState(SCE_REBOL_QUOTEDSTRING);
  228. } else if (sc.ch == '{') {
  229. sc.SetState(blockComment ? SCE_REBOL_COMMENTBLOCK : SCE_REBOL_BRACEDSTRING);
  230. ++stringLevel;
  231. } else if (sc.ch == ';') {
  232. sc.SetState(SCE_REBOL_COMMENTLINE);
  233. } else if (sc.ch == '$') {
  234. sc.SetState(SCE_REBOL_MONEY);
  235. } else if (sc.ch == '%') {
  236. sc.SetState(SCE_REBOL_FILE);
  237. } else if (sc.ch == '<') {
  238. sc.SetState(SCE_REBOL_TAG);
  239. } else if (sc.ch == '#' && sc.chNext == '"') {
  240. sc.SetState(SCE_REBOL_CHARACTER);
  241. sc.Forward();
  242. } else if (sc.ch == '#' && sc.chNext != '"' && sc.chNext != '{' ) {
  243. sc.SetState(SCE_REBOL_ISSUE);
  244. }
  245. }
  246. }
  247. sc.Complete();
  248. }
  249. static void FoldRebolDoc(Sci_PositionU startPos, Sci_Position length, int /* initStyle */, WordList *[],
  250. Accessor &styler) {
  251. Sci_PositionU lengthDoc = startPos + length;
  252. int visibleChars = 0;
  253. Sci_Position lineCurrent = styler.GetLine(startPos);
  254. int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
  255. int levelCurrent = levelPrev;
  256. char chNext = styler[startPos];
  257. int styleNext = styler.StyleAt(startPos);
  258. for (Sci_PositionU i = startPos; i < lengthDoc; i++) {
  259. char ch = chNext;
  260. chNext = styler.SafeGetCharAt(i + 1);
  261. int style = styleNext;
  262. styleNext = styler.StyleAt(i + 1);
  263. bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
  264. if (style == SCE_REBOL_DEFAULT) {
  265. if (ch == '[') {
  266. levelCurrent++;
  267. } else if (ch == ']') {
  268. levelCurrent--;
  269. }
  270. }
  271. if (atEOL) {
  272. int lev = levelPrev;
  273. if (visibleChars == 0)
  274. lev |= SC_FOLDLEVELWHITEFLAG;
  275. if ((levelCurrent > levelPrev) && (visibleChars > 0))
  276. lev |= SC_FOLDLEVELHEADERFLAG;
  277. if (lev != styler.LevelAt(lineCurrent)) {
  278. styler.SetLevel(lineCurrent, lev);
  279. }
  280. lineCurrent++;
  281. levelPrev = levelCurrent;
  282. visibleChars = 0;
  283. }
  284. if (!isspacechar(ch))
  285. visibleChars++;
  286. }
  287. // Fill in the real level of the next line, keeping the current flags as they will be filled in later
  288. int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
  289. styler.SetLevel(lineCurrent, levelPrev | flagsNext);
  290. }
  291. static const char * const rebolWordListDesc[] = {
  292. "Keywords",
  293. 0
  294. };
  295. LexerModule lmREBOL(SCLEX_REBOL, ColouriseRebolDoc, "rebol", FoldRebolDoc, rebolWordListDesc);