scintilla: Update scintilla with changeset 3662:1d1c06df8a2f using gtk+3
[anjuta-extras.git] / plugins / scintilla / scintilla / LexCOBOL.cxx
bloba9a8f551969a15e1c8ce576dcb3194ee657b65f1
1 // Scintilla source code edit control
2 /** @file LexCOBOL.cxx
3 ** Lexer for COBOL
4 ** Based on LexPascal.cxx
5 ** Written by Laurent le Tynevez
6 ** Updated by Simon Steele <s.steele@pnotepad.org> September 2002
7 ** Updated by Mathias Rauen <scite@madshi.net> May 2003 (Delphi adjustments)
8 ** Updated by Rod Falck, Aug 2006 Converted to COBOL
9 **/
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdio.h>
14 #include <stdarg.h>
15 #include <assert.h>
16 #include <ctype.h>
18 #include "ILexer.h"
19 #include "Scintilla.h"
20 #include "SciLexer.h"
22 #include "WordList.h"
23 #include "LexAccessor.h"
24 #include "Accessor.h"
25 #include "StyleContext.h"
26 #include "CharacterSet.h"
27 #include "LexerModule.h"
29 #ifdef SCI_NAMESPACE
30 using namespace Scintilla;
31 #endif
33 #define IN_DIVISION 0x01
34 #define IN_DECLARATIVES 0x02
35 #define IN_SECTION 0x04
36 #define IN_PARAGRAPH 0x08
37 #define IN_FLAGS 0xF
38 #define NOT_HEADER 0x10
40 inline bool isCOBOLoperator(char ch)
42 return isoperator(ch);
45 inline bool isCOBOLwordchar(char ch)
47 return isascii(ch) && (isalnum(ch) || ch == '-');
51 inline bool isCOBOLwordstart(char ch)
53 return isascii(ch) && isalnum(ch);
56 static int CountBits(int nBits)
58 int count = 0;
59 for (int i = 0; i < 32; ++i)
61 count += nBits & 1;
62 nBits >>= 1;
64 return count;
67 static void getRange(unsigned int start,
68 unsigned int end,
69 Accessor &styler,
70 char *s,
71 unsigned int len) {
72 unsigned int i = 0;
73 while ((i < end - start + 1) && (i < len-1)) {
74 s[i] = static_cast<char>(tolower(styler[start + i]));
75 i++;
77 s[i] = '\0';
80 static void ColourTo(Accessor &styler, unsigned int end, unsigned int attr) {
81 styler.ColourTo(end, attr);
85 static int classifyWordCOBOL(unsigned int start, unsigned int end, /*WordList &keywords*/WordList *keywordlists[], Accessor &styler, int nContainment, bool *bAarea) {
86 int ret = 0;
88 WordList& a_keywords = *keywordlists[0];
89 WordList& b_keywords = *keywordlists[1];
90 WordList& c_keywords = *keywordlists[2];
92 char s[100];
93 getRange(start, end, styler, s, sizeof(s));
95 char chAttr = SCE_C_IDENTIFIER;
96 if (isdigit(s[0]) || (s[0] == '.') || (s[0] == 'v')) {
97 chAttr = SCE_C_NUMBER;
98 char *p = s + 1;
99 while (*p) {
100 if ((!isdigit(*p) && (*p) != 'v') && isCOBOLwordchar(*p)) {
101 chAttr = SCE_C_IDENTIFIER;
102 break;
104 ++p;
107 else {
108 if (a_keywords.InList(s)) {
109 chAttr = SCE_C_WORD;
111 else if (b_keywords.InList(s)) {
112 chAttr = SCE_C_WORD2;
114 else if (c_keywords.InList(s)) {
115 chAttr = SCE_C_UUID;
118 if (*bAarea) {
119 if (strcmp(s, "division") == 0) {
120 ret = IN_DIVISION;
121 // we've determined the containment, anything else is just ignored for those purposes
122 *bAarea = false;
123 } else if (strcmp(s, "declaratives") == 0) {
124 ret = IN_DIVISION | IN_DECLARATIVES;
125 if (nContainment & IN_DECLARATIVES)
126 ret |= NOT_HEADER | IN_SECTION;
127 // we've determined the containment, anything else is just ignored for those purposes
128 *bAarea = false;
129 } else if (strcmp(s, "section") == 0) {
130 ret = (nContainment &~ IN_PARAGRAPH) | IN_SECTION;
131 // we've determined the containment, anything else is just ignored for those purposes
132 *bAarea = false;
133 } else if (strcmp(s, "end") == 0 && (nContainment & IN_DECLARATIVES)) {
134 ret = IN_DIVISION | IN_DECLARATIVES | IN_SECTION | NOT_HEADER;
135 } else {
136 ret = nContainment | IN_PARAGRAPH;
139 ColourTo(styler, end, chAttr);
140 return ret;
143 static void ColouriseCOBOLDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
144 Accessor &styler) {
146 styler.StartAt(startPos);
148 int state = initStyle;
149 if (state == SCE_C_CHARACTER) // Does not leak onto next line
150 state = SCE_C_DEFAULT;
151 char chPrev = ' ';
152 char chNext = styler[startPos];
153 unsigned int lengthDoc = startPos + length;
155 int nContainment;
157 int currentLine = styler.GetLine(startPos);
158 if (currentLine > 0) {
159 styler.SetLineState(currentLine, styler.GetLineState(currentLine-1));
160 nContainment = styler.GetLineState(currentLine);
161 nContainment &= ~NOT_HEADER;
162 } else {
163 styler.SetLineState(currentLine, 0);
164 nContainment = 0;
167 styler.StartSegment(startPos);
168 bool bNewLine = true;
169 bool bAarea = !isspacechar(chNext);
170 int column = 0;
171 for (unsigned int i = startPos; i < lengthDoc; i++) {
172 char ch = chNext;
174 chNext = styler.SafeGetCharAt(i + 1);
176 ++column;
178 if (bNewLine) {
179 column = 0;
181 if (column <= 1 && !bAarea) {
182 bAarea = !isspacechar(ch);
184 bool bSetNewLine = false;
185 if ((ch == '\r' && chNext != '\n') || (ch == '\n')) {
186 // Trigger on CR only (Mac style) or either on LF from CR+LF (Dos/Win) or on LF alone (Unix)
187 // Avoid triggering two times on Dos/Win
188 // End of line
189 if (state == SCE_C_CHARACTER) {
190 ColourTo(styler, i, state);
191 state = SCE_C_DEFAULT;
193 styler.SetLineState(currentLine, nContainment);
194 currentLine++;
195 bSetNewLine = true;
196 if (nContainment & NOT_HEADER)
197 nContainment &= ~(NOT_HEADER | IN_DECLARATIVES | IN_SECTION);
200 if (styler.IsLeadByte(ch)) {
201 chNext = styler.SafeGetCharAt(i + 2);
202 chPrev = ' ';
203 i += 1;
204 continue;
207 if (state == SCE_C_DEFAULT) {
208 if (isCOBOLwordstart(ch) || (ch == '$' && isascii(chNext) && isalpha(chNext))) {
209 ColourTo(styler, i-1, state);
210 state = SCE_C_IDENTIFIER;
211 } else if (column == 0 && ch == '*' && chNext != '*') {
212 ColourTo(styler, i-1, state);
213 state = SCE_C_COMMENTLINE;
214 } else if (column == 0 && ch == '/' && chNext != '*') {
215 ColourTo(styler, i-1, state);
216 state = SCE_C_COMMENTLINE;
217 } else if (column == 0 && ch == '*' && chNext == '*') {
218 ColourTo(styler, i-1, state);
219 state = SCE_C_COMMENTDOC;
220 } else if (column == 0 && ch == '/' && chNext == '*') {
221 ColourTo(styler, i-1, state);
222 state = SCE_C_COMMENTDOC;
223 } else if (ch == '"') {
224 ColourTo(styler, i-1, state);
225 state = SCE_C_STRING;
226 } else if (ch == '\'') {
227 ColourTo(styler, i-1, state);
228 state = SCE_C_CHARACTER;
229 } else if (ch == '?' && column == 0) {
230 ColourTo(styler, i-1, state);
231 state = SCE_C_PREPROCESSOR;
232 } else if (isCOBOLoperator(ch)) {
233 ColourTo(styler, i-1, state);
234 ColourTo(styler, i, SCE_C_OPERATOR);
236 } else if (state == SCE_C_IDENTIFIER) {
237 if (!isCOBOLwordchar(ch)) {
238 int lStateChange = classifyWordCOBOL(styler.GetStartSegment(), i - 1, keywordlists, styler, nContainment, &bAarea);
240 if(lStateChange != 0) {
241 styler.SetLineState(currentLine, lStateChange);
242 nContainment = lStateChange;
245 state = SCE_C_DEFAULT;
246 chNext = styler.SafeGetCharAt(i + 1);
247 if (ch == '"') {
248 state = SCE_C_STRING;
249 } else if (ch == '\'') {
250 state = SCE_C_CHARACTER;
251 } else if (isCOBOLoperator(ch)) {
252 ColourTo(styler, i, SCE_C_OPERATOR);
255 } else {
256 if (state == SCE_C_PREPROCESSOR) {
257 if ((ch == '\r' || ch == '\n') && !(chPrev == '\\' || chPrev == '\r')) {
258 ColourTo(styler, i-1, state);
259 state = SCE_C_DEFAULT;
261 } else if (state == SCE_C_COMMENT) {
262 if (ch == '\r' || ch == '\n') {
263 ColourTo(styler, i, state);
264 state = SCE_C_DEFAULT;
266 } else if (state == SCE_C_COMMENTDOC) {
267 if (ch == '\r' || ch == '\n') {
268 if (((i > styler.GetStartSegment() + 2) || (
269 (initStyle == SCE_C_COMMENTDOC) &&
270 (styler.GetStartSegment() == static_cast<unsigned int>(startPos))))) {
271 ColourTo(styler, i, state);
272 state = SCE_C_DEFAULT;
275 } else if (state == SCE_C_COMMENTLINE) {
276 if (ch == '\r' || ch == '\n') {
277 ColourTo(styler, i-1, state);
278 state = SCE_C_DEFAULT;
280 } else if (state == SCE_C_STRING) {
281 if (ch == '"') {
282 ColourTo(styler, i, state);
283 state = SCE_C_DEFAULT;
285 } else if (state == SCE_C_CHARACTER) {
286 if (ch == '\'') {
287 ColourTo(styler, i, state);
288 state = SCE_C_DEFAULT;
292 chPrev = ch;
293 bNewLine = bSetNewLine;
294 if (bNewLine)
296 bAarea = false;
299 ColourTo(styler, lengthDoc - 1, state);
302 static void FoldCOBOLDoc(unsigned int startPos, int length, int, WordList *[],
303 Accessor &styler) {
304 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
305 unsigned int endPos = startPos + length;
306 int visibleChars = 0;
307 int lineCurrent = styler.GetLine(startPos);
308 int levelPrev = lineCurrent > 0 ? styler.LevelAt(lineCurrent - 1) & SC_FOLDLEVELNUMBERMASK : 0xFFF;
309 char chNext = styler[startPos];
311 bool bNewLine = true;
312 bool bAarea = !isspacechar(chNext);
313 int column = 0;
314 bool bComment = false;
315 for (unsigned int i = startPos; i < endPos; i++) {
316 char ch = chNext;
317 chNext = styler.SafeGetCharAt(i + 1);
318 ++column;
320 if (bNewLine) {
321 column = 0;
322 bComment = (ch == '*' || ch == '/' || ch == '?');
324 if (column <= 1 && !bAarea) {
325 bAarea = !isspacechar(ch);
327 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
328 if (atEOL) {
329 int nContainment = styler.GetLineState(lineCurrent);
330 int lev = CountBits(nContainment & IN_FLAGS) | SC_FOLDLEVELBASE;
331 if (bAarea && !bComment)
332 --lev;
333 if (visibleChars == 0 && foldCompact)
334 lev |= SC_FOLDLEVELWHITEFLAG;
335 if ((bAarea) && (visibleChars > 0) && !(nContainment & NOT_HEADER) && !bComment)
336 lev |= SC_FOLDLEVELHEADERFLAG;
337 if (lev != styler.LevelAt(lineCurrent)) {
338 styler.SetLevel(lineCurrent, lev);
340 if ((lev & SC_FOLDLEVELNUMBERMASK) <= (levelPrev & SC_FOLDLEVELNUMBERMASK)) {
341 // this level is at the same level or less than the previous line
342 // therefore these is nothing for the previous header to collapse, so remove the header
343 styler.SetLevel(lineCurrent - 1, levelPrev & ~SC_FOLDLEVELHEADERFLAG);
345 levelPrev = lev;
346 visibleChars = 0;
347 bAarea = false;
348 bNewLine = true;
349 lineCurrent++;
350 } else {
351 bNewLine = false;
355 if (!isspacechar(ch))
356 visibleChars++;
359 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
360 int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
361 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
364 static const char * const COBOLWordListDesc[] = {
365 "A Keywords",
366 "B Keywords",
367 "Extended Keywords",
371 LexerModule lmCOBOL(SCLEX_COBOL, ColouriseCOBOLDoc, "COBOL", FoldCOBOLDoc, COBOLWordListDesc);