Update Scintilla to 3.5.1 pre-release
[geany-mirror.git] / scintilla / lexers / LexSQL.cxx
blobb9f15ea1a606fc9eb571c057bed47e4e64cb82f4
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.
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdarg.h>
14 #include <assert.h>
15 #include <ctype.h>
17 #include <string>
18 #include <vector>
19 #include <map>
20 #include <algorithm>
22 #include "ILexer.h"
23 #include "Scintilla.h"
24 #include "SciLexer.h"
26 #include "WordList.h"
27 #include "LexAccessor.h"
28 #include "Accessor.h"
29 #include "StyleContext.h"
30 #include "CharacterSet.h"
31 #include "LexerModule.h"
32 #include "OptionSet.h"
33 #include "SparseState.h"
35 #ifdef SCI_NAMESPACE
36 using namespace Scintilla;
37 #endif
39 static inline bool IsAWordChar(int ch, bool sqlAllowDottedWord) {
40 if (!sqlAllowDottedWord)
41 return (ch < 0x80) && (isalnum(ch) || ch == '_');
42 else
43 return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '.');
46 static inline bool IsAWordStart(int ch) {
47 return (ch < 0x80) && (isalpha(ch) || ch == '_');
50 static inline bool IsADoxygenChar(int ch) {
51 return (islower(ch) || ch == '$' || ch == '@' ||
52 ch == '\\' || ch == '&' || ch == '<' ||
53 ch == '>' || ch == '#' || ch == '{' ||
54 ch == '}' || ch == '[' || ch == ']');
57 static inline bool IsANumberChar(int ch) {
58 // Not exactly following number definition (several dots are seen as OK, etc.)
59 // but probably enough in most cases.
60 return (ch < 0x80) &&
61 (isdigit(ch) || toupper(ch) == 'E' ||
62 ch == '.' || ch == '-' || ch == '+');
65 typedef unsigned int sql_state_t;
67 class SQLStates {
68 public :
69 void Set(int lineNumber, unsigned short int sqlStatesLine) {
70 sqlStatement.Set(lineNumber, sqlStatesLine);
73 sql_state_t IgnoreWhen (sql_state_t sqlStatesLine, bool enable) {
74 if (enable)
75 sqlStatesLine |= MASK_IGNORE_WHEN;
76 else
77 sqlStatesLine &= ~MASK_IGNORE_WHEN;
79 return sqlStatesLine;
82 sql_state_t IntoCondition (sql_state_t sqlStatesLine, bool enable) {
83 if (enable)
84 sqlStatesLine |= MASK_INTO_CONDITION;
85 else
86 sqlStatesLine &= ~MASK_INTO_CONDITION;
88 return sqlStatesLine;
91 sql_state_t IntoExceptionBlock (sql_state_t sqlStatesLine, bool enable) {
92 if (enable)
93 sqlStatesLine |= MASK_INTO_EXCEPTION;
94 else
95 sqlStatesLine &= ~MASK_INTO_EXCEPTION;
97 return sqlStatesLine;
100 sql_state_t IntoDeclareBlock (sql_state_t sqlStatesLine, bool enable) {
101 if (enable)
102 sqlStatesLine |= MASK_INTO_DECLARE;
103 else
104 sqlStatesLine &= ~MASK_INTO_DECLARE;
106 return sqlStatesLine;
109 sql_state_t IntoMergeStatement (sql_state_t sqlStatesLine, bool enable) {
110 if (enable)
111 sqlStatesLine |= MASK_MERGE_STATEMENT;
112 else
113 sqlStatesLine &= ~MASK_MERGE_STATEMENT;
115 return sqlStatesLine;
118 sql_state_t CaseMergeWithoutWhenFound (sql_state_t sqlStatesLine, bool found) {
119 if (found)
120 sqlStatesLine |= MASK_CASE_MERGE_WITHOUT_WHEN_FOUND;
121 else
122 sqlStatesLine &= ~MASK_CASE_MERGE_WITHOUT_WHEN_FOUND;
124 return sqlStatesLine;
126 sql_state_t IntoSelectStatementOrAssignment (sql_state_t sqlStatesLine, bool found) {
127 if (found)
128 sqlStatesLine |= MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT;
129 else
130 sqlStatesLine &= ~MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT;
131 return sqlStatesLine;
134 sql_state_t BeginCaseBlock (sql_state_t sqlStatesLine) {
135 if ((sqlStatesLine & MASK_NESTED_CASES) < MASK_NESTED_CASES) {
136 sqlStatesLine++;
138 return sqlStatesLine;
141 sql_state_t EndCaseBlock (sql_state_t sqlStatesLine) {
142 if ((sqlStatesLine & MASK_NESTED_CASES) > 0) {
143 sqlStatesLine--;
145 return sqlStatesLine;
148 sql_state_t IntoCreateStatement (sql_state_t sqlStatesLine, bool enable) {
149 if (enable)
150 sqlStatesLine |= MASK_INTO_CREATE;
151 else
152 sqlStatesLine &= ~MASK_INTO_CREATE;
154 return sqlStatesLine;
157 sql_state_t IntoCreateViewStatement (sql_state_t sqlStatesLine, bool enable) {
158 if (enable)
159 sqlStatesLine |= MASK_INTO_CREATE_VIEW;
160 else
161 sqlStatesLine &= ~MASK_INTO_CREATE_VIEW;
163 return sqlStatesLine;
166 sql_state_t IntoCreateViewAsStatement (sql_state_t sqlStatesLine, bool enable) {
167 if (enable)
168 sqlStatesLine |= MASK_INTO_CREATE_VIEW_AS_STATEMENT;
169 else
170 sqlStatesLine &= ~MASK_INTO_CREATE_VIEW_AS_STATEMENT;
172 return sqlStatesLine;
175 bool IsIgnoreWhen (sql_state_t sqlStatesLine) {
176 return (sqlStatesLine & MASK_IGNORE_WHEN) != 0;
179 bool IsIntoCondition (sql_state_t sqlStatesLine) {
180 return (sqlStatesLine & MASK_INTO_CONDITION) != 0;
183 bool IsIntoCaseBlock (sql_state_t sqlStatesLine) {
184 return (sqlStatesLine & MASK_NESTED_CASES) != 0;
187 bool IsIntoExceptionBlock (sql_state_t sqlStatesLine) {
188 return (sqlStatesLine & MASK_INTO_EXCEPTION) != 0;
190 bool IsIntoSelectStatementOrAssignment (sql_state_t sqlStatesLine) {
191 return (sqlStatesLine & MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT) != 0;
193 bool IsCaseMergeWithoutWhenFound (sql_state_t sqlStatesLine) {
194 return (sqlStatesLine & MASK_CASE_MERGE_WITHOUT_WHEN_FOUND) != 0;
197 bool IsIntoDeclareBlock (sql_state_t sqlStatesLine) {
198 return (sqlStatesLine & MASK_INTO_DECLARE) != 0;
201 bool IsIntoMergeStatement (sql_state_t sqlStatesLine) {
202 return (sqlStatesLine & MASK_MERGE_STATEMENT) != 0;
205 bool IsIntoCreateStatement (sql_state_t sqlStatesLine) {
206 return (sqlStatesLine & MASK_INTO_CREATE) != 0;
209 bool IsIntoCreateViewStatement (sql_state_t sqlStatesLine) {
210 return (sqlStatesLine & MASK_INTO_CREATE_VIEW) != 0;
213 bool IsIntoCreateViewAsStatement (sql_state_t sqlStatesLine) {
214 return (sqlStatesLine & MASK_INTO_CREATE_VIEW_AS_STATEMENT) != 0;
217 sql_state_t ForLine(int lineNumber) {
218 return sqlStatement.ValueAt(lineNumber);
221 SQLStates() {}
223 private :
224 SparseState <sql_state_t> sqlStatement;
225 enum {
226 MASK_NESTED_CASES = 0x0001FF,
227 MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT = 0x000200,
228 MASK_CASE_MERGE_WITHOUT_WHEN_FOUND = 0x000400,
229 MASK_MERGE_STATEMENT = 0x000800,
230 MASK_INTO_DECLARE = 0x001000,
231 MASK_INTO_EXCEPTION = 0x002000,
232 MASK_INTO_CONDITION = 0x004000,
233 MASK_IGNORE_WHEN = 0x008000,
234 MASK_INTO_CREATE = 0x010000,
235 MASK_INTO_CREATE_VIEW = 0x020000,
236 MASK_INTO_CREATE_VIEW_AS_STATEMENT = 0x040000
240 // Options used for LexerSQL
241 struct OptionsSQL {
242 bool fold;
243 bool foldAtElse;
244 bool foldComment;
245 bool foldCompact;
246 bool foldOnlyBegin;
247 bool sqlBackticksIdentifier;
248 bool sqlNumbersignComment;
249 bool sqlBackslashEscapes;
250 bool sqlAllowDottedWord;
251 OptionsSQL() {
252 fold = false;
253 foldAtElse = false;
254 foldComment = false;
255 foldCompact = false;
256 foldOnlyBegin = false;
257 sqlBackticksIdentifier = false;
258 sqlNumbersignComment = false;
259 sqlBackslashEscapes = false;
260 sqlAllowDottedWord = false;
264 static const char * const sqlWordListDesc[] = {
265 "Keywords",
266 "Database Objects",
267 "PLDoc",
268 "SQL*Plus",
269 "User Keywords 1",
270 "User Keywords 2",
271 "User Keywords 3",
272 "User Keywords 4",
276 struct OptionSetSQL : public OptionSet<OptionsSQL> {
277 OptionSetSQL() {
278 DefineProperty("fold", &OptionsSQL::fold);
280 DefineProperty("fold.sql.at.else", &OptionsSQL::foldAtElse,
281 "This option enables SQL folding on a \"ELSE\" and \"ELSIF\" line of an IF statement.");
283 DefineProperty("fold.comment", &OptionsSQL::foldComment);
285 DefineProperty("fold.compact", &OptionsSQL::foldCompact);
287 DefineProperty("fold.sql.only.begin", &OptionsSQL::foldOnlyBegin);
289 DefineProperty("lexer.sql.backticks.identifier", &OptionsSQL::sqlBackticksIdentifier);
291 DefineProperty("lexer.sql.numbersign.comment", &OptionsSQL::sqlNumbersignComment,
292 "If \"lexer.sql.numbersign.comment\" property is set to 0 a line beginning with '#' will not be a comment.");
294 DefineProperty("sql.backslash.escapes", &OptionsSQL::sqlBackslashEscapes,
295 "Enables backslash as an escape character in SQL.");
297 DefineProperty("lexer.sql.allow.dotted.word", &OptionsSQL::sqlAllowDottedWord,
298 "Set to 1 to colourise recognized words with dots "
299 "(recommended for Oracle PL/SQL objects).");
301 DefineWordListSets(sqlWordListDesc);
305 class LexerSQL : public ILexer {
306 public :
307 LexerSQL() {}
309 virtual ~LexerSQL() {}
311 int SCI_METHOD Version () const {
312 return lvOriginal;
315 void SCI_METHOD Release() {
316 delete this;
319 const char * SCI_METHOD PropertyNames() {
320 return osSQL.PropertyNames();
323 int SCI_METHOD PropertyType(const char *name) {
324 return osSQL.PropertyType(name);
327 const char * SCI_METHOD DescribeProperty(const char *name) {
328 return osSQL.DescribeProperty(name);
331 int SCI_METHOD PropertySet(const char *key, const char *val) {
332 if (osSQL.PropertySet(&options, key, val)) {
333 return 0;
335 return -1;
338 const char * SCI_METHOD DescribeWordListSets() {
339 return osSQL.DescribeWordListSets();
342 int SCI_METHOD WordListSet(int n, const char *wl);
343 void SCI_METHOD Lex (unsigned int startPos, int lengthDoc, int initStyle, IDocument *pAccess);
344 void SCI_METHOD Fold(unsigned int startPos, int lengthDoc, int initStyle, IDocument *pAccess);
346 void * SCI_METHOD PrivateCall(int, void *) {
347 return 0;
350 static ILexer *LexerFactorySQL() {
351 return new LexerSQL();
353 private:
354 bool IsStreamCommentStyle(int style) {
355 return style == SCE_SQL_COMMENT ||
356 style == SCE_SQL_COMMENTDOC ||
357 style == SCE_SQL_COMMENTDOCKEYWORD ||
358 style == SCE_SQL_COMMENTDOCKEYWORDERROR;
361 bool IsCommentStyle (int style) {
362 switch (style) {
363 case SCE_SQL_COMMENT :
364 case SCE_SQL_COMMENTDOC :
365 case SCE_SQL_COMMENTLINE :
366 case SCE_SQL_COMMENTLINEDOC :
367 case SCE_SQL_COMMENTDOCKEYWORD :
368 case SCE_SQL_COMMENTDOCKEYWORDERROR :
369 return true;
370 default :
371 return false;
375 bool IsCommentLine (int line, LexAccessor &styler) {
376 int pos = styler.LineStart(line);
377 int eol_pos = styler.LineStart(line + 1) - 1;
378 for (int i = pos; i + 1 < eol_pos; i++) {
379 int style = styler.StyleAt(i);
380 // MySQL needs -- comments to be followed by space or control char
381 if (style == SCE_SQL_COMMENTLINE && styler.Match(i, "--"))
382 return true;
383 else if (!IsASpaceOrTab(styler[i]))
384 return false;
386 return false;
389 OptionsSQL options;
390 OptionSetSQL osSQL;
391 SQLStates sqlStates;
393 WordList keywords1;
394 WordList keywords2;
395 WordList kw_pldoc;
396 WordList kw_sqlplus;
397 WordList kw_user1;
398 WordList kw_user2;
399 WordList kw_user3;
400 WordList kw_user4;
403 int SCI_METHOD LexerSQL::WordListSet(int n, const char *wl) {
404 WordList *wordListN = 0;
405 switch (n) {
406 case 0:
407 wordListN = &keywords1;
408 break;
409 case 1:
410 wordListN = &keywords2;
411 break;
412 case 2:
413 wordListN = &kw_pldoc;
414 break;
415 case 3:
416 wordListN = &kw_sqlplus;
417 break;
418 case 4:
419 wordListN = &kw_user1;
420 break;
421 case 5:
422 wordListN = &kw_user2;
423 break;
424 case 6:
425 wordListN = &kw_user3;
426 break;
427 case 7:
428 wordListN = &kw_user4;
430 int firstModification = -1;
431 if (wordListN) {
432 WordList wlNew;
433 wlNew.Set(wl);
434 if (*wordListN != wlNew) {
435 wordListN->Set(wl);
436 firstModification = 0;
439 return firstModification;
442 void SCI_METHOD LexerSQL::Lex(unsigned int startPos, int length, int initStyle, IDocument *pAccess) {
443 LexAccessor styler(pAccess);
444 StyleContext sc(startPos, length, initStyle, styler);
445 int styleBeforeDCKeyword = SCE_SQL_DEFAULT;
446 int offset = 0;
447 char qOperator = 0x00;
449 for (; sc.More(); sc.Forward(), offset++) {
450 // Determine if the current state should terminate.
451 switch (sc.state) {
452 case SCE_SQL_OPERATOR:
453 sc.SetState(SCE_SQL_DEFAULT);
454 break;
455 case SCE_SQL_NUMBER:
456 // We stop the number definition on non-numerical non-dot non-eE non-sign char
457 if (!IsANumberChar(sc.ch)) {
458 sc.SetState(SCE_SQL_DEFAULT);
460 break;
461 case SCE_SQL_IDENTIFIER:
462 if (!IsAWordChar(sc.ch, options.sqlAllowDottedWord)) {
463 int nextState = SCE_SQL_DEFAULT;
464 char s[1000];
465 sc.GetCurrentLowered(s, sizeof(s));
466 if (keywords1.InList(s)) {
467 sc.ChangeState(SCE_SQL_WORD);
468 } else if (keywords2.InList(s)) {
469 sc.ChangeState(SCE_SQL_WORD2);
470 } else if (kw_sqlplus.InListAbbreviated(s, '~')) {
471 sc.ChangeState(SCE_SQL_SQLPLUS);
472 if (strncmp(s, "rem", 3) == 0) {
473 nextState = SCE_SQL_SQLPLUS_COMMENT;
474 } else if (strncmp(s, "pro", 3) == 0) {
475 nextState = SCE_SQL_SQLPLUS_PROMPT;
477 } else if (kw_user1.InList(s)) {
478 sc.ChangeState(SCE_SQL_USER1);
479 } else if (kw_user2.InList(s)) {
480 sc.ChangeState(SCE_SQL_USER2);
481 } else if (kw_user3.InList(s)) {
482 sc.ChangeState(SCE_SQL_USER3);
483 } else if (kw_user4.InList(s)) {
484 sc.ChangeState(SCE_SQL_USER4);
486 sc.SetState(nextState);
488 break;
489 case SCE_SQL_QUOTEDIDENTIFIER:
490 if (sc.ch == 0x60) {
491 if (sc.chNext == 0x60) {
492 sc.Forward(); // Ignore it
493 } else {
494 sc.ForwardSetState(SCE_SQL_DEFAULT);
497 break;
498 case SCE_SQL_COMMENT:
499 if (sc.Match('*', '/')) {
500 sc.Forward();
501 sc.ForwardSetState(SCE_SQL_DEFAULT);
503 break;
504 case SCE_SQL_COMMENTDOC:
505 if (sc.Match('*', '/')) {
506 sc.Forward();
507 sc.ForwardSetState(SCE_SQL_DEFAULT);
508 } else if (sc.ch == '@' || sc.ch == '\\') { // Doxygen support
509 // Verify that we have the conditions to mark a comment-doc-keyword
510 if ((IsASpace(sc.chPrev) || sc.chPrev == '*') && (!IsASpace(sc.chNext))) {
511 styleBeforeDCKeyword = SCE_SQL_COMMENTDOC;
512 sc.SetState(SCE_SQL_COMMENTDOCKEYWORD);
515 break;
516 case SCE_SQL_COMMENTLINE:
517 case SCE_SQL_COMMENTLINEDOC:
518 case SCE_SQL_SQLPLUS_COMMENT:
519 case SCE_SQL_SQLPLUS_PROMPT:
520 if (sc.atLineStart) {
521 sc.SetState(SCE_SQL_DEFAULT);
523 break;
524 case SCE_SQL_COMMENTDOCKEYWORD:
525 if ((styleBeforeDCKeyword == SCE_SQL_COMMENTDOC) && sc.Match('*', '/')) {
526 sc.ChangeState(SCE_SQL_COMMENTDOCKEYWORDERROR);
527 sc.Forward();
528 sc.ForwardSetState(SCE_SQL_DEFAULT);
529 } else if (!IsADoxygenChar(sc.ch)) {
530 char s[100];
531 sc.GetCurrentLowered(s, sizeof(s));
532 if (!isspace(sc.ch) || !kw_pldoc.InList(s + 1)) {
533 sc.ChangeState(SCE_SQL_COMMENTDOCKEYWORDERROR);
535 sc.SetState(styleBeforeDCKeyword);
537 break;
538 case SCE_SQL_CHARACTER:
539 if (options.sqlBackslashEscapes && sc.ch == '\\') {
540 sc.Forward();
541 } else if (sc.ch == '\'') {
542 if (sc.chNext == '\"') {
543 sc.Forward();
544 } else {
545 sc.ForwardSetState(SCE_SQL_DEFAULT);
548 break;
549 case SCE_SQL_STRING:
550 if (sc.ch == '\\') {
551 // Escape sequence
552 sc.Forward();
553 } else if (sc.ch == '\"') {
554 if (sc.chNext == '\"') {
555 sc.Forward();
556 } else {
557 sc.ForwardSetState(SCE_SQL_DEFAULT);
560 break;
561 case SCE_SQL_QOPERATOR:
562 if (qOperator == 0x00) {
563 qOperator = sc.ch;
564 } else {
565 char qComplement = 0x00;
567 if (qOperator == '<') {
568 qComplement = '>';
569 } else if (qOperator == '(') {
570 qComplement = ')';
571 } else if (qOperator == '{') {
572 qComplement = '}';
573 } else if (qOperator == '[') {
574 qComplement = ']';
575 } else {
576 qComplement = qOperator;
579 if (sc.Match(qComplement, '\'')) {
580 sc.Forward();
581 sc.ForwardSetState(SCE_SQL_DEFAULT);
582 qOperator = 0x00;
585 break;
588 // Determine if a new state should be entered.
589 if (sc.state == SCE_SQL_DEFAULT) {
590 if (sc.Match('q', '\'') || sc.Match('Q', '\'')) {
591 sc.SetState(SCE_SQL_QOPERATOR);
592 sc.Forward();
593 } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
594 sc.SetState(SCE_SQL_NUMBER);
595 } else if (IsAWordStart(sc.ch)) {
596 sc.SetState(SCE_SQL_IDENTIFIER);
597 } else if (sc.ch == 0x60 && options.sqlBackticksIdentifier) {
598 sc.SetState(SCE_SQL_QUOTEDIDENTIFIER);
599 } else if (sc.Match('/', '*')) {
600 if (sc.Match("/**") || sc.Match("/*!")) { // Support of Doxygen doc. style
601 sc.SetState(SCE_SQL_COMMENTDOC);
602 } else {
603 sc.SetState(SCE_SQL_COMMENT);
605 sc.Forward(); // Eat the * so it isn't used for the end of the comment
606 } else if (sc.Match('-', '-')) {
607 // MySQL requires a space or control char after --
608 // http://dev.mysql.com/doc/mysql/en/ansi-diff-comments.html
609 // Perhaps we should enforce that with proper property:
610 //~ } else if (sc.Match("-- ")) {
611 sc.SetState(SCE_SQL_COMMENTLINE);
612 } else if (sc.ch == '#' && options.sqlNumbersignComment) {
613 sc.SetState(SCE_SQL_COMMENTLINEDOC);
614 } else if (sc.ch == '\'') {
615 sc.SetState(SCE_SQL_CHARACTER);
616 } else if (sc.ch == '\"') {
617 sc.SetState(SCE_SQL_STRING);
618 } else if (isoperator(static_cast<char>(sc.ch))) {
619 sc.SetState(SCE_SQL_OPERATOR);
623 sc.Complete();
626 void SCI_METHOD LexerSQL::Fold(unsigned int startPos, int length, int initStyle, IDocument *pAccess) {
627 if (!options.fold)
628 return;
629 LexAccessor styler(pAccess);
630 unsigned int endPos = startPos + length;
631 int visibleChars = 0;
632 int lineCurrent = styler.GetLine(startPos);
633 int levelCurrent = SC_FOLDLEVELBASE;
635 if (lineCurrent > 0) {
636 // Backtrack to previous line in case need to fix its fold status for folding block of single-line comments (i.e. '--').
637 int lastNLPos = -1;
638 // And keep going back until we find an operator ';' followed
639 // by white-space and/or comments. This will improve folding.
640 while (--startPos > 0) {
641 char ch = styler[startPos];
642 if (ch == '\n' || (ch == '\r' && styler[startPos + 1] != '\n')) {
643 lastNLPos = startPos;
644 } else if (ch == ';' &&
645 styler.StyleAt(startPos) == SCE_SQL_OPERATOR) {
646 bool isAllClear = true;
647 for (int tempPos = startPos + 1;
648 tempPos < lastNLPos;
649 ++tempPos) {
650 int tempStyle = styler.StyleAt(tempPos);
651 if (!IsCommentStyle(tempStyle)
652 && tempStyle != SCE_SQL_DEFAULT) {
653 isAllClear = false;
654 break;
657 if (isAllClear) {
658 startPos = lastNLPos + 1;
659 break;
663 lineCurrent = styler.GetLine(startPos);
664 if (lineCurrent > 0)
665 levelCurrent = styler.LevelAt(lineCurrent - 1) >> 16;
667 // And because folding ends at ';', keep going until we find one
668 // Otherwise if create ... view ... as is split over multiple
669 // lines the folding won't always update immediately.
670 unsigned int docLength = styler.Length();
671 for (; endPos < docLength; ++endPos) {
672 if (styler.SafeGetCharAt(endPos) == ';') {
673 break;
677 int levelNext = levelCurrent;
678 char chNext = styler[startPos];
679 int styleNext = styler.StyleAt(startPos);
680 int style = initStyle;
681 bool endFound = false;
682 bool isUnfoldingIgnored = false;
683 // this statementFound flag avoids to fold when the statement is on only one line by ignoring ELSE or ELSIF
684 // eg. "IF condition1 THEN ... ELSIF condition2 THEN ... ELSE ... END IF;"
685 bool statementFound = false;
686 sql_state_t sqlStatesCurrentLine = 0;
687 if (!options.foldOnlyBegin) {
688 sqlStatesCurrentLine = sqlStates.ForLine(lineCurrent);
690 for (unsigned int i = startPos; i < endPos; i++) {
691 char ch = chNext;
692 chNext = styler.SafeGetCharAt(i + 1);
693 int stylePrev = style;
694 style = styleNext;
695 styleNext = styler.StyleAt(i + 1);
696 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
697 if (atEOL || (!IsCommentStyle(style) && ch == ';')) {
698 if (endFound) {
699 //Maybe this is the end of "EXCEPTION" BLOCK (eg. "BEGIN ... EXCEPTION ... END;")
700 sqlStatesCurrentLine = sqlStates.IntoExceptionBlock(sqlStatesCurrentLine, false);
702 // set endFound and isUnfoldingIgnored to false if EOL is reached or ';' is found
703 endFound = false;
704 isUnfoldingIgnored = false;
706 if ((!IsCommentStyle(style) && ch == ';')) {
707 if (sqlStates.IsIntoMergeStatement(sqlStatesCurrentLine)) {
708 // This is the end of "MERGE" statement.
709 if (!sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine))
710 levelNext--;
711 sqlStatesCurrentLine = sqlStates.IntoMergeStatement(sqlStatesCurrentLine, false);
712 levelNext--;
714 if (sqlStates.IsIntoSelectStatementOrAssignment(sqlStatesCurrentLine))
715 sqlStatesCurrentLine = sqlStates.IntoSelectStatementOrAssignment(sqlStatesCurrentLine, false);
716 if (sqlStates.IsIntoCreateStatement(sqlStatesCurrentLine)) {
717 if (sqlStates.IsIntoCreateViewStatement(sqlStatesCurrentLine)) {
718 if (sqlStates.IsIntoCreateViewAsStatement(sqlStatesCurrentLine)) {
719 levelNext--;
720 sqlStatesCurrentLine = sqlStates.IntoCreateViewAsStatement(sqlStatesCurrentLine, false);
722 sqlStatesCurrentLine = sqlStates.IntoCreateViewStatement(sqlStatesCurrentLine, false);
724 sqlStatesCurrentLine = sqlStates.IntoCreateStatement(sqlStatesCurrentLine, false);
727 if (ch == ':' && chNext == '=' && !IsCommentStyle(style))
728 sqlStatesCurrentLine = sqlStates.IntoSelectStatementOrAssignment(sqlStatesCurrentLine, true);
730 if (options.foldComment && IsStreamCommentStyle(style)) {
731 if (!IsStreamCommentStyle(stylePrev)) {
732 levelNext++;
733 } else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
734 // Comments don't end at end of line and the next character may be unstyled.
735 levelNext--;
738 if (options.foldComment && (style == SCE_SQL_COMMENTLINE)) {
739 // MySQL needs -- comments to be followed by space or control char
740 if ((ch == '-') && (chNext == '-')) {
741 char chNext2 = styler.SafeGetCharAt(i + 2);
742 char chNext3 = styler.SafeGetCharAt(i + 3);
743 if (chNext2 == '{' || chNext3 == '{') {
744 levelNext++;
745 } else if (chNext2 == '}' || chNext3 == '}') {
746 levelNext--;
750 // Fold block of single-line comments (i.e. '--').
751 if (options.foldComment && atEOL && IsCommentLine(lineCurrent, styler)) {
752 if (!IsCommentLine(lineCurrent - 1, styler) && IsCommentLine(lineCurrent + 1, styler))
753 levelNext++;
754 else if (IsCommentLine(lineCurrent - 1, styler) && !IsCommentLine(lineCurrent + 1, styler))
755 levelNext--;
757 if (style == SCE_SQL_OPERATOR) {
758 if (ch == '(') {
759 if (levelCurrent > levelNext)
760 levelCurrent--;
761 levelNext++;
762 } else if (ch == ')') {
763 levelNext--;
764 } else if ((!options.foldOnlyBegin) && ch == ';') {
765 sqlStatesCurrentLine = sqlStates.IgnoreWhen(sqlStatesCurrentLine, false);
768 // If new keyword (cannot trigger on elseif or nullif, does less tests)
769 if (style == SCE_SQL_WORD && stylePrev != SCE_SQL_WORD) {
770 const int MAX_KW_LEN = 9; // Maximum length of folding keywords
771 char s[MAX_KW_LEN + 2];
772 unsigned int j = 0;
773 for (; j < MAX_KW_LEN + 1; j++) {
774 if (!iswordchar(styler[i + j])) {
775 break;
777 s[j] = static_cast<char>(tolower(styler[i + j]));
779 if (j == MAX_KW_LEN + 1) {
780 // Keyword too long, don't test it
781 s[0] = '\0';
782 } else {
783 s[j] = '\0';
785 if (!options.foldOnlyBegin &&
786 strcmp(s, "select") == 0) {
787 sqlStatesCurrentLine = sqlStates.IntoSelectStatementOrAssignment(sqlStatesCurrentLine, true);
788 } else if (strcmp(s, "if") == 0) {
789 if (endFound) {
790 endFound = false;
791 if (options.foldOnlyBegin && !isUnfoldingIgnored) {
792 // this end isn't for begin block, but for if block ("end if;")
793 // so ignore previous "end" by increment levelNext.
794 levelNext++;
796 } else {
797 if (!options.foldOnlyBegin)
798 sqlStatesCurrentLine = sqlStates.IntoCondition(sqlStatesCurrentLine, true);
799 if (levelCurrent > levelNext) {
800 // doesn't include this line into the folding block
801 // because doesn't hide IF (eg "END; IF")
802 levelCurrent = levelNext;
805 } else if (!options.foldOnlyBegin &&
806 strcmp(s, "then") == 0 &&
807 sqlStates.IsIntoCondition(sqlStatesCurrentLine)) {
808 sqlStatesCurrentLine = sqlStates.IntoCondition(sqlStatesCurrentLine, false);
809 if (!options.foldOnlyBegin) {
810 if (levelCurrent > levelNext) {
811 levelCurrent = levelNext;
813 if (!statementFound)
814 levelNext++;
816 statementFound = true;
817 } else if (levelCurrent > levelNext) {
818 // doesn't include this line into the folding block
819 // because doesn't hide LOOP or CASE (eg "END; LOOP" or "END; CASE")
820 levelCurrent = levelNext;
822 } else if (strcmp(s, "loop") == 0 ||
823 strcmp(s, "case") == 0) {
824 if (endFound) {
825 endFound = false;
826 if (options.foldOnlyBegin && !isUnfoldingIgnored) {
827 // this end isn't for begin block, but for loop block ("end loop;") or case block ("end case;")
828 // so ignore previous "end" by increment levelNext.
829 levelNext++;
831 if ((!options.foldOnlyBegin) && strcmp(s, "case") == 0) {
832 sqlStatesCurrentLine = sqlStates.EndCaseBlock(sqlStatesCurrentLine);
833 if (!sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine))
834 levelNext--; //again for the "end case;" and block when
836 } else if (!options.foldOnlyBegin) {
837 if (strcmp(s, "case") == 0) {
838 sqlStatesCurrentLine = sqlStates.BeginCaseBlock(sqlStatesCurrentLine);
839 sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, true);
842 if (levelCurrent > levelNext)
843 levelCurrent = levelNext;
845 if (!statementFound)
846 levelNext++;
848 statementFound = true;
849 } else if (levelCurrent > levelNext) {
850 // doesn't include this line into the folding block
851 // because doesn't hide LOOP or CASE (eg "END; LOOP" or "END; CASE")
852 levelCurrent = levelNext;
854 } else if ((!options.foldOnlyBegin) && (
855 // folding for ELSE and ELSIF block only if foldAtElse is set
856 // and IF or CASE aren't on only one line with ELSE or ELSIF (with flag statementFound)
857 options.foldAtElse && !statementFound) && strcmp(s, "elsif") == 0) {
858 sqlStatesCurrentLine = sqlStates.IntoCondition(sqlStatesCurrentLine, true);
859 levelCurrent--;
860 levelNext--;
861 } else if ((!options.foldOnlyBegin) && (
862 // folding for ELSE and ELSIF block only if foldAtElse is set
863 // and IF or CASE aren't on only one line with ELSE or ELSIF (with flag statementFound)
864 options.foldAtElse && !statementFound) && strcmp(s, "else") == 0) {
865 // prevent also ELSE is on the same line (eg. "ELSE ... END IF;")
866 statementFound = true;
867 if (sqlStates.IsIntoCaseBlock(sqlStatesCurrentLine) && sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine)) {
868 sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, false);
869 levelNext++;
870 } else {
871 // we are in same case "} ELSE {" in C language
872 levelCurrent--;
874 } else if (strcmp(s, "begin") == 0) {
875 levelNext++;
876 sqlStatesCurrentLine = sqlStates.IntoDeclareBlock(sqlStatesCurrentLine, false);
877 } else if ((strcmp(s, "end") == 0) ||
878 // SQL Anywhere permits IF ... ELSE ... ENDIF
879 // will only be active if "endif" appears in the
880 // keyword list.
881 (strcmp(s, "endif") == 0)) {
882 endFound = true;
883 levelNext--;
884 if (sqlStates.IsIntoSelectStatementOrAssignment(sqlStatesCurrentLine) && !sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine))
885 levelNext--;
886 if (levelNext < SC_FOLDLEVELBASE) {
887 levelNext = SC_FOLDLEVELBASE;
888 isUnfoldingIgnored = true;
890 } else if ((!options.foldOnlyBegin) &&
891 strcmp(s, "when") == 0 &&
892 !sqlStates.IsIgnoreWhen(sqlStatesCurrentLine) &&
893 !sqlStates.IsIntoExceptionBlock(sqlStatesCurrentLine) && (
894 sqlStates.IsIntoCaseBlock(sqlStatesCurrentLine) ||
895 sqlStates.IsIntoMergeStatement(sqlStatesCurrentLine)
898 sqlStatesCurrentLine = sqlStates.IntoCondition(sqlStatesCurrentLine, true);
900 // 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")
901 // and same way for MERGE statement.
902 if (!statementFound) {
903 if (!sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine)) {
904 levelCurrent--;
905 levelNext--;
907 sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, false);
909 } else if ((!options.foldOnlyBegin) && strcmp(s, "exit") == 0) {
910 sqlStatesCurrentLine = sqlStates.IgnoreWhen(sqlStatesCurrentLine, true);
911 } else if ((!options.foldOnlyBegin) && !sqlStates.IsIntoDeclareBlock(sqlStatesCurrentLine) && strcmp(s, "exception") == 0) {
912 sqlStatesCurrentLine = sqlStates.IntoExceptionBlock(sqlStatesCurrentLine, true);
913 } else if ((!options.foldOnlyBegin) &&
914 (strcmp(s, "declare") == 0 ||
915 strcmp(s, "function") == 0 ||
916 strcmp(s, "procedure") == 0 ||
917 strcmp(s, "package") == 0)) {
918 sqlStatesCurrentLine = sqlStates.IntoDeclareBlock(sqlStatesCurrentLine, true);
919 } else if ((!options.foldOnlyBegin) &&
920 strcmp(s, "merge") == 0) {
921 sqlStatesCurrentLine = sqlStates.IntoMergeStatement(sqlStatesCurrentLine, true);
922 sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, true);
923 levelNext++;
924 statementFound = true;
925 } else if ((!options.foldOnlyBegin) &&
926 strcmp(s, "create") == 0) {
927 sqlStatesCurrentLine = sqlStates.IntoCreateStatement(sqlStatesCurrentLine, true);
928 } else if ((!options.foldOnlyBegin) &&
929 strcmp(s, "view") == 0 &&
930 sqlStates.IsIntoCreateStatement(sqlStatesCurrentLine)) {
931 sqlStatesCurrentLine = sqlStates.IntoCreateViewStatement(sqlStatesCurrentLine, true);
932 } else if ((!options.foldOnlyBegin) &&
933 strcmp(s, "as") == 0 &&
934 sqlStates.IsIntoCreateViewStatement(sqlStatesCurrentLine) &&
935 ! sqlStates.IsIntoCreateViewAsStatement(sqlStatesCurrentLine)) {
936 sqlStatesCurrentLine = sqlStates.IntoCreateViewAsStatement(sqlStatesCurrentLine, true);
937 levelNext++;
940 if (atEOL) {
941 int levelUse = levelCurrent;
942 int lev = levelUse | levelNext << 16;
943 if (visibleChars == 0 && options.foldCompact)
944 lev |= SC_FOLDLEVELWHITEFLAG;
945 if (levelUse < levelNext)
946 lev |= SC_FOLDLEVELHEADERFLAG;
947 if (lev != styler.LevelAt(lineCurrent)) {
948 styler.SetLevel(lineCurrent, lev);
950 lineCurrent++;
951 levelCurrent = levelNext;
952 visibleChars = 0;
953 statementFound = false;
954 if (!options.foldOnlyBegin)
955 sqlStates.Set(lineCurrent, sqlStatesCurrentLine);
957 if (!isspacechar(ch)) {
958 visibleChars++;
963 LexerModule lmSQL(SCLEX_SQL, LexerSQL::LexerFactorySQL, "sql", sqlWordListDesc);