Diff of /lexers/LexSQL.cxx [9642b6] .. [1daf52]  Maximize  Restore

Switch to unified view

a/lexers/LexSQL.cxx b/lexers/LexSQL.cxx
...
...
60
    return (ch < 0x80) &&
60
    return (ch < 0x80) &&
61
           (isdigit(ch) || toupper(ch) == 'E' ||
61
           (isdigit(ch) || toupper(ch) == 'E' ||
62
            ch == '.' || ch == '-' || ch == '+');
62
            ch == '.' || ch == '-' || ch == '+');
63
}
63
}
64
64
65
typedef unsigned int sql_state_t;
65
66
66
class SQLStates {
67
class SQLStates {
67
public :
68
public :
68
    void Set(int lineNumber, unsigned short int sqlStatesLine) {
69
    void Set(int lineNumber, unsigned short int sqlStatesLine) {
69
        sqlStatement.Set(lineNumber, sqlStatesLine);
70
        sqlStatement.Set(lineNumber, sqlStatesLine);
70
    }
71
    }
71
72
72
  unsigned short int IgnoreWhen (unsigned short int sqlStatesLine, bool enable) {
73
  sql_state_t IgnoreWhen (sql_state_t sqlStatesLine, bool enable) {
73
        if (enable)
74
        if (enable)
74
            sqlStatesLine |= MASK_IGNORE_WHEN;
75
            sqlStatesLine |= MASK_IGNORE_WHEN;
75
        else
76
        else
76
            sqlStatesLine &= ~MASK_IGNORE_WHEN;
77
            sqlStatesLine &= ~MASK_IGNORE_WHEN;
77
78
78
        return sqlStatesLine;
79
        return sqlStatesLine;
79
    }
80
    }
80
81
81
  unsigned short int IntoCondition (unsigned short int sqlStatesLine, bool enable) {
82
  sql_state_t IntoCondition (sql_state_t sqlStatesLine, bool enable) {
82
        if (enable)
83
        if (enable)
83
            sqlStatesLine |= MASK_INTO_CONDITION;
84
            sqlStatesLine |= MASK_INTO_CONDITION;
84
        else
85
        else
85
            sqlStatesLine &= ~MASK_INTO_CONDITION;
86
            sqlStatesLine &= ~MASK_INTO_CONDITION;
86
87
87
        return sqlStatesLine;
88
        return sqlStatesLine;
88
    }
89
    }
89
90
90
  unsigned short int IntoExceptionBlock (unsigned short int sqlStatesLine, bool enable) {
91
  sql_state_t IntoExceptionBlock (sql_state_t sqlStatesLine, bool enable) {
91
        if (enable)
92
        if (enable)
92
            sqlStatesLine |= MASK_INTO_EXCEPTION;
93
            sqlStatesLine |= MASK_INTO_EXCEPTION;
93
        else
94
        else
94
            sqlStatesLine &= ~MASK_INTO_EXCEPTION;
95
            sqlStatesLine &= ~MASK_INTO_EXCEPTION;
95
96
96
        return sqlStatesLine;
97
        return sqlStatesLine;
97
    }
98
    }
98
99
99
  unsigned short int IntoDeclareBlock (unsigned short int sqlStatesLine, bool enable) {
100
  sql_state_t IntoDeclareBlock (sql_state_t sqlStatesLine, bool enable) {
100
        if (enable)
101
        if (enable)
101
            sqlStatesLine |= MASK_INTO_DECLARE;
102
            sqlStatesLine |= MASK_INTO_DECLARE;
102
        else
103
        else
103
            sqlStatesLine &= ~MASK_INTO_DECLARE;
104
            sqlStatesLine &= ~MASK_INTO_DECLARE;
104
105
105
        return sqlStatesLine;
106
        return sqlStatesLine;
106
    }
107
    }
107
108
108
  unsigned short int IntoMergeStatement (unsigned short int sqlStatesLine, bool enable) {
109
  sql_state_t IntoMergeStatement (sql_state_t sqlStatesLine, bool enable) {
109
        if (enable)
110
        if (enable)
110
            sqlStatesLine |= MASK_MERGE_STATEMENT;
111
            sqlStatesLine |= MASK_MERGE_STATEMENT;
111
        else
112
        else
112
            sqlStatesLine &= ~MASK_MERGE_STATEMENT;
113
            sqlStatesLine &= ~MASK_MERGE_STATEMENT;
113
114
114
        return sqlStatesLine;
115
        return sqlStatesLine;
115
    }
116
    }
116
117
117
  unsigned short int CaseMergeWithoutWhenFound (unsigned short int sqlStatesLine, bool found) {
118
  sql_state_t CaseMergeWithoutWhenFound (sql_state_t sqlStatesLine, bool found) {
118
        if (found)
119
        if (found)
119
            sqlStatesLine |= MASK_CASE_MERGE_WITHOUT_WHEN_FOUND;
120
            sqlStatesLine |= MASK_CASE_MERGE_WITHOUT_WHEN_FOUND;
120
        else
121
        else
121
            sqlStatesLine &= ~MASK_CASE_MERGE_WITHOUT_WHEN_FOUND;
122
            sqlStatesLine &= ~MASK_CASE_MERGE_WITHOUT_WHEN_FOUND;
122
123
123
        return sqlStatesLine;
124
        return sqlStatesLine;
124
    }
125
    }
125
    unsigned short int IntoSelectStatementOrAssignment (unsigned short int sqlStatesLine, bool found) {
126
    sql_state_t IntoSelectStatementOrAssignment (sql_state_t sqlStatesLine, bool found) {
126
        if (found)
127
        if (found)
127
            sqlStatesLine |= MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT;
128
            sqlStatesLine |= MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT;
128
        else
129
        else
129
            sqlStatesLine &= ~MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT;
130
            sqlStatesLine &= ~MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT;
130
        return sqlStatesLine;
131
        return sqlStatesLine;
131
    }
132
    }
132
133
133
  unsigned short int BeginCaseBlock (unsigned short int sqlStatesLine) {
134
  sql_state_t BeginCaseBlock (sql_state_t sqlStatesLine) {
134
        if ((sqlStatesLine & MASK_NESTED_CASES) < MASK_NESTED_CASES) {
135
        if ((sqlStatesLine & MASK_NESTED_CASES) < MASK_NESTED_CASES) {
135
            sqlStatesLine++;
136
            sqlStatesLine++;
136
        }
137
        }
137
        return sqlStatesLine;
138
        return sqlStatesLine;
138
    }
139
    }
139
140
140
  unsigned short int EndCaseBlock (unsigned short int sqlStatesLine) {
141
  sql_state_t EndCaseBlock (sql_state_t sqlStatesLine) {
141
        if ((sqlStatesLine & MASK_NESTED_CASES) > 0) {
142
        if ((sqlStatesLine & MASK_NESTED_CASES) > 0) {
142
            sqlStatesLine--;
143
            sqlStatesLine--;
143
        }
144
        }
144
        return sqlStatesLine;
145
        return sqlStatesLine;
145
    }
146
    }
146
147
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;
153
154
      return sqlStatesLine;
155
  }
156
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;
162
163
      return sqlStatesLine;
164
  }
