]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/LexOthers.cxx
90f41e3bf3bd1686a7172fe2a60d64a12833316f
[wxWidgets.git] / contrib / 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 ColourisePropsLine(char *lineBuffer, int lengthLine, int startLine, int endPos, Accessor &styler) {
53 int i = 0;
54 while (isspace(lineBuffer[i]) && (i < lengthLine)) // Skip initial spaces
55 i++;
56 if (lineBuffer[i] == '#' || lineBuffer[i] == '!' || lineBuffer[i] == ';') {
57 styler.ColourTo(endPos, 1);
58 } else if (lineBuffer[i] == '[') {
59 styler.ColourTo(endPos, 2);
60 } else if (lineBuffer[i] == '@') {
61 styler.ColourTo(startLine+i, 4);
62 if (lineBuffer[++i] == '=')
63 styler.ColourTo(startLine+i, 3);
64 styler.ColourTo(endPos, 0);
65 } else {
66 while (lineBuffer[i] != '=' && (i < lengthLine)) // Search the '=' character
67 i++;
68 if (lineBuffer[i] == '=') {
69 styler.ColourTo(startLine+i-1, 0);
70 styler.ColourTo(startLine+i, 3);
71 styler.ColourTo(endPos, 0);
72 } else {
73 styler.ColourTo(endPos, 0);
74 }
75 }
76 }
77
78 static void ColourisePropsDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
79 char lineBuffer[1024];
80 styler.StartAt(startPos);
81 styler.StartSegment(startPos);
82 unsigned int linePos = 0;
83 int startLine = startPos;
84 for (unsigned int i = startPos; i <= startPos + length; i++) {
85 lineBuffer[linePos++] = styler[i];
86 if ((styler[i] == '\r' && styler.SafeGetCharAt(i+1) != '\n') ||
87 styler[i] == '\n' ||
88 (linePos >= sizeof(lineBuffer) - 1)) {
89 lineBuffer[linePos] = '\0';
90 ColourisePropsLine(lineBuffer, linePos, startLine, i, styler);
91 linePos = 0;
92 startLine = i+1;
93 }
94 }
95 if (linePos > 0)
96 ColourisePropsLine(lineBuffer, linePos, startLine, startPos + length, styler);
97 }
98
99 static void ColouriseMakeLine(char *lineBuffer, int lengthLine, int endPos, Accessor &styler) {
100 int i = 0;
101 while (isspace(lineBuffer[i]) && (i < lengthLine))
102 i++;
103 if (lineBuffer[i] == '#' || lineBuffer[i] == '!') {
104 styler.ColourTo(endPos, 1);
105 } else {
106 styler.ColourTo(endPos, 0);
107 }
108 }
109
110 static void ColouriseMakeDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
111 char lineBuffer[1024];
112 styler.StartAt(startPos);
113 styler.StartSegment(startPos);
114 unsigned int linePos = 0;
115 for (unsigned int i = startPos; i <= startPos + length; i++) {
116 lineBuffer[linePos++] = styler[i];
117 if (styler[i] == '\r' || styler[i] == '\n' || (linePos >= sizeof(lineBuffer) - 1)) {
118 ColouriseMakeLine(lineBuffer, linePos, i, styler);
119 linePos = 0;
120 }
121 }
122 if (linePos > 0)
123 ColouriseMakeLine(lineBuffer, linePos, startPos + length, styler);
124 }
125
126 static void ColouriseErrorListLine(char *lineBuffer, int lengthLine, int endPos, Accessor &styler) {
127 if (lineBuffer[0] == '>') {
128 // Command or return status
129 styler.ColourTo(endPos, 4);
130 } else if (strstr(lineBuffer, "File \"") && strstr(lineBuffer, ", line ")) {
131 styler.ColourTo(endPos, 1);
132 } else if (0 == strncmp(lineBuffer, "Error ", strlen("Error "))) {
133 // Borland error message
134 styler.ColourTo(endPos, 5);
135 } else if (0 == strncmp(lineBuffer, "Warning ", strlen("Warning "))) {
136 // Borland warning message
137 styler.ColourTo(endPos, 5);
138 } else {
139 // Look for <filename>:<line>:message
140 // Look for <filename>(line)message
141 // Look for <filename>(line,pos)message
142 int state = 0;
143 for (int i = 0; i < lengthLine; i++) {
144 if (state == 0 && lineBuffer[i] == ':' && isdigit(lineBuffer[i + 1])) {
145 state = 1;
146 } else if (state == 0 && lineBuffer[i] == '(') {
147 state = 10;
148 } else if (state == 1 && isdigit(lineBuffer[i])) {
149 state = 2;
150 } else if (state == 2 && lineBuffer[i] == ':') {
151 state = 3;
152 break;
153 } else if (state == 2 && !isdigit(lineBuffer[i])) {
154 state = 99;
155 } else if (state == 10 && isdigit(lineBuffer[i])) {
156 state = 11;
157 } else if (state == 11 && lineBuffer[i] == ',') {
158 state = 14;
159 } else if (state == 11 && lineBuffer[i] == ')') {
160 state = 12;
161 } else if (state == 12 && lineBuffer[i] == ':') {
162 state = 13;
163 } else if (state == 14 && lineBuffer[i] == ')') {
164 state = 15;
165 break;
166 } else if (((state == 11) || (state == 14)) && !((lineBuffer[i] == ' ') || isdigit(lineBuffer[i]))) {
167 state = 99;
168 }
169 }
170 if (state == 3) {
171 styler.ColourTo(endPos, 2);
172 } else if ((state == 13) || (state == 14) || (state == 15)) {
173 styler.ColourTo(endPos, 3);
174 } else {
175 styler.ColourTo(endPos, 0);
176 }
177 }
178 }
179
180 static void ColouriseErrorListDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
181 char lineBuffer[1024];
182 styler.StartAt(startPos);
183 styler.StartSegment(startPos);
184 unsigned int linePos = 0;
185 for (unsigned int i = startPos; i <= startPos + length; i++) {
186 lineBuffer[linePos++] = styler[i];
187 if (styler[i] == '\r' || styler[i] == '\n' || (linePos >= sizeof(lineBuffer) - 1)) {
188 ColouriseErrorListLine(lineBuffer, linePos, i, styler);
189 linePos = 0;
190 }
191 }
192 if (linePos > 0)
193 ColouriseErrorListLine(lineBuffer, linePos, startPos + length, styler);
194 }
195
196 static int isSpecial(char s) {
197
198 return (s == '\\') || (s == ',') || (s == ';') || (s == '\'') || (s == ' ') ||
199 (s == '\"') || (s == '`') || (s == '^') || (s == '~');
200 }
201
202 static int isTag(int start, Accessor &styler) {
203
204 char s[6];
205 unsigned int i = 0, e=1;
206 while (i < 5 && e) {
207 s[i] = styler[start + i];
208 i++;
209 e = styler[start + i] != '{';
210 }
211 s[i] = '\0';
212 return (strcmp(s, "begin") == 0) || (strcmp(s, "end") == 0);
213 }
214
215 static void ColouriseLatexDoc(unsigned int startPos, int length, int initStyle,
216 WordList *[], Accessor &styler) {
217
218 styler.StartAt(startPos);
219
220 int state = initStyle;
221 char chNext = styler[startPos];
222 styler.StartSegment(startPos);
223 int lengthDoc = startPos + length;
224
225 for (int i = startPos; i < lengthDoc; i++) {
226 char ch = chNext;
227 chNext = styler.SafeGetCharAt(i + 1);
228
229 if (styler.IsLeadByte(ch)) {
230 chNext = styler.SafeGetCharAt(i + 2);
231 i++;
232 continue;
233 }
234 switch(state) {
235 case SCE_L_DEFAULT :
236 switch(ch) {
237 case '\\' :
238 styler.ColourTo(i - 1, state);
239 if (isSpecial(styler[i + 1])) {
240 styler.ColourTo(i + 1, SCE_L_COMMAND);
241 i++;
242 chNext = styler.SafeGetCharAt(i + 1);
243 }
244 else {
245 if (isTag(i+1, styler))
246 state = SCE_L_TAG;
247 else
248 state = SCE_L_COMMAND;
249 }
250 break;
251 case '$' :
252 styler.ColourTo(i - 1, state);
253 state = SCE_L_MATH;
254 if (chNext == '$') {
255 i++;
256 chNext = styler.SafeGetCharAt(i + 1);
257 }
258 break;
259 case '%' :
260 styler.ColourTo(i - 1, state);
261 state = SCE_L_COMMENT;
262 break;
263 }
264 break;
265 case SCE_L_COMMAND :
266 if (chNext == '[' || chNext == '{' || chNext == '}' ||
267 chNext == ' ' || chNext == '\r' || chNext == '\n') {
268 styler.ColourTo(i, state);
269 state = SCE_L_DEFAULT;
270 i++;
271 chNext = styler.SafeGetCharAt(i + 1);
272 }
273 break;
274 case SCE_L_TAG :
275 if (ch == '}') {
276 styler.ColourTo(i, state);
277 state = SCE_L_DEFAULT;
278 }
279 break;
280 case SCE_L_MATH :
281 if (ch == '$') {
282 if (chNext == '$') {
283 i++;
284 chNext = styler.SafeGetCharAt(i + 1);
285 }
286 styler.ColourTo(i, state);
287 state = SCE_L_DEFAULT;
288 }
289 break;
290 case SCE_L_COMMENT :
291 if (ch == '\r' || ch == '\n') {
292 styler.ColourTo(i - 1, state);
293 state = SCE_L_DEFAULT;
294 }
295 }
296 }
297 styler.ColourTo(lengthDoc, state);
298 }
299
300 LexerModule lmProps(SCLEX_PROPERTIES, ColourisePropsDoc);
301 LexerModule lmErrorList(SCLEX_ERRORLIST, ColouriseErrorListDoc);
302 LexerModule lmMake(SCLEX_MAKEFILE, ColouriseMakeDoc);
303 LexerModule lmBatch(SCLEX_BATCH, ColouriseBatchDoc);
304 LexerModule lmLatex(SCLEX_LATEX, ColouriseLatexDoc);