LexMySQL.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /**
  2. * Scintilla source code edit control
  3. * @file LexMySQL.cxx
  4. * Lexer for MySQL
  5. *
  6. * Improved by Mike Lischke <mike.lischke@oracle.com>
  7. * Adopted from LexSQL.cxx by Anders Karlsson <anders@mysql.com>
  8. * Original work by Neil Hodgson <neilh@scintilla.org>
  9. * Copyright 1998-2005 by Neil Hodgson <neilh@scintilla.org>
  10. * The License.txt file describes the conditions under which this software may be distributed.
  11. */
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <stdarg.h>
  16. #include <assert.h>
  17. #include <ctype.h>
  18. #include "ILexer.h"
  19. #include "Scintilla.h"
  20. #include "SciLexer.h"
  21. #include "WordList.h"
  22. #include "LexAccessor.h"
  23. #include "Accessor.h"
  24. #include "StyleContext.h"
  25. #include "CharacterSet.h"
  26. #include "LexerModule.h"
  27. #ifdef SCI_NAMESPACE
  28. using namespace Scintilla;
  29. #endif
  30. static inline bool IsAWordChar(int ch) {
  31. return (ch < 0x80) && (isalnum(ch) || ch == '_');
  32. }
  33. static inline bool IsAWordStart(int ch) {
  34. return (ch < 0x80) && (isalpha(ch) || ch == '_');
  35. }
  36. static inline bool IsANumberChar(int ch) {
  37. // Not exactly following number definition (several dots are seen as OK, etc.)
  38. // but probably enough in most cases.
  39. return (ch < 0x80) &&
  40. (isdigit(ch) || toupper(ch) == 'E' ||
  41. ch == '.' || ch == '-' || ch == '+');
  42. }
  43. //--------------------------------------------------------------------------------------------------
  44. /**
  45. * Check if the current content context represent a keyword and set the context state if so.
  46. */
  47. static void CheckForKeyword(StyleContext& sc, WordList* keywordlists[], int activeState)
  48. {
  49. int length = sc.LengthCurrent() + 1; // +1 for the next char
  50. char* s = new char[length];
  51. sc.GetCurrentLowered(s, length);
  52. if (keywordlists[0]->InList(s))
  53. sc.ChangeState(SCE_MYSQL_MAJORKEYWORD | activeState);
  54. else
  55. if (keywordlists[1]->InList(s))
  56. sc.ChangeState(SCE_MYSQL_KEYWORD | activeState);
  57. else
  58. if (keywordlists[2]->InList(s))
  59. sc.ChangeState(SCE_MYSQL_DATABASEOBJECT | activeState);
  60. else
  61. if (keywordlists[3]->InList(s))
  62. sc.ChangeState(SCE_MYSQL_FUNCTION | activeState);
  63. else
  64. if (keywordlists[5]->InList(s))
  65. sc.ChangeState(SCE_MYSQL_PROCEDUREKEYWORD | activeState);
  66. else
  67. if (keywordlists[6]->InList(s))
  68. sc.ChangeState(SCE_MYSQL_USER1 | activeState);
  69. else
  70. if (keywordlists[7]->InList(s))
  71. sc.ChangeState(SCE_MYSQL_USER2 | activeState);
  72. else
  73. if (keywordlists[8]->InList(s))
  74. sc.ChangeState(SCE_MYSQL_USER3 | activeState);
  75. delete [] s;
  76. }
  77. //--------------------------------------------------------------------------------------------------
  78. #define HIDDENCOMMAND_STATE 0x40 // Offset for states within a hidden command.
  79. #define MASKACTIVE(style) (style & ~HIDDENCOMMAND_STATE)
  80. static void SetDefaultState(StyleContext& sc, int activeState)
  81. {
  82. if (activeState == 0)
  83. sc.SetState(SCE_MYSQL_DEFAULT);
  84. else
  85. sc.SetState(SCE_MYSQL_HIDDENCOMMAND);
  86. }
  87. static void ForwardDefaultState(StyleContext& sc, int activeState)
  88. {
  89. if (activeState == 0)
  90. sc.ForwardSetState(SCE_MYSQL_DEFAULT);
  91. else
  92. sc.ForwardSetState(SCE_MYSQL_HIDDENCOMMAND);
  93. }
  94. static void ColouriseMySQLDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
  95. Accessor &styler)
  96. {
  97. StyleContext sc(startPos, length, initStyle, styler, 127);
  98. int activeState = (initStyle == SCE_MYSQL_HIDDENCOMMAND) ? HIDDENCOMMAND_STATE : initStyle & HIDDENCOMMAND_STATE;
  99. for (; sc.More(); sc.Forward())
  100. {
  101. // Determine if the current state should terminate.
  102. switch (MASKACTIVE(sc.state))
  103. {
  104. case SCE_MYSQL_OPERATOR:
  105. SetDefaultState(sc, activeState);
  106. break;
  107. case SCE_MYSQL_NUMBER:
  108. // We stop the number definition on non-numerical non-dot non-eE non-sign char.
  109. if (!IsANumberChar(sc.ch))
  110. SetDefaultState(sc, activeState);
  111. break;
  112. case SCE_MYSQL_IDENTIFIER:
  113. // Switch from identifier to keyword state and open a new state for the new char.
  114. if (!IsAWordChar(sc.ch))
  115. {
  116. CheckForKeyword(sc, keywordlists, activeState);
  117. // Additional check for function keywords needed.
  118. // A function name must be followed by an opening parenthesis.
  119. if (MASKACTIVE(sc.state) == SCE_MYSQL_FUNCTION && sc.ch != '(')
  120. {
  121. if (activeState > 0)
  122. sc.ChangeState(SCE_MYSQL_HIDDENCOMMAND);
  123. else
  124. sc.ChangeState(SCE_MYSQL_DEFAULT);
  125. }
  126. SetDefaultState(sc, activeState);
  127. }
  128. break;
  129. case SCE_MYSQL_VARIABLE:
  130. if (!IsAWordChar(sc.ch))
  131. SetDefaultState(sc, activeState);
  132. break;
  133. case SCE_MYSQL_SYSTEMVARIABLE:
  134. if (!IsAWordChar(sc.ch))
  135. {
  136. Sci_Position length = sc.LengthCurrent() + 1;
  137. char* s = new char[length];
  138. sc.GetCurrentLowered(s, length);
  139. // Check for known system variables here.
  140. if (keywordlists[4]->InList(&s[2]))
  141. sc.ChangeState(SCE_MYSQL_KNOWNSYSTEMVARIABLE | activeState);
  142. delete [] s;
  143. SetDefaultState(sc, activeState);
  144. }
  145. break;
  146. case SCE_MYSQL_QUOTEDIDENTIFIER:
  147. if (sc.ch == '`')
  148. {
  149. if (sc.chNext == '`')
  150. sc.Forward(); // Ignore it
  151. else
  152. ForwardDefaultState(sc, activeState);
  153. }
  154. break;
  155. case SCE_MYSQL_COMMENT:
  156. if (sc.Match('*', '/'))
  157. {
  158. sc.Forward();
  159. ForwardDefaultState(sc, activeState);
  160. }
  161. break;
  162. case SCE_MYSQL_COMMENTLINE:
  163. if (sc.atLineStart)
  164. SetDefaultState(sc, activeState);
  165. break;
  166. case SCE_MYSQL_SQSTRING:
  167. if (sc.ch == '\\')
  168. sc.Forward(); // Escape sequence
  169. else
  170. if (sc.ch == '\'')
  171. {
  172. // End of single quoted string reached?
  173. if (sc.chNext == '\'')
  174. sc.Forward();
  175. else
  176. ForwardDefaultState(sc, activeState);
  177. }
  178. break;
  179. case SCE_MYSQL_DQSTRING:
  180. if (sc.ch == '\\')
  181. sc.Forward(); // Escape sequence
  182. else
  183. if (sc.ch == '\"')
  184. {
  185. // End of single quoted string reached?
  186. if (sc.chNext == '\"')
  187. sc.Forward();
  188. else
  189. ForwardDefaultState(sc, activeState);
  190. }
  191. break;
  192. case SCE_MYSQL_PLACEHOLDER:
  193. if (sc.Match('}', '>'))
  194. {
  195. sc.Forward();
  196. ForwardDefaultState(sc, activeState);
  197. }
  198. break;
  199. }
  200. if (sc.state == SCE_MYSQL_HIDDENCOMMAND && sc.Match('*', '/'))
  201. {
  202. activeState = 0;
  203. sc.Forward();
  204. ForwardDefaultState(sc, activeState);
  205. }
  206. // Determine if a new state should be entered.
  207. if (sc.state == SCE_MYSQL_DEFAULT || sc.state == SCE_MYSQL_HIDDENCOMMAND)
  208. {
  209. switch (sc.ch)
  210. {
  211. case '@':
  212. if (sc.chNext == '@')
  213. {
  214. sc.SetState(SCE_MYSQL_SYSTEMVARIABLE | activeState);
  215. sc.Forward(2); // Skip past @@.
  216. }
  217. else
  218. if (IsAWordStart(sc.ch))
  219. {
  220. sc.SetState(SCE_MYSQL_VARIABLE | activeState);
  221. sc.Forward(); // Skip past @.
  222. }
  223. else
  224. sc.SetState(SCE_MYSQL_OPERATOR | activeState);
  225. break;
  226. case '`':
  227. sc.SetState(SCE_MYSQL_QUOTEDIDENTIFIER | activeState);
  228. break;
  229. case '#':
  230. sc.SetState(SCE_MYSQL_COMMENTLINE | activeState);
  231. break;
  232. case '\'':
  233. sc.SetState(SCE_MYSQL_SQSTRING | activeState);
  234. break;
  235. case '\"':
  236. sc.SetState(SCE_MYSQL_DQSTRING | activeState);
  237. break;
  238. default:
  239. if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext)))
  240. sc.SetState(SCE_MYSQL_NUMBER | activeState);
  241. else
  242. if (IsAWordStart(sc.ch))
  243. sc.SetState(SCE_MYSQL_IDENTIFIER | activeState);
  244. else
  245. if (sc.Match('/', '*'))
  246. {
  247. sc.SetState(SCE_MYSQL_COMMENT | activeState);
  248. // Skip first char of comment introducer and check for hidden command.
  249. // The second char is skipped by the outer loop.
  250. sc.Forward();
  251. if (sc.GetRelativeCharacter(1) == '!')
  252. {
  253. // Version comment found. Skip * now.
  254. sc.Forward();
  255. activeState = HIDDENCOMMAND_STATE;
  256. sc.ChangeState(SCE_MYSQL_HIDDENCOMMAND);
  257. }
  258. }
  259. else if (sc.Match('<', '{'))
  260. {
  261. sc.SetState(SCE_MYSQL_PLACEHOLDER | activeState);
  262. }
  263. else
  264. if (sc.Match("--"))
  265. {
  266. // Special MySQL single line comment.
  267. sc.SetState(SCE_MYSQL_COMMENTLINE | activeState);
  268. sc.Forward(2);
  269. // Check the third character too. It must be a space or EOL.
  270. if (sc.ch != ' ' && sc.ch != '\n' && sc.ch != '\r')
  271. sc.ChangeState(SCE_MYSQL_OPERATOR | activeState);
  272. }
  273. else
  274. if (isoperator(static_cast<char>(sc.ch)))
  275. sc.SetState(SCE_MYSQL_OPERATOR | activeState);
  276. }
  277. }
  278. }
  279. // Do a final check for keywords if we currently have an identifier, to highlight them
  280. // also at the end of a line.
  281. if (sc.state == SCE_MYSQL_IDENTIFIER)
  282. {
  283. CheckForKeyword(sc, keywordlists, activeState);
  284. // Additional check for function keywords needed.
  285. // A function name must be followed by an opening parenthesis.
  286. if (sc.state == SCE_MYSQL_FUNCTION && sc.ch != '(')
  287. SetDefaultState(sc, activeState);
  288. }
  289. sc.Complete();
  290. }
  291. //--------------------------------------------------------------------------------------------------
  292. /**
  293. * Helper function to determine if we have a foldable comment currently.
  294. */
  295. static bool IsStreamCommentStyle(int style)
  296. {
  297. return MASKACTIVE(style) == SCE_MYSQL_COMMENT;
  298. }
  299. //--------------------------------------------------------------------------------------------------
  300. /**
  301. * Code copied from StyleContext and modified to work here. Should go into Accessor as a
  302. * companion to Match()...
  303. */
  304. static bool MatchIgnoreCase(Accessor &styler, Sci_Position currentPos, const char *s)
  305. {
  306. for (Sci_Position n = 0; *s; n++)
  307. {
  308. if (*s != tolower(styler.SafeGetCharAt(currentPos + n)))
  309. return false;
  310. s++;
  311. }
  312. return true;
  313. }
  314. //--------------------------------------------------------------------------------------------------
  315. // Store both the current line's fold level and the next lines in the
  316. // level store to make it easy to pick up with each increment.
  317. static void FoldMySQLDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *[], Accessor &styler)
  318. {
  319. bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
  320. bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
  321. bool foldOnlyBegin = styler.GetPropertyInt("fold.sql.only.begin", 0) != 0;
  322. int visibleChars = 0;
  323. Sci_Position lineCurrent = styler.GetLine(startPos);
  324. int levelCurrent = SC_FOLDLEVELBASE;
  325. if (lineCurrent > 0)
  326. levelCurrent = styler.LevelAt(lineCurrent - 1) >> 16;
  327. int levelNext = levelCurrent;
  328. int styleNext = styler.StyleAt(startPos);
  329. int style = initStyle;
  330. int activeState = (style == SCE_MYSQL_HIDDENCOMMAND) ? HIDDENCOMMAND_STATE : style & HIDDENCOMMAND_STATE;
  331. bool endPending = false;
  332. bool whenPending = false;
  333. bool elseIfPending = false;
  334. char nextChar = styler.SafeGetCharAt(startPos);
  335. for (Sci_PositionU i = startPos; length > 0; i++, length--)
  336. {
  337. int stylePrev = style;
  338. int lastActiveState = activeState;
  339. style = styleNext;
  340. styleNext = styler.StyleAt(i + 1);
  341. activeState = (style == SCE_MYSQL_HIDDENCOMMAND) ? HIDDENCOMMAND_STATE : style & HIDDENCOMMAND_STATE;
  342. char currentChar = nextChar;
  343. nextChar = styler.SafeGetCharAt(i + 1);
  344. bool atEOL = (currentChar == '\r' && nextChar != '\n') || (currentChar == '\n');
  345. switch (MASKACTIVE(style))
  346. {
  347. case SCE_MYSQL_COMMENT:
  348. if (foldComment)
  349. {
  350. // Multiline comment style /* .. */ just started or is still in progress.
  351. if (IsStreamCommentStyle(style) && !IsStreamCommentStyle(stylePrev))
  352. levelNext++;
  353. }
  354. break;
  355. case SCE_MYSQL_COMMENTLINE:
  356. if (foldComment)
  357. {
  358. // Not really a standard, but we add support for single line comments
  359. // with special curly braces syntax as foldable comments too.
  360. // MySQL needs -- comments to be followed by space or control char
  361. if (styler.Match(i, "--"))
  362. {
  363. char chNext2 = styler.SafeGetCharAt(i + 2);
  364. char chNext3 = styler.SafeGetCharAt(i + 3);
  365. if (chNext2 == '{' || chNext3 == '{')
  366. levelNext++;
  367. else
  368. if (chNext2 == '}' || chNext3 == '}')
  369. levelNext--;
  370. }
  371. }
  372. break;
  373. case SCE_MYSQL_HIDDENCOMMAND:
  374. /*
  375. if (endPending)
  376. {
  377. // A conditional command is not a white space so it should end the current block
  378. // before opening a new one.
  379. endPending = false;
  380. levelNext--;
  381. if (levelNext < SC_FOLDLEVELBASE)
  382. levelNext = SC_FOLDLEVELBASE;
  383. }
  384. }*/
  385. if (activeState != lastActiveState)
  386. levelNext++;
  387. break;
  388. case SCE_MYSQL_OPERATOR:
  389. if (endPending)
  390. {
  391. endPending = false;
  392. levelNext--;
  393. if (levelNext < SC_FOLDLEVELBASE)
  394. levelNext = SC_FOLDLEVELBASE;
  395. }
  396. if (currentChar == '(')
  397. levelNext++;
  398. else
  399. if (currentChar == ')')
  400. {
  401. levelNext--;
  402. if (levelNext < SC_FOLDLEVELBASE)
  403. levelNext = SC_FOLDLEVELBASE;
  404. }
  405. break;
  406. case SCE_MYSQL_MAJORKEYWORD:
  407. case SCE_MYSQL_KEYWORD:
  408. case SCE_MYSQL_FUNCTION:
  409. case SCE_MYSQL_PROCEDUREKEYWORD:
  410. // Reserved and other keywords.
  411. if (style != stylePrev)
  412. {
  413. // END decreases the folding level, regardless which keyword follows.
  414. bool endFound = MatchIgnoreCase(styler, i, "end");
  415. if (endPending)
  416. {
  417. levelNext--;
  418. if (levelNext < SC_FOLDLEVELBASE)
  419. levelNext = SC_FOLDLEVELBASE;
  420. }
  421. else
  422. if (!endFound)
  423. {
  424. if (MatchIgnoreCase(styler, i, "begin"))
  425. levelNext++;
  426. else
  427. {
  428. if (!foldOnlyBegin)
  429. {
  430. bool whileFound = MatchIgnoreCase(styler, i, "while");
  431. bool loopFound = MatchIgnoreCase(styler, i, "loop");
  432. bool repeatFound = MatchIgnoreCase(styler, i, "repeat");
  433. bool caseFound = MatchIgnoreCase(styler, i, "case");
  434. if (whileFound || loopFound || repeatFound || caseFound)
  435. levelNext++;
  436. else
  437. {
  438. // IF alone does not increase the fold level as it is also used in non-block'ed
  439. // code like DROP PROCEDURE blah IF EXISTS.
  440. // Instead THEN opens the new level (if not part of an ELSEIF or WHEN (case) branch).
  441. if (MatchIgnoreCase(styler, i, "then"))
  442. {
  443. if (!elseIfPending && !whenPending)
  444. levelNext++;
  445. else
  446. {
  447. elseIfPending = false;
  448. whenPending = false;
  449. }
  450. }
  451. else
  452. {
  453. // Neither of if/then/while/loop/repeat/case, so check for
  454. // sub parts of IF and CASE.
  455. if (MatchIgnoreCase(styler, i, "elseif"))
  456. elseIfPending = true;
  457. if (MatchIgnoreCase(styler, i, "when"))
  458. whenPending = true;
  459. }
  460. }
  461. }
  462. }
  463. }
  464. // Keep the current end state for the next round.
  465. endPending = endFound;
  466. }
  467. break;
  468. default:
  469. if (!isspacechar(currentChar) && endPending)
  470. {
  471. // END followed by a non-whitespace character (not covered by other cases like identifiers)
  472. // also should end a folding block. Typical case: END followed by self defined delimiter.
  473. levelNext--;
  474. if (levelNext < SC_FOLDLEVELBASE)
  475. levelNext = SC_FOLDLEVELBASE;
  476. }
  477. break;
  478. }
  479. // Go up one level if we just ended a multi line comment.
  480. if (IsStreamCommentStyle(stylePrev) && !IsStreamCommentStyle(style))
  481. {
  482. levelNext--;
  483. if (levelNext < SC_FOLDLEVELBASE)
  484. levelNext = SC_FOLDLEVELBASE;
  485. }
  486. if (activeState == 0 && lastActiveState != 0)
  487. {
  488. // Decrease fold level when we left a hidden command.
  489. levelNext--;
  490. if (levelNext < SC_FOLDLEVELBASE)
  491. levelNext = SC_FOLDLEVELBASE;
  492. }
  493. if (atEOL)
  494. {
  495. // Apply the new folding level to this line.
  496. // Leave pending states as they are otherwise a line break will de-sync
  497. // code folding and valid syntax.
  498. int levelUse = levelCurrent;
  499. int lev = levelUse | levelNext << 16;
  500. if (visibleChars == 0 && foldCompact)
  501. lev |= SC_FOLDLEVELWHITEFLAG;
  502. if (levelUse < levelNext)
  503. lev |= SC_FOLDLEVELHEADERFLAG;
  504. if (lev != styler.LevelAt(lineCurrent))
  505. styler.SetLevel(lineCurrent, lev);
  506. lineCurrent++;
  507. levelCurrent = levelNext;
  508. visibleChars = 0;
  509. }
  510. if (!isspacechar(currentChar))
  511. visibleChars++;
  512. }
  513. }
  514. //--------------------------------------------------------------------------------------------------
  515. static const char * const mysqlWordListDesc[] = {
  516. "Major Keywords",
  517. "Keywords",
  518. "Database Objects",
  519. "Functions",
  520. "System Variables",
  521. "Procedure keywords",
  522. "User Keywords 1",
  523. "User Keywords 2",
  524. "User Keywords 3",
  525. 0
  526. };
  527. LexerModule lmMySQL(SCLEX_MYSQL, ColouriseMySQLDoc, "mysql", FoldMySQLDoc, mysqlWordListDesc);