Fix "Contributing to this document" for new HTML/PDF generation process
[geany-mirror.git] / scintilla / lexers / LexSQL.cxx
blobfa22f8e633c7465c2a384f9531b5e1249c085dfa
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 for (; sc.More(); sc.Forward(), offset++) {
448 // Determine if the current state should terminate.
449 switch (sc.state) {
450 case SCE_SQL_OPERATOR:
451 sc.SetState(SCE_SQL_DEFAULT);
452 break;
453 case SCE_SQL_NUMBER:
454 // We stop the number definition on non-numerical non-dot non-eE non-sign char
455 if (!IsANumberChar(sc.ch)) {
456 sc.SetState(SCE_SQL_DEFAULT);
458 break;
459 case SCE_SQL_IDENTIFIER:
460 if (!IsAWordChar(sc.ch, options.sqlAllowDottedWord)) {
461 int nextState = SCE_SQL_DEFAULT;
462 char s[1000];
463 sc.GetCurrentLowered(s, sizeof(s));
464 if (keywords1.InList(s)) {
465 sc.ChangeState(SCE_SQL_WORD);
466 } else if (keywords2.InList(s)) {
467 sc.ChangeState(SCE_SQL_WORD2);
468 } else if (kw_sqlplus.InListAbbreviated(s, '~')) {
469 sc.ChangeState(SCE_SQL_SQLPLUS);
470 if (strncmp(s, "rem", 3) == 0) {
471 nextState = SCE_SQL_SQLPLUS_COMMENT;
472 } else if (strncmp(s, "pro", 3) == 0) {
473 nextState = SCE_SQL_SQLPLUS_PROMPT;
475 } else if (kw_user1.InList(s)) {
476 sc.ChangeState(SCE_SQL_USER1);
477 } else if (kw_user2.InList(s)) {
478 sc.ChangeState(SCE_SQL_USER2);
479 } else if (kw_user3.InList(s)) {
480 sc.ChangeState(SCE_SQL_USER3);
481 } else if (kw_user4.InList(s)) {
482 sc.ChangeState(SCE_SQL_USER4);
484 sc.SetState(nextState);
486 break;
487 case SCE_SQL_QUOTEDIDENTIFIER:
488 if (sc.ch == 0x60) {
489 if (sc.chNext == 0x60) {
490 sc.Forward(); // Ignore it
491 } else {
492 sc.ForwardSetState(SCE_SQL_DEFAULT);
495 break;
496 case SCE_SQL_COMMENT:
497 if (sc.Match('*', '/')) {
498 sc.Forward();
499 sc.ForwardSetState(SCE_SQL_DEFAULT);
501 break;
502 case SCE_SQL_COMMENTDOC:
503 if (sc.Match('*', '/')) {
504 sc.Forward();
505 sc.ForwardSetState(SCE_SQL_DEFAULT);
506 } else if (sc.ch == '@' || sc.ch == '\\') { // Doxygen support
507 // Verify that we have the conditions to mark a comment-doc-keyword
508 if ((IsASpace(sc.chPrev) || sc.chPrev == '*') && (!IsASpace(sc.chNext))) {
509 styleBeforeDCKeyword = SCE_SQL_COMMENTDOC;
510 sc.SetState(SCE_SQL_COMMENTDOCKEYWORD);
513 break;
514 case SCE_SQL_COMMENTLINE:
515 case SCE_SQL_COMMENTLINEDOC:
516 case SCE_SQL_SQLPLUS_COMMENT:
517 case SCE_SQL_SQLPLUS_PROMPT:
518 if (sc.atLineStart) {
519 sc.SetState(SCE_SQL_DEFAULT);
521 break;
522 case SCE_SQL_COMMENTDOCKEYWORD:
523 if ((styleBeforeDCKeyword == SCE_SQL_COMMENTDOC) && sc.Match('*', '/')) {
524 sc.ChangeState(SCE_SQL_COMMENTDOCKEYWORDERROR);
525 sc.Forward();
526 sc.ForwardSetState(SCE_SQL_DEFAULT);
527 } else if (!IsADoxygenChar(sc.ch)) {
528 char s[100];
529 sc.GetCurrentLowered(s, sizeof(s));
530 if (!isspace(sc.ch) || !kw_pldoc.InList(s + 1)) {
531 sc.ChangeState(SCE_SQL_COMMENTDOCKEYWORDERROR);
533 sc.SetState(styleBeforeDCKeyword);
535 break;
536 case SCE_SQL_CHARACTER:
537 if (options.sqlBackslashEscapes && sc.ch == '\\') {
538 sc.Forward();
539 } else if (sc.ch == '\'') {
540 if (sc.chNext == '\"') {
541 sc.Forward();
542 } else {
543 sc.ForwardSetState(SCE_SQL_DEFAULT);
546 break;
547 case SCE_SQL_STRING:
548 if (sc.ch == '\\') {
549 // Escape sequence
550 sc.Forward();
551 } else if (sc.ch == '\"') {
552 if (sc.chNext == '\"') {
553 sc.Forward();
554 } else {
555 sc.ForwardSetState(SCE_SQL_DEFAULT);
558 break;
561 // Determine if a new state should be entered.
562 if (sc.state == SCE_SQL_DEFAULT) {
563 if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
564 sc.SetState(SCE_SQL_NUMBER);
565 } else if (IsAWordStart(sc.ch)) {
566 sc.SetState(SCE_SQL_IDENTIFIER);
567 } else if (sc.ch == 0x60 && options.sqlBackticksIdentifier) {
568 sc.SetState(SCE_SQL_QUOTEDIDENTIFIER);
569 } else if (sc.Match('/', '*')) {
570 if (sc.Match("/**") || sc.Match("/*!")) { // Support of Doxygen doc. style
571 sc.SetState(SCE_SQL_COMMENTDOC);
572 } else {
573 sc.SetState(SCE_SQL_COMMENT);
575 sc.Forward(); // Eat the * so it isn't used for the end of the comment
576 } else if (sc.Match('-', '-')) {
577 // MySQL requires a space or control char after --
578 // http://dev.mysql.com/doc/mysql/en/ansi-diff-comments.html
579 // Perhaps we should enforce that with proper property:
580 //~ } else if (sc.Match("-- ")) {
581 sc.SetState(SCE_SQL_COMMENTLINE);
582 } else if (sc.ch == '#' && options.sqlNumbersignComment) {
583 sc.SetState(SCE_SQL_COMMENTLINEDOC);
584 } else if (sc.ch == '\'') {
585 sc.SetState(SCE_SQL_CHARACTER);
586 } else if (sc.ch == '\"') {
587 sc.SetState(SCE_SQL_STRING);
588 } else if (isoperator(static_cast<char>(sc.ch))) {
589 sc.SetState(SCE_SQL_OPERATOR);
593 sc.Complete();
596 void SCI_METHOD LexerSQL::Fold(unsigned int startPos, int length, int initStyle, IDocument *pAccess) {
597 if (!options.fold)
598 return;
599 LexAccessor styler(pAccess);
600 unsigned int endPos = startPos + length;
601 int visibleChars = 0;
602 int lineCurrent = styler.GetLine(startPos);
603 int levelCurrent = SC_FOLDLEVELBASE;
605 if (lineCurrent > 0) {
606 // Backtrack to previous line in case need to fix its fold status for folding block of single-line comments (i.e. '--').
607 int lastNLPos = -1;
608 // And keep going back until we find an operator ';' followed
609 // by white-space and/or comments. This will improve folding.
610 while (--startPos > 0) {
611 char ch = styler[startPos];
612 if (ch == '\n' || (ch == '\r' && styler[startPos + 1] != '\n')) {
613 lastNLPos = startPos;
614 } else if (ch == ';' &&
615 styler.StyleAt(startPos) == SCE_SQL_OPERATOR) {
616 bool isAllClear = true;
617 for (int tempPos = startPos + 1;
618 tempPos < lastNLPos;
619 ++tempPos) {
620 int tempStyle = styler.StyleAt(tempPos);
621 if (!IsCommentStyle(tempStyle)
622 && tempStyle != SCE_SQL_DEFAULT) {
623 isAllClear = false;
624 break;
627 if (isAllClear) {
628 startPos = lastNLPos + 1;
629 break;
633 lineCurrent = styler.GetLine(startPos);
634 if (lineCurrent > 0)
635 levelCurrent = styler.LevelAt(lineCurrent - 1) >> 16;
637 // And because folding ends at ';', keep going until we find one
638 // Otherwise if create ... view ... as is split over multiple
639 // lines the folding won't always update immediately.
640 unsigned int docLength = styler.Length();
641 for (; endPos < docLength; ++endPos) {
642 if (styler.SafeGetCharAt(endPos) == ';') {
643 break;
647 int levelNext = levelCurrent;
648 char chNext = styler[startPos];
649 int styleNext = styler.StyleAt(startPos);
650 int style = initStyle;
651 bool endFound = false;
652 bool isUnfoldingIgnored = false;
653 // this statementFound flag avoids to fold when the statement is on only one line by ignoring ELSE or ELSIF
654 // eg. "IF condition1 THEN ... ELSIF condition2 THEN ... ELSE ... END IF;"
655 bool statementFound = false;
656 sql_state_t sqlStatesCurrentLine = 0;
657 if (!options.foldOnlyBegin) {
658 sqlStatesCurrentLine = sqlStates.ForLine(lineCurrent);
660 for (unsigned int i = startPos; i < endPos; i++) {
661 char ch = chNext;
662 chNext = styler.SafeGetCharAt(i + 1);
663 int stylePrev = style;
664 style = styleNext;
665 styleNext = styler.StyleAt(i + 1);
666 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
667 if (atEOL || (!IsCommentStyle(style) && ch == ';')) {
668 if (endFound) {
669 //Maybe this is the end of "EXCEPTION" BLOCK (eg. "BEGIN ... EXCEPTION ... END;")
670 sqlStatesCurrentLine = sqlStates.IntoExceptionBlock(sqlStatesCurrentLine, false);
672 // set endFound and isUnfoldingIgnored to false if EOL is reached or ';' is found
673 endFound = false;
674 isUnfoldingIgnored = false;
676 if ((!IsCommentStyle(style) && ch == ';')) {
677 if (sqlStates.IsIntoMergeStatement(sqlStatesCurrentLine)) {
678 // This is the end of "MERGE" statement.
679 if (!sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine))
680 levelNext--;
681 sqlStatesCurrentLine = sqlStates.IntoMergeStatement(sqlStatesCurrentLine, false);
682 levelNext--;
684 if (sqlStates.IsIntoSelectStatementOrAssignment(sqlStatesCurrentLine))
685 sqlStatesCurrentLine = sqlStates.IntoSelectStatementOrAssignment(sqlStatesCurrentLine, false);
686 if (sqlStates.IsIntoCreateStatement(sqlStatesCurrentLine)) {
687 if (sqlStates.IsIntoCreateViewStatement(sqlStatesCurrentLine)) {
688 if (sqlStates.IsIntoCreateViewAsStatement(sqlStatesCurrentLine)) {
689 levelNext--;
690 sqlStatesCurrentLine = sqlStates.IntoCreateViewAsStatement(sqlStatesCurrentLine, false);
692 sqlStatesCurrentLine = sqlStates.IntoCreateViewStatement(sqlStatesCurrentLine, false);
694 sqlStatesCurrentLine = sqlStates.IntoCreateStatement(sqlStatesCurrentLine, false);
697 if (ch == ':' && chNext == '=' && !IsCommentStyle(style))
698 sqlStatesCurrentLine = sqlStates.IntoSelectStatementOrAssignment(sqlStatesCurrentLine, true);
700 if (options.foldComment && IsStreamCommentStyle(style)) {
701 if (!IsStreamCommentStyle(stylePrev)) {
702 levelNext++;
703 } else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
704 // Comments don't end at end of line and the next character may be unstyled.
705 levelNext--;
708 if (options.foldComment && (style == SCE_SQL_COMMENTLINE)) {
709 // MySQL needs -- comments to be followed by space or control char
710 if ((ch == '-') && (chNext == '-')) {
711 char chNext2 = styler.SafeGetCharAt(i + 2);
712 char chNext3 = styler.SafeGetCharAt(i + 3);
713 if (chNext2 == '{' || chNext3 == '{') {
714 levelNext++;
715 } else if (chNext2 == '}' || chNext3 == '}') {
716 levelNext--;
720 // Fold block of single-line comments (i.e. '--').
721 if (options.foldComment && atEOL && IsCommentLine(lineCurrent, styler)) {
722 if (!IsCommentLine(lineCurrent - 1, styler) && IsCommentLine(lineCurrent + 1, styler))
723 levelNext++;
724 else if (IsCommentLine(lineCurrent - 1, styler) && !IsCommentLine(lineCurrent + 1, styler))
725 levelNext--;
727 if (style == SCE_SQL_OPERATOR) {
728 if (ch == '(') {
729 if (levelCurrent > levelNext)
730 levelCurrent--;
731 levelNext++;
732 } else if (ch == ')') {
733 levelNext--;
734 } else if ((!options.foldOnlyBegin) && ch == ';') {
735 sqlStatesCurrentLine = sqlStates.IgnoreWhen(sqlStatesCurrentLine, false);
738 // If new keyword (cannot trigger on elseif or nullif, does less tests)
739 if (style == SCE_SQL_WORD && stylePrev != SCE_SQL_WORD) {
740 const int MAX_KW_LEN = 9; // Maximum length of folding keywords
741 char s[MAX_KW_LEN + 2];
742 unsigned int j = 0;
743 for (; j < MAX_KW_LEN + 1; j++) {
744 if (!iswordchar(styler[i + j])) {
745 break;
747 s[j] = static_cast<char>(tolower(styler[i + j]));
749 if (j == MAX_KW_LEN + 1) {
750 // Keyword too long, don't test it
751 s[0] = '\0';
752 } else {
753 s[j] = '\0';
755 if (!options.foldOnlyBegin &&
756 strcmp(s, "select") == 0) {
757 sqlStatesCurrentLine = sqlStates.IntoSelectStatementOrAssignment(sqlStatesCurrentLine, true);
758 } else if (strcmp(s, "if") == 0) {
759 if (endFound) {
760 endFound = false;
761 if (options.foldOnlyBegin && !isUnfoldingIgnored) {
762 // this end isn't for begin block, but for if block ("end if;")
763 // so ignore previous "end" by increment levelNext.
764 levelNext++;
766 } else {
767 if (!options.foldOnlyBegin)
768 sqlStatesCurrentLine = sqlStates.IntoCondition(sqlStatesCurrentLine, true);
769 if (levelCurrent > levelNext) {
770 // doesn't include this line into the folding block
771 // because doesn't hide IF (eg "END; IF")
772 levelCurrent = levelNext;
775 } else if (!options.foldOnlyBegin &&
776 strcmp(s, "then") == 0 &&
777 sqlStates.IsIntoCondition(sqlStatesCurrentLine)) {
778 sqlStatesCurrentLine = sqlStates.IntoCondition(sqlStatesCurrentLine, false);
779 if (!options.foldOnlyBegin) {
780 if (levelCurrent > levelNext) {
781 levelCurrent = levelNext;
783 if (!statementFound)
784 levelNext++;
786 statementFound = true;
787 } else if (levelCurrent > levelNext) {
788 // doesn't include this line into the folding block
789 // because doesn't hide LOOP or CASE (eg "END; LOOP" or "END; CASE")
790 levelCurrent = levelNext;
792 } else if (strcmp(s, "loop") == 0 ||
793 strcmp(s, "case") == 0) {
794 if (endFound) {
795 endFound = false;
796 if (options.foldOnlyBegin && !isUnfoldingIgnored) {
797 // this end isn't for begin block, but for loop block ("end loop;") or case block ("end case;")
798 // so ignore previous "end" by increment levelNext.
799 levelNext++;
801 if ((!options.foldOnlyBegin) && strcmp(s, "case") == 0) {
802 sqlStatesCurrentLine = sqlStates.EndCaseBlock(sqlStatesCurrentLine);
803 if (!sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine))
804 levelNext--; //again for the "end case;" and block when
806 } else if (!options.foldOnlyBegin) {
807 if (strcmp(s, "case") == 0) {
808 sqlStatesCurrentLine = sqlStates.BeginCaseBlock(sqlStatesCurrentLine);
809 sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, true);
812 if (levelCurrent > levelNext)
813 levelCurrent = levelNext;
815 if (!statementFound)
816 levelNext++;
818 statementFound = true;
819 } else if (levelCurrent > levelNext) {
820 // doesn't include this line into the folding block
821 // because doesn't hide LOOP or CASE (eg "END; LOOP" or "END; CASE")
822 levelCurrent = levelNext;
824 } else if ((!options.foldOnlyBegin) && (
825 // folding for ELSE and ELSIF block only if foldAtElse is set
826 // and IF or CASE aren't on only one line with ELSE or ELSIF (with flag statementFound)
827 options.foldAtElse && !statementFound) && strcmp(s, "elsif") == 0) {
828 sqlStatesCurrentLine = sqlStates.IntoCondition(sqlStatesCurrentLine, true);
829 levelCurrent--;
830 levelNext--;
831 } else if ((!options.foldOnlyBegin) && (
832 // folding for ELSE and ELSIF block only if foldAtElse is set
833 // and IF or CASE aren't on only one line with ELSE or ELSIF (with flag statementFound)
834 options.foldAtElse && !statementFound) && strcmp(s, "else") == 0) {
835 // prevent also ELSE is on the same line (eg. "ELSE ... END IF;")
836 statementFound = true;
837 if (sqlStates.IsIntoCaseBlock(sqlStatesCurrentLine) && sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine)) {
838 sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, false);
839 levelNext++;
840 } else {
841 // we are in same case "} ELSE {" in C language
842 levelCurrent--;
844 } else if (strcmp(s, "begin") == 0) {
845 levelNext++;
846 sqlStatesCurrentLine = sqlStates.IntoDeclareBlock(sqlStatesCurrentLine, false);
847 } else if ((strcmp(s, "end") == 0) ||
848 // SQL Anywhere permits IF ... ELSE ... ENDIF
849 // will only be active if "endif" appears in the
850 // keyword list.
851 (strcmp(s, "endif") == 0)) {
852 endFound = true;
853 levelNext--;
854 if (sqlStates.IsIntoSelectStatementOrAssignment(sqlStatesCurrentLine) && !sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine))
855 levelNext--;
856 if (levelNext < SC_FOLDLEVELBASE) {
857 levelNext = SC_FOLDLEVELBASE;
858 isUnfoldingIgnored = true;
860 } else if ((!options.foldOnlyBegin) &&
861 strcmp(s, "when") == 0 &&
862 !sqlStates.IsIgnoreWhen(sqlStatesCurrentLine) &&
863 !sqlStates.IsIntoExceptionBlock(sqlStatesCurrentLine) && (
864 sqlStates.IsIntoCaseBlock(sqlStatesCurrentLine) ||
865 sqlStates.IsIntoMergeStatement(sqlStatesCurrentLine)
868 sqlStatesCurrentLine = sqlStates.IntoCondition(sqlStatesCurrentLine, true);
870 // 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")
871 // and same way for MERGE statement.
872 if (!statementFound) {
873 if (!sqlStates.IsCaseMergeWithoutWhenFound(sqlStatesCurrentLine)) {
874 levelCurrent--;
875 levelNext--;
877 sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, false);
879 } else if ((!options.foldOnlyBegin) && strcmp(s, "exit") == 0) {
880 sqlStatesCurrentLine = sqlStates.IgnoreWhen(sqlStatesCurrentLine, true);
881 } else if ((!options.foldOnlyBegin) && !sqlStates.IsIntoDeclareBlock(sqlStatesCurrentLine) && strcmp(s, "exception") == 0) {
882 sqlStatesCurrentLine = sqlStates.IntoExceptionBlock(sqlStatesCurrentLine, true);
883 } else if ((!options.foldOnlyBegin) &&
884 (strcmp(s, "declare") == 0 ||
885 strcmp(s, "function") == 0 ||
886 strcmp(s, "procedure") == 0 ||
887 strcmp(s, "package") == 0)) {
888 sqlStatesCurrentLine = sqlStates.IntoDeclareBlock(sqlStatesCurrentLine, true);
889 } else if ((!options.foldOnlyBegin) &&
890 strcmp(s, "merge") == 0) {
891 sqlStatesCurrentLine = sqlStates.IntoMergeStatement(sqlStatesCurrentLine, true);
892 sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, true);
893 levelNext++;
894 statementFound = true;
895 } else if ((!options.foldOnlyBegin) &&
896 strcmp(s, "create") == 0) {
897 sqlStatesCurrentLine = sqlStates.IntoCreateStatement(sqlStatesCurrentLine, true);
898 } else if ((!options.foldOnlyBegin) &&
899 strcmp(s, "view") == 0 &&
900 sqlStates.IsIntoCreateStatement(sqlStatesCurrentLine)) {
901 sqlStatesCurrentLine = sqlStates.IntoCreateViewStatement(sqlStatesCurrentLine, true);
902 } else if ((!options.foldOnlyBegin) &&
903 strcmp(s, "as") == 0 &&
904 sqlStates.IsIntoCreateViewStatement(sqlStatesCurrentLine) &&
905 ! sqlStates.IsIntoCreateViewAsStatement(sqlStatesCurrentLine)) {
906 sqlStatesCurrentLine = sqlStates.IntoCreateViewAsStatement(sqlStatesCurrentLine, true);
907 levelNext++;
910 if (atEOL) {
911 int levelUse = levelCurrent;
912 int lev = levelUse | levelNext << 16;
913 if (visibleChars == 0 && options.foldCompact)
914 lev |= SC_FOLDLEVELWHITEFLAG;
915 if (levelUse < levelNext)
916 lev |= SC_FOLDLEVELHEADERFLAG;
917 if (lev != styler.LevelAt(lineCurrent)) {
918 styler.SetLevel(lineCurrent, lev);
920 lineCurrent++;
921 levelCurrent = levelNext;
922 visibleChars = 0;
923 statementFound = false;
924 if (!options.foldOnlyBegin)
925 sqlStates.Set(lineCurrent, sqlStatesCurrentLine);
927 if (!isspacechar(ch)) {
928 visibleChars++;
933 LexerModule lmSQL(SCLEX_SQL, LexerSQL::LexerFactorySQL, "sql", sqlWordListDesc);