LexBasic.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // Scintilla source code edit control
  2. /** @file LexBasic.cxx
  3. ** Lexer for BlitzBasic and PureBasic.
  4. ** Converted to lexer object and added further folding features/properties by "Udo Lechner" <dlchnr(at)gmx(dot)net>
  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. // This tries to be a unified Lexer/Folder for all the BlitzBasic/BlitzMax/PurBasic basics
  9. // and derivatives. Once they diverge enough, might want to split it into multiple
  10. // lexers for more code clearity.
  11. //
  12. // Mail me (elias <at> users <dot> sf <dot> net) for any bugs.
  13. // Folding only works for simple things like functions or types.
  14. // You may want to have a look at my ctags lexer as well, if you additionally to coloring
  15. // and folding need to extract things like label tags in your editor.
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <stdarg.h>
  20. #include <assert.h>
  21. #include <ctype.h>
  22. #include <string>
  23. #include <map>
  24. #include "ILexer.h"
  25. #include "Scintilla.h"
  26. #include "SciLexer.h"
  27. #include "WordList.h"
  28. #include "LexAccessor.h"
  29. #include "StyleContext.h"
  30. #include "CharacterSet.h"
  31. #include "LexerModule.h"
  32. #include "OptionSet.h"
  33. #ifdef SCI_NAMESPACE
  34. using namespace Scintilla;
  35. #endif
  36. /* Bits:
  37. * 1 - whitespace
  38. * 2 - operator
  39. * 4 - identifier
  40. * 8 - decimal digit
  41. * 16 - hex digit
  42. * 32 - bin digit
  43. * 64 - letter
  44. */
  45. static int character_classification[128] =
  46. {
  47. 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. 1, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 10, 2,
  50. 60, 60, 28, 28, 28, 28, 28, 28, 28, 28, 2, 2, 2, 2, 2, 2,
  51. 2, 84, 84, 84, 84, 84, 84, 68, 68, 68, 68, 68, 68, 68, 68, 68,
  52. 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 2, 2, 2, 2, 68,
  53. 2, 84, 84, 84, 84, 84, 84, 68, 68, 68, 68, 68, 68, 68, 68, 68,
  54. 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 2, 2, 2, 2, 0
  55. };
  56. static bool IsSpace(int c) {
  57. return c < 128 && (character_classification[c] & 1);
  58. }
  59. static bool IsOperator(int c) {
  60. return c < 128 && (character_classification[c] & 2);
  61. }
  62. static bool IsIdentifier(int c) {
  63. return c < 128 && (character_classification[c] & 4);
  64. }
  65. static bool IsDigit(int c) {
  66. return c < 128 && (character_classification[c] & 8);
  67. }
  68. static bool IsHexDigit(int c) {
  69. return c < 128 && (character_classification[c] & 16);
  70. }
  71. static bool IsBinDigit(int c) {
  72. return c < 128 && (character_classification[c] & 32);
  73. }
  74. static bool IsLetter(int c) {
  75. return c < 128 && (character_classification[c] & 64);
  76. }
  77. static int LowerCase(int c)
  78. {
  79. if (c >= 'A' && c <= 'Z')
  80. return 'a' + c - 'A';
  81. return c;
  82. }
  83. static int CheckBlitzFoldPoint(char const *token, int &level) {
  84. if (!strcmp(token, "function") ||
  85. !strcmp(token, "type")) {
  86. level |= SC_FOLDLEVELHEADERFLAG;
  87. return 1;
  88. }
  89. if (!strcmp(token, "end function") ||
  90. !strcmp(token, "end type")) {
  91. return -1;
  92. }
  93. return 0;
  94. }
  95. static int CheckPureFoldPoint(char const *token, int &level) {
  96. if (!strcmp(token, "procedure") ||
  97. !strcmp(token, "enumeration") ||
  98. !strcmp(token, "interface") ||
  99. !strcmp(token, "structure")) {
  100. level |= SC_FOLDLEVELHEADERFLAG;
  101. return 1;
  102. }
  103. if (!strcmp(token, "endprocedure") ||
  104. !strcmp(token, "endenumeration") ||
  105. !strcmp(token, "endinterface") ||
  106. !strcmp(token, "endstructure")) {
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. static int CheckFreeFoldPoint(char const *token, int &level) {
  112. if (!strcmp(token, "function") ||
  113. !strcmp(token, "sub") ||
  114. !strcmp(token, "enum") ||
  115. !strcmp(token, "type") ||
  116. !strcmp(token, "union") ||
  117. !strcmp(token, "property") ||
  118. !strcmp(token, "destructor") ||
  119. !strcmp(token, "constructor")) {
  120. level |= SC_FOLDLEVELHEADERFLAG;
  121. return 1;
  122. }
  123. if (!strcmp(token, "end function") ||
  124. !strcmp(token, "end sub") ||
  125. !strcmp(token, "end enum") ||
  126. !strcmp(token, "end type") ||
  127. !strcmp(token, "end union") ||
  128. !strcmp(token, "end property") ||
  129. !strcmp(token, "end destructor") ||
  130. !strcmp(token, "end constructor")) {
  131. return -1;
  132. }
  133. return 0;
  134. }
  135. // An individual named option for use in an OptionSet
  136. // Options used for LexerBasic
  137. struct OptionsBasic {
  138. bool fold;
  139. bool foldSyntaxBased;
  140. bool foldCommentExplicit;
  141. std::string foldExplicitStart;
  142. std::string foldExplicitEnd;
  143. bool foldExplicitAnywhere;
  144. bool foldCompact;
  145. OptionsBasic() {
  146. fold = false;
  147. foldSyntaxBased = true;
  148. foldCommentExplicit = false;
  149. foldExplicitStart = "";
  150. foldExplicitEnd = "";
  151. foldExplicitAnywhere = false;
  152. foldCompact = true;
  153. }
  154. };
  155. static const char * const blitzbasicWordListDesc[] = {
  156. "BlitzBasic Keywords",
  157. "user1",
  158. "user2",
  159. "user3",
  160. 0
  161. };
  162. static const char * const purebasicWordListDesc[] = {
  163. "PureBasic Keywords",
  164. "PureBasic PreProcessor Keywords",
  165. "user defined 1",
  166. "user defined 2",
  167. 0
  168. };
  169. static const char * const freebasicWordListDesc[] = {
  170. "FreeBasic Keywords",
  171. "FreeBasic PreProcessor Keywords",
  172. "user defined 1",
  173. "user defined 2",
  174. 0
  175. };
  176. struct OptionSetBasic : public OptionSet<OptionsBasic> {
  177. OptionSetBasic(const char * const wordListDescriptions[]) {
  178. DefineProperty("fold", &OptionsBasic::fold);
  179. DefineProperty("fold.basic.syntax.based", &OptionsBasic::foldSyntaxBased,
  180. "Set this property to 0 to disable syntax based folding.");
  181. DefineProperty("fold.basic.comment.explicit", &OptionsBasic::foldCommentExplicit,
  182. "This option enables folding explicit fold points when using the Basic lexer. "
  183. "Explicit fold points allows adding extra folding by placing a ;{ (BB/PB) or '{ (FB) comment at the start "
  184. "and a ;} (BB/PB) or '} (FB) at the end of a section that should be folded.");
  185. DefineProperty("fold.basic.explicit.start", &OptionsBasic::foldExplicitStart,
  186. "The string to use for explicit fold start points, replacing the standard ;{ (BB/PB) or '{ (FB).");
  187. DefineProperty("fold.basic.explicit.end", &OptionsBasic::foldExplicitEnd,
  188. "The string to use for explicit fold end points, replacing the standard ;} (BB/PB) or '} (FB).");
  189. DefineProperty("fold.basic.explicit.anywhere", &OptionsBasic::foldExplicitAnywhere,
  190. "Set this property to 1 to enable explicit fold points anywhere, not just in line comments.");
  191. DefineProperty("fold.compact", &OptionsBasic::foldCompact);
  192. DefineWordListSets(wordListDescriptions);
  193. }
  194. };
  195. class LexerBasic : public ILexer {
  196. char comment_char;
  197. int (*CheckFoldPoint)(char const *, int &);
  198. WordList keywordlists[4];
  199. OptionsBasic options;
  200. OptionSetBasic osBasic;
  201. public:
  202. LexerBasic(char comment_char_, int (*CheckFoldPoint_)(char const *, int &), const char * const wordListDescriptions[]) :
  203. comment_char(comment_char_),
  204. CheckFoldPoint(CheckFoldPoint_),
  205. osBasic(wordListDescriptions) {
  206. }
  207. virtual ~LexerBasic() {
  208. }
  209. void SCI_METHOD Release() {
  210. delete this;
  211. }
  212. int SCI_METHOD Version() const {
  213. return lvOriginal;
  214. }
  215. const char * SCI_METHOD PropertyNames() {
  216. return osBasic.PropertyNames();
  217. }
  218. int SCI_METHOD PropertyType(const char *name) {
  219. return osBasic.PropertyType(name);
  220. }
  221. const char * SCI_METHOD DescribeProperty(const char *name) {
  222. return osBasic.DescribeProperty(name);
  223. }
  224. Sci_Position SCI_METHOD PropertySet(const char *key, const char *val);
  225. const char * SCI_METHOD DescribeWordListSets() {
  226. return osBasic.DescribeWordListSets();
  227. }
  228. Sci_Position SCI_METHOD WordListSet(int n, const char *wl);
  229. void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess);
  230. void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess);
  231. void * SCI_METHOD PrivateCall(int, void *) {
  232. return 0;
  233. }
  234. static ILexer *LexerFactoryBlitzBasic() {
  235. return new LexerBasic(';', CheckBlitzFoldPoint, blitzbasicWordListDesc);
  236. }
  237. static ILexer *LexerFactoryPureBasic() {
  238. return new LexerBasic(';', CheckPureFoldPoint, purebasicWordListDesc);
  239. }
  240. static ILexer *LexerFactoryFreeBasic() {
  241. return new LexerBasic('\'', CheckFreeFoldPoint, freebasicWordListDesc );
  242. }
  243. };
  244. Sci_Position SCI_METHOD LexerBasic::PropertySet(const char *key, const char *val) {
  245. if (osBasic.PropertySet(&options, key, val)) {
  246. return 0;
  247. }
  248. return -1;
  249. }
  250. Sci_Position SCI_METHOD LexerBasic::WordListSet(int n, const char *wl) {
  251. WordList *wordListN = 0;
  252. switch (n) {
  253. case 0:
  254. wordListN = &keywordlists[0];
  255. break;
  256. case 1:
  257. wordListN = &keywordlists[1];
  258. break;
  259. case 2:
  260. wordListN = &keywordlists[2];
  261. break;
  262. case 3:
  263. wordListN = &keywordlists[3];
  264. break;
  265. }
  266. Sci_Position firstModification = -1;
  267. if (wordListN) {
  268. WordList wlNew;
  269. wlNew.Set(wl);
  270. if (*wordListN != wlNew) {
  271. wordListN->Set(wl);
  272. firstModification = 0;
  273. }
  274. }
  275. return firstModification;
  276. }
  277. void SCI_METHOD LexerBasic::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
  278. LexAccessor styler(pAccess);
  279. bool wasfirst = true, isfirst = true; // true if first token in a line
  280. styler.StartAt(startPos);
  281. int styleBeforeKeyword = SCE_B_DEFAULT;
  282. StyleContext sc(startPos, length, initStyle, styler);
  283. // Can't use sc.More() here else we miss the last character
  284. for (; ; sc.Forward()) {
  285. if (sc.state == SCE_B_IDENTIFIER) {
  286. if (!IsIdentifier(sc.ch)) {
  287. // Labels
  288. if (wasfirst && sc.Match(':')) {
  289. sc.ChangeState(SCE_B_LABEL);
  290. sc.ForwardSetState(SCE_B_DEFAULT);
  291. } else {
  292. char s[100];
  293. int kstates[4] = {
  294. SCE_B_KEYWORD,
  295. SCE_B_KEYWORD2,
  296. SCE_B_KEYWORD3,
  297. SCE_B_KEYWORD4,
  298. };
  299. sc.GetCurrentLowered(s, sizeof(s));
  300. for (int i = 0; i < 4; i++) {
  301. if (keywordlists[i].InList(s)) {
  302. sc.ChangeState(kstates[i]);
  303. }
  304. }
  305. // Types, must set them as operator else they will be
  306. // matched as number/constant
  307. if (sc.Match('.') || sc.Match('$') || sc.Match('%') ||
  308. sc.Match('#')) {
  309. sc.SetState(SCE_B_OPERATOR);
  310. } else {
  311. sc.SetState(SCE_B_DEFAULT);
  312. }
  313. }
  314. }
  315. } else if (sc.state == SCE_B_OPERATOR) {
  316. if (!IsOperator(sc.ch) || sc.Match('#'))
  317. sc.SetState(SCE_B_DEFAULT);
  318. } else if (sc.state == SCE_B_LABEL) {
  319. if (!IsIdentifier(sc.ch))
  320. sc.SetState(SCE_B_DEFAULT);
  321. } else if (sc.state == SCE_B_CONSTANT) {
  322. if (!IsIdentifier(sc.ch))
  323. sc.SetState(SCE_B_DEFAULT);
  324. } else if (sc.state == SCE_B_NUMBER) {
  325. if (!IsDigit(sc.ch))
  326. sc.SetState(SCE_B_DEFAULT);
  327. } else if (sc.state == SCE_B_HEXNUMBER) {
  328. if (!IsHexDigit(sc.ch))
  329. sc.SetState(SCE_B_DEFAULT);
  330. } else if (sc.state == SCE_B_BINNUMBER) {
  331. if (!IsBinDigit(sc.ch))
  332. sc.SetState(SCE_B_DEFAULT);
  333. } else if (sc.state == SCE_B_STRING) {
  334. if (sc.ch == '"') {
  335. sc.ForwardSetState(SCE_B_DEFAULT);
  336. }
  337. if (sc.atLineEnd) {
  338. sc.ChangeState(SCE_B_ERROR);
  339. sc.SetState(SCE_B_DEFAULT);
  340. }
  341. } else if (sc.state == SCE_B_COMMENT || sc.state == SCE_B_PREPROCESSOR) {
  342. if (sc.atLineEnd) {
  343. sc.SetState(SCE_B_DEFAULT);
  344. }
  345. } else if (sc.state == SCE_B_DOCLINE) {
  346. if (sc.atLineEnd) {
  347. sc.SetState(SCE_B_DEFAULT);
  348. } else if (sc.ch == '\\' || sc.ch == '@') {
  349. if (IsLetter(sc.chNext) && sc.chPrev != '\\') {
  350. styleBeforeKeyword = sc.state;
  351. sc.SetState(SCE_B_DOCKEYWORD);
  352. };
  353. }
  354. } else if (sc.state == SCE_B_DOCKEYWORD) {
  355. if (IsSpace(sc.ch)) {
  356. sc.SetState(styleBeforeKeyword);
  357. } else if (sc.atLineEnd && styleBeforeKeyword == SCE_B_DOCLINE) {
  358. sc.SetState(SCE_B_DEFAULT);
  359. }
  360. } else if (sc.state == SCE_B_COMMENTBLOCK) {
  361. if (sc.Match("\'/")) {
  362. sc.Forward();
  363. sc.ForwardSetState(SCE_B_DEFAULT);
  364. }
  365. } else if (sc.state == SCE_B_DOCBLOCK) {
  366. if (sc.Match("\'/")) {
  367. sc.Forward();
  368. sc.ForwardSetState(SCE_B_DEFAULT);
  369. } else if (sc.ch == '\\' || sc.ch == '@') {
  370. if (IsLetter(sc.chNext) && sc.chPrev != '\\') {
  371. styleBeforeKeyword = sc.state;
  372. sc.SetState(SCE_B_DOCKEYWORD);
  373. };
  374. }
  375. }
  376. if (sc.atLineStart)
  377. isfirst = true;
  378. if (sc.state == SCE_B_DEFAULT || sc.state == SCE_B_ERROR) {
  379. if (isfirst && sc.Match('.') && comment_char != '\'') {
  380. sc.SetState(SCE_B_LABEL);
  381. } else if (isfirst && sc.Match('#')) {
  382. wasfirst = isfirst;
  383. sc.SetState(SCE_B_IDENTIFIER);
  384. } else if (sc.Match(comment_char)) {
  385. // Hack to make deprecated QBASIC '$Include show
  386. // up in freebasic with SCE_B_PREPROCESSOR.
  387. if (comment_char == '\'' && sc.Match(comment_char, '$'))
  388. sc.SetState(SCE_B_PREPROCESSOR);
  389. else if (sc.Match("\'*") || sc.Match("\'!")) {
  390. sc.SetState(SCE_B_DOCLINE);
  391. } else {
  392. sc.SetState(SCE_B_COMMENT);
  393. }
  394. } else if (sc.Match("/\'")) {
  395. if (sc.Match("/\'*") || sc.Match("/\'!")) { // Support of gtk-doc/Doxygen doc. style
  396. sc.SetState(SCE_B_DOCBLOCK);
  397. } else {
  398. sc.SetState(SCE_B_COMMENTBLOCK);
  399. }
  400. sc.Forward(); // Eat the ' so it isn't used for the end of the comment
  401. } else if (sc.Match('"')) {
  402. sc.SetState(SCE_B_STRING);
  403. } else if (IsDigit(sc.ch)) {
  404. sc.SetState(SCE_B_NUMBER);
  405. } else if (sc.Match('$') || sc.Match("&h") || sc.Match("&H") || sc.Match("&o") || sc.Match("&O")) {
  406. sc.SetState(SCE_B_HEXNUMBER);
  407. } else if (sc.Match('%') || sc.Match("&b") || sc.Match("&B")) {
  408. sc.SetState(SCE_B_BINNUMBER);
  409. } else if (sc.Match('#')) {
  410. sc.SetState(SCE_B_CONSTANT);
  411. } else if (IsOperator(sc.ch)) {
  412. sc.SetState(SCE_B_OPERATOR);
  413. } else if (IsIdentifier(sc.ch)) {
  414. wasfirst = isfirst;
  415. sc.SetState(SCE_B_IDENTIFIER);
  416. } else if (!IsSpace(sc.ch)) {
  417. sc.SetState(SCE_B_ERROR);
  418. }
  419. }
  420. if (!IsSpace(sc.ch))
  421. isfirst = false;
  422. if (!sc.More())
  423. break;
  424. }
  425. sc.Complete();
  426. }
  427. void SCI_METHOD LexerBasic::Fold(Sci_PositionU startPos, Sci_Position length, int /* initStyle */, IDocument *pAccess) {
  428. if (!options.fold)
  429. return;
  430. LexAccessor styler(pAccess);
  431. Sci_Position line = styler.GetLine(startPos);
  432. int level = styler.LevelAt(line);
  433. int go = 0, done = 0;
  434. Sci_Position endPos = startPos + length;
  435. char word[256];
  436. int wordlen = 0;
  437. const bool userDefinedFoldMarkers = !options.foldExplicitStart.empty() && !options.foldExplicitEnd.empty();
  438. int cNext = styler[startPos];
  439. // Scan for tokens at the start of the line (they may include
  440. // whitespace, for tokens like "End Function"
  441. for (Sci_Position i = startPos; i < endPos; i++) {
  442. int c = cNext;
  443. cNext = styler.SafeGetCharAt(i + 1);
  444. bool atEOL = (c == '\r' && cNext != '\n') || (c == '\n');
  445. if (options.foldSyntaxBased && !done && !go) {
  446. if (wordlen) { // are we scanning a token already?
  447. word[wordlen] = static_cast<char>(LowerCase(c));
  448. if (!IsIdentifier(c)) { // done with token
  449. word[wordlen] = '\0';
  450. go = CheckFoldPoint(word, level);
  451. if (!go) {
  452. // Treat any whitespace as single blank, for
  453. // things like "End Function".
  454. if (IsSpace(c) && IsIdentifier(word[wordlen - 1])) {
  455. word[wordlen] = ' ';
  456. if (wordlen < 255)
  457. wordlen++;
  458. }
  459. else // done with this line
  460. done = 1;
  461. }
  462. } else if (wordlen < 255) {
  463. wordlen++;
  464. }
  465. } else { // start scanning at first non-whitespace character
  466. if (!IsSpace(c)) {
  467. if (IsIdentifier(c)) {
  468. word[0] = static_cast<char>(LowerCase(c));
  469. wordlen = 1;
  470. } else // done with this line
  471. done = 1;
  472. }
  473. }
  474. }
  475. if (options.foldCommentExplicit && ((styler.StyleAt(i) == SCE_B_COMMENT) || options.foldExplicitAnywhere)) {
  476. if (userDefinedFoldMarkers) {
  477. if (styler.Match(i, options.foldExplicitStart.c_str())) {
  478. level |= SC_FOLDLEVELHEADERFLAG;
  479. go = 1;
  480. } else if (styler.Match(i, options.foldExplicitEnd.c_str())) {
  481. go = -1;
  482. }
  483. } else {
  484. if (c == comment_char) {
  485. if (cNext == '{') {
  486. level |= SC_FOLDLEVELHEADERFLAG;
  487. go = 1;
  488. } else if (cNext == '}') {
  489. go = -1;
  490. }
  491. }
  492. }
  493. }
  494. if (atEOL) { // line end
  495. if (!done && wordlen == 0 && options.foldCompact) // line was only space
  496. level |= SC_FOLDLEVELWHITEFLAG;
  497. if (level != styler.LevelAt(line))
  498. styler.SetLevel(line, level);
  499. level += go;
  500. line++;
  501. // reset state
  502. wordlen = 0;
  503. level &= ~SC_FOLDLEVELHEADERFLAG;
  504. level &= ~SC_FOLDLEVELWHITEFLAG;
  505. go = 0;
  506. done = 0;
  507. }
  508. }
  509. }
  510. LexerModule lmBlitzBasic(SCLEX_BLITZBASIC, LexerBasic::LexerFactoryBlitzBasic, "blitzbasic", blitzbasicWordListDesc);
  511. LexerModule lmPureBasic(SCLEX_PUREBASIC, LexerBasic::LexerFactoryPureBasic, "purebasic", purebasicWordListDesc);
  512. LexerModule lmFreeBasic(SCLEX_FREEBASIC, LexerBasic::LexerFactoryFreeBasic, "freebasic", freebasicWordListDesc);