LexSQL.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. //-*- coding: utf-8 -*-
  2. // Scintilla source code edit control
  3. /** @file LexSQL.cxx
  4. ** Lexer for SQL, including PL/SQL and SQL*Plus.
  5. ** Improved by Jérôme LAFORGE <jerome.laforge_AT_gmail_DOT_com> from 2010 to 2012.
  6. **/
  7. // Copyright 1998-2012 by Neil Hodgson <neilh@scintilla.org>
  8. // The License.txt file describes the conditions under which this software may be distributed.
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <stdarg.h>
  13. #include <assert.h>
  14. #include <ctype.h>
  15. #include <string>
  16. #include <vector>
  17. #include <map>
  18. #include <algorithm>
  19. #include "ILexer.h"
  20. #include "Scintilla.h"
  21. #include "SciLexer.h"
  22. #include "WordList.h"
  23. #include "LexAccessor.h"
  24. #include "Accessor.h"
  25. #include "StyleContext.h"
  26. #include "CharacterSet.h"
  27. #include "LexerModule.h"
  28. #include "OptionSet.h"
  29. #include "SparseState.h"
  30. #ifdef SCI_NAMESPACE
  31. using namespace Scintilla;
  32. #endif
  33. static inline bool IsAWordChar(int ch, bool sqlAllowDottedWord) {
  34. if (!sqlAllowDottedWord)
  35. return (ch < 0x80) && (isalnum(ch) || ch == '_');
  36. else
  37. return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '.');
  38. }
  39. static inline bool IsAWordStart(int ch) {
  40. return (ch < 0x80) && (isalpha(ch) || ch == '_');
  41. }
  42. static inline bool IsADoxygenChar(int ch) {
  43. return (islower(ch) || ch == '$' || ch == '@' ||
  44. ch == '\\' || ch == '&' || ch == '<' ||
  45. ch == '>' || ch == '#' || ch == '{' ||
  46. ch == '}' || ch == '[' || ch == ']');
  47. }
  48. static inline bool IsANumberChar(int ch, int chPrev) {
  49. // Not exactly following number definition (several dots are seen as OK, etc.)
  50. // but probably enough in most cases.
  51. return (ch < 0x80) &&
  52. (isdigit(ch) || toupper(ch) == 'E' ||
  53. ch == '.' || ((ch == '-' || ch == '+') && chPrev < 0x80 && toupper(chPrev) == 'E'));
  54. }
  55. typedef unsigned int sql_state_t;
  56. class SQLStates {
  57. public :
  58. void Set(Sci_Position lineNumber, unsigned short int sqlStatesLine) {
  59. sqlStatement.Set(lineNumber, sqlStatesLine);
  60. }
  61. sql_state_t IgnoreWhen (sql_state_t sqlStatesLine, bool enable) {
  62. if (enable)
  63. sqlStatesLine |= MASK_IGNORE_WHEN;
  64. else
  65. sqlStatesLine &= ~MASK_IGNORE_WHEN;
  66. return sqlStatesLine;
  67. }
  68. sql_state_t IntoCondition (sql_state_t sqlStatesLine, bool enable) {
  69. if (enable)
  70. sqlStatesLine |= MASK_INTO_CONDITION;
  71. else
  72. sqlStatesLine &= ~MASK_INTO_CONDITION;
  73. return sqlStatesLine;
  74. }
  75. sql_state_t IntoExceptionBlock (sql_state_t sqlStatesLine, bool enable) {
  76. if (enable)
  77. sqlStatesLine |= MASK_INTO_EXCEPTION;
  78. else
  79. sqlStatesLine &= ~MASK_INTO_EXCEPTION;
  80. return sqlStatesLine;
  81. }
  82. sql_state_t IntoDeclareBlock (sql_state_t sqlStatesLine, bool enable) {
  83. if (enable)
  84. sqlStatesLine |= MASK_INTO_DECLARE;
  85. else
  86. sqlStatesLine &= ~MASK_INTO_DECLARE;
  87. return sqlStatesLine;
  88. }
  89. sql_state_t IntoMergeStatement (sql_state_t sqlStatesLine, bool enable) {
  90. if (enable)
  91. sqlStatesLine |= MASK_MERGE_STATEMENT;
  92. else
  93. sqlStatesLine &= ~MASK_MERGE_STATEMENT;
  94. return sqlStatesLine;
  95. }
  96. sql_state_t CaseMergeWithoutWhenFound (sql_state_t sqlStatesLine, bool found) {
  97. if (found)
  98. sqlStatesLine |= MASK_CASE_MERGE_WITHOUT_WHEN_FOUND;
  99. else
  100. sqlStatesLine &= ~MASK_CASE_MERGE_WITHOUT_WHEN_FOUND;
  101. return sqlStatesLine;
  102. }
  103. sql_state_t IntoSelectStatementOrAssignment (sql_state_t sqlStatesLine, bool found) {
  104. if (found)
  105. sqlStatesLine |= MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT;
  106. else
  107. sqlStatesLine &= ~MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT;
  108. return sqlStatesLine;
  109. }
  110. sql_state_t BeginCaseBlock (sql_state_t sqlStatesLine) {
  111. if ((sqlStatesLine & MASK_NESTED_CASES) < MASK_NESTED_CASES) {
  112. sqlStatesLine++;
  113. }
  114. return sqlStatesLine;
  115. }
  116. sql_state_t EndCaseBlock (sql_state_t sqlStatesLine) {
  117. if ((sqlStatesLine & MASK_NESTED_CASES) > 0) {
  118. sqlStatesLine--;
  119. }
  120. return sqlStatesLine;
  121. }
  122. sql_state_t IntoCreateStatement (sql_state_t sqlStatesLine, bool enable) {
  123. if (enable)
  124. sqlStatesLine |= MASK_INTO_CREATE;
  125. else
  126. sqlStatesLine &= ~MASK_INTO_CREATE;
  127. return sqlStatesLine;
  128. }
  129. sql_state_t IntoCreateViewStatement (sql_state_t sqlStatesLine, bool enable) {
  130. if (enable)
  131. sqlStatesLine |= MASK_INTO_CREATE_VIEW;
  132. else
  133. sqlStatesLine &= ~MASK_INTO_CREATE_VIEW;
  134. return sqlStatesLine;
  135. }
  136. sql_state_t IntoCreateViewAsStatement (sql_state_t sqlStatesLine, bool enable) {
  137. if (enable)
  138. sqlStatesLine |= MASK_INTO_CREATE_VIEW_AS_STATEMENT;
  139. else
  140. sqlStatesLine &= ~MASK_INTO_CREATE_VIEW_AS_STATEMENT;
  141. return sqlStatesLine;
  142. }
  143. bool IsIgnoreWhen (sql_state_t sqlStatesLine) {
  144. return (sqlStatesLine & MASK_IGNORE_WHEN) != 0;
  145. }
  146. bool IsIntoCondition (sql_state_t sqlStatesLine) {
  147. return (sqlStatesLine & MASK_INTO_CONDITION) != 0;
  148. }
  149. bool IsIntoCaseBlock (sql_state_t sqlStatesLine) {
  150. return (sqlStatesLine & MASK_NESTED_CASES) != 0;
  151. }
  152. bool IsIntoExceptionBlock (sql_state_t sqlStatesLine) {
  153. return (sqlStatesLine & MASK_INTO_EXCEPTION) != 0;
  154. }
  155. bool IsIntoSelectStatementOrAssignment (sql_state_t sqlStatesLine) {
  156. return (sqlStatesLine & MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT) != 0;
  157. }
  158. bool IsCaseMergeWithoutWhenFound (sql_state_t sqlStatesLine) {
  159. return (sqlStatesLine & MASK_CASE_MERGE_WITHOUT_WHEN_FOUND) != 0;
  160. }
  161. bool IsIntoDeclareBlock (sql_state_t sqlStatesLine) {
  162. return (sqlStatesLine & MASK_INTO_DECLARE) != 0;
  163. }
  164. bool IsIntoMergeStatement (sql_state_t sqlStatesLine) {
  165. return (sqlStatesLine & MASK_MERGE_STATEMENT) != 0;
  166. }
  167. bool IsIntoCreateStatement (sql_state_t sqlStatesLine) {
  168. return (sqlStatesLine & MASK_INTO_CREATE) != 0;
  169. }
  170. bool IsIntoCreateViewStatement (sql_state_t sqlStatesLine) {
  171. return (sqlStatesLine & MASK_INTO_CREATE_VIEW) != 0;
  172. }
  173. bool IsIntoCreateViewAsStatement (sql_state_t sqlStatesLine) {
  174. return (sqlStatesLine & MASK_INTO_CREATE_VIEW_AS_STATEMENT) != 0;
  175. }
  176. sql_state_t ForLine(Sci_Position lineNumber) {
  177. return sqlStatement.ValueAt(lineNumber);
  178. }
  179. SQLStates() {}
  180. private :
  181. SparseState <sql_state_t> sqlStatement;
  182. enum {
  183. MASK_NESTED_CASES = 0x0001FF,
  184. MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT = 0x000200,
  185. MASK_CASE_MERGE_WITHOUT_WHEN_FOUND = 0x000400,
  186. MASK_MERGE_STATEMENT = 0x000800,
  187. MASK_INTO_DECLARE = 0x001000,
  188. MASK_INTO_EXCEPTION = 0x002000,
  189. MASK_INTO_CONDITION = 0x004000,
  190. MASK_IGNORE_WHEN = 0x008000,
  191. MASK_INTO_CREATE = 0x010000,
  192. MASK_INTO_CREATE_VIEW = 0x020000,
  193. MASK_INTO_CREATE_VIEW_AS_STATEMENT = 0x040000
  194. };
  195. };
  196. // Options used for LexerSQL
  197. struct OptionsSQL {
  198. bool fold;
  199. bool foldAtElse;
  200. bool foldComment;
  201. bool foldCompact;
  202. bool foldOnlyBegin;
  203. bool sqlBackticksIdentifier;
  204. bool sqlNumbersignComment;
  205. bool sqlBackslashEscapes;
  206. bool sqlAllowDottedWord;
  207. OptionsSQL() {
  208. fold = false;
  209. foldAtElse = false;
  210. foldComment = false;
  211. foldCompact = false;
  212. foldOnlyBegin = false;
  213. sqlBackticksIdentifier = false;
  214. sqlNumbersignComment = false;
  215. sqlBackslashEscapes = false;
  216. sqlAllowDottedWord = false;
  217. }
  218. };
  219. static const char * const sqlWordListDesc[] = {
  220. "Keywords",
  221. "Database Objects",
  222. "PLDoc",
  223. "SQL*Plus",
  224. "User Keywords 1",
  225. "User Keywords 2",
  226. "User Keywords 3",
  227. "User Keywords 4",
  228. 0
  229. };
  230. struct OptionSetSQL : public OptionSet<OptionsSQL> {
  231. OptionSetSQL() {
  232. DefineProperty("fold", &OptionsSQL::fold);
  233. DefineProperty("fold.sql.at.else", &OptionsSQL::foldAtElse,
  234. "This option enables SQL folding on a \"ELSE\" and \"ELSIF\" line of an IF statement.");
  235. DefineProperty("fold.comment", &OptionsSQL::foldComment);
  236. DefineProperty("fold.compact", &OptionsSQL::foldCompact);
  237. DefineProperty("fold.sql.only.begin", &OptionsSQL::foldOnlyBegin);
  238. DefineProperty("lexer.sql.backticks.identifier", &OptionsSQL::sqlBackticksIdentifier);
  239. DefineProperty("lexer.sql.numbersign.comment", &OptionsSQL::sqlNumbersignComment,
  240. "If \"lexer.sql.numbersign.comment\" property is set to 0 a line beginning with '#' will not be a comment.");
  241. DefineProperty("sql.backslash.escapes", &OptionsSQL::sqlBackslashEscapes,
  242. "Enables backslash as an escape character in SQL.");
  243. DefineProperty("lexer.sql.allow.dotted.word", &OptionsSQL::sqlAllowDottedWord,
  244. "Set to 1 to colourise recognized words with dots "
  245. "(recommended for Oracle PL/SQL objects).");
  246. DefineWordListSets(sqlWordListDesc);
  247. }
  248. };
  249. class LexerSQL : public ILexer {
  250. public :
  251. LexerSQL() {}
  252. virtual ~LexerSQL() {}
  253. int SCI_METHOD Version () const {
  254. return lvOriginal;
  255. }
  256. void SCI_METHOD Release() {
  257. delete this;
  258. }
  259. const char * SCI_METHOD PropertyNames() {
  260. return osSQL.PropertyNames();
  261. }
  262. int SCI_METHOD PropertyType(const char *name) {
  263. return osSQL.PropertyType(name);
  264. }
  265. const char * SCI_METHOD DescribeProperty(const char *name) {
  266. return osSQL.DescribeProperty(name);
  267. }
  268. Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) {
  269. if (osSQL.PropertySet(&options, key, val)) {
  270. return 0;
  271. }
  272. return -1;
  273. }
  274. const char * SCI_METHOD DescribeWordListSets() {
  275. return osSQL.DescribeWordListSets();
  276. }
  277. Sci_Position SCI_METHOD WordListSet(int n, const char *wl);
  278. void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle, IDocument *pAccess);
  279. void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle, IDocument *pAccess);
  280. void * SCI_METHOD PrivateCall(int, void *) {
  281. return 0;
  282. }
  283. static ILexer *LexerFactorySQL() {
  284. return new LexerSQL();
  285. }
  286. private:
  287. bool IsStreamCommentStyle(int style) {
  288. return style == SCE_SQL_COMMENT ||
  289. style == SCE_SQL_COMMENTDOC ||
  290. style == SCE_SQL_COMMENTDOCKEYWORD ||
  291. style == SCE_SQL_COMMENTDOCKEYWORDERROR;
  292. }
  293. bool IsCommentStyle (int style) {
  294. switch (style) {
  295. case SCE_SQL_COMMENT :
  296. case SCE_SQL_COMMENTDOC :
  297. case SCE_SQL_COMMENTLINE :
  298. case SCE_SQL_COMMENTLINEDOC :
  299. case SCE_SQL_COMMENTDOCKEYWORD :
  300. case SCE_SQL_COMMENTDOCKEYWORDERROR :
  301. return true;
  302. default :
  303. return false;
  304. }
  305. }
  306. bool IsCommentLine (Sci_Position line, LexAccessor &styler) {
  307. Sci_Position pos = styler.LineStart(line);
  308. Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
  309. for (Sci_Position i = pos; i + 1 < eol_pos; i++) {
  310. int style = styler.StyleAt(i);
  311. // MySQL needs -- comments to be followed by space or control char
  312. if (style == SCE_SQL_COMMENTLINE && styler.Match(i, "--"))
  313. return true;
  314. else if (!IsASpaceOrTab(styler[i]))
  315. return false;
  316. }
  317. return false;
  318. }
  319. OptionsSQL options;
  320. OptionSetSQL osSQL;
  321. SQLStates sqlStates;
  322. WordList keywords1;
  323. WordList keywords2;
  324. WordList kw_pldoc;
  325. WordList kw_sqlplus;
  326. WordList kw_user1;
  327. WordList kw_user2;
  328. WordList kw_user3;
  329. WordList kw_user4;
  330. };
  331. Sci_Position SCI_METHOD LexerSQL::WordListSet(int n, const char *wl) {
  332. WordList *wordListN = 0;
  333. switch (n) {
  334. case 0:
  335. wordListN = &keywords1;
  336. break;
  337. case 1:
  338. wordListN = &keywords2;
  339. break;
  340. case 2:
  341. wordListN = &kw_pldoc;
  342. break;
  343. case 3:
  344. wordListN = &kw_sqlplus;
  345. break;
  346. case 4:
  347. wordListN = &kw_user1;
  348. break;
  349. case 5:
  350. wordListN = &kw_user2;
  351. break;
  352. case 6:
  353. wordListN = &kw_user3;
  354. break;
  355. case 7:
  356. wordListN = &kw_user4;
  357. }
  358. Sci_Position firstModification = -1;
  359. if (wordListN) {
  360. WordList wlNew;
  361. wlNew.Set(wl);
  362. if (*wordListN != wlNew) {
  363. wordListN->Set(wl);
  364. firstModification = 0;
  365. }
  366. }
  367. return firstModification;
  368. }
  369. void SCI_METHOD LexerSQL::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
  370. LexAccessor styler(pAccess);
  371. StyleContext sc(startPos, length, initStyle, styler);
  372. int styleBeforeDCKeyword = SCE_SQL_DEFAULT;
  373. Sci_Position offset = 0;
  374. for (; sc.More(); sc.Forward(), offset++) {
  375. // Determine if the current state should terminate.
  376. switch (sc.state) {
  377. case SCE_SQL_OPERATOR:
  378. sc.SetState(SCE_SQL_DEFAULT);
  379. break;
  380. case SCE_SQL_NUMBER:
  381. // We stop the number definition on non-numerical non-dot non-eE non-sign char
  382. if (!IsANumberChar(sc.ch, sc.chPrev)) {
  383. sc.SetState(SCE_SQL_DEFAULT);
  384. }
  385. break;
  386. case SCE_SQL_IDENTIFIER:
  387. if (!IsAWordChar(sc.ch, options.sqlAllowDottedWord)) {
  388. int nextState = SCE_SQL_DEFAULT;
  389. char s[1000];
  390. sc.GetCurrentLowered(s, sizeof(s));
  391. if (keywords1.InList(s)) {
  392. sc.ChangeState(SCE_SQL_WORD);
  393. } else if (keywords2.InList(s)) {
  394. sc.ChangeState(SCE_SQL_WORD2);
  395. } else if (kw_sqlplus.InListAbbreviated(s, '~')) {
  396. sc.ChangeState(SCE_SQL_SQLPLUS);
  397. if (strncmp(s, "rem", 3) == 0) {
  398. nextState = SCE_SQL_SQLPLUS_COMMENT;
  399. } else if (strncmp(s, "pro", 3) == 0) {
  400. nextState = SCE_SQL_SQLPLUS_PROMPT;
  401. }
  402. } else if (kw_user1.InList(s)) {
  403. sc.ChangeState(SCE_SQL_USER1);
  404. } else if (kw_user2.InList(s)) {
  405. sc.ChangeState(SCE_SQL_USER2);
  406. } else if (kw_user3.InList(s)) {
  407. sc.ChangeState(SCE_SQL_USER3);
  408. } else if (kw_user4.InList(s)) {
  409. sc.ChangeState(SCE_SQL_USER4);
  410. }
  411. sc.SetState(nextState);
  412. }
  413. break;
  414. case SCE_SQL_QUOTEDIDENTIFIER:
  415. if (sc.ch == 0x60) {
  416. if (sc.chNext == 0x60) {
  417. sc.Forward(); // Ignore it
  418. } else {
  419. sc.ForwardSetState(SCE_SQL_DEFAULT);
  420. }
  421. }
  422. break;
  423. case SCE_SQL_COMMENT:
  424. if (sc.Match('*', '/')) {
  425. sc.Forward();
  426. sc.ForwardSetState(SCE_SQL_DEFAULT);
  427. }
  428. break;
  429. case SCE_SQL_COMMENTDOC:
  430. if (sc.Match('*', '/')) {
  431. sc.Forward();
  432. sc.ForwardSetState(SCE_SQL_DEFAULT);
  433. } else if (sc.ch == '@' || sc.ch == '\\') { // Doxygen support
  434. // Verify that we have the conditions to mark a comment-doc-keyword
  435. if ((IsASpace(sc.chPrev) || sc.chPrev == '*') && (!IsASpace(sc.chNext))) {
  436. styleBeforeDCKeyword = SCE_SQL_COMMENTDOC;
  437. sc.SetState(SCE_SQL_COMMENTDOCKEYWORD);
  438. }
  439. }
  440. break;
  441. case SCE_SQL_COMMENTLINE:
  442. case SCE_SQL_COMMENTLINEDOC:
  443. case SCE_SQL_SQLPLUS_COMMENT:
  444. case SCE_SQL_SQLPLUS_PROMPT:
  445. if (sc.atLineStart) {
  446. sc.SetState(SCE_SQL_DEFAULT);
  447. }
  448. break;
  449. case SCE_SQL_COMMENTDOCKEYWORD:
  450. if ((styleBeforeDCKeyword == SCE_SQL_COMMENTDOC) && sc.Match('*', '/')) {
  451. sc.ChangeState(SCE_SQL_COMMENTDOCKEYWORDERROR);
  452. sc.Forward();
  453. sc.ForwardSetState(SCE_SQL_DEFAULT);
  454. } else if (!IsADoxygenChar(sc.ch)) {
  455. char s[100];
  456. sc.GetCurrentLowered(s, sizeof(s));
  457. if (!isspace(sc.ch) || !kw_pldoc.InList(s + 1)) {
  458. sc.ChangeState(SCE_SQL_COMMENTDOCKEYWORDERROR);
  459. }
  460. sc.SetState(styleBeforeDCKeyword);
  461. }
  462. break;
  463. case SCE_SQL_CHARACTER:
  464. if (options.sqlBackslashEscapes && sc.ch == '\\') {
  465. sc.Forward();
  466. } else if (sc.ch == '\'') {
  467. if (sc.chNext == '\"') {
  468. sc.Forward();
  469. } else {
  470. sc.ForwardSetState(SCE_SQL_DEFAULT);
  471. }
  472. }
  473. break;
  474. case SCE_SQL_STRING:
  475. if (sc.ch == '\\') {
  476. // Escape sequence
  477. sc.Forward();
  478. } else if (sc.ch == '\"') {
  479. if (sc.chNext == '\"') {
  480. sc.Forward();
  481. } else {
  482. sc.ForwardSetState(SCE_SQL_DEFAULT);
  483. }
  484. }
  485. break;
  486. case SCE_SQL_QOPERATOR:
  487. // Locate the unique Q operator character
  488. sc.Complete();
  489. char qOperator = 0x00;
  490. for (Sci_Position styleStartPos = sc.currentPos; styleStartPos > 0; --styleStartPos) {
  491. if (styler.StyleAt(styleStartPos - 1) != SCE_SQL_QOPERATOR) {
  492. qOperator = styler.SafeGetCharAt(styleStartPos + 2);
  493. break;
  494. }
  495. }
  496. char qComplement = 0x00;
  497. if (qOperator == '<') {
  498. qComplement = '>';
  499. } else if (qOperator == '(') {
  500. qComplement = ')';
  501. } else if (qOperator == '{') {
  502. qComplement = '}';
  503. } else if (qOperator == '[') {
  504. qComplement = ']';
  505. } else {
  506. qComplement = qOperator;
  507. }
  508. if (sc.Match(qComplement, '\'')) {
  509. sc.Forward();
  510. sc.ForwardSetState(SCE_SQL_DEFAULT);
  511. }
  512. break;
  513. }
  514. // Determine if a new state should be entered.
  515. if (sc.state == SCE_SQL_DEFAULT) {
  516. if (sc.Match('q', '\'') || sc.Match('Q', '\'')) {
  517. sc.SetState(SCE_SQL_QOPERATOR);
  518. sc.Forward();
  519. } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext)) ||
  520. ((sc.ch == '-' || sc.ch == '+') && IsADigit(sc.chNext) && !IsADigit(sc.chPrev))) {
  521. sc.SetState(SCE_SQL_NUMBER);
  522. } else if (IsAWordStart(sc.ch)) {
  523. sc.SetState(SCE_SQL_IDENTIFIER);
  524. } else if (sc.ch == 0x60 && options.sqlBackticksIdentifier) {
  525. sc.SetState(SCE_SQL_QUOTEDIDENTIFIER);
  526. } else if (sc.Match('/', '*')) {
  527. if (sc.Match("/**") || sc.Match("/*!")) { // Support of Doxygen doc. style
  528. sc.SetState(SCE_SQL_COMMENTDOC);
  529. } else {
  530. sc.SetState(SCE_SQL_COMMENT);
  531. }
  532. sc.Forward(); // Eat the * so it isn't used for the end of the comment
  533. } else if (sc.Match('-', '-')) {
  534. // MySQL requires a space or control char after --
  535. // http://dev.mysql.com/doc/mysql/en/ansi-diff-comments.html
  536. // Perhaps we should enforce that with proper property:
  537. //~ } else if (sc.Match("-- ")) {
  538. sc.SetState(SCE_SQL_COMMENTLINE);
  539. } else if (sc.ch == '#' && options.sqlNumbersignComment) {
  540. sc.SetState(SCE_SQL_COMMENTLINEDOC);
  541. } else if (sc.ch == '\'') {
  542. sc.SetState(SCE_SQL_CHARACTER);
  543. } else if (sc.ch == '\"') {
  544. sc.SetState(SCE_SQL_STRING);
  545. } else if (isoperator(static_cast<char>(sc.ch))) {
  546. sc.SetState(SCE_SQL_OPERATOR);
  547. }
  548. }
  549. }
  550. sc.Complete();
  551. }
  552. void SCI_METHOD LexerSQL::Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
  553. if (!options.fold)
  554. return;
  555. LexAccessor styler(pAccess);
  556. Sci_PositionU endPos = startPos + length;
  557. int visibleChars = 0;
  558. Sci_Position lineCurrent = styler.GetLine(startPos);
  559. int levelCurrent = SC_FOLDLEVELBASE;
  560. if (lineCurrent > 0) {
  561. // Backtrack to previous line in case need to fix its fold status for folding block of single-line comments (i.e. '--').
  562. Sci_Position lastNLPos = -1;
  563. // And keep going back until we find an operator ';' followed
  564. // by white-space and/or comments. This will improve folding.
  565. while (--startPos > 0) {
  566. char ch = styler[startPos];
  567. if (ch == '\n' || (ch == '\r' && styler[startPos + 1] != '\n')) {
  568. lastNLPos = startPos;
  569. } else if (ch == ';' &&
  570. styler.StyleAt(startPos) == SCE_SQL_OPERATOR) {
  571. bool isAllClear = true;
  572. for (Sci_Position tempPos = startPos + 1;
  573. tempPos < lastNLPos;
  574. ++tempPos) {
  575. int tempStyle = styler.StyleAt(tempPos);
  576. if (!IsCommentStyle(tempStyle)
  577. && tempStyle != SCE_SQL_DEFAULT) {
  578. isAllClear = false;
  579. break;
  580. }
  581. }
  582. if (isAllClear) {
  583. startPos = lastNLPos + 1;
  584. break;
  585. }
  586. }
  587. }
  588. lineCurrent = styler.GetLine(startPos);
  589. if (lineCurrent > 0)
  590. levelCurrent = styler.LevelAt(lineCurrent - 1) >> 16;
  591. }
  592. // And because folding ends at ';', keep going until we find one
  593. // Otherwise if create ... view ... as is split over multiple
  594. // lines the folding won't always update immediately.
  595. Sci_PositionU docLength = styler.Length();
  596. for (; endPos < docLength; ++endPos) {
  597. if (styler.SafeGetCharAt(endPos) == ';') {
  598. break;
  599. }
  600. }
  601. int levelNext = levelCurrent;
  602. char chNext = styler[startPos];
  603. int styleNext = styler.StyleAt(startPos);
  604. int style = initStyle;
  605. bool endFound = false;
  606. bool isUnfoldingIgnored = false;
  607. // this statementFound flag avoids to fold when the statement is on only one line by ignoring ELSE or ELSIF
  608. // eg. "IF condition1 THEN ... ELSIF condition2 THEN ... ELSE ... END IF;"
  609. bool statementFound = false;
  610. sql_state_t sqlStatesCurrentLine = 0;
  611. if (!options.foldOnlyBegin) {
  612. sqlStatesCurrentLine = sqlStates.ForLine(lineCurrent);
  613. }
  614. for (Sci_PositionU i = startPos; i < endPos; i++) {
  615. char ch = chNext;
  616. chNext = styler.SafeGetCharAt(i + 1);
  617. int stylePrev = style;
  618. style = styleNext;
  619. styleNext = styler.StyleAt(i + 1);
  620. bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
  621. if (atEOL || (!IsCommentStyle(style) && ch == ';')) {
  622. if (endFound) {
  623. //Maybe this is the end of "EXCEPTION" BLOCK (eg. "BEGIN ... EXCEPTION ... END;")
  624. sqlStatesCurrentLine = sqlStates.IntoExceptionBlock(sqlStatesCurrentLine, false);
  625. }
  626. // set endFound and isUnfoldingIgnored to false if EOL is reached or ';' is found
  627. endFound = false;
  628. isUnfoldingIgnored = false;
  629. }
  630. if ((!IsCommentStyle(style) && ch == ';')) {
  631. if (sqlStates.IsIntoMergeStatement(sqlStatesCurrentLine)) {
  632. // This is the end of "MERGE" statement.
  633. if (!sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine))
  634. levelNext--;
  635. sqlStatesCurrentLine = sqlStates.IntoMergeStatement(sqlStatesCurrentLine, false);
  636. levelNext--;
  637. }
  638. if (sqlStates.IsIntoSelectStatementOrAssignment(sqlStatesCurrentLine))
  639. sqlStatesCurrentLine = sqlStates.IntoSelectStatementOrAssignment(sqlStatesCurrentLine, false);
  640. if (sqlStates.IsIntoCreateStatement(sqlStatesCurrentLine)) {
  641. if (sqlStates.IsIntoCreateViewStatement(sqlStatesCurrentLine)) {
  642. if (sqlStates.IsIntoCreateViewAsStatement(sqlStatesCurrentLine)) {
  643. levelNext--;
  644. sqlStatesCurrentLine = sqlStates.IntoCreateViewAsStatement(sqlStatesCurrentLine, false);
  645. }
  646. sqlStatesCurrentLine = sqlStates.IntoCreateViewStatement(sqlStatesCurrentLine, false);
  647. }
  648. sqlStatesCurrentLine = sqlStates.IntoCreateStatement(sqlStatesCurrentLine, false);
  649. }
  650. }
  651. if (ch == ':' && chNext == '=' && !IsCommentStyle(style))
  652. sqlStatesCurrentLine = sqlStates.IntoSelectStatementOrAssignment(sqlStatesCurrentLine, true);
  653. if (options.foldComment && IsStreamCommentStyle(style)) {
  654. if (!IsStreamCommentStyle(stylePrev)) {
  655. levelNext++;
  656. } else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
  657. // Comments don't end at end of line and the next character may be unstyled.
  658. levelNext--;
  659. }
  660. }
  661. if (options.foldComment && (style == SCE_SQL_COMMENTLINE)) {
  662. // MySQL needs -- comments to be followed by space or control char
  663. if ((ch == '-') && (chNext == '-')) {
  664. char chNext2 = styler.SafeGetCharAt(i + 2);
  665. char chNext3 = styler.SafeGetCharAt(i + 3);
  666. if (chNext2 == '{' || chNext3 == '{') {
  667. levelNext++;
  668. } else if (chNext2 == '}' || chNext3 == '}') {
  669. levelNext--;
  670. }
  671. }
  672. }
  673. // Fold block of single-line comments (i.e. '--').
  674. if (options.foldComment && atEOL && IsCommentLine(lineCurrent, styler)) {
  675. if (!IsCommentLine(lineCurrent - 1, styler) && IsCommentLine(lineCurrent + 1, styler))
  676. levelNext++;
  677. else if (IsCommentLine(lineCurrent - 1, styler) && !IsCommentLine(lineCurrent + 1, styler))
  678. levelNext--;
  679. }
  680. if (style == SCE_SQL_OPERATOR) {
  681. if (ch == '(') {
  682. if (levelCurrent > levelNext)
  683. levelCurrent--;
  684. levelNext++;
  685. } else if (ch == ')') {
  686. levelNext--;
  687. } else if ((!options.foldOnlyBegin) && ch == ';') {
  688. sqlStatesCurrentLine = sqlStates.IgnoreWhen(sqlStatesCurrentLine, false);
  689. }
  690. }
  691. // If new keyword (cannot trigger on elseif or nullif, does less tests)
  692. if (style == SCE_SQL_WORD && stylePrev != SCE_SQL_WORD) {
  693. const int MAX_KW_LEN = 9; // Maximum length of folding keywords
  694. char s[MAX_KW_LEN + 2];
  695. unsigned int j = 0;
  696. for (; j < MAX_KW_LEN + 1; j++) {
  697. if (!iswordchar(styler[i + j])) {
  698. break;
  699. }
  700. s[j] = static_cast<char>(tolower(styler[i + j]));
  701. }
  702. if (j == MAX_KW_LEN + 1) {
  703. // Keyword too long, don't test it
  704. s[0] = '\0';
  705. } else {
  706. s[j] = '\0';
  707. }
  708. if (!options.foldOnlyBegin &&
  709. strcmp(s, "select") == 0) {
  710. sqlStatesCurrentLine = sqlStates.IntoSelectStatementOrAssignment(sqlStatesCurrentLine, true);
  711. } else if (strcmp(s, "if") == 0) {
  712. if (endFound) {
  713. endFound = false;
  714. if (options.foldOnlyBegin && !isUnfoldingIgnored) {
  715. // this end isn't for begin block, but for if block ("end if;")
  716. // so ignore previous "end" by increment levelNext.
  717. levelNext++;
  718. }
  719. } else {
  720. if (!options.foldOnlyBegin)
  721. sqlStatesCurrentLine = sqlStates.IntoCondition(sqlStatesCurrentLine, true);
  722. if (levelCurrent > levelNext) {
  723. // doesn't include this line into the folding block
  724. // because doesn't hide IF (eg "END; IF")
  725. levelCurrent = levelNext;
  726. }
  727. }
  728. } else if (!options.foldOnlyBegin &&
  729. strcmp(s, "then") == 0 &&
  730. sqlStates.IsIntoCondition(sqlStatesCurrentLine)) {
  731. sqlStatesCurrentLine = sqlStates.IntoCondition(sqlStatesCurrentLine, false);
  732. if (!options.foldOnlyBegin) {
  733. if (levelCurrent > levelNext) {
  734. levelCurrent = levelNext;
  735. }
  736. if (!statementFound)
  737. levelNext++;
  738. statementFound = true;
  739. } else if (levelCurrent > levelNext) {
  740. // doesn't include this line into the folding block
  741. // because doesn't hide LOOP or CASE (eg "END; LOOP" or "END; CASE")
  742. levelCurrent = levelNext;
  743. }
  744. } else if (strcmp(s, "loop") == 0 ||
  745. strcmp(s, "case") == 0) {
  746. if (endFound) {
  747. endFound = false;
  748. if (options.foldOnlyBegin && !isUnfoldingIgnored) {
  749. // this end isn't for begin block, but for loop block ("end loop;") or case block ("end case;")
  750. // so ignore previous "end" by increment levelNext.
  751. levelNext++;
  752. }
  753. if ((!options.foldOnlyBegin) && strcmp(s, "case") == 0) {
  754. sqlStatesCurrentLine = sqlStates.EndCaseBlock(sqlStatesCurrentLine);
  755. if (!sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine))
  756. levelNext--; //again for the "end case;" and block when
  757. }
  758. } else if (!options.foldOnlyBegin) {
  759. if (strcmp(s, "case") == 0) {
  760. sqlStatesCurrentLine = sqlStates.BeginCaseBlock(sqlStatesCurrentLine);
  761. sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, true);
  762. }
  763. if (levelCurrent > levelNext)
  764. levelCurrent = levelNext;
  765. if (!statementFound)
  766. levelNext++;
  767. statementFound = true;
  768. } else if (levelCurrent > levelNext) {
  769. // doesn't include this line into the folding block
  770. // because doesn't hide LOOP or CASE (eg "END; LOOP" or "END; CASE")
  771. levelCurrent = levelNext;
  772. }
  773. } else if ((!options.foldOnlyBegin) && (
  774. // folding for ELSE and ELSIF block only if foldAtElse is set
  775. // and IF or CASE aren't on only one line with ELSE or ELSIF (with flag statementFound)
  776. options.foldAtElse && !statementFound) && strcmp(s, "elsif") == 0) {
  777. sqlStatesCurrentLine = sqlStates.IntoCondition(sqlStatesCurrentLine, true);
  778. levelCurrent--;
  779. levelNext--;
  780. } else if ((!options.foldOnlyBegin) && (
  781. // folding for ELSE and ELSIF block only if foldAtElse is set
  782. // and IF or CASE aren't on only one line with ELSE or ELSIF (with flag statementFound)
  783. options.foldAtElse && !statementFound) && strcmp(s, "else") == 0) {
  784. // prevent also ELSE is on the same line (eg. "ELSE ... END IF;")
  785. statementFound = true;
  786. if (sqlStates.IsIntoCaseBlock(sqlStatesCurrentLine) && sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine)) {
  787. sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, false);
  788. levelNext++;
  789. } else {
  790. // we are in same case "} ELSE {" in C language
  791. levelCurrent--;
  792. }
  793. } else if (strcmp(s, "begin") == 0) {
  794. levelNext++;
  795. sqlStatesCurrentLine = sqlStates.IntoDeclareBlock(sqlStatesCurrentLine, false);
  796. } else if ((strcmp(s, "end") == 0) ||
  797. // SQL Anywhere permits IF ... ELSE ... ENDIF
  798. // will only be active if "endif" appears in the
  799. // keyword list.
  800. (strcmp(s, "endif") == 0)) {
  801. endFound = true;
  802. levelNext--;
  803. if (sqlStates.IsIntoSelectStatementOrAssignment(sqlStatesCurrentLine) && !sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine))
  804. levelNext--;
  805. if (levelNext < SC_FOLDLEVELBASE) {
  806. levelNext = SC_FOLDLEVELBASE;
  807. isUnfoldingIgnored = true;
  808. }
  809. } else if ((!options.foldOnlyBegin) &&
  810. strcmp(s, "when") == 0 &&
  811. !sqlStates.IsIgnoreWhen(sqlStatesCurrentLine) &&
  812. !sqlStates.IsIntoExceptionBlock(sqlStatesCurrentLine) && (
  813. sqlStates.IsIntoCaseBlock(sqlStatesCurrentLine) ||
  814. sqlStates.IsIntoMergeStatement(sqlStatesCurrentLine)
  815. )
  816. ) {
  817. sqlStatesCurrentLine = sqlStates.IntoCondition(sqlStatesCurrentLine, true);
  818. // Don't foldind when CASE and WHEN are on the same line (with flag statementFound) (eg. "CASE selector WHEN expression1 THEN sequence_of_statements1;\n")
  819. // and same way for MERGE statement.
  820. if (!statementFound) {
  821. if (!sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine)) {
  822. levelCurrent--;
  823. levelNext--;
  824. }
  825. sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, false);
  826. }
  827. } else if ((!options.foldOnlyBegin) && strcmp(s, "exit") == 0) {
  828. sqlStatesCurrentLine = sqlStates.IgnoreWhen(sqlStatesCurrentLine, true);
  829. } else if ((!options.foldOnlyBegin) && !sqlStates.IsIntoDeclareBlock(sqlStatesCurrentLine) && strcmp(s, "exception") == 0) {
  830. sqlStatesCurrentLine = sqlStates.IntoExceptionBlock(sqlStatesCurrentLine, true);
  831. } else if ((!options.foldOnlyBegin) &&
  832. (strcmp(s, "declare") == 0 ||
  833. strcmp(s, "function") == 0 ||
  834. strcmp(s, "procedure") == 0 ||
  835. strcmp(s, "package") == 0)) {
  836. sqlStatesCurrentLine = sqlStates.IntoDeclareBlock(sqlStatesCurrentLine, true);
  837. } else if ((!options.foldOnlyBegin) &&
  838. strcmp(s, "merge") == 0) {
  839. sqlStatesCurrentLine = sqlStates.IntoMergeStatement(sqlStatesCurrentLine, true);
  840. sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, true);
  841. levelNext++;
  842. statementFound = true;
  843. } else if ((!options.foldOnlyBegin) &&
  844. strcmp(s, "create") == 0) {
  845. sqlStatesCurrentLine = sqlStates.IntoCreateStatement(sqlStatesCurrentLine, true);
  846. } else if ((!options.foldOnlyBegin) &&
  847. strcmp(s, "view") == 0 &&
  848. sqlStates.IsIntoCreateStatement(sqlStatesCurrentLine)) {
  849. sqlStatesCurrentLine = sqlStates.IntoCreateViewStatement(sqlStatesCurrentLine, true);
  850. } else if ((!options.foldOnlyBegin) &&
  851. strcmp(s, "as") == 0 &&
  852. sqlStates.IsIntoCreateViewStatement(sqlStatesCurrentLine) &&
  853. ! sqlStates.IsIntoCreateViewAsStatement(sqlStatesCurrentLine)) {
  854. sqlStatesCurrentLine = sqlStates.IntoCreateViewAsStatement(sqlStatesCurrentLine, true);
  855. levelNext++;
  856. }
  857. }
  858. if (atEOL) {
  859. int levelUse = levelCurrent;
  860. int lev = levelUse | levelNext << 16;
  861. if (visibleChars == 0 && options.foldCompact)
  862. lev |= SC_FOLDLEVELWHITEFLAG;
  863. if (levelUse < levelNext)
  864. lev |= SC_FOLDLEVELHEADERFLAG;
  865. if (lev != styler.LevelAt(lineCurrent)) {
  866. styler.SetLevel(lineCurrent, lev);
  867. }
  868. lineCurrent++;
  869. levelCurrent = levelNext;
  870. visibleChars = 0;
  871. statementFound = false;
  872. if (!options.foldOnlyBegin)
  873. sqlStates.Set(lineCurrent, sqlStatesCurrentLine);
  874. }
  875. if (!isspacechar(ch)) {
  876. visibleChars++;
  877. }
  878. }
  879. }
  880. LexerModule lmSQL(SCLEX_SQL, LexerSQL::LexerFactorySQL, "sql", sqlWordListDesc);