LexAVS.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Scintilla source code edit control
  2. /** @file LexAVS.cxx
  3. ** Lexer for AviSynth.
  4. **/
  5. // Copyright 2012 by Bruno Barbieri <brunorex@gmail.com>
  6. // Heavily based on LexPOV by Neil Hodgson
  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 IsAWordStart(int ch) {
  30. return isalpha(ch) || (ch != ' ' && ch != '\n' && ch != '(' && ch != '.' && ch != ',');
  31. }
  32. static inline bool IsANumberChar(int ch) {
  33. // Not exactly following number definition (several dots are seen as OK, etc.)
  34. // but probably enough in most cases.
  35. return (ch < 0x80) &&
  36. (isdigit(ch) || ch == '.' || ch == '-' || ch == '+');
  37. }
  38. static void ColouriseAvsDoc(
  39. Sci_PositionU startPos,
  40. Sci_Position length,
  41. int initStyle,
  42. WordList *keywordlists[],
  43. Accessor &styler) {
  44. WordList &keywords = *keywordlists[0];
  45. WordList &filters = *keywordlists[1];
  46. WordList &plugins = *keywordlists[2];
  47. WordList &functions = *keywordlists[3];
  48. WordList &clipProperties = *keywordlists[4];
  49. WordList &userDefined = *keywordlists[5];
  50. Sci_Position currentLine = styler.GetLine(startPos);
  51. // Initialize the block comment nesting level, if we are inside such a comment.
  52. int blockCommentLevel = 0;
  53. if (initStyle == SCE_AVS_COMMENTBLOCK || initStyle == SCE_AVS_COMMENTBLOCKN) {
  54. blockCommentLevel = styler.GetLineState(currentLine - 1);
  55. }
  56. // Do not leak onto next line
  57. if (initStyle == SCE_AVS_COMMENTLINE) {
  58. initStyle = SCE_AVS_DEFAULT;
  59. }
  60. StyleContext sc(startPos, length, initStyle, styler);
  61. for (; sc.More(); sc.Forward()) {
  62. if (sc.atLineEnd) {
  63. // Update the line state, so it can be seen by next line
  64. currentLine = styler.GetLine(sc.currentPos);
  65. if (sc.state == SCE_AVS_COMMENTBLOCK || sc.state == SCE_AVS_COMMENTBLOCKN) {
  66. // Inside a block comment, we set the line state
  67. styler.SetLineState(currentLine, blockCommentLevel);
  68. } else {
  69. // Reset the line state
  70. styler.SetLineState(currentLine, 0);
  71. }
  72. }
  73. // Determine if the current state should terminate.
  74. if (sc.state == SCE_AVS_OPERATOR) {
  75. sc.SetState(SCE_AVS_DEFAULT);
  76. } else if (sc.state == SCE_AVS_NUMBER) {
  77. // We stop the number definition on non-numerical non-dot non-sign char
  78. if (!IsANumberChar(sc.ch)) {
  79. sc.SetState(SCE_AVS_DEFAULT);
  80. }
  81. } else if (sc.state == SCE_AVS_IDENTIFIER) {
  82. if (!IsAWordChar(sc.ch)) {
  83. char s[100];
  84. sc.GetCurrentLowered(s, sizeof(s));
  85. if (keywords.InList(s)) {
  86. sc.ChangeState(SCE_AVS_KEYWORD);
  87. } else if (filters.InList(s)) {
  88. sc.ChangeState(SCE_AVS_FILTER);
  89. } else if (plugins.InList(s)) {
  90. sc.ChangeState(SCE_AVS_PLUGIN);
  91. } else if (functions.InList(s)) {
  92. sc.ChangeState(SCE_AVS_FUNCTION);
  93. } else if (clipProperties.InList(s)) {
  94. sc.ChangeState(SCE_AVS_CLIPPROP);
  95. } else if (userDefined.InList(s)) {
  96. sc.ChangeState(SCE_AVS_USERDFN);
  97. }
  98. sc.SetState(SCE_AVS_DEFAULT);
  99. }
  100. } else if (sc.state == SCE_AVS_COMMENTBLOCK) {
  101. if (sc.Match('/', '*')) {
  102. blockCommentLevel++;
  103. sc.Forward();
  104. } else if (sc.Match('*', '/') && blockCommentLevel > 0) {
  105. blockCommentLevel--;
  106. sc.Forward();
  107. if (blockCommentLevel == 0) {
  108. sc.ForwardSetState(SCE_AVS_DEFAULT);
  109. }
  110. }
  111. } else if (sc.state == SCE_AVS_COMMENTBLOCKN) {
  112. if (sc.Match('[', '*')) {
  113. blockCommentLevel++;
  114. sc.Forward();
  115. } else if (sc.Match('*', ']') && blockCommentLevel > 0) {
  116. blockCommentLevel--;
  117. sc.Forward();
  118. if (blockCommentLevel == 0) {
  119. sc.ForwardSetState(SCE_AVS_DEFAULT);
  120. }
  121. }
  122. } else if (sc.state == SCE_AVS_COMMENTLINE) {
  123. if (sc.atLineEnd) {
  124. sc.ForwardSetState(SCE_AVS_DEFAULT);
  125. }
  126. } else if (sc.state == SCE_AVS_STRING) {
  127. if (sc.ch == '\"') {
  128. sc.ForwardSetState(SCE_AVS_DEFAULT);
  129. }
  130. } else if (sc.state == SCE_AVS_TRIPLESTRING) {
  131. if (sc.Match("\"\"\"")) {
  132. sc.Forward();
  133. sc.Forward();
  134. sc.ForwardSetState(SCE_AVS_DEFAULT);
  135. }
  136. }
  137. // Determine if a new state should be entered.
  138. if (sc.state == SCE_AVS_DEFAULT) {
  139. if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
  140. sc.SetState(SCE_AVS_NUMBER);
  141. } else if (IsADigit(sc.ch) || (sc.ch == ',' && IsADigit(sc.chNext))) {
  142. sc.Forward();
  143. sc.SetState(SCE_AVS_NUMBER);
  144. } else if (sc.Match('/', '*')) {
  145. blockCommentLevel = 1;
  146. sc.SetState(SCE_AVS_COMMENTBLOCK);
  147. sc.Forward(); // Eat the * so it isn't used for the end of the comment
  148. } else if (sc.Match('[', '*')) {
  149. blockCommentLevel = 1;
  150. sc.SetState(SCE_AVS_COMMENTBLOCKN);
  151. sc.Forward(); // Eat the * so it isn't used for the end of the comment
  152. } else if (sc.ch == '#') {
  153. sc.SetState(SCE_AVS_COMMENTLINE);
  154. } else if (sc.ch == '\"') {
  155. if (sc.Match("\"\"\"")) {
  156. sc.SetState(SCE_AVS_TRIPLESTRING);
  157. } else {
  158. sc.SetState(SCE_AVS_STRING);
  159. }
  160. } else if (isoperator(static_cast<char>(sc.ch))) {
  161. sc.SetState(SCE_AVS_OPERATOR);
  162. } else if (IsAWordStart(sc.ch)) {
  163. sc.SetState(SCE_AVS_IDENTIFIER);
  164. }
  165. }
  166. }
  167. // End of file: complete any pending changeState
  168. if (sc.state == SCE_AVS_IDENTIFIER) {
  169. if (!IsAWordChar(sc.ch)) {
  170. char s[100];
  171. sc.GetCurrentLowered(s, sizeof(s));
  172. if (keywords.InList(s)) {
  173. sc.ChangeState(SCE_AVS_KEYWORD);
  174. } else if (filters.InList(s)) {
  175. sc.ChangeState(SCE_AVS_FILTER);
  176. } else if (plugins.InList(s)) {
  177. sc.ChangeState(SCE_AVS_PLUGIN);
  178. } else if (functions.InList(s)) {
  179. sc.ChangeState(SCE_AVS_FUNCTION);
  180. } else if (clipProperties.InList(s)) {
  181. sc.ChangeState(SCE_AVS_CLIPPROP);
  182. } else if (userDefined.InList(s)) {
  183. sc.ChangeState(SCE_AVS_USERDFN);
  184. }
  185. sc.SetState(SCE_AVS_DEFAULT);
  186. }
  187. }
  188. sc.Complete();
  189. }
  190. static void FoldAvsDoc(
  191. Sci_PositionU startPos,
  192. Sci_Position length,
  193. int initStyle,
  194. WordList *[],
  195. Accessor &styler) {
  196. bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
  197. bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
  198. Sci_PositionU endPos = startPos + length;
  199. int visibleChars = 0;
  200. Sci_Position lineCurrent = styler.GetLine(startPos);
  201. int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
  202. int levelCurrent = levelPrev;
  203. char chNext = styler[startPos];
  204. int styleNext = styler.StyleAt(startPos);
  205. int style = initStyle;
  206. for (Sci_PositionU i = startPos; i < endPos; i++) {
  207. char ch = chNext;
  208. chNext = styler.SafeGetCharAt(i + 1);
  209. int stylePrev = style;
  210. style = styleNext;
  211. styleNext = styler.StyleAt(i + 1);
  212. bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
  213. if (foldComment && style == SCE_AVS_COMMENTBLOCK) {
  214. if (stylePrev != SCE_AVS_COMMENTBLOCK) {
  215. levelCurrent++;
  216. } else if ((styleNext != SCE_AVS_COMMENTBLOCK) && !atEOL) {
  217. // Comments don't end at end of line and the next character may be unstyled.
  218. levelCurrent--;
  219. }
  220. }
  221. if (foldComment && style == SCE_AVS_COMMENTBLOCKN) {
  222. if (stylePrev != SCE_AVS_COMMENTBLOCKN) {
  223. levelCurrent++;
  224. } else if ((styleNext != SCE_AVS_COMMENTBLOCKN) && !atEOL) {
  225. // Comments don't end at end of line and the next character may be unstyled.
  226. levelCurrent--;
  227. }
  228. }
  229. if (style == SCE_AVS_OPERATOR) {
  230. if (ch == '{') {
  231. levelCurrent++;
  232. } else if (ch == '}') {
  233. levelCurrent--;
  234. }
  235. }
  236. if (atEOL) {
  237. int lev = levelPrev;
  238. if (visibleChars == 0 && foldCompact)
  239. lev |= SC_FOLDLEVELWHITEFLAG;
  240. if ((levelCurrent > levelPrev) && (visibleChars > 0))
  241. lev |= SC_FOLDLEVELHEADERFLAG;
  242. if (lev != styler.LevelAt(lineCurrent)) {
  243. styler.SetLevel(lineCurrent, lev);
  244. }
  245. lineCurrent++;
  246. levelPrev = levelCurrent;
  247. visibleChars = 0;
  248. }
  249. if (!isspacechar(ch))
  250. visibleChars++;
  251. }
  252. // Fill in the real level of the next line, keeping the current flags as they will be filled in later
  253. int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
  254. styler.SetLevel(lineCurrent, levelPrev | flagsNext);
  255. }
  256. static const char * const avsWordLists[] = {
  257. "Keywords",
  258. "Filters",
  259. "Plugins",
  260. "Functions",
  261. "Clip properties",
  262. "User defined functions",
  263. 0,
  264. };
  265. LexerModule lmAVS(SCLEX_AVS, ColouriseAvsDoc, "avs", FoldAvsDoc, avsWordLists);