LexMetapost.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // Scintilla source code edit control
  2. // File: LexMetapost.cxx - general context conformant metapost coloring scheme
  3. // Author: Hans Hagen - PRAGMA ADE - Hasselt NL - www.pragma-ade.com
  4. // Version: September 28, 2003
  5. // Modified by instanton: July 10, 2007
  6. // Folding based on keywordlists[]
  7. // Copyright: 1998-2003 by Neil Hodgson <neilh@scintilla.org>
  8. // The License.txt file describes the conditions under which this software may be distributed.
  9. // This lexer is derived from the one written for the texwork environment (1999++) which in
  10. // turn is inspired on texedit (1991++) which finds its roots in wdt (1986).
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <stdarg.h>
  15. #include <assert.h>
  16. #include <ctype.h>
  17. #include "ILexer.h"
  18. #include "Scintilla.h"
  19. #include "SciLexer.h"
  20. #include "WordList.h"
  21. #include "LexAccessor.h"
  22. #include "Accessor.h"
  23. #include "StyleContext.h"
  24. #include "CharacterSet.h"
  25. #include "LexerModule.h"
  26. #ifdef SCI_NAMESPACE
  27. using namespace Scintilla;
  28. #endif
  29. // val SCE_METAPOST_DEFAULT = 0
  30. // val SCE_METAPOST_SPECIAL = 1
  31. // val SCE_METAPOST_GROUP = 2
  32. // val SCE_METAPOST_SYMBOL = 3
  33. // val SCE_METAPOST_COMMAND = 4
  34. // val SCE_METAPOST_TEXT = 5
  35. // Definitions in SciTEGlobal.properties:
  36. //
  37. // Metapost Highlighting
  38. //
  39. // # Default
  40. // style.metapost.0=fore:#7F7F00
  41. // # Special
  42. // style.metapost.1=fore:#007F7F
  43. // # Group
  44. // style.metapost.2=fore:#880000
  45. // # Symbol
  46. // style.metapost.3=fore:#7F7F00
  47. // # Command
  48. // style.metapost.4=fore:#008800
  49. // # Text
  50. // style.metapost.5=fore:#000000
  51. // lexer.tex.comment.process=0
  52. // Auxiliary functions:
  53. static inline bool endOfLine(Accessor &styler, Sci_PositionU i) {
  54. return
  55. (styler[i] == '\n') || ((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n')) ;
  56. }
  57. static inline bool isMETAPOSTcomment(int ch) {
  58. return
  59. (ch == '%') ;
  60. }
  61. static inline bool isMETAPOSTone(int ch) {
  62. return
  63. (ch == '[') || (ch == ']') || (ch == '(') || (ch == ')') ||
  64. (ch == ':') || (ch == '=') || (ch == '<') || (ch == '>') ||
  65. (ch == '{') || (ch == '}') || (ch == '\'') || (ch == '\"') ;
  66. }
  67. static inline bool isMETAPOSTtwo(int ch) {
  68. return
  69. (ch == ';') || (ch == '$') || (ch == '@') || (ch == '#');
  70. }
  71. static inline bool isMETAPOSTthree(int ch) {
  72. return
  73. (ch == '.') || (ch == '-') || (ch == '+') || (ch == '/') ||
  74. (ch == '*') || (ch == ',') || (ch == '|') || (ch == '`') ||
  75. (ch == '!') || (ch == '?') || (ch == '^') || (ch == '&') ||
  76. (ch == '%') ;
  77. }
  78. static inline bool isMETAPOSTidentifier(int ch) {
  79. return
  80. ((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) ||
  81. (ch == '_') ;
  82. }
  83. static inline bool isMETAPOSTnumber(int ch) {
  84. return
  85. (ch >= '0') && (ch <= '9') ;
  86. }
  87. static inline bool isMETAPOSTstring(int ch) {
  88. return
  89. (ch == '\"') ;
  90. }
  91. static inline bool isMETAPOSTcolon(int ch) {
  92. return
  93. (ch == ':') ;
  94. }
  95. static inline bool isMETAPOSTequal(int ch) {
  96. return
  97. (ch == '=') ;
  98. }
  99. static int CheckMETAPOSTInterface(
  100. Sci_PositionU startPos,
  101. Sci_Position length,
  102. Accessor &styler,
  103. int defaultInterface) {
  104. char lineBuffer[1024] ;
  105. Sci_PositionU linePos = 0 ;
  106. // some day we can make something lexer.metapost.mapping=(none,0)(metapost,1)(mp,1)(metafun,2)...
  107. if (styler.SafeGetCharAt(0) == '%') {
  108. for (Sci_PositionU i = 0; i < startPos + length; i++) {
  109. lineBuffer[linePos++] = styler.SafeGetCharAt(i) ;
  110. if (endOfLine(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
  111. lineBuffer[linePos] = '\0';
  112. if (strstr(lineBuffer, "interface=none")) {
  113. return 0 ;
  114. } else if (strstr(lineBuffer, "interface=metapost") || strstr(lineBuffer, "interface=mp")) {
  115. return 1 ;
  116. } else if (strstr(lineBuffer, "interface=metafun")) {
  117. return 2 ;
  118. } else if (styler.SafeGetCharAt(1) == 'D' && strstr(lineBuffer, "%D \\module")) {
  119. // better would be to limit the search to just one line
  120. return 2 ;
  121. } else {
  122. return defaultInterface ;
  123. }
  124. }
  125. }
  126. }
  127. return defaultInterface ;
  128. }
  129. static void ColouriseMETAPOSTDoc(
  130. Sci_PositionU startPos,
  131. Sci_Position length,
  132. int,
  133. WordList *keywordlists[],
  134. Accessor &styler) {
  135. styler.StartAt(startPos) ;
  136. styler.StartSegment(startPos) ;
  137. bool processComment = styler.GetPropertyInt("lexer.metapost.comment.process", 0) == 1 ;
  138. int defaultInterface = styler.GetPropertyInt("lexer.metapost.interface.default", 1) ;
  139. int currentInterface = CheckMETAPOSTInterface(startPos,length,styler,defaultInterface) ;
  140. // 0 no keyword highlighting
  141. // 1 metapost keyword hightlighting
  142. // 2+ metafun keyword hightlighting
  143. int extraInterface = 0 ;
  144. if (currentInterface != 0) {
  145. extraInterface = currentInterface ;
  146. }
  147. WordList &keywords = *keywordlists[0] ;
  148. WordList &keywords2 = *keywordlists[extraInterface-1] ;
  149. StyleContext sc(startPos, length, SCE_METAPOST_TEXT, styler) ;
  150. char key[100] ;
  151. bool inTeX = false ;
  152. bool inComment = false ;
  153. bool inString = false ;
  154. bool inClause = false ;
  155. bool going = sc.More() ; // needed because of a fuzzy end of file state
  156. for (; going; sc.Forward()) {
  157. if (! sc.More()) { going = false ; } // we need to go one behind the end of text
  158. if (inClause) {
  159. sc.SetState(SCE_METAPOST_TEXT) ;
  160. inClause = false ;
  161. }
  162. if (inComment) {
  163. if (sc.atLineEnd) {
  164. sc.SetState(SCE_METAPOST_TEXT) ;
  165. inTeX = false ;
  166. inComment = false ;
  167. inClause = false ;
  168. inString = false ; // not correct but we want to stimulate one-lines
  169. }
  170. } else if (inString) {
  171. if (isMETAPOSTstring(sc.ch)) {
  172. sc.SetState(SCE_METAPOST_SPECIAL) ;
  173. sc.ForwardSetState(SCE_METAPOST_TEXT) ;
  174. inString = false ;
  175. } else if (sc.atLineEnd) {
  176. sc.SetState(SCE_METAPOST_TEXT) ;
  177. inTeX = false ;
  178. inComment = false ;
  179. inClause = false ;
  180. inString = false ; // not correct but we want to stimulate one-lines
  181. }
  182. } else {
  183. if ((! isMETAPOSTidentifier(sc.ch)) && (sc.LengthCurrent() > 0)) {
  184. if (sc.state == SCE_METAPOST_COMMAND) {
  185. sc.GetCurrent(key, sizeof(key)) ;
  186. if ((strcmp(key,"btex") == 0) || (strcmp(key,"verbatimtex") == 0)) {
  187. sc.ChangeState(SCE_METAPOST_GROUP) ;
  188. inTeX = true ;
  189. } else if (inTeX) {
  190. if (strcmp(key,"etex") == 0) {
  191. sc.ChangeState(SCE_METAPOST_GROUP) ;
  192. inTeX = false ;
  193. } else {
  194. sc.ChangeState(SCE_METAPOST_TEXT) ;
  195. }
  196. } else {
  197. if (keywords && keywords.InList(key)) {
  198. sc.ChangeState(SCE_METAPOST_COMMAND) ;
  199. } else if (keywords2 && keywords2.InList(key)) {
  200. sc.ChangeState(SCE_METAPOST_EXTRA) ;
  201. } else {
  202. sc.ChangeState(SCE_METAPOST_TEXT) ;
  203. }
  204. }
  205. }
  206. }
  207. if (isMETAPOSTcomment(sc.ch)) {
  208. if (! inTeX) {
  209. sc.SetState(SCE_METAPOST_SYMBOL) ;
  210. sc.ForwardSetState(SCE_METAPOST_DEFAULT) ;
  211. inComment = ! processComment ;
  212. } else {
  213. sc.SetState(SCE_METAPOST_TEXT) ;
  214. }
  215. } else if (isMETAPOSTstring(sc.ch)) {
  216. if (! inTeX) {
  217. sc.SetState(SCE_METAPOST_SPECIAL) ;
  218. if (! isMETAPOSTstring(sc.chNext)) {
  219. sc.ForwardSetState(SCE_METAPOST_TEXT) ;
  220. }
  221. inString = true ;
  222. } else {
  223. sc.SetState(SCE_METAPOST_TEXT) ;
  224. }
  225. } else if (isMETAPOSTcolon(sc.ch)) {
  226. if (! inTeX) {
  227. if (! isMETAPOSTequal(sc.chNext)) {
  228. sc.SetState(SCE_METAPOST_COMMAND) ;
  229. inClause = true ;
  230. } else {
  231. sc.SetState(SCE_METAPOST_SPECIAL) ;
  232. }
  233. } else {
  234. sc.SetState(SCE_METAPOST_TEXT) ;
  235. }
  236. } else if (isMETAPOSTone(sc.ch)) {
  237. if (! inTeX) {
  238. sc.SetState(SCE_METAPOST_SPECIAL) ;
  239. } else {
  240. sc.SetState(SCE_METAPOST_TEXT) ;
  241. }
  242. } else if (isMETAPOSTtwo(sc.ch)) {
  243. if (! inTeX) {
  244. sc.SetState(SCE_METAPOST_GROUP) ;
  245. } else {
  246. sc.SetState(SCE_METAPOST_TEXT) ;
  247. }
  248. } else if (isMETAPOSTthree(sc.ch)) {
  249. if (! inTeX) {
  250. sc.SetState(SCE_METAPOST_SYMBOL) ;
  251. } else {
  252. sc.SetState(SCE_METAPOST_TEXT) ;
  253. }
  254. } else if (isMETAPOSTidentifier(sc.ch)) {
  255. if (sc.state != SCE_METAPOST_COMMAND) {
  256. sc.SetState(SCE_METAPOST_TEXT) ;
  257. sc.ChangeState(SCE_METAPOST_COMMAND) ;
  258. }
  259. } else if (isMETAPOSTnumber(sc.ch)) {
  260. // rather redundant since for the moment we don't handle numbers
  261. sc.SetState(SCE_METAPOST_TEXT) ;
  262. } else if (sc.atLineEnd) {
  263. sc.SetState(SCE_METAPOST_TEXT) ;
  264. inTeX = false ;
  265. inComment = false ;
  266. inClause = false ;
  267. inString = false ;
  268. } else {
  269. sc.SetState(SCE_METAPOST_TEXT) ;
  270. }
  271. }
  272. }
  273. sc.Complete();
  274. }
  275. // Hooks info the system:
  276. static const char * const metapostWordListDesc[] = {
  277. "MetaPost",
  278. "MetaFun",
  279. 0
  280. } ;
  281. static int classifyFoldPointMetapost(const char* s,WordList *keywordlists[]) {
  282. WordList& keywordsStart=*keywordlists[3];
  283. WordList& keywordsStop1=*keywordlists[4];
  284. if (keywordsStart.InList(s)) {return 1;}
  285. else if (keywordsStop1.InList(s)) {return -1;}
  286. return 0;
  287. }
  288. static int ParseMetapostWord(Sci_PositionU pos, Accessor &styler, char *word)
  289. {
  290. int length=0;
  291. char ch=styler.SafeGetCharAt(pos);
  292. *word=0;
  293. while(isMETAPOSTidentifier(ch) && isalpha(ch) && length<100){
  294. word[length]=ch;
  295. length++;
  296. ch=styler.SafeGetCharAt(pos+length);
  297. }
  298. word[length]=0;
  299. return length;
  300. }
  301. static void FoldMetapostDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *keywordlists[], Accessor &styler)
  302. {
  303. bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
  304. Sci_PositionU endPos = startPos+length;
  305. int visibleChars=0;
  306. Sci_Position lineCurrent=styler.GetLine(startPos);
  307. int levelPrev=styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
  308. int levelCurrent=levelPrev;
  309. char chNext=styler[startPos];
  310. char buffer[100]="";
  311. for (Sci_PositionU i=startPos; i < endPos; i++) {
  312. char ch=chNext;
  313. chNext=styler.SafeGetCharAt(i+1);
  314. char chPrev=styler.SafeGetCharAt(i-1);
  315. bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
  316. if(i==0 || chPrev == '\r' || chPrev=='\n'|| chPrev==' '|| chPrev=='(' || chPrev=='$')
  317. {
  318. ParseMetapostWord(i, styler, buffer);
  319. levelCurrent += classifyFoldPointMetapost(buffer,keywordlists);
  320. }
  321. if (atEOL) {
  322. int lev = levelPrev;
  323. if (visibleChars == 0 && foldCompact)
  324. lev |= SC_FOLDLEVELWHITEFLAG;
  325. if ((levelCurrent > levelPrev) && (visibleChars > 0))
  326. lev |= SC_FOLDLEVELHEADERFLAG;
  327. if (lev != styler.LevelAt(lineCurrent)) {
  328. styler.SetLevel(lineCurrent, lev);
  329. }
  330. lineCurrent++;
  331. levelPrev = levelCurrent;
  332. visibleChars = 0;
  333. }
  334. if (!isspacechar(ch))
  335. visibleChars++;
  336. }
  337. // Fill in the real level of the next line, keeping the current flags as they will be filled in later
  338. int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
  339. styler.SetLevel(lineCurrent, levelPrev | flagsNext);
  340. }
  341. LexerModule lmMETAPOST(SCLEX_METAPOST, ColouriseMETAPOSTDoc, "metapost", FoldMetapostDoc, metapostWordListDesc);