LexAPDL.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Scintilla source code edit control
  2. /** @file LexAPDL.cxx
  3. ** Lexer for APDL. Based on the lexer for Assembler by The Black Horus.
  4. ** By Hadar Raz.
  5. **/
  6. // Copyright 1998-2003 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 == '_'));
  28. }
  29. static inline bool IsAnOperator(char ch) {
  30. // '.' left out as it is used to make up numbers
  31. if (ch == '*' || ch == '/' || ch == '-' || ch == '+' ||
  32. ch == '(' || ch == ')' || ch == '=' || ch == '^' ||
  33. ch == '[' || ch == ']' || ch == '<' || ch == '&' ||
  34. ch == '>' || ch == ',' || ch == '|' || ch == '~' ||
  35. ch == '$' || ch == ':' || ch == '%')
  36. return true;
  37. return false;
  38. }
  39. static void ColouriseAPDLDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
  40. Accessor &styler) {
  41. int stringStart = ' ';
  42. WordList &processors = *keywordlists[0];
  43. WordList &commands = *keywordlists[1];
  44. WordList &slashcommands = *keywordlists[2];
  45. WordList &starcommands = *keywordlists[3];
  46. WordList &arguments = *keywordlists[4];
  47. WordList &functions = *keywordlists[5];
  48. // Do not leak onto next line
  49. initStyle = SCE_APDL_DEFAULT;
  50. StyleContext sc(startPos, length, initStyle, styler);
  51. for (; sc.More(); sc.Forward()) {
  52. // Determine if the current state should terminate.
  53. if (sc.state == SCE_APDL_NUMBER) {
  54. if (!(IsADigit(sc.ch) || sc.ch == '.' || (sc.ch == 'e' || sc.ch == 'E') ||
  55. ((sc.ch == '+' || sc.ch == '-') && (sc.chPrev == 'e' || sc.chPrev == 'E')))) {
  56. sc.SetState(SCE_APDL_DEFAULT);
  57. }
  58. } else if (sc.state == SCE_APDL_COMMENT) {
  59. if (sc.atLineEnd) {
  60. sc.SetState(SCE_APDL_DEFAULT);
  61. }
  62. } else if (sc.state == SCE_APDL_COMMENTBLOCK) {
  63. if (sc.atLineEnd) {
  64. if (sc.ch == '\r') {
  65. sc.Forward();
  66. }
  67. sc.ForwardSetState(SCE_APDL_DEFAULT);
  68. }
  69. } else if (sc.state == SCE_APDL_STRING) {
  70. if (sc.atLineEnd) {
  71. sc.SetState(SCE_APDL_DEFAULT);
  72. } else if ((sc.ch == '\'' && stringStart == '\'') || (sc.ch == '\"' && stringStart == '\"')) {
  73. sc.ForwardSetState(SCE_APDL_DEFAULT);
  74. }
  75. } else if (sc.state == SCE_APDL_WORD) {
  76. if (!IsAWordChar(sc.ch)) {
  77. char s[100];
  78. sc.GetCurrentLowered(s, sizeof(s));
  79. if (processors.InList(s)) {
  80. sc.ChangeState(SCE_APDL_PROCESSOR);
  81. } else if (slashcommands.InList(s)) {
  82. sc.ChangeState(SCE_APDL_SLASHCOMMAND);
  83. } else if (starcommands.InList(s)) {
  84. sc.ChangeState(SCE_APDL_STARCOMMAND);
  85. } else if (commands.InList(s)) {
  86. sc.ChangeState(SCE_APDL_COMMAND);
  87. } else if (arguments.InList(s)) {
  88. sc.ChangeState(SCE_APDL_ARGUMENT);
  89. } else if (functions.InList(s)) {
  90. sc.ChangeState(SCE_APDL_FUNCTION);
  91. }
  92. sc.SetState(SCE_APDL_DEFAULT);
  93. }
  94. } else if (sc.state == SCE_APDL_OPERATOR) {
  95. if (!IsAnOperator(static_cast<char>(sc.ch))) {
  96. sc.SetState(SCE_APDL_DEFAULT);
  97. }
  98. }
  99. // Determine if a new state should be entered.
  100. if (sc.state == SCE_APDL_DEFAULT) {
  101. if (sc.ch == '!' && sc.chNext == '!') {
  102. sc.SetState(SCE_APDL_COMMENTBLOCK);
  103. } else if (sc.ch == '!') {
  104. sc.SetState(SCE_APDL_COMMENT);
  105. } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
  106. sc.SetState(SCE_APDL_NUMBER);
  107. } else if (sc.ch == '\'' || sc.ch == '\"') {
  108. sc.SetState(SCE_APDL_STRING);
  109. stringStart = sc.ch;
  110. } else if (IsAWordChar(sc.ch) || ((sc.ch == '*' || sc.ch == '/') && !isgraph(sc.chPrev))) {
  111. sc.SetState(SCE_APDL_WORD);
  112. } else if (IsAnOperator(static_cast<char>(sc.ch))) {
  113. sc.SetState(SCE_APDL_OPERATOR);
  114. }
  115. }
  116. }
  117. sc.Complete();
  118. }
  119. //------------------------------------------------------------------------------
  120. // 06-27-07 Sergio Lucato
  121. // - Included code folding for Ansys APDL lexer
  122. // - Copyied from LexBasic.cxx and modified for APDL
  123. //------------------------------------------------------------------------------
  124. /* Bits:
  125. * 1 - whitespace
  126. * 2 - operator
  127. * 4 - identifier
  128. * 8 - decimal digit
  129. * 16 - hex digit
  130. * 32 - bin digit
  131. */
  132. static int character_classification[128] =
  133. {
  134. 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
  135. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  136. 1, 2, 0, 2, 2, 2, 2, 2, 2, 2, 6, 2, 2, 2, 10, 6,
  137. 60, 60, 28, 28, 28, 28, 28, 28, 28, 28, 2, 2, 2, 2, 2, 2,
  138. 2, 20, 20, 20, 20, 20, 20, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  139. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 4,
  140. 2, 20, 20, 20, 20, 20, 20, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  141. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 0
  142. };
  143. static bool IsSpace(int c) {
  144. return c < 128 && (character_classification[c] & 1);
  145. }
  146. static bool IsIdentifier(int c) {
  147. return c < 128 && (character_classification[c] & 4);
  148. }
  149. static int LowerCase(int c)
  150. {
  151. if (c >= 'A' && c <= 'Z')
  152. return 'a' + c - 'A';
  153. return c;
  154. }
  155. static int CheckAPDLFoldPoint(char const *token, int &level) {
  156. if (!strcmp(token, "*if") ||
  157. !strcmp(token, "*do") ||
  158. !strcmp(token, "*dowhile") ) {
  159. level |= SC_FOLDLEVELHEADERFLAG;
  160. return 1;
  161. }
  162. if (!strcmp(token, "*endif") ||
  163. !strcmp(token, "*enddo") ) {
  164. return -1;
  165. }
  166. return 0;
  167. }
  168. static void FoldAPDLDoc(Sci_PositionU startPos, Sci_Position length, int,
  169. WordList *[], Accessor &styler) {
  170. Sci_Position line = styler.GetLine(startPos);
  171. int level = styler.LevelAt(line);
  172. int go = 0, done = 0;
  173. Sci_Position endPos = startPos + length;
  174. char word[256];
  175. int wordlen = 0;
  176. Sci_Position i;
  177. bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
  178. // Scan for tokens at the start of the line (they may include
  179. // whitespace, for tokens like "End Function"
  180. for (i = startPos; i < endPos; i++) {
  181. int c = styler.SafeGetCharAt(i);
  182. if (!done && !go) {
  183. if (wordlen) { // are we scanning a token already?
  184. word[wordlen] = static_cast<char>(LowerCase(c));
  185. if (!IsIdentifier(c)) { // done with token
  186. word[wordlen] = '\0';
  187. go = CheckAPDLFoldPoint(word, level);
  188. if (!go) {
  189. // Treat any whitespace as single blank, for
  190. // things like "End Function".
  191. if (IsSpace(c) && IsIdentifier(word[wordlen - 1])) {
  192. word[wordlen] = ' ';
  193. if (wordlen < 255)
  194. wordlen++;
  195. }
  196. else // done with this line
  197. done = 1;
  198. }
  199. } else if (wordlen < 255) {
  200. wordlen++;
  201. }
  202. } else { // start scanning at first non-whitespace character
  203. if (!IsSpace(c)) {
  204. if (IsIdentifier(c)) {
  205. word[0] = static_cast<char>(LowerCase(c));
  206. wordlen = 1;
  207. } else // done with this line
  208. done = 1;
  209. }
  210. }
  211. }
  212. if (c == '\n') { // line end
  213. if (!done && wordlen == 0 && foldCompact) // line was only space
  214. level |= SC_FOLDLEVELWHITEFLAG;
  215. if (level != styler.LevelAt(line))
  216. styler.SetLevel(line, level);
  217. level += go;
  218. line++;
  219. // reset state
  220. wordlen = 0;
  221. level &= ~SC_FOLDLEVELHEADERFLAG;
  222. level &= ~SC_FOLDLEVELWHITEFLAG;
  223. go = 0;
  224. done = 0;
  225. }
  226. }
  227. }
  228. static const char * const apdlWordListDesc[] = {
  229. "processors",
  230. "commands",
  231. "slashommands",
  232. "starcommands",
  233. "arguments",
  234. "functions",
  235. 0
  236. };
  237. LexerModule lmAPDL(SCLEX_APDL, ColouriseAPDLDoc, "apdl", FoldAPDLDoc, apdlWordListDesc);