LexR.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Scintilla source code edit control
  2. /** @file Lexr.cxx
  3. ** Lexer for R, S, SPlus Statistics Program (Heavily derived from CPP Lexer).
  4. **
  5. **/
  6. // Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
  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. #ifdef SCI_NAMESPACE
  24. using namespace Scintilla;
  25. #endif
  26. static inline bool IsAWordChar(const int ch) {
  27. return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
  28. }
  29. static inline bool IsAWordStart(const int ch) {
  30. return (ch < 0x80) && (isalnum(ch) || ch == '_');
  31. }
  32. static inline bool IsAnOperator(const int ch) {
  33. if (IsASCII(ch) && isalnum(ch))
  34. return false;
  35. // '.' left out as it is used to make up numbers
  36. if (ch == '-' || ch == '+' || ch == '!' || ch == '~' ||
  37. ch == '?' || ch == ':' || ch == '*' || ch == '/' ||
  38. ch == '^' || ch == '<' || ch == '>' || ch == '=' ||
  39. ch == '&' || ch == '|' || ch == '$' || ch == '(' ||
  40. ch == ')' || ch == '}' || ch == '{' || ch == '[' ||
  41. ch == ']')
  42. return true;
  43. return false;
  44. }
  45. static void ColouriseRDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
  46. Accessor &styler) {
  47. WordList &keywords = *keywordlists[0];
  48. WordList &keywords2 = *keywordlists[1];
  49. WordList &keywords3 = *keywordlists[2];
  50. // Do not leak onto next line
  51. if (initStyle == SCE_R_INFIXEOL)
  52. initStyle = SCE_R_DEFAULT;
  53. StyleContext sc(startPos, length, initStyle, styler);
  54. for (; sc.More(); sc.Forward()) {
  55. if (sc.atLineStart && (sc.state == SCE_R_STRING)) {
  56. // Prevent SCE_R_STRINGEOL from leaking back to previous line
  57. sc.SetState(SCE_R_STRING);
  58. }
  59. // Determine if the current state should terminate.
  60. if (sc.state == SCE_R_OPERATOR) {
  61. sc.SetState(SCE_R_DEFAULT);
  62. } else if (sc.state == SCE_R_NUMBER) {
  63. if (!IsADigit(sc.ch) && !(sc.ch == '.' && IsADigit(sc.chNext))) {
  64. sc.SetState(SCE_R_DEFAULT);
  65. }
  66. } else if (sc.state == SCE_R_IDENTIFIER) {
  67. if (!IsAWordChar(sc.ch)) {
  68. char s[100];
  69. sc.GetCurrent(s, sizeof(s));
  70. if (keywords.InList(s)) {
  71. sc.ChangeState(SCE_R_KWORD);
  72. } else if (keywords2.InList(s)) {
  73. sc.ChangeState(SCE_R_BASEKWORD);
  74. } else if (keywords3.InList(s)) {
  75. sc.ChangeState(SCE_R_OTHERKWORD);
  76. }
  77. sc.SetState(SCE_R_DEFAULT);
  78. }
  79. } else if (sc.state == SCE_R_COMMENT) {
  80. if (sc.ch == '\r' || sc.ch == '\n') {
  81. sc.SetState(SCE_R_DEFAULT);
  82. }
  83. } else if (sc.state == SCE_R_STRING) {
  84. if (sc.ch == '\\') {
  85. if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
  86. sc.Forward();
  87. }
  88. } else if (sc.ch == '\"') {
  89. sc.ForwardSetState(SCE_R_DEFAULT);
  90. }
  91. } else if (sc.state == SCE_R_INFIX) {
  92. if (sc.ch == '%') {
  93. sc.ForwardSetState(SCE_R_DEFAULT);
  94. } else if (sc.atLineEnd) {
  95. sc.ChangeState(SCE_R_INFIXEOL);
  96. sc.ForwardSetState(SCE_R_DEFAULT);
  97. }
  98. }else if (sc.state == SCE_R_STRING2) {
  99. if (sc.ch == '\\') {
  100. if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
  101. sc.Forward();
  102. }
  103. } else if (sc.ch == '\'') {
  104. sc.ForwardSetState(SCE_R_DEFAULT);
  105. }
  106. }
  107. // Determine if a new state should be entered.
  108. if (sc.state == SCE_R_DEFAULT) {
  109. if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
  110. sc.SetState(SCE_R_NUMBER);
  111. } else if (IsAWordStart(sc.ch) ) {
  112. sc.SetState(SCE_R_IDENTIFIER);
  113. } else if (sc.Match('#')) {
  114. sc.SetState(SCE_R_COMMENT);
  115. } else if (sc.ch == '\"') {
  116. sc.SetState(SCE_R_STRING);
  117. } else if (sc.ch == '%') {
  118. sc.SetState(SCE_R_INFIX);
  119. } else if (sc.ch == '\'') {
  120. sc.SetState(SCE_R_STRING2);
  121. } else if (IsAnOperator(sc.ch)) {
  122. sc.SetState(SCE_R_OPERATOR);
  123. }
  124. }
  125. }
  126. sc.Complete();
  127. }
  128. // Store both the current line's fold level and the next lines in the
  129. // level store to make it easy to pick up with each increment
  130. // and to make it possible to fiddle the current level for "} else {".
  131. static void FoldRDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[],
  132. Accessor &styler) {
  133. bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
  134. bool foldAtElse = styler.GetPropertyInt("fold.at.else", 0) != 0;
  135. Sci_PositionU endPos = startPos + length;
  136. int visibleChars = 0;
  137. Sci_Position lineCurrent = styler.GetLine(startPos);
  138. int levelCurrent = SC_FOLDLEVELBASE;
  139. if (lineCurrent > 0)
  140. levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
  141. int levelMinCurrent = levelCurrent;
  142. int levelNext = levelCurrent;
  143. char chNext = styler[startPos];
  144. int styleNext = styler.StyleAt(startPos);
  145. for (Sci_PositionU i = startPos; i < endPos; i++) {
  146. char ch = chNext;
  147. chNext = styler.SafeGetCharAt(i + 1);
  148. int style = styleNext;
  149. styleNext = styler.StyleAt(i + 1);
  150. bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
  151. if (style == SCE_R_OPERATOR) {
  152. if (ch == '{') {
  153. // Measure the minimum before a '{' to allow
  154. // folding on "} else {"
  155. if (levelMinCurrent > levelNext) {
  156. levelMinCurrent = levelNext;
  157. }
  158. levelNext++;
  159. } else if (ch == '}') {
  160. levelNext--;
  161. }
  162. }
  163. if (atEOL) {
  164. int levelUse = levelCurrent;
  165. if (foldAtElse) {
  166. levelUse = levelMinCurrent;
  167. }
  168. int lev = levelUse | levelNext << 16;
  169. if (visibleChars == 0 && foldCompact)
  170. lev |= SC_FOLDLEVELWHITEFLAG;
  171. if (levelUse < levelNext)
  172. lev |= SC_FOLDLEVELHEADERFLAG;
  173. if (lev != styler.LevelAt(lineCurrent)) {
  174. styler.SetLevel(lineCurrent, lev);
  175. }
  176. lineCurrent++;
  177. levelCurrent = levelNext;
  178. levelMinCurrent = levelCurrent;
  179. visibleChars = 0;
  180. }
  181. if (!isspacechar(ch))
  182. visibleChars++;
  183. }
  184. }
  185. static const char * const RWordLists[] = {
  186. "Language Keywords",
  187. "Base / Default package function",
  188. "Other Package Functions",
  189. "Unused",
  190. "Unused",
  191. 0,
  192. };
  193. LexerModule lmR(SCLEX_R, ColouriseRDoc, "r", FoldRDoc, RWordLists);