LexCsound.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Scintilla source code edit control
  2. /** @file LexCsound.cxx
  3. ** Lexer for Csound (Orchestra & Score)
  4. ** Written by Georg Ritter - <ritterfuture A T gmail D O T com>
  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. ch == '_' || ch == '?');
  29. }
  30. static inline bool IsAWordStart(const int ch) {
  31. return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '.' ||
  32. ch == '%' || ch == '@' || ch == '$' || ch == '?');
  33. }
  34. static inline bool IsCsoundOperator(char ch) {
  35. if (IsASCII(ch) && isalnum(ch))
  36. return false;
  37. // '.' left out as it is used to make up numbers
  38. if (ch == '*' || ch == '/' || ch == '-' || ch == '+' ||
  39. ch == '(' || ch == ')' || ch == '=' || ch == '^' ||
  40. ch == '[' || ch == ']' || ch == '<' || ch == '&' ||
  41. ch == '>' || ch == ',' || ch == '|' || ch == '~' ||
  42. ch == '%' || ch == ':')
  43. return true;
  44. return false;
  45. }
  46. static void ColouriseCsoundDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
  47. Accessor &styler) {
  48. WordList &opcode = *keywordlists[0];
  49. WordList &headerStmt = *keywordlists[1];
  50. WordList &otherKeyword = *keywordlists[2];
  51. // Do not leak onto next line
  52. if (initStyle == SCE_CSOUND_STRINGEOL)
  53. initStyle = SCE_CSOUND_DEFAULT;
  54. StyleContext sc(startPos, length, initStyle, styler);
  55. for (; sc.More(); sc.Forward())
  56. {
  57. // Handle line continuation generically.
  58. if (sc.ch == '\\') {
  59. if (sc.chNext == '\n' || sc.chNext == '\r') {
  60. sc.Forward();
  61. if (sc.ch == '\r' && sc.chNext == '\n') {
  62. sc.Forward();
  63. }
  64. continue;
  65. }
  66. }
  67. // Determine if the current state should terminate.
  68. if (sc.state == SCE_CSOUND_OPERATOR) {
  69. if (!IsCsoundOperator(static_cast<char>(sc.ch))) {
  70. sc.SetState(SCE_CSOUND_DEFAULT);
  71. }
  72. }else if (sc.state == SCE_CSOUND_NUMBER) {
  73. if (!IsAWordChar(sc.ch)) {
  74. sc.SetState(SCE_CSOUND_DEFAULT);
  75. }
  76. } else if (sc.state == SCE_CSOUND_IDENTIFIER) {
  77. if (!IsAWordChar(sc.ch) ) {
  78. char s[100];
  79. sc.GetCurrent(s, sizeof(s));
  80. if (opcode.InList(s)) {
  81. sc.ChangeState(SCE_CSOUND_OPCODE);
  82. } else if (headerStmt.InList(s)) {
  83. sc.ChangeState(SCE_CSOUND_HEADERSTMT);
  84. } else if (otherKeyword.InList(s)) {
  85. sc.ChangeState(SCE_CSOUND_USERKEYWORD);
  86. } else if (s[0] == 'p') {
  87. sc.ChangeState(SCE_CSOUND_PARAM);
  88. } else if (s[0] == 'a') {
  89. sc.ChangeState(SCE_CSOUND_ARATE_VAR);
  90. } else if (s[0] == 'k') {
  91. sc.ChangeState(SCE_CSOUND_KRATE_VAR);
  92. } else if (s[0] == 'i') { // covers both i-rate variables and i-statements
  93. sc.ChangeState(SCE_CSOUND_IRATE_VAR);
  94. } else if (s[0] == 'g') {
  95. sc.ChangeState(SCE_CSOUND_GLOBAL_VAR);
  96. }
  97. sc.SetState(SCE_CSOUND_DEFAULT);
  98. }
  99. }
  100. else if (sc.state == SCE_CSOUND_COMMENT ) {
  101. if (sc.atLineEnd) {
  102. sc.SetState(SCE_CSOUND_DEFAULT);
  103. }
  104. }
  105. else if ((sc.state == SCE_CSOUND_ARATE_VAR) ||
  106. (sc.state == SCE_CSOUND_KRATE_VAR) ||
  107. (sc.state == SCE_CSOUND_IRATE_VAR)) {
  108. if (!IsAWordChar(sc.ch)) {
  109. sc.SetState(SCE_CSOUND_DEFAULT);
  110. }
  111. }
  112. // Determine if a new state should be entered.
  113. if (sc.state == SCE_CSOUND_DEFAULT) {
  114. if (sc.ch == ';'){
  115. sc.SetState(SCE_CSOUND_COMMENT);
  116. } else if (isdigit(sc.ch) || (sc.ch == '.' && isdigit(sc.chNext))) {
  117. sc.SetState(SCE_CSOUND_NUMBER);
  118. } else if (IsAWordStart(sc.ch)) {
  119. sc.SetState(SCE_CSOUND_IDENTIFIER);
  120. } else if (IsCsoundOperator(static_cast<char>(sc.ch))) {
  121. sc.SetState(SCE_CSOUND_OPERATOR);
  122. } else if (sc.ch == 'p') {
  123. sc.SetState(SCE_CSOUND_PARAM);
  124. } else if (sc.ch == 'a') {
  125. sc.SetState(SCE_CSOUND_ARATE_VAR);
  126. } else if (sc.ch == 'k') {
  127. sc.SetState(SCE_CSOUND_KRATE_VAR);
  128. } else if (sc.ch == 'i') { // covers both i-rate variables and i-statements
  129. sc.SetState(SCE_CSOUND_IRATE_VAR);
  130. } else if (sc.ch == 'g') {
  131. sc.SetState(SCE_CSOUND_GLOBAL_VAR);
  132. }
  133. }
  134. }
  135. sc.Complete();
  136. }
  137. static void FoldCsoundInstruments(Sci_PositionU startPos, Sci_Position length, int /* initStyle */, WordList *[],
  138. Accessor &styler) {
  139. Sci_PositionU lengthDoc = startPos + length;
  140. int visibleChars = 0;
  141. Sci_Position lineCurrent = styler.GetLine(startPos);
  142. int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
  143. int levelCurrent = levelPrev;
  144. char chNext = styler[startPos];
  145. int stylePrev = 0;
  146. int styleNext = styler.StyleAt(startPos);
  147. for (Sci_PositionU i = startPos; i < lengthDoc; i++) {
  148. char ch = chNext;
  149. chNext = styler.SafeGetCharAt(i + 1);
  150. int style = styleNext;
  151. styleNext = styler.StyleAt(i + 1);
  152. bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
  153. if ((stylePrev != SCE_CSOUND_OPCODE) && (style == SCE_CSOUND_OPCODE)) {
  154. char s[20];
  155. unsigned int j = 0;
  156. while ((j < (sizeof(s) - 1)) && (iswordchar(styler[i + j]))) {
  157. s[j] = styler[i + j];
  158. j++;
  159. }
  160. s[j] = '\0';
  161. if (strcmp(s, "instr") == 0)
  162. levelCurrent++;
  163. if (strcmp(s, "endin") == 0)
  164. levelCurrent--;
  165. }
  166. if (atEOL) {
  167. int lev = levelPrev;
  168. if (visibleChars == 0)
  169. lev |= SC_FOLDLEVELWHITEFLAG;
  170. if ((levelCurrent > levelPrev) && (visibleChars > 0))
  171. lev |= SC_FOLDLEVELHEADERFLAG;
  172. if (lev != styler.LevelAt(lineCurrent)) {
  173. styler.SetLevel(lineCurrent, lev);
  174. }
  175. lineCurrent++;
  176. levelPrev = levelCurrent;
  177. visibleChars = 0;
  178. }
  179. if (!isspacechar(ch))
  180. visibleChars++;
  181. stylePrev = style;
  182. }
  183. // Fill in the real level of the next line, keeping the current flags as they will be filled in later
  184. int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
  185. styler.SetLevel(lineCurrent, levelPrev | flagsNext);
  186. }
  187. static const char * const csoundWordListDesc[] = {
  188. "Opcodes",
  189. "Header Statements",
  190. "User keywords",
  191. 0
  192. };
  193. LexerModule lmCsound(SCLEX_CSOUND, ColouriseCsoundDoc, "csound", FoldCsoundInstruments, csoundWordListDesc);