1 //-*- coding: utf-8 -*-
2 // Scintilla source code edit control
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.
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.
23 #include "Scintilla.h"
27 #include "LexAccessor.h"
29 #include "StyleContext.h"
30 #include "CharacterSet.h"
31 #include "LexerModule.h"
32 #include "OptionSet.h"
33 #include "SparseState.h"
36 using namespace Scintilla
;
39 static inline bool IsAWordChar(int ch
, bool sqlAllowDottedWord
) {
40 if (!sqlAllowDottedWord
)
41 return (ch
< 0x80) && (isalnum(ch
) || ch
== '_');
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.
61 (isdigit(ch
) || toupper(ch
) == 'E' ||
62 ch
== '.' || ch
== '-' || ch
== '+');
68 void Set(int lineNumber
, unsigned short int sqlStatesLine
) {
69 sqlStatement
.Set(lineNumber
, sqlStatesLine
);
72 unsigned short int IgnoreWhen (unsigned short int sqlStatesLine
, bool enable
) {
74 sqlStatesLine
|= MASK_IGNORE_WHEN
;
76 sqlStatesLine
&= ~MASK_IGNORE_WHEN
;
81 unsigned short int IntoCondition (unsigned short int sqlStatesLine
, bool enable
) {
83 sqlStatesLine
|= MASK_INTO_CONDITION
;
85 sqlStatesLine
&= ~MASK_INTO_CONDITION
;
90 unsigned short int IntoExceptionBlock (unsigned short int sqlStatesLine
, bool enable
) {
92 sqlStatesLine
|= MASK_INTO_EXCEPTION
;
94 sqlStatesLine
&= ~MASK_INTO_EXCEPTION
;
99 unsigned short int IntoDeclareBlock (unsigned short int sqlStatesLine
, bool enable
) {
101 sqlStatesLine
|= MASK_INTO_DECLARE
;
103 sqlStatesLine
&= ~MASK_INTO_DECLARE
;
105 return sqlStatesLine
;
108 unsigned short int IntoMergeStatement (unsigned short int sqlStatesLine
, bool enable
) {
110 sqlStatesLine
|= MASK_MERGE_STATEMENT
;
112 sqlStatesLine
&= ~MASK_MERGE_STATEMENT
;
114 return sqlStatesLine
;
117 unsigned short int CaseMergeWithoutWhenFound (unsigned short int sqlStatesLine
, bool found
) {
119 sqlStatesLine
|= MASK_CASE_MERGE_WITHOUT_WHEN_FOUND
;
121 sqlStatesLine
&= ~MASK_CASE_MERGE_WITHOUT_WHEN_FOUND
;
123 return sqlStatesLine
;
126 unsigned short int IntoSelectStatement (unsigned short int sqlStatesLine
, bool found
) {
128 sqlStatesLine
|= MASK_INTO_SELECT_STATEMENT
;
130 sqlStatesLine
&= ~MASK_INTO_SELECT_STATEMENT
;
132 return sqlStatesLine
;
135 unsigned short int BeginCaseBlock (unsigned short int sqlStatesLine
) {
136 if ((sqlStatesLine
& MASK_NESTED_CASES
) < MASK_NESTED_CASES
) {
139 return sqlStatesLine
;
142 unsigned short int EndCaseBlock (unsigned short int sqlStatesLine
) {
143 if ((sqlStatesLine
& MASK_NESTED_CASES
) > 0) {
146 return sqlStatesLine
;
149 bool IsIgnoreWhen (unsigned short int sqlStatesLine
) {
150 return (sqlStatesLine
& MASK_IGNORE_WHEN
) != 0;
153 bool IsIntoCondition (unsigned short int sqlStatesLine
) {
154 return (sqlStatesLine
& MASK_INTO_CONDITION
) != 0;
157 bool IsIntoCaseBlock (unsigned short int sqlStatesLine
) {
158 return (sqlStatesLine
& MASK_NESTED_CASES
) != 0;
161 bool IsIntoExceptionBlock (unsigned short int sqlStatesLine
) {
162 return (sqlStatesLine
& MASK_INTO_EXCEPTION
) != 0;
165 bool IsIntoSelectStatement (unsigned short int sqlStatesLine
) {
166 return (sqlStatesLine
& MASK_INTO_SELECT_STATEMENT
) != 0;
169 bool IsCaseMergeWithoutWhenFound (unsigned short int sqlStatesLine
) {
170 return (sqlStatesLine
& MASK_CASE_MERGE_WITHOUT_WHEN_FOUND
) != 0;
173 bool IsIntoDeclareBlock (unsigned short int sqlStatesLine
) {
174 return (sqlStatesLine
& MASK_INTO_DECLARE
) != 0;
177 bool IsIntoMergeStatement (unsigned short int sqlStatesLine
) {
178 return (sqlStatesLine
& MASK_MERGE_STATEMENT
) != 0;
181 unsigned short int ForLine(int lineNumber
) {
182 return sqlStatement
.ValueAt(lineNumber
);
188 SparseState
<unsigned short int> sqlStatement
;
190 MASK_NESTED_CASES
= 0x01FF,
191 MASK_INTO_SELECT_STATEMENT
= 0x0200,
192 MASK_CASE_MERGE_WITHOUT_WHEN_FOUND
= 0x0400,
193 MASK_MERGE_STATEMENT
= 0x0800,
194 MASK_INTO_DECLARE
= 0x1000,
195 MASK_INTO_EXCEPTION
= 0x2000,
196 MASK_INTO_CONDITION
= 0x4000,
197 MASK_IGNORE_WHEN
= 0x8000
201 // Options used for LexerSQL
208 bool sqlBackticksIdentifier
;
209 bool sqlNumbersignComment
;
210 bool sqlBackslashEscapes
;
211 bool sqlAllowDottedWord
;
217 foldOnlyBegin
= false;
218 sqlBackticksIdentifier
= false;
219 sqlNumbersignComment
= false;
220 sqlBackslashEscapes
= false;
221 sqlAllowDottedWord
= false;
225 static const char * const sqlWordListDesc
[] = {
237 struct OptionSetSQL
: public OptionSet
<OptionsSQL
> {
239 DefineProperty("fold", &OptionsSQL::fold
);
241 DefineProperty("fold.sql.at.else", &OptionsSQL::foldAtElse
,
242 "This option enables SQL folding on a \"ELSE\" and \"ELSIF\" line of an IF statement.");
244 DefineProperty("fold.comment", &OptionsSQL::foldComment
);
246 DefineProperty("fold.compact", &OptionsSQL::foldCompact
);
248 DefineProperty("fold.sql.only.begin", &OptionsSQL::foldOnlyBegin
);
250 DefineProperty("lexer.sql.backticks.identifier", &OptionsSQL::sqlBackticksIdentifier
);
252 DefineProperty("lexer.sql.numbersign.comment", &OptionsSQL::sqlNumbersignComment
,
253 "If \"lexer.sql.numbersign.comment\" property is set to 0 a line beginning with '#' will not be a comment.");
255 DefineProperty("sql.backslash.escapes", &OptionsSQL::sqlBackslashEscapes
,
256 "Enables backslash as an escape character in SQL.");
258 DefineProperty("lexer.sql.allow.dotted.word", &OptionsSQL::sqlAllowDottedWord
,
259 "Set to 1 to colourise recognized words with dots "
260 "(recommended for Oracle PL/SQL objects).");
262 DefineWordListSets(sqlWordListDesc
);
266 class LexerSQL
: public ILexer
{
270 virtual ~LexerSQL() {}
272 int SCI_METHOD
Version () const {
276 void SCI_METHOD
Release() {
280 const char * SCI_METHOD
PropertyNames() {
281 return osSQL
.PropertyNames();
284 int SCI_METHOD
PropertyType(const char *name
) {
285 return osSQL
.PropertyType(name
);
288 const char * SCI_METHOD
DescribeProperty(const char *name
) {
289 return osSQL
.DescribeProperty(name
);
292 int SCI_METHOD
PropertySet(const char *key
, const char *val
) {
293 if (osSQL
.PropertySet(&options
, key
, val
)) {
299 const char * SCI_METHOD
DescribeWordListSets() {
300 return osSQL
.DescribeWordListSets();
303 int SCI_METHOD
WordListSet(int n
, const char *wl
);
304 void SCI_METHOD
Lex (unsigned int startPos
, int lengthDoc
, int initStyle
, IDocument
*pAccess
);
305 void SCI_METHOD
Fold(unsigned int startPos
, int lengthDoc
, int initStyle
, IDocument
*pAccess
);
307 void * SCI_METHOD
PrivateCall(int, void *) {
311 static ILexer
*LexerFactorySQL() {
312 return new LexerSQL();
315 bool IsStreamCommentStyle(int style
) {
316 return style
== SCE_SQL_COMMENT
||
317 style
== SCE_SQL_COMMENTDOC
||
318 style
== SCE_SQL_COMMENTDOCKEYWORD
||
319 style
== SCE_SQL_COMMENTDOCKEYWORDERROR
;
322 bool IsCommentStyle (int style
) {
324 case SCE_SQL_COMMENT
:
325 case SCE_SQL_COMMENTDOC
:
326 case SCE_SQL_COMMENTLINE
:
327 case SCE_SQL_COMMENTLINEDOC
:
328 case SCE_SQL_COMMENTDOCKEYWORD
:
329 case SCE_SQL_COMMENTDOCKEYWORDERROR
:
336 bool IsCommentLine (int line
, LexAccessor
&styler
) {
337 int pos
= styler
.LineStart(line
);
338 int eol_pos
= styler
.LineStart(line
+ 1) - 1;
339 for (int i
= pos
; i
+ 1 < eol_pos
; i
++) {
340 int style
= styler
.StyleAt(i
);
341 // MySQL needs -- comments to be followed by space or control char
342 if (style
== SCE_SQL_COMMENTLINE
&& styler
.Match(i
, "--"))
344 else if (!IsASpaceOrTab(styler
[i
]))
364 int SCI_METHOD
LexerSQL::WordListSet(int n
, const char *wl
) {
365 WordList
*wordListN
= 0;
368 wordListN
= &keywords1
;
371 wordListN
= &keywords2
;
374 wordListN
= &kw_pldoc
;
377 wordListN
= &kw_sqlplus
;
380 wordListN
= &kw_user1
;
383 wordListN
= &kw_user2
;
386 wordListN
= &kw_user3
;
389 wordListN
= &kw_user4
;
391 int firstModification
= -1;
395 if (*wordListN
!= wlNew
) {
397 firstModification
= 0;
400 return firstModification
;
403 void SCI_METHOD
LexerSQL::Lex(unsigned int startPos
, int length
, int initStyle
, IDocument
*pAccess
) {
404 LexAccessor
styler(pAccess
);
405 StyleContext
sc(startPos
, length
, initStyle
, styler
);
406 int styleBeforeDCKeyword
= SCE_SQL_DEFAULT
;
408 for (; sc
.More(); sc
.Forward(), offset
++) {
409 // Determine if the current state should terminate.
411 case SCE_SQL_OPERATOR
:
412 sc
.SetState(SCE_SQL_DEFAULT
);
415 // We stop the number definition on non-numerical non-dot non-eE non-sign char
416 if (!IsANumberChar(sc
.ch
)) {
417 sc
.SetState(SCE_SQL_DEFAULT
);
420 case SCE_SQL_IDENTIFIER
:
421 if (!IsAWordChar(sc
.ch
, options
.sqlAllowDottedWord
)) {
422 int nextState
= SCE_SQL_DEFAULT
;
424 sc
.GetCurrentLowered(s
, sizeof(s
));
425 if (keywords1
.InList(s
)) {
426 sc
.ChangeState(SCE_SQL_WORD
);
427 } else if (keywords2
.InList(s
)) {
428 sc
.ChangeState(SCE_SQL_WORD2
);
429 } else if (kw_sqlplus
.InListAbbreviated(s
, '~')) {
430 sc
.ChangeState(SCE_SQL_SQLPLUS
);
431 if (strncmp(s
, "rem", 3) == 0) {
432 nextState
= SCE_SQL_SQLPLUS_COMMENT
;
433 } else if (strncmp(s
, "pro", 3) == 0) {
434 nextState
= SCE_SQL_SQLPLUS_PROMPT
;
436 } else if (kw_user1
.InList(s
)) {
437 sc
.ChangeState(SCE_SQL_USER1
);
438 } else if (kw_user2
.InList(s
)) {
439 sc
.ChangeState(SCE_SQL_USER2
);
440 } else if (kw_user3
.InList(s
)) {
441 sc
.ChangeState(SCE_SQL_USER3
);
442 } else if (kw_user4
.InList(s
)) {
443 sc
.ChangeState(SCE_SQL_USER4
);
445 sc
.SetState(nextState
);
448 case SCE_SQL_QUOTEDIDENTIFIER
:
450 if (sc
.chNext
== 0x60) {
451 sc
.Forward(); // Ignore it
453 sc
.ForwardSetState(SCE_SQL_DEFAULT
);
457 case SCE_SQL_COMMENT
:
458 if (sc
.Match('*', '/')) {
460 sc
.ForwardSetState(SCE_SQL_DEFAULT
);
463 case SCE_SQL_COMMENTDOC
:
464 if (sc
.Match('*', '/')) {
466 sc
.ForwardSetState(SCE_SQL_DEFAULT
);
467 } else if (sc
.ch
== '@' || sc
.ch
== '\\') { // Doxygen support
468 // Verify that we have the conditions to mark a comment-doc-keyword
469 if ((IsASpace(sc
.chPrev
) || sc
.chPrev
== '*') && (!IsASpace(sc
.chNext
))) {
470 styleBeforeDCKeyword
= SCE_SQL_COMMENTDOC
;
471 sc
.SetState(SCE_SQL_COMMENTDOCKEYWORD
);
475 case SCE_SQL_COMMENTLINE
:
476 case SCE_SQL_COMMENTLINEDOC
:
477 case SCE_SQL_SQLPLUS_COMMENT
:
478 case SCE_SQL_SQLPLUS_PROMPT
:
479 if (sc
.atLineStart
) {
480 sc
.SetState(SCE_SQL_DEFAULT
);
483 case SCE_SQL_COMMENTDOCKEYWORD
:
484 if ((styleBeforeDCKeyword
== SCE_SQL_COMMENTDOC
) && sc
.Match('*', '/')) {
485 sc
.ChangeState(SCE_SQL_COMMENTDOCKEYWORDERROR
);
487 sc
.ForwardSetState(SCE_SQL_DEFAULT
);
488 } else if (!IsADoxygenChar(sc
.ch
)) {
490 sc
.GetCurrentLowered(s
, sizeof(s
));
491 if (!isspace(sc
.ch
) || !kw_pldoc
.InList(s
+ 1)) {
492 sc
.ChangeState(SCE_SQL_COMMENTDOCKEYWORDERROR
);
494 sc
.SetState(styleBeforeDCKeyword
);
497 case SCE_SQL_CHARACTER
:
498 if (options
.sqlBackslashEscapes
&& sc
.ch
== '\\') {
500 } else if (sc
.ch
== '\'') {
501 if (sc
.chNext
== '\"') {
504 sc
.ForwardSetState(SCE_SQL_DEFAULT
);
512 } else if (sc
.ch
== '\"') {
513 if (sc
.chNext
== '\"') {
516 sc
.ForwardSetState(SCE_SQL_DEFAULT
);
522 // Determine if a new state should be entered.
523 if (sc
.state
== SCE_SQL_DEFAULT
) {
524 if (IsADigit(sc
.ch
) || (sc
.ch
== '.' && IsADigit(sc
.chNext
))) {
525 sc
.SetState(SCE_SQL_NUMBER
);
526 } else if (IsAWordStart(sc
.ch
)) {
527 sc
.SetState(SCE_SQL_IDENTIFIER
);
528 } else if (sc
.ch
== 0x60 && options
.sqlBackticksIdentifier
) {
529 sc
.SetState(SCE_SQL_QUOTEDIDENTIFIER
);
530 } else if (sc
.Match('/', '*')) {
531 if (sc
.Match("/**") || sc
.Match("/*!")) { // Support of Doxygen doc. style
532 sc
.SetState(SCE_SQL_COMMENTDOC
);
534 sc
.SetState(SCE_SQL_COMMENT
);
536 sc
.Forward(); // Eat the * so it isn't used for the end of the comment
537 } else if (sc
.Match('-', '-')) {
538 // MySQL requires a space or control char after --
539 // http://dev.mysql.com/doc/mysql/en/ansi-diff-comments.html
540 // Perhaps we should enforce that with proper property:
541 //~ } else if (sc.Match("-- ")) {
542 sc
.SetState(SCE_SQL_COMMENTLINE
);
543 } else if (sc
.ch
== '#' && options
.sqlNumbersignComment
) {
544 sc
.SetState(SCE_SQL_COMMENTLINEDOC
);
545 } else if (sc
.ch
== '\'') {
546 sc
.SetState(SCE_SQL_CHARACTER
);
547 } else if (sc
.ch
== '\"') {
548 sc
.SetState(SCE_SQL_STRING
);
549 } else if (isoperator(static_cast<char>(sc
.ch
))) {
550 sc
.SetState(SCE_SQL_OPERATOR
);
557 void SCI_METHOD
LexerSQL::Fold(unsigned int startPos
, int length
, int initStyle
, IDocument
*pAccess
) {
560 LexAccessor
styler(pAccess
);
561 unsigned int endPos
= startPos
+ length
;
562 int visibleChars
= 0;
563 int lineCurrent
= styler
.GetLine(startPos
);
564 int levelCurrent
= SC_FOLDLEVELBASE
;
566 if (lineCurrent
> 0) {
567 // Backtrack to previous line in case need to fix its fold status for folding block of single-line comments (i.e. '--').
569 startPos
= styler
.LineStart(lineCurrent
);
572 levelCurrent
= styler
.LevelAt(lineCurrent
- 1) >> 16;
574 int levelNext
= levelCurrent
;
575 char chNext
= styler
[startPos
];
576 int styleNext
= styler
.StyleAt(startPos
);
577 int style
= initStyle
;
578 bool endFound
= false;
579 bool isUnfoldingIgnored
= false;
580 // this statementFound flag avoids to fold when the statement is on only one line by ignoring ELSE or ELSIF
581 // eg. "IF condition1 THEN ... ELSIF condition2 THEN ... ELSE ... END IF;"
582 bool statementFound
= false;
583 unsigned short int sqlStatesCurrentLine
= 0;
584 if (!options
.foldOnlyBegin
) {
585 sqlStatesCurrentLine
= sqlStates
.ForLine(lineCurrent
);
587 for (unsigned int i
= startPos
; i
< endPos
; i
++) {
589 chNext
= styler
.SafeGetCharAt(i
+ 1);
590 int stylePrev
= style
;
592 styleNext
= styler
.StyleAt(i
+ 1);
593 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
594 if (atEOL
|| (!IsCommentStyle(style
) && ch
== ';')) {
596 //Maybe this is the end of "EXCEPTION" BLOCK (eg. "BEGIN ... EXCEPTION ... END;")
597 sqlStatesCurrentLine
= sqlStates
.IntoExceptionBlock(sqlStatesCurrentLine
, false);
599 // set endFound and isUnfoldingIgnored to false if EOL is reached or ';' is found
601 isUnfoldingIgnored
= false;
603 if ((!IsCommentStyle(style
) && ch
== ';')) {
604 if (sqlStates
.IsIntoMergeStatement(sqlStatesCurrentLine
)) {
605 // This is the end of "MERGE" statement.
606 if (!sqlStates
.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine
))
608 sqlStatesCurrentLine
= sqlStates
.IntoMergeStatement(sqlStatesCurrentLine
, false);
611 if (sqlStates
.IsIntoSelectStatement(sqlStatesCurrentLine
))
612 sqlStatesCurrentLine
= sqlStates
.IntoSelectStatement(sqlStatesCurrentLine
, false);
614 if (options
.foldComment
&& IsStreamCommentStyle(style
)) {
615 if (!IsStreamCommentStyle(stylePrev
)) {
617 } else if (!IsStreamCommentStyle(styleNext
) && !atEOL
) {
618 // Comments don't end at end of line and the next character may be unstyled.
622 if (options
.foldComment
&& (style
== SCE_SQL_COMMENTLINE
)) {
623 // MySQL needs -- comments to be followed by space or control char
624 if ((ch
== '-') && (chNext
== '-')) {
625 char chNext2
= styler
.SafeGetCharAt(i
+ 2);
626 char chNext3
= styler
.SafeGetCharAt(i
+ 3);
627 if (chNext2
== '{' || chNext3
== '{') {
629 } else if (chNext2
== '}' || chNext3
== '}') {
634 // Fold block of single-line comments (i.e. '--').
635 if (options
.foldComment
&& atEOL
&& IsCommentLine(lineCurrent
, styler
)) {
636 if (!IsCommentLine(lineCurrent
- 1, styler
) && IsCommentLine(lineCurrent
+ 1, styler
))
638 else if (IsCommentLine(lineCurrent
- 1, styler
) && !IsCommentLine(lineCurrent
+ 1, styler
))
641 if (style
== SCE_SQL_OPERATOR
) {
643 if (levelCurrent
> levelNext
)
646 } else if (ch
== ')') {
648 } else if ((!options
.foldOnlyBegin
) && ch
== ';') {
649 sqlStatesCurrentLine
= sqlStates
.IgnoreWhen(sqlStatesCurrentLine
, false);
652 // If new keyword (cannot trigger on elseif or nullif, does less tests)
653 if (style
== SCE_SQL_WORD
&& stylePrev
!= SCE_SQL_WORD
) {
654 const int MAX_KW_LEN
= 9; // Maximum length of folding keywords
655 char s
[MAX_KW_LEN
+ 2];
657 for (; j
< MAX_KW_LEN
+ 1; j
++) {
658 if (!iswordchar(styler
[i
+ j
])) {
661 s
[j
] = static_cast<char>(tolower(styler
[i
+ j
]));
663 if (j
== MAX_KW_LEN
+ 1) {
664 // Keyword too long, don't test it
670 if (!options
.foldOnlyBegin
&&
671 strcmp(s
, "select") == 0) {
672 sqlStatesCurrentLine
= sqlStates
.IntoSelectStatement(sqlStatesCurrentLine
, true);
673 } else if (strcmp(s
, "if") == 0) {
676 if (options
.foldOnlyBegin
&& !isUnfoldingIgnored
) {
677 // this end isn't for begin block, but for if block ("end if;")
678 // so ignore previous "end" by increment levelNext.
682 if (!options
.foldOnlyBegin
)
683 sqlStatesCurrentLine
= sqlStates
.IntoCondition(sqlStatesCurrentLine
, true);
684 if (levelCurrent
> levelNext
) {
685 // doesn't include this line into the folding block
686 // because doesn't hide IF (eg "END; IF")
687 levelCurrent
= levelNext
;
690 } else if (!options
.foldOnlyBegin
&&
691 strcmp(s
, "then") == 0 &&
692 sqlStates
.IsIntoCondition(sqlStatesCurrentLine
)) {
693 sqlStatesCurrentLine
= sqlStates
.IntoCondition(sqlStatesCurrentLine
, false);
694 if (!options
.foldOnlyBegin
) {
695 if (levelCurrent
> levelNext
) {
696 levelCurrent
= levelNext
;
701 statementFound
= true;
702 } else if (levelCurrent
> levelNext
) {
703 // doesn't include this line into the folding block
704 // because doesn't hide LOOP or CASE (eg "END; LOOP" or "END; CASE")
705 levelCurrent
= levelNext
;
707 } else if (strcmp(s
, "loop") == 0 ||
708 strcmp(s
, "case") == 0) {
711 if (options
.foldOnlyBegin
&& !isUnfoldingIgnored
) {
712 // this end isn't for begin block, but for loop block ("end loop;") or case block ("end case;")
713 // so ignore previous "end" by increment levelNext.
716 if ((!options
.foldOnlyBegin
) && strcmp(s
, "case") == 0) {
717 sqlStatesCurrentLine
= sqlStates
.EndCaseBlock(sqlStatesCurrentLine
);
718 if (!sqlStates
.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine
))
719 levelNext
--; //again for the "end case;" and block when
721 } else if (!options
.foldOnlyBegin
) {
722 if (strcmp(s
, "case") == 0)
723 sqlStatesCurrentLine
= sqlStates
.BeginCaseBlock(sqlStatesCurrentLine
);
725 if (levelCurrent
> levelNext
)
726 levelCurrent
= levelNext
;
731 sqlStatesCurrentLine
= sqlStates
.CaseMergeWithoutWhenFound(sqlStatesCurrentLine
, true);
732 statementFound
= true;
733 } else if (levelCurrent
> levelNext
) {
734 // doesn't include this line into the folding block
735 // because doesn't hide LOOP or CASE (eg "END; LOOP" or "END; CASE")
736 levelCurrent
= levelNext
;
738 } else if ((!options
.foldOnlyBegin
) && (
739 // folding for ELSE and ELSIF block only if foldAtElse is set
740 // and IF or CASE aren't on only one line with ELSE or ELSIF (with flag statementFound)
741 options
.foldAtElse
&& !statementFound
) && strcmp(s
, "elsif") == 0) {
742 sqlStatesCurrentLine
= sqlStates
.IntoCondition(sqlStatesCurrentLine
, true);
745 } else if ((!options
.foldOnlyBegin
) && (
746 // folding for ELSE and ELSIF block only if foldAtElse is set
747 // and IF or CASE aren't on only one line with ELSE or ELSIF (with flag statementFound)
748 options
.foldAtElse
&& !statementFound
) && strcmp(s
, "else") == 0) {
749 // prevent also ELSE is on the same line (eg. "ELSE ... END IF;")
750 statementFound
= true;
751 if (sqlStates
.IsIntoCaseBlock(sqlStatesCurrentLine
) && sqlStates
.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine
)) {
752 sqlStatesCurrentLine
= sqlStates
.CaseMergeWithoutWhenFound(sqlStatesCurrentLine
, false);
755 // we are in same case "} ELSE {" in C language
758 } else if (strcmp(s
, "begin") == 0) {
760 sqlStatesCurrentLine
= sqlStates
.IntoDeclareBlock(sqlStatesCurrentLine
, false);
761 } else if ((strcmp(s
, "end") == 0) ||
762 // SQL Anywhere permits IF ... ELSE ... ENDIF
763 // will only be active if "endif" appears in the
765 (strcmp(s
, "endif") == 0)) {
768 if (sqlStates
.IsIntoSelectStatement(sqlStatesCurrentLine
) && !sqlStates
.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine
))
770 if (levelNext
< SC_FOLDLEVELBASE
) {
771 levelNext
= SC_FOLDLEVELBASE
;
772 isUnfoldingIgnored
= true;
774 } else if ((!options
.foldOnlyBegin
) &&
775 strcmp(s
, "when") == 0 &&
776 !sqlStates
.IsIgnoreWhen(sqlStatesCurrentLine
) &&
777 !sqlStates
.IsIntoExceptionBlock(sqlStatesCurrentLine
) && (
778 sqlStates
.IsIntoCaseBlock(sqlStatesCurrentLine
) ||
779 sqlStates
.IsIntoMergeStatement(sqlStatesCurrentLine
)
782 sqlStatesCurrentLine
= sqlStates
.IntoCondition(sqlStatesCurrentLine
, true);
784 // 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")
785 // and same way for MERGE statement.
786 if (!statementFound
) {
787 if (!sqlStates
.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine
)) {
791 sqlStatesCurrentLine
= sqlStates
.CaseMergeWithoutWhenFound(sqlStatesCurrentLine
, false);
793 } else if ((!options
.foldOnlyBegin
) && strcmp(s
, "exit") == 0) {
794 sqlStatesCurrentLine
= sqlStates
.IgnoreWhen(sqlStatesCurrentLine
, true);
795 } else if ((!options
.foldOnlyBegin
) && !sqlStates
.IsIntoDeclareBlock(sqlStatesCurrentLine
) && strcmp(s
, "exception") == 0) {
796 sqlStatesCurrentLine
= sqlStates
.IntoExceptionBlock(sqlStatesCurrentLine
, true);
797 } else if ((!options
.foldOnlyBegin
) &&
798 (strcmp(s
, "declare") == 0 ||
799 strcmp(s
, "function") == 0 ||
800 strcmp(s
, "procedure") == 0 ||
801 strcmp(s
, "package") == 0)) {
802 sqlStatesCurrentLine
= sqlStates
.IntoDeclareBlock(sqlStatesCurrentLine
, true);
803 } else if ((!options
.foldOnlyBegin
) &&
804 strcmp(s
, "merge") == 0) {
805 sqlStatesCurrentLine
= sqlStates
.IntoMergeStatement(sqlStatesCurrentLine
, true);
806 sqlStatesCurrentLine
= sqlStates
.CaseMergeWithoutWhenFound(sqlStatesCurrentLine
, true);
808 statementFound
= true;
812 int levelUse
= levelCurrent
;
813 int lev
= levelUse
| levelNext
<< 16;
814 if (visibleChars
== 0 && options
.foldCompact
)
815 lev
|= SC_FOLDLEVELWHITEFLAG
;
816 if (levelUse
< levelNext
)
817 lev
|= SC_FOLDLEVELHEADERFLAG
;
818 if (lev
!= styler
.LevelAt(lineCurrent
)) {
819 styler
.SetLevel(lineCurrent
, lev
);
822 levelCurrent
= levelNext
;
824 statementFound
= false;
825 if (!options
.foldOnlyBegin
)
826 sqlStates
.Set(lineCurrent
, sqlStatesCurrentLine
);
828 if (!isspacechar(ch
)) {
834 LexerModule
lmSQL(SCLEX_SQL
, LexerSQL::LexerFactorySQL
, "sql", sqlWordListDesc
);