]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexOthers.cxx
removed/replaced include 'wx/wx.h'
[wxWidgets.git] / src / stc / scintilla / src / LexOthers.cxx
1 // SciTE - Scintilla based Text Editor
2 // LexOthers.cxx - lexers for properties files, batch files, make files and error lists
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 ColouriseBatchLine(char *lineBuffer, int endLine, Accessor &styler) {
21 if (0 == strncmp(lineBuffer, "REM", 3)) {
22 styler.ColourTo(endLine, 1);
23 } else if (0 == strncmp(lineBuffer, "rem", 3)) {
24 styler.ColourTo(endLine, 1);
25 } else if (0 == strncmp(lineBuffer, "SET", 3)) {
26 styler.ColourTo(endLine, 2);
27 } else if (0 == strncmp(lineBuffer, "set", 3)) {
28 styler.ColourTo(endLine, 2);
29 } else if (lineBuffer[0] == ':') {
30 styler.ColourTo(endLine, 3);
31 } else {
32 styler.ColourTo(endLine, 0);
33 }
34 }
35
36 static void ColouriseBatchDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
37 char lineBuffer[1024];
38 styler.StartAt(startPos);
39 styler.StartSegment(startPos);
40 unsigned int linePos = 0;
41 for (unsigned int i = startPos; i < startPos + length; i++) {
42 lineBuffer[linePos++] = styler[i];
43 if (styler[i] == '\r' || styler[i] == '\n' || (linePos >= sizeof(lineBuffer) - 1)) {
44 ColouriseBatchLine(lineBuffer, i, styler);
45 linePos = 0;
46 }
47 }
48 if (linePos > 0)
49 ColouriseBatchLine(lineBuffer, startPos + length, styler);
50 }
51
52 static void ColouriseDiffLine(char *lineBuffer, int endLine, Accessor &styler) {
53 // It is needed to remember the current state to recognize starting
54 // comment lines before the first "diff " or "--- ". If a real
55 // difference starts then each line starting with ' ' is a whitespace
56 // otherwise it is considered a comment (Only in..., Binary file...)
57 if (0 == strncmp(lineBuffer, "diff ", 3)) {
58 styler.ColourTo(endLine, 2);
59 } else if (0 == strncmp(lineBuffer, "--- ", 3)) {
60 styler.ColourTo(endLine, 3);
61 } else if (0 == strncmp(lineBuffer, "+++ ", 3)) {
62 styler.ColourTo(endLine, 3);
63 } else if (lineBuffer[0] == '@') {
64 styler.ColourTo(endLine, 4);
65 } else if (lineBuffer[0] == '-') {
66 styler.ColourTo(endLine, 5);
67 } else if (lineBuffer[0] == '+') {
68 styler.ColourTo(endLine, 6);
69 } else if (lineBuffer[0] != ' ') {
70 styler.ColourTo(endLine, 1);
71 } else {
72 styler.ColourTo(endLine, 0);
73 }
74 }
75
76 static void ColouriseDiffDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
77 char lineBuffer[1024];
78 styler.StartAt(startPos);
79 styler.StartSegment(startPos);
80 unsigned int linePos = 0;
81 for (unsigned int i = startPos; i < startPos + length; i++) {
82 lineBuffer[linePos++] = styler[i];
83 if (styler[i] == '\r' || styler[i] == '\n' || (linePos >= sizeof(lineBuffer) - 1)) {
84 ColouriseDiffLine(lineBuffer, i, styler);
85 linePos = 0;
86 }
87 }
88 if (linePos > 0)
89 ColouriseDiffLine(lineBuffer, startPos + length, styler);
90 }
91
92 static void ColourisePropsLine(char *lineBuffer, int lengthLine, int startLine, int endPos, Accessor &styler) {
93 int i = 0;
94 while (isspace(lineBuffer[i]) && (i < lengthLine)) // Skip initial spaces
95 i++;
96 if (lineBuffer[i] == '#' || lineBuffer[i] == '!' || lineBuffer[i] == ';') {
97 styler.ColourTo(endPos, 1);
98 } else if (lineBuffer[i] == '[') {
99 styler.ColourTo(endPos, 2);
100 } else if (lineBuffer[i] == '@') {
101 styler.ColourTo(startLine+i, 4);
102 if (lineBuffer[++i] == '=')
103 styler.ColourTo(startLine+i, 3);
104 styler.ColourTo(endPos, 0);
105 } else {
106 while (lineBuffer[i] != '=' && (i < lengthLine)) // Search the '=' character
107 i++;
108 if (lineBuffer[i] == '=') {
109 styler.ColourTo(startLine+i-1, 0);
110 styler.ColourTo(startLine+i, 3);
111 styler.ColourTo(endPos, 0);
112 } else {
113 styler.ColourTo(endPos, 0);
114 }
115 }
116 }
117
118 static void ColourisePropsDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
119 char lineBuffer[1024];
120 styler.StartAt(startPos);
121 styler.StartSegment(startPos);
122 unsigned int linePos = 0;
123 int startLine = startPos;
124 for (unsigned int i = startPos; i <= startPos + length; i++) {
125 lineBuffer[linePos++] = styler[i];
126 if ((styler[i] == '\r' && styler.SafeGetCharAt(i+1) != '\n') ||
127 styler[i] == '\n' ||
128 (linePos >= sizeof(lineBuffer) - 1)) {
129 lineBuffer[linePos] = '\0';
130 ColourisePropsLine(lineBuffer, linePos, startLine, i, styler);
131 linePos = 0;
132 startLine = i+1;
133 }
134 }
135 if (linePos > 0)
136 ColourisePropsLine(lineBuffer, linePos, startLine, startPos + length, styler);
137 }
138
139 static void ColouriseMakeLine(char *lineBuffer, int lengthLine, int endPos, Accessor &styler) {
140 int i = 0;
141 while (isspace(lineBuffer[i]) && (i < lengthLine))
142 i++;
143 if (lineBuffer[i] == '#' || lineBuffer[i] == '!') {
144 styler.ColourTo(endPos, 1);
145 } else {
146 styler.ColourTo(endPos, 0);
147 }
148 }
149
150 static void ColouriseMakeDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
151 char lineBuffer[1024];
152 styler.StartAt(startPos);
153 styler.StartSegment(startPos);
154 unsigned int linePos = 0;
155 for (unsigned int i = startPos; i <= startPos + length; i++) {
156 lineBuffer[linePos++] = styler[i];
157 if (styler[i] == '\r' || styler[i] == '\n' || (linePos >= sizeof(lineBuffer) - 1)) {
158 ColouriseMakeLine(lineBuffer, linePos, i, styler);
159 linePos = 0;
160 }
161 }
162 if (linePos > 0)
163 ColouriseMakeLine(lineBuffer, linePos, startPos + length, styler);
164 }
165
166 static void ColouriseErrorListLine(char *lineBuffer, int lengthLine, int endPos, Accessor &styler) {
167 if (lineBuffer[0] == '>') {
168 // Command or return status
169 styler.ColourTo(endPos, SCE_ERR_CMD);
170 } else if (strstr(lineBuffer, "File \"") && strstr(lineBuffer, ", line ")) {
171 styler.ColourTo(endPos, SCE_ERR_PYTHON);
172 } else if (0 == strncmp(lineBuffer, "Error ", strlen("Error "))) {
173 // Borland error message
174 styler.ColourTo(endPos, SCE_ERR_BORLAND);
175 } else if (0 == strncmp(lineBuffer, "Warning ", strlen("Warning "))) {
176 // Borland warning message
177 styler.ColourTo(endPos, SCE_ERR_BORLAND);
178 } else if (strstr(lineBuffer, " at " ) &&
179 strstr(lineBuffer, " at " ) < lineBuffer+lengthLine &&
180 strstr(lineBuffer, " line ") &&
181 strstr(lineBuffer, " line ") < lineBuffer+lengthLine) {
182 // perl error message
183 styler.ColourTo(endPos, SCE_ERR_PERL);
184 } else {
185 // Look for <filename>:<line>:message
186 // Look for <filename>(line)message
187 // Look for <filename>(line,pos)message
188 int state = 0;
189 for (int i = 0; i < lengthLine; i++) {
190 if (state == 0 && lineBuffer[i] == ':' && isdigit(lineBuffer[i + 1])) {
191 state = 1;
192 } else if (state == 0 && lineBuffer[i] == '(') {
193 state = 10;
194 } else if (state == 1 && isdigit(lineBuffer[i])) {
195 state = 2;
196 } else if (state == 2 && lineBuffer[i] == ':') {
197 state = 3;
198 break;
199 } else if (state == 2 && !isdigit(lineBuffer[i])) {
200 state = 99;
201 } else if (state == 10 && isdigit(lineBuffer[i])) {
202 state = 11;
203 } else if (state == 11 && lineBuffer[i] == ',') {
204 state = 14;
205 } else if (state == 11 && lineBuffer[i] == ')') {
206 state = 12;
207 } else if (state == 12 && lineBuffer[i] == ':') {
208 state = 13;
209 } else if (state == 14 && lineBuffer[i] == ')') {
210 state = 15;
211 break;
212 } else if (((state == 11) || (state == 14)) && !((lineBuffer[i] == ' ') || isdigit(lineBuffer[i]))) {
213 state = 99;
214 }
215 }
216 if (state == 3) {
217 styler.ColourTo(endPos, SCE_ERR_GCC);
218 } else if ((state == 13) || (state == 14) || (state == 15)) {
219 styler.ColourTo(endPos, SCE_ERR_MS);
220 } else {
221 styler.ColourTo(endPos, SCE_ERR_DEFAULT);
222 }
223 }
224 }
225
226 static void ColouriseErrorListDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
227 char lineBuffer[1024];
228 styler.StartAt(startPos);
229 styler.StartSegment(startPos);
230 unsigned int linePos = 0;
231 for (unsigned int i = startPos; i <= startPos + length; i++) {
232 lineBuffer[linePos++] = styler[i];
233 if (styler[i] == '\r' || styler[i] == '\n' || (linePos >= sizeof(lineBuffer) - 1)) {
234 ColouriseErrorListLine(lineBuffer, linePos, i, styler);
235 linePos = 0;
236 }
237 }
238 if (linePos > 0)
239 ColouriseErrorListLine(lineBuffer, linePos, startPos + length, styler);
240 }
241
242 static int isSpecial(char s) {
243
244 return (s == '\\') || (s == ',') || (s == ';') || (s == '\'') || (s == ' ') ||
245 (s == '\"') || (s == '`') || (s == '^') || (s == '~');
246 }
247
248 static int isTag(int start, Accessor &styler) {
249
250 char s[6];
251 unsigned int i = 0, e=1;
252 while (i < 5 && e) {
253 s[i] = styler[start + i];
254 i++;
255 e = styler[start + i] != '{';
256 }
257 s[i] = '\0';
258 return (strcmp(s, "begin") == 0) || (strcmp(s, "end") == 0);
259 }
260
261 static void ColouriseLatexDoc(unsigned int startPos, int length, int initStyle,
262 WordList *[], Accessor &styler) {
263
264 styler.StartAt(startPos);
265
266 int state = initStyle;
267 char chNext = styler[startPos];
268 styler.StartSegment(startPos);
269 int lengthDoc = startPos + length;
270
271 for (int i = startPos; i < lengthDoc; i++) {
272 char ch = chNext;
273 chNext = styler.SafeGetCharAt(i + 1);
274
275 if (styler.IsLeadByte(ch)) {
276 chNext = styler.SafeGetCharAt(i + 2);
277 i++;
278 continue;
279 }
280 switch(state) {
281 case SCE_L_DEFAULT :
282 switch(ch) {
283 case '\\' :
284 styler.ColourTo(i - 1, state);
285 if (isSpecial(styler[i + 1])) {
286 styler.ColourTo(i + 1, SCE_L_COMMAND);
287 i++;
288 chNext = styler.SafeGetCharAt(i + 1);
289 }
290 else {
291 if (isTag(i+1, styler))
292 state = SCE_L_TAG;
293 else
294 state = SCE_L_COMMAND;
295 }
296 break;
297 case '$' :
298 styler.ColourTo(i - 1, state);
299 state = SCE_L_MATH;
300 if (chNext == '$') {
301 i++;
302 chNext = styler.SafeGetCharAt(i + 1);
303 }
304 break;
305 case '%' :
306 styler.ColourTo(i - 1, state);
307 state = SCE_L_COMMENT;
308 break;
309 }
310 break;
311 case SCE_L_COMMAND :
312 if (chNext == '[' || chNext == '{' || chNext == '}' ||
313 chNext == ' ' || chNext == '\r' || chNext == '\n') {
314 styler.ColourTo(i, state);
315 state = SCE_L_DEFAULT;
316 i++;
317 chNext = styler.SafeGetCharAt(i + 1);
318 }
319 break;
320 case SCE_L_TAG :
321 if (ch == '}') {
322 styler.ColourTo(i, state);
323 state = SCE_L_DEFAULT;
324 }
325 break;
326 case SCE_L_MATH :
327 if (ch == '$') {
328 if (chNext == '$') {
329 i++;
330 chNext = styler.SafeGetCharAt(i + 1);
331 }
332 styler.ColourTo(i, state);
333 state = SCE_L_DEFAULT;
334 }
335 break;
336 case SCE_L_COMMENT :
337 if (ch == '\r' || ch == '\n') {
338 styler.ColourTo(i - 1, state);
339 state = SCE_L_DEFAULT;
340 }
341 }
342 }
343 styler.ColourTo(lengthDoc, state);
344 }
345
346 LexerModule lmBatch(SCLEX_BATCH, ColouriseBatchDoc);
347 LexerModule lmDiff(SCLEX_DIFF, ColouriseDiffDoc);
348 LexerModule lmProps(SCLEX_PROPERTIES, ColourisePropsDoc);
349 LexerModule lmMake(SCLEX_MAKEFILE, ColouriseMakeDoc);
350 LexerModule lmErrorList(SCLEX_ERRORLIST, ColouriseErrorListDoc);
351 LexerModule lmLatex(SCLEX_LATEX, ColouriseLatexDoc);