Debian Codegraph @

scintilla/lexilla/lexers/LexSQL.cxx

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