]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexSQL.cxx
wxFormatConverter test rewritten for CppUnit (patch #910051)
[wxWidgets.git] / src / stc / scintilla / src / LexSQL.cxx
1 // Scintilla source code edit control
2 /** @file LexSQL.cxx
3 ** Lexer for SQL.
4 **/
5 // Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13
14 #include "Platform.h"
15
16 #include "PropSet.h"
17 #include "Accessor.h"
18 #include "KeyWords.h"
19 #include "Scintilla.h"
20 #include "SciLexer.h"
21
22 static void classifyWordSQL(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler) {
23 char s[100];
24 bool wordIsNumber = isdigit(styler[start]) || (styler[start] == '.');
25 for (unsigned int i = 0; i < end - start + 1 && i < 30; i++) {
26 s[i] = static_cast<char>(tolower(styler[start + i]));
27 s[i + 1] = '\0';
28 }
29 char chAttr = SCE_C_IDENTIFIER;
30 if (wordIsNumber)
31 chAttr = SCE_C_NUMBER;
32 else {
33 if (keywords.InList(s))
34 chAttr = SCE_C_WORD;
35 }
36 styler.ColourTo(end, chAttr);
37 }
38
39 static void ColouriseSQLDoc(unsigned int startPos, int length,
40 int initStyle, WordList *keywordlists[], Accessor &styler) {
41
42 WordList &keywords = *keywordlists[0];
43
44 styler.StartAt(startPos);
45
46 bool fold = styler.GetPropertyInt("fold") != 0;
47 int lineCurrent = styler.GetLine(startPos);
48 int spaceFlags = 0;
49
50 int state = initStyle;
51 char chPrev = ' ';
52 char chNext = styler[startPos];
53 styler.StartSegment(startPos);
54 unsigned int lengthDoc = startPos + length;
55 for (unsigned int i = startPos; i < lengthDoc; i++) {
56 char ch = chNext;
57 chNext = styler.SafeGetCharAt(i + 1);
58
59 if ((ch == '\r' && chNext != '\n') || (ch == '\n')) {
60 int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags);
61 int lev = indentCurrent;
62 if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
63 // Only non whitespace lines can be headers
64 int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags);
65 if (indentCurrent < (indentNext & ~SC_FOLDLEVELWHITEFLAG)) {
66 lev |= SC_FOLDLEVELHEADERFLAG;
67 }
68 }
69 if (fold) {
70 styler.SetLevel(lineCurrent, lev);
71 }
72 }
73
74 if (styler.IsLeadByte(ch)) {
75 chNext = styler.SafeGetCharAt(i + 2);
76 chPrev = ' ';
77 i += 1;
78 continue;
79 }
80
81 if (state == SCE_C_DEFAULT) {
82 if (iswordstart(ch)) {
83 styler.ColourTo(i - 1, state);
84 state = SCE_C_WORD;
85 } else if (ch == '/' && chNext == '*') {
86 styler.ColourTo(i - 1, state);
87 state = SCE_C_COMMENT;
88 } else if (ch == '-' && chNext == '-') {
89 styler.ColourTo(i - 1, state);
90 state = SCE_C_COMMENTLINE;
91 } else if (ch == '\'') {
92 styler.ColourTo(i - 1, state);
93 state = SCE_C_CHARACTER;
94 } else if (ch == '"') {
95 styler.ColourTo(i - 1, state);
96 state = SCE_C_STRING;
97 } else if (isoperator(ch)) {
98 styler.ColourTo(i - 1, state);
99 styler.ColourTo(i, SCE_C_OPERATOR);
100 }
101 } else if (state == SCE_C_WORD) {
102 if (!iswordchar(ch)) {
103 classifyWordSQL(styler.GetStartSegment(), i - 1, keywords, styler);
104 state = SCE_C_DEFAULT;
105 if (ch == '/' && chNext == '*') {
106 state = SCE_C_COMMENT;
107 } else if (ch == '-' && chNext == '-') {
108 state = SCE_C_COMMENTLINE;
109 } else if (ch == '\'') {
110 state = SCE_C_CHARACTER;
111 } else if (ch == '"') {
112 state = SCE_C_STRING;
113 } else if (isoperator(ch)) {
114 styler.ColourTo(i, SCE_C_OPERATOR);
115 }
116 }
117 } else {
118 if (state == SCE_C_COMMENT) {
119 if (ch == '/' && chPrev == '*') {
120 if (((i > (styler.GetStartSegment() + 2)) || ((initStyle == SCE_C_COMMENT) &&
121 (styler.GetStartSegment() == startPos)))) {
122 styler.ColourTo(i, state);
123 state = SCE_C_DEFAULT;
124 }
125 }
126 } else if (state == SCE_C_COMMENTLINE) {
127 if (ch == '\r' || ch == '\n') {
128 styler.ColourTo(i - 1, state);
129 state = SCE_C_DEFAULT;
130 }
131 } else if (state == SCE_C_CHARACTER) {
132 if (ch == '\'') {
133 if ( chNext == '\'' ) {
134 i++;
135 } else {
136 styler.ColourTo(i, state);
137 state = SCE_C_DEFAULT;
138 i++;
139 }
140 ch = chNext;
141 chNext = styler.SafeGetCharAt(i + 1);
142 }
143 } else if (state == SCE_C_STRING) {
144 if (ch == '"') {
145 if (chNext == '"') {
146 i++;
147 } else {
148 styler.ColourTo(i, state);
149 state = SCE_C_DEFAULT;
150 i++;
151 }
152 ch = chNext;
153 chNext = styler.SafeGetCharAt(i + 1);
154 }
155 }
156 if (state == SCE_C_DEFAULT) { // One of the above succeeded
157 if (ch == '/' && chNext == '*') {
158 state = SCE_C_COMMENT;
159 } else if (ch == '-' && chNext == '-') {
160 state = SCE_C_COMMENTLINE;
161 } else if (ch == '\'') {
162 state = SCE_C_CHARACTER;
163 } else if (ch == '"') {
164 state = SCE_C_STRING;
165 } else if (iswordstart(ch)) {
166 state = SCE_C_WORD;
167 } else if (isoperator(ch)) {
168 styler.ColourTo(i, SCE_C_OPERATOR);
169 }
170 }
171 }
172 chPrev = ch;
173 }
174 styler.ColourTo(lengthDoc - 1, state);
175 }
176
177 static const char * const sqlWordListDesc[] = {
178 "Keywords",
179 0
180 };
181
182 LexerModule lmSQL(SCLEX_SQL, ColouriseSQLDoc, "sql", 0, sqlWordListDesc);