165
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;
171
172
      return sqlStatesLine;
173
  }
174
147
    bool IsIgnoreWhen (unsigned short int sqlStatesLine) {
175
    bool IsIgnoreWhen (sql_state_t sqlStatesLine) {
148
        return (sqlStatesLine & MASK_IGNORE_WHEN) != 0;
176
        return (sqlStatesLine & MASK_IGNORE_WHEN) != 0;
149
    }
177
    }
150
178
151
    bool IsIntoCondition (unsigned short int sqlStatesLine) {
179
    bool IsIntoCondition (sql_state_t sqlStatesLine) {
152
        return (sqlStatesLine & MASK_INTO_CONDITION) != 0;
180
        return (sqlStatesLine & MASK_INTO_CONDITION) != 0;
153
    }
181
    }
154
182
155
    bool IsIntoCaseBlock (unsigned short int sqlStatesLine) {
183
    bool IsIntoCaseBlock (sql_state_t sqlStatesLine) {
156
        return (sqlStatesLine & MASK_NESTED_CASES) != 0;
184
        return (sqlStatesLine & MASK_NESTED_CASES) != 0;
157
    }
185
    }
158
186
159
    bool IsIntoExceptionBlock (unsigned short int sqlStatesLine) {
187
    bool IsIntoExceptionBlock (sql_state_t sqlStatesLine) {
160
        return (sqlStatesLine & MASK_INTO_EXCEPTION) != 0;
188
        return (sqlStatesLine & MASK_INTO_EXCEPTION) != 0;
161
    }
189
    }
162
    bool IsIntoSelectStatementOrAssignment (unsigned short int sqlStatesLine) {
190
    bool IsIntoSelectStatementOrAssignment (sql_state_t sqlStatesLine) {
163
        return (sqlStatesLine & MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT) != 0;
191
        return (sqlStatesLine & MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT) != 0;
164
    }
192
    }
165
    bool IsCaseMergeWithoutWhenFound (unsigned short int sqlStatesLine) {
193
    bool IsCaseMergeWithoutWhenFound (sql_state_t sqlStatesLine) {
166
        return (sqlStatesLine & MASK_CASE_MERGE_WITHOUT_WHEN_FOUND) != 0;
194
        return (sqlStatesLine & MASK_CASE_MERGE_WITHOUT_WHEN_FOUND) != 0;
167
    }
195
    }
168
196
169
    bool IsIntoDeclareBlock (unsigned short int sqlStatesLine) {
197
    bool IsIntoDeclareBlock (sql_state_t sqlStatesLine) {
170
        return (sqlStatesLine & MASK_INTO_DECLARE) != 0;
198
        return (sqlStatesLine & MASK_INTO_DECLARE) != 0;
171
    }
199
    }
172
200
173
    bool IsIntoMergeStatement (unsigned short int sqlStatesLine) {
201
    bool IsIntoMergeStatement (sql_state_t sqlStatesLine) {
174
        return (sqlStatesLine & MASK_MERGE_STATEMENT) != 0;
202
        return (sqlStatesLine & MASK_MERGE_STATEMENT) != 0;
175
    }
203
    }
176
204
177
  unsigned short int ForLine(int lineNumber) {
205
  bool IsIntoCreateStatement (sql_state_t sqlStatesLine) {
206
      return (sqlStatesLine & MASK_INTO_CREATE) != 0;
207
  }
208
209
  bool IsIntoCreateViewStatement (sql_state_t sqlStatesLine) {
210
      return (sqlStatesLine & MASK_INTO_CREATE_VIEW) != 0;
211
  }
212
213
  bool IsIntoCreateViewAsStatement (sql_state_t sqlStatesLine) {
214
      return (sqlStatesLine & MASK_INTO_CREATE_VIEW_AS_STATEMENT) != 0;
215
  }
216
217
  sql_state_t ForLine(int lineNumber) {
178
        return sqlStatement.ValueAt(lineNumber);
218
        return sqlStatement.ValueAt(lineNumber);
179
    }
219
    }
180
220
181
    SQLStates() {}
221
    SQLStates() {}
182
222
183
private :
223
private :
184
  SparseState <unsigned short int> sqlStatement;
224
  SparseState <sql_state_t> sqlStatement;
185
    enum {
225
    enum {
186
      MASK_NESTED_CASES = 0x01FF,
226
      MASK_NESTED_CASES                         = 0x0001FF,
187
        MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT = 0x0200,
227
        MASK_INTO_SELECT_STATEMENT_OR_ASSIGNEMENT = 0x000200,
188
        MASK_CASE_MERGE_WITHOUT_WHEN_FOUND = 0x0400,
228
        MASK_CASE_MERGE_WITHOUT_WHEN_FOUND        = 0x000400,
189
      MASK_MERGE_STATEMENT = 0x0800,
229
      MASK_MERGE_STATEMENT                      = 0x000800,
190
      MASK_INTO_DECLARE = 0x1000,
230
      MASK_INTO_DECLARE                         = 0x001000,
191
      MASK_INTO_EXCEPTION = 0x2000,
231
      MASK_INTO_EXCEPTION                       = 0x002000,
192
      MASK_INTO_CONDITION = 0x4000,
232
      MASK_INTO_CONDITION                       = 0x004000,
193
      MASK_IGNORE_WHEN = 0x8000
233
      MASK_IGNORE_WHEN                          = 0x008000,
234
      MASK_INTO_CREATE                          = 0x010000,
235
      MASK_INTO_CREATE_VIEW                     = 0x020000,
236
      MASK_INTO_CREATE_VIEW_AS_STATEMENT        = 0x040000
194
    };
237
    };
195
};
238
};
196
239
197
// Options used for LexerSQL
240
// Options used for LexerSQL
198
struct OptionsSQL {
241
struct OptionsSQL {
...
...
559
    int lineCurrent = styler.GetLine(startPos);
602
    int lineCurrent = styler.GetLine(startPos);
560
    int levelCurrent = SC_FOLDLEVELBASE;
603
    int levelCurrent = SC_FOLDLEVELBASE;
561
604
562
    if (lineCurrent > 0) {
605
    if (lineCurrent > 0) {
563
        // Backtrack to previous line in case need to fix its fold status for folding block of single-line comments (i.e. '--').
606
        // Backtrack to previous line in case need to fix its fold status for folding block of single-line comments (i.e. '--').
564
      lineCurrent -= 1;
607
      int lastNLPos = -1;
565
      startPos = styler.LineStart(lineCurrent);
608
      // And keep going back until we find an operator ';' followed
566
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;
625
                  }
626
              }
627
              if (isAllClear) {
628
                  startPos = lastNLPos + 1;
629
                  break;
630
              }
631
          }
632
      }
633
      lineCurrent = styler.GetLine(startPos);
567
        if (lineCurrent > 0)
634
        if (lineCurrent > 0)
568
            levelCurrent = styler.LevelAt(lineCurrent - 1) >> 16;
635
            levelCurrent = styler.LevelAt(lineCurrent - 1) >> 16;
569
    }
636
    }
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;
644
      }
645
  }
646
  
570
    int levelNext = levelCurrent;
647
    int levelNext = levelCurrent;
571
    char chNext = styler[startPos];
648
    char chNext = styler[startPos];
572
    int styleNext = styler.StyleAt(startPos);
649
    int styleNext = styler.StyleAt(startPos);
573
    int style = initStyle;
650
    int style = initStyle;
574
    bool endFound = false;
651
    bool endFound = false;
575
    bool isUnfoldingIgnored = false;
652
    bool isUnfoldingIgnored = false;
576
    // this statementFound flag avoids to fold when the statement is on only one line by ignoring ELSE or ELSIF
653
    // this statementFound flag avoids to fold when the statement is on only one line by ignoring ELSE or ELSIF
577
    // eg. "IF condition1 THEN ... ELSIF condition2 THEN ... ELSE ... END IF;"
654
    // eg. "IF condition1 THEN ... ELSIF condition2 THEN ... ELSE ... END IF;"
578
    bool statementFound = false;
655
    bool statementFound = false;
579
  unsigned short int sqlStatesCurrentLine = 0;
656
  sql_state_t sqlStatesCurrentLine = 0;
580
    if (!options.foldOnlyBegin) {
657
    if (!options.foldOnlyBegin) {
581
        sqlStatesCurrentLine = sqlStates.ForLine(lineCurrent);
658
        sqlStatesCurrentLine = sqlStates.ForLine(lineCurrent);
582
    }
659
    }
583
    for (unsigned int i = startPos; i < endPos; i++) {
660
    for (unsigned int i = startPos; i < endPos; i++) {
584
        char ch = chNext;
661
        char ch = chNext;
...
...
604
                sqlStatesCurrentLine = sqlStates.IntoMergeStatement(sqlStatesCurrentLine, false);
681
                sqlStatesCurrentLine = sqlStates.IntoMergeStatement(sqlStatesCurrentLine, false);
605
                levelNext--;
682
                levelNext--;
606
            }
683
            }
607
            if (sqlStates.IsIntoSelectStatementOrAssignment(sqlStatesCurrentLine))
684
            if (sqlStates.IsIntoSelectStatementOrAssignment(sqlStatesCurrentLine))
608
                sqlStatesCurrentLine = sqlStates.IntoSelectStatementOrAssignment(sqlStatesCurrentLine, false);
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);
691
                  }
692
                  sqlStatesCurrentLine = sqlStates.IntoCreateViewStatement(sqlStatesCurrentLine, false);
693
              }
694
              sqlStatesCurrentLine = sqlStates.IntoCreateStatement(sqlStatesCurrentLine, false);
695
          }
609
        }
696
        }
610
        if (ch == ':' && chNext == '=' && !IsCommentStyle(style))
697
        if (ch == ':' && chNext == '=' && !IsCommentStyle(style))
611
            sqlStatesCurrentLine = sqlStates.IntoSelectStatementOrAssignment(sqlStatesCurrentLine, true);
698
            sqlStatesCurrentLine = sqlStates.IntoSelectStatementOrAssignment(sqlStatesCurrentLine, true);
612
699
613
        if (options.foldComment && IsStreamCommentStyle(style)) {
700
        if (options.foldComment && IsStreamCommentStyle(style)) {
...
...
803
                       strcmp(s, "merge") == 0) {
890
                       strcmp(s, "merge") == 0) {
804
                sqlStatesCurrentLine = sqlStates.IntoMergeStatement(sqlStatesCurrentLine, true);
891
                sqlStatesCurrentLine = sqlStates.IntoMergeStatement(sqlStatesCurrentLine, true);
805
                sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, true);
892
                sqlStatesCurrentLine = sqlStates.CaseMergeWithoutWhenFound(sqlStatesCurrentLine, true);
806
                levelNext++;
893
                levelNext++;
807
                statementFound = true;
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++;
808
            }
908
            }
809
        }
909
        }
810
        if (atEOL) {
910
        if (atEOL) {
811
            int levelUse = levelCurrent;
911
            int levelUse = levelCurrent;
812
            int lev = levelUse | levelNext << 16;
912
            int lev = levelUse | levelNext << 16;

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks