]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexSQL.cxx
0f29c830749f36e65afbd6d8fe1f8734c9ebc75f
[wxWidgets.git] / src / stc / scintilla / src / LexSQL.cxx
1 // SciTE - Scintilla based Text Editor
2 // LexSQL.cxx - lexer for SQL
3 // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org>
4 // The License.txt file describes the conditions under which this software may be distributed.
5
6 #include <stdlib.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include <stdio.h>
10 #include <stdarg.h>
11
12 #include "Platform.h"
13
14 #include "PropSet.h"
15 #include "Accessor.h"
16 #include "KeyWords.h"
17 #include "Scintilla.h"
18 #include "SciLexer.h"
19
20 static void classifyWordSQL(unsigned int start, unsigned int end, WordList &keywords, StylingContext &styler) {
21 char s[100];
22 bool wordIsNumber = isdigit(styler[start]) || (styler[start] == '.');
23 for (unsigned int i = 0; i < end - start + 1 && i < 30; i++) {
24 s[i] = toupper(styler[start + i]);
25 s[i + 1] = '\0';
26 }
27 char chAttr = SCE_C_IDENTIFIER;
28 if (wordIsNumber)
29 chAttr = SCE_C_NUMBER;
30 else {
31 if (keywords.InList(s))
32 chAttr = SCE_C_WORD;
33 }
34 styler.ColourTo(end, chAttr);
35 }
36
37 static void ColouriseSQLDoc(unsigned int startPos, int length,
38 int initStyle, WordList *keywordlists[], StylingContext &styler) {
39
40 WordList &keywords = *keywordlists[0];
41
42 styler.StartAt(startPos);
43
44 bool fold = styler.GetPropSet().GetInt("fold");
45 int lineCurrent = styler.GetLine(startPos);
46 int spaceFlags = 0;
47 int indentCurrent = 0;
48
49 int state = initStyle;
50 char chPrev = ' ';
51 char chNext = styler[startPos];
52 styler.StartSegment(startPos);
53 unsigned int lengthDoc = startPos + length;
54 bool prevCr = false;
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 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_STRING;
94 } else if (isoperator(ch)) {
95 styler.ColourTo(i - 1, state);
96 styler.ColourTo(i, SCE_C_OPERATOR);
97 }
98 } else if (state == SCE_C_WORD) {
99 if (!iswordchar(ch)) {
100 classifyWordSQL(styler.GetStartSegment(), i - 1, keywords, styler);
101 state = SCE_C_DEFAULT;
102 if (ch == '/' && chNext == '*') {
103 state = SCE_C_COMMENT;
104 } else if (ch == '-' && chNext == '-') {
105 state = SCE_C_COMMENTLINE;
106 } else if (ch == '\'') {
107 state = SCE_C_STRING;
108 } else if (isoperator(ch)) {
109 styler.ColourTo(i, SCE_C_OPERATOR);
110 }
111 }
112 } else {
113 if (state == SCE_C_COMMENT) {
114 if (ch == '/' && chPrev == '*') {
115 if (((i > (styler.GetStartSegment() + 2)) || ((initStyle == SCE_C_COMMENT) &&
116 (styler.GetStartSegment() == startPos)))) {
117 styler.ColourTo(i, state);
118 state = SCE_C_DEFAULT;
119 }
120 }
121 } else if (state == SCE_C_COMMENTLINE) {
122 if (ch == '\r' || ch == '\n') {
123 styler.ColourTo(i - 1, state);
124 state = SCE_C_DEFAULT;
125 }
126 } else if (state == SCE_C_STRING) {
127 if (ch == '\'') {
128 if ( chNext == '\'' ) {
129 i++;
130 } else {
131 styler.ColourTo(i, state);
132 state = SCE_C_DEFAULT;
133 i++;
134 }
135 ch = chNext;
136 chNext = styler.SafeGetCharAt(i + 1);
137 }
138 }
139 if (state == SCE_C_DEFAULT) { // One of the above succeeded
140 if (ch == '/' && chNext == '*') {
141 state = SCE_C_COMMENT;
142 } else if (ch == '-' && chNext == '-') {
143 state = SCE_C_COMMENTLINE;
144 } else if (ch == '\'') {
145 state = SCE_C_STRING;
146 } else if (iswordstart(ch)) {
147 state = SCE_C_WORD;
148 } else if (isoperator(ch)) {
149 styler.ColourTo(i, SCE_C_OPERATOR);
150 }
151 }
152 }
153 chPrev = ch;
154 }
155 styler.ColourTo(lengthDoc - 1, state);
156 }
157
158 LexerModule lmSQL(SCLEX_SQL, ColouriseSQLDoc);