LexSpecman.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Scintilla source code edit control
  2. /** @file LexSpecman.cxx
  3. ** Lexer for Specman E language.
  4. ** Written by Avi Yegudin, based on C++ lexer by Neil Hodgson
  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 == '_' || ch == '\'');
  28. }
  29. static inline bool IsANumberChar(const int ch) {
  30. return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '\'');
  31. }
  32. static inline bool IsAWordStart(const int ch) {
  33. return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '`');
  34. }
  35. static void ColouriseSpecmanDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
  36. Accessor &styler, bool caseSensitive) {
  37. WordList &keywords = *keywordlists[0];
  38. WordList &keywords2 = *keywordlists[1];
  39. WordList &keywords3 = *keywordlists[2];
  40. WordList &keywords4 = *keywordlists[3];
  41. // Do not leak onto next line
  42. if (initStyle == SCE_SN_STRINGEOL)
  43. initStyle = SCE_SN_CODE;
  44. int visibleChars = 0;
  45. StyleContext sc(startPos, length, initStyle, styler);
  46. for (; sc.More(); sc.Forward()) {
  47. if (sc.atLineStart && (sc.state == SCE_SN_STRING)) {
  48. // Prevent SCE_SN_STRINGEOL from leaking back to previous line
  49. sc.SetState(SCE_SN_STRING);
  50. }
  51. // Handle line continuation generically.
  52. if (sc.ch == '\\') {
  53. if (sc.chNext == '\n' || sc.chNext == '\r') {
  54. sc.Forward();
  55. if (sc.ch == '\r' && sc.chNext == '\n') {
  56. sc.Forward();
  57. }
  58. continue;
  59. }
  60. }
  61. // Determine if the current state should terminate.
  62. if (sc.state == SCE_SN_OPERATOR) {
  63. sc.SetState(SCE_SN_CODE);
  64. } else if (sc.state == SCE_SN_NUMBER) {
  65. if (!IsANumberChar(sc.ch)) {
  66. sc.SetState(SCE_SN_CODE);
  67. }
  68. } else if (sc.state == SCE_SN_IDENTIFIER) {
  69. if (!IsAWordChar(sc.ch) || (sc.ch == '.')) {
  70. char s[100];
  71. if (caseSensitive) {
  72. sc.GetCurrent(s, sizeof(s));
  73. } else {
  74. sc.GetCurrentLowered(s, sizeof(s));
  75. }
  76. if (keywords.InList(s)) {
  77. sc.ChangeState(SCE_SN_WORD);
  78. } else if (keywords2.InList(s)) {
  79. sc.ChangeState(SCE_SN_WORD2);
  80. } else if (keywords3.InList(s)) {
  81. sc.ChangeState(SCE_SN_WORD3);
  82. } else if (keywords4.InList(s)) {
  83. sc.ChangeState(SCE_SN_USER);
  84. }
  85. sc.SetState(SCE_SN_CODE);
  86. }
  87. } else if (sc.state == SCE_SN_PREPROCESSOR) {
  88. if (IsASpace(sc.ch)) {
  89. sc.SetState(SCE_SN_CODE);
  90. }
  91. } else if (sc.state == SCE_SN_DEFAULT) {
  92. if (sc.Match('<', '\'')) {
  93. sc.Forward();
  94. sc.ForwardSetState(SCE_SN_CODE);
  95. }
  96. } else if (sc.state == SCE_SN_COMMENTLINE || sc.state == SCE_SN_COMMENTLINEBANG) {
  97. if (sc.atLineEnd) {
  98. sc.SetState(SCE_SN_CODE);
  99. visibleChars = 0;
  100. }
  101. } else if (sc.state == SCE_SN_STRING) {
  102. if (sc.ch == '\\') {
  103. if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
  104. sc.Forward();
  105. }
  106. } else if (sc.ch == '\"') {
  107. sc.ForwardSetState(SCE_SN_CODE);
  108. } else if (sc.atLineEnd) {
  109. sc.ChangeState(SCE_SN_STRINGEOL);
  110. sc.ForwardSetState(SCE_SN_CODE);
  111. visibleChars = 0;
  112. }
  113. } else if (sc.state == SCE_SN_SIGNAL) {
  114. if (sc.atLineEnd) {
  115. sc.ChangeState(SCE_SN_STRINGEOL);
  116. sc.ForwardSetState(SCE_SN_CODE);
  117. visibleChars = 0;
  118. } else if (sc.ch == '\\') {
  119. if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
  120. sc.Forward();
  121. }
  122. } else if (sc.ch == '\'') {
  123. sc.ForwardSetState(SCE_SN_CODE);
  124. }
  125. } else if (sc.state == SCE_SN_REGEXTAG) {
  126. if (!IsADigit(sc.ch)) {
  127. sc.SetState(SCE_SN_CODE);
  128. }
  129. }
  130. // Determine if a new state should be entered.
  131. if (sc.state == SCE_SN_CODE) {
  132. if (sc.ch == '$' && IsADigit(sc.chNext)) {
  133. sc.SetState(SCE_SN_REGEXTAG);
  134. sc.Forward();
  135. } else if (IsADigit(sc.ch)) {
  136. sc.SetState(SCE_SN_NUMBER);
  137. } else if (IsAWordStart(sc.ch)) {
  138. sc.SetState(SCE_SN_IDENTIFIER);
  139. } else if (sc.Match('\'', '>')) {
  140. sc.SetState(SCE_SN_DEFAULT);
  141. sc.Forward(); // Eat the * so it isn't used for the end of the comment
  142. } else if (sc.Match('/', '/')) {
  143. if (sc.Match("//!")) // Nice to have a different comment style
  144. sc.SetState(SCE_SN_COMMENTLINEBANG);
  145. else
  146. sc.SetState(SCE_SN_COMMENTLINE);
  147. } else if (sc.Match('-', '-')) {
  148. if (sc.Match("--!")) // Nice to have a different comment style
  149. sc.SetState(SCE_SN_COMMENTLINEBANG);
  150. else
  151. sc.SetState(SCE_SN_COMMENTLINE);
  152. } else if (sc.ch == '\"') {
  153. sc.SetState(SCE_SN_STRING);
  154. } else if (sc.ch == '\'') {
  155. sc.SetState(SCE_SN_SIGNAL);
  156. } else if (sc.ch == '#' && visibleChars == 0) {
  157. // Preprocessor commands are alone on their line
  158. sc.SetState(SCE_SN_PREPROCESSOR);
  159. // Skip whitespace between # and preprocessor word
  160. do {
  161. sc.Forward();
  162. } while ((sc.ch == ' ' || sc.ch == '\t') && sc.More());
  163. if (sc.atLineEnd) {
  164. sc.SetState(SCE_SN_CODE);
  165. }
  166. } else if (isoperator(static_cast<char>(sc.ch)) || sc.ch == '@') {
  167. sc.SetState(SCE_SN_OPERATOR);
  168. }
  169. }
  170. if (sc.atLineEnd) {
  171. // Reset states to begining of colourise so no surprises
  172. // if different sets of lines lexed.
  173. visibleChars = 0;
  174. }
  175. if (!IsASpace(sc.ch)) {
  176. visibleChars++;
  177. }
  178. }
  179. sc.Complete();
  180. }
  181. // Store both the current line's fold level and the next lines in the
  182. // level store to make it easy to pick up with each increment
  183. // and to make it possible to fiddle the current level for "} else {".
  184. static void FoldNoBoxSpecmanDoc(Sci_PositionU startPos, Sci_Position length, int,
  185. Accessor &styler) {
  186. bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
  187. bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
  188. bool foldAtElse = styler.GetPropertyInt("fold.at.else", 0) != 0;
  189. Sci_PositionU endPos = startPos + length;
  190. int visibleChars = 0;
  191. Sci_Position lineCurrent = styler.GetLine(startPos);
  192. int levelCurrent = SC_FOLDLEVELBASE;
  193. if (lineCurrent > 0)
  194. levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
  195. int levelMinCurrent = levelCurrent;
  196. int levelNext = levelCurrent;
  197. char chNext = styler[startPos];
  198. int styleNext = styler.StyleAt(startPos);
  199. int style;
  200. for (Sci_PositionU i = startPos; i < endPos; i++) {
  201. char ch = chNext;
  202. chNext = styler.SafeGetCharAt(i + 1);
  203. //int stylePrev = style;
  204. style = styleNext;
  205. styleNext = styler.StyleAt(i + 1);
  206. bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
  207. if (foldComment && (style == SCE_SN_COMMENTLINE)) {
  208. if (((ch == '/') && (chNext == '/')) ||
  209. ((ch == '-') && (chNext == '-'))) {
  210. char chNext2 = styler.SafeGetCharAt(i + 2);
  211. if (chNext2 == '{') {
  212. levelNext++;
  213. } else if (chNext2 == '}') {
  214. levelNext--;
  215. }
  216. }
  217. }
  218. if (style == SCE_SN_OPERATOR) {
  219. if (ch == '{') {
  220. // Measure the minimum before a '{' to allow
  221. // folding on "} else {"
  222. if (levelMinCurrent > levelNext) {
  223. levelMinCurrent = levelNext;
  224. }
  225. levelNext++;
  226. } else if (ch == '}') {
  227. levelNext--;
  228. }
  229. }
  230. if (atEOL) {
  231. int levelUse = levelCurrent;
  232. if (foldAtElse) {
  233. levelUse = levelMinCurrent;
  234. }
  235. int lev = levelUse | levelNext << 16;
  236. if (visibleChars == 0 && foldCompact)
  237. lev |= SC_FOLDLEVELWHITEFLAG;
  238. if (levelUse < levelNext)
  239. lev |= SC_FOLDLEVELHEADERFLAG;
  240. if (lev != styler.LevelAt(lineCurrent)) {
  241. styler.SetLevel(lineCurrent, lev);
  242. }
  243. lineCurrent++;
  244. levelCurrent = levelNext;
  245. levelMinCurrent = levelCurrent;
  246. visibleChars = 0;
  247. }
  248. if (!isspacechar(ch))
  249. visibleChars++;
  250. }
  251. }
  252. static void FoldSpecmanDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *[],
  253. Accessor &styler) {
  254. FoldNoBoxSpecmanDoc(startPos, length, initStyle, styler);
  255. }
  256. static const char * const specmanWordLists[] = {
  257. "Primary keywords and identifiers",
  258. "Secondary keywords and identifiers",
  259. "Sequence keywords and identifiers",
  260. "User defined keywords and identifiers",
  261. "Unused",
  262. 0,
  263. };
  264. static void ColouriseSpecmanDocSensitive(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
  265. Accessor &styler) {
  266. ColouriseSpecmanDoc(startPos, length, initStyle, keywordlists, styler, true);
  267. }
  268. LexerModule lmSpecman(SCLEX_SPECMAN, ColouriseSpecmanDocSensitive, "specman", FoldSpecmanDoc, specmanWordLists);