Debian Codegraph @

scintilla/lexilla/lexers/LexCOBOL.cxx

raw
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 <[email protected]> September 2002
7 ** Updated by Mathias Rauen <[email protected]> May 2003 (Delphi adjustments)
8 ** Updated by Rod Falck, Aug 2006 Converted to COBOL
9 **/
10
11 #include <cstdlib>
12 #include <cassert>
13 #include <cstring>
14 #include <cctype>
15 #include <cstdio>
16
17 #include <string>
18 #include <string_view>
19
20 #include "ILexer.h"
21 #include "Scintilla.h"
22 #include "SciLexer.h"
23
24 #include "WordList.h"
25 #include "LexAccessor.h"
26 #include "Accessor.h"
27 #include "StyleContext.h"
28 #include "CharacterSet.h"
29 #include "LexerModule.h"
30
31 using namespace Lexilla;
32
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
39
40 namespace {
41
42 bool isCOBOLoperator(char ch)
43 {
44 return isoperator(ch);
45 }
46
47 bool isCOBOLwordchar(char ch)
48 {
49 return IsASCII(ch) && (isalnum(ch) || ch == '-');
50
51 }
52
53 bool isCOBOLwordstart(char ch)
54 {
55 return IsASCII(ch) && isalnum(ch);
56 }
57
58 int CountBits(int nBits)
59 {
60 int count = 0;
61 for (int i = 0; i < 32; ++i)
62 {
63 count += nBits & 1;
64 nBits >>= 1;
65 }
66 return count;
67 }
68
69 void getRange(Sci_PositionU start,
70 Sci_PositionU end,
71 Accessor &styler,
72 char *s,
73 Sci_PositionU len) {
74 Sci_PositionU i = 0;
75 while ((i < end - start + 1) && (i < len-1)) {
76 s[i] = static_cast<char>(tolower(styler[start + i]));
77 i++;
78 }
79 s[i] = '\0';
80 }
81
82 void ColourTo(Accessor &styler, Sci_PositionU end, unsigned int attr) {
83 styler.ColourTo(end, attr);
84 }
85
86
87 int classifyWordCOBOL(Sci_PositionU start, Sci_PositionU end, /*WordList &keywords*/WordList *keywordlists[], Accessor &styler, int nContainment, bool *bAarea) {
88 int ret = 0;
89
90 char s[100];
91 s[0] = '\0';
92 s[1] = '\0';
93 getRange(start, end, styler, s, sizeof(s));
94
95 int chAttr = SCE_COBOL_IDENTIFIER;
96 if (isdigit(s[0]) || (s[0] == '.') || (s[0] == 'v')) {
97 chAttr = SCE_COBOL_NUMBER;
98 char *p = s + 1;
99 while (*p) {
100 if ((!isdigit(*p) && (*p) != 'v') && isCOBOLwordchar(*p)) {
101 chAttr = SCE_COBOL_IDENTIFIER;
102 break;
103 }
104 ++p;
105 }
106 }
107 if (chAttr == SCE_COBOL_IDENTIFIER) {
108 WordList& a_keywords = *keywordlists[0];
109 WordList& b_keywords = *keywordlists[1];
110 WordList& c_keywords = *keywordlists[2];
111
112 if (a_keywords.InList(s)) {
113 chAttr = SCE_COBOL_WORD;
114 }
115 else if (b_keywords.InList(s)) {
116 chAttr = SCE_COBOL_WORD2;
117 }
118 else if (c_keywords.InList(s)) {
119 chAttr = SCE_COBOL_WORD3;
120 }
121 }
122 if (*bAarea) {
123 if (strcmp(s, "division") == 0) {
124 ret = IN_DIVISION;
125 // we've determined the containment, anything else is just ignored for those purposes
126 *bAarea = false;
127 } else if (strcmp(s, "declaratives") == 0) {
128 ret = IN_DIVISION | IN_DECLARATIVES;
129 if (nContainment & IN_DECLARATIVES)
130 ret |= NOT_HEADER | IN_SECTION;
131 // we've determined the containment, anything else is just ignored for those purposes
132 *bAarea = false;
133 } else if (strcmp(s, "section") == 0) {
134 ret = (nContainment &~ IN_PARAGRAPH) | IN_SECTION;
135 // we've determined the containment, anything else is just ignored for those purposes
136 *bAarea = false;
137 } else if (strcmp(s, "end") == 0 && (nContainment & IN_DECLARATIVES)) {
138 ret = IN_DIVISION | IN_DECLARATIVES | IN_SECTION | NOT_HEADER;
139 } else {
140 ret = nContainment | IN_PARAGRAPH;
141 }
142 }
143 ColourTo(styler, end, chAttr);
144 return ret;
145 }
146
147 void ColouriseCOBOLDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
148 Accessor &styler) {
149
150 styler.StartAt(startPos);
151
152 int state = initStyle;
153 if (state == SCE_COBOL_CHARACTER) // Does not leak onto next line
154 state = SCE_COBOL_DEFAULT;
155 char chPrev = ' ';
156 char chNext = styler[startPos];
157 Sci_PositionU lengthDoc = startPos + length;
158
159 int nContainment;
160
161 Sci_Position currentLine = styler.GetLine(startPos);
162 if (currentLine > 0) {
163 styler.SetLineState(currentLine, styler.GetLineState(currentLine-1));
164 nContainment = styler.GetLineState(currentLine);
165 nContainment &= ~NOT_HEADER;
166 } else {
167 styler.SetLineState(currentLine, 0);
168 nContainment = 0;
169 }
170
171 styler.StartSegment(startPos);
172 bool bNewLine = true;
173 bool bAarea = !isspacechar(chNext);
174 int column = 0;
175 for (Sci_PositionU i = startPos; i < lengthDoc; i++) {
176 char ch = chNext;
177
178 chNext = styler.SafeGetCharAt(i + 1);
179
180 ++column;
181
182 if (bNewLine) {
183 column = 0;
184 }
185 if (column <= 1 && !bAarea) {
186 bAarea = !isspacechar(ch);
187 }
188 bool bSetNewLine = false;
189 if ((ch == '\r' && chNext != '\n') || (ch == '\n')) {
190 // Trigger on CR only (Mac style) or either on LF from CR+LF (Dos/Win) or on LF alone (Unix)
191 // Avoid triggering two times on Dos/Win
192 // End of line
193 if (state == SCE_COBOL_CHARACTER) {
194 ColourTo(styler, i, state);
195 state = SCE_COBOL_DEFAULT;
196 }
197 styler.SetLineState(currentLine, nContainment);
198 currentLine++;
199 bSetNewLine = true;
200 if (nContainment & NOT_HEADER)
201 nContainment &= ~(NOT_HEADER | IN_DECLARATIVES | IN_SECTION);
202 }
203
204 if (styler.IsLeadByte(ch)) {
205 chNext = styler.SafeGetCharAt(i + 2);
206 chPrev = ' ';
207 i += 1;
208 continue;
209 }
210
211 if (state == SCE_COBOL_DEFAULT) {
212 if (isCOBOLwordstart(ch) || (ch == '$' && IsASCII(chNext) && isalpha(chNext))) {
213 ColourTo(styler, i-1, state);
214 state = SCE_COBOL_IDENTIFIER;
215 } else if (column == 6 && (ch == '*' || ch == '/')) {
216 // Cobol comment line: asterisk in column 7.
217 ColourTo(styler, i-1, state);
218 state = SCE_COBOL_COMMENTLINE;
219 } else if (ch == '*' && chNext == '>') {
220 // Cobol inline comment: asterisk, followed by greater than.
221 ColourTo(styler, i-1, state);
222 state = SCE_COBOL_COMMENTLINE;
223 } else if (column == 0 && ch == '*' && chNext != '*') {
224 ColourTo(styler, i-1, state);
225 state = SCE_COBOL_COMMENTLINE;
226 } else if (column == 0 && ch == '/' && chNext != '*') {
227 ColourTo(styler, i-1, state);
228 state = SCE_COBOL_COMMENTLINE;
229 } else if (column == 0 && ch == '*' && chNext == '*') {
230 ColourTo(styler, i-1, state);
231 state = SCE_COBOL_COMMENTDOC;
232 } else if (column == 0 && ch == '/' && chNext == '*') {
233 ColourTo(styler, i-1, state);
234 state = SCE_COBOL_COMMENTDOC;
235 } else if (ch == '"') {
236 ColourTo(styler, i-1, state);
237 state = SCE_COBOL_STRING;
238 } else if (ch == '\'') {
239 ColourTo(styler, i-1, state);
240 state = SCE_COBOL_CHARACTER;
241 } else if (ch == '?' && column == 0) {
242 ColourTo(styler, i-1, state);
243 state = SCE_COBOL_PREPROCESSOR;
244 } else if (isCOBOLoperator(ch)) {
245 ColourTo(styler, i-1, state);
246 ColourTo(styler, i, SCE_COBOL_OPERATOR);
247 }
248 } else if (state == SCE_COBOL_IDENTIFIER) {
249 if (!isCOBOLwordchar(ch)) {
250 int lStateChange = classifyWordCOBOL(styler.GetStartSegment(), i - 1, keywordlists, styler, nContainment, &bAarea);
251
252 if(lStateChange != 0) {
253 styler.SetLineState(currentLine, lStateChange);
254 nContainment = lStateChange;
255 }
256
257 state = SCE_COBOL_DEFAULT;
258 chNext = styler.SafeGetCharAt(i + 1);
259 if (column == 6 && (ch == '*' || ch == '/')) {
260 state = SCE_COBOL_COMMENTLINE;
261 } else if (ch == '"') {
262 state = SCE_COBOL_STRING;
263 } else if (ch == '\'') {
264 state = SCE_COBOL_CHARACTER;
265 } else if (isCOBOLoperator(ch)) {
266 ColourTo(styler, i, SCE_COBOL_OPERATOR);
267 }
268 }
269 } else {
270 if (state == SCE_COBOL_PREPROCESSOR) {
271 if ((ch == '\r' || ch == '\n') && !(chPrev == '\\' || chPrev == '\r')) {
272 ColourTo(styler, i-1, state);
273 state = SCE_COBOL_DEFAULT;
274 }
275 } else if (state == SCE_COBOL_COMMENT) {
276 if (ch == '\r' || ch == '\n') {
277 ColourTo(styler, i-1, state);
278 state = SCE_COBOL_DEFAULT;
279 }
280 } else if (state == SCE_COBOL_COMMENTDOC) {
281 if (ch == '\r' || ch == '\n') {
282 if (((i > styler.GetStartSegment() + 2) || (
283 (initStyle == SCE_COBOL_COMMENTDOC) &&
284 (styler.GetStartSegment() == static_cast<Sci_PositionU>(startPos))))) {
285 ColourTo(styler, i-1, state);
286 state = SCE_COBOL_DEFAULT;
287 }
288 }
289 } else if (state == SCE_COBOL_COMMENTLINE) {
290 if (ch == '\r' || ch == '\n') {
291 ColourTo(styler, i-1, state);
292 state = SCE_COBOL_DEFAULT;
293 }
294 } else if (state == SCE_COBOL_STRING) {
295 if (ch == '"') {
296 ColourTo(styler, i, state);
297 state = SCE_COBOL_DEFAULT;
298 } else if (ch == '\r' || ch == '\n') {
299 ColourTo(styler, i-1, state);
300 state = SCE_COBOL_DEFAULT;
301 }
302 } else if (state == SCE_COBOL_CHARACTER) {
303 if (ch == '\'') {
304 ColourTo(styler, i, state);
305 state = SCE_COBOL_DEFAULT;
306 }
307 }
308 }
309 chPrev = ch;
310 bNewLine = bSetNewLine;
311 if (bNewLine)
312 {
313 bAarea = false;
314 }
315 }
316 ColourTo(styler, lengthDoc - 1, state);
317 }
318
319 void FoldCOBOLDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[],
320 Accessor &styler) {
321 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
322 Sci_PositionU endPos = startPos + length;
323 int visibleChars = 0;
324 Sci_Position lineCurrent = styler.GetLine(startPos);
325 int levelPrev = lineCurrent > 0 ? styler.LevelAt(lineCurrent - 1) & SC_FOLDLEVELNUMBERMASK : 0xFFF;
326 char chNext = styler[startPos];
327
328 bool bNewLine = true;
329 bool bAarea = !isspacechar(chNext);
330 int column = 0;
331 bool bComment = false;
332 for (Sci_PositionU i = startPos; i < endPos; i++) {
333 char ch = chNext;
334 chNext = styler.SafeGetCharAt(i + 1);
335 ++column;
336
337 if (bNewLine) {
338 column = 0;
339 bComment = (ch == '*' || ch == '/' || ch == '?');
340 }
341 if (column <= 1 && !bAarea) {
342 bAarea = !isspacechar(ch);
343 }
344 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
345 if (atEOL) {
346 int nContainment = styler.GetLineState(lineCurrent);
347 int lev = CountBits(nContainment & IN_FLAGS) | SC_FOLDLEVELBASE;
348 if (bAarea && !bComment)
349 --lev;
350 if (visibleChars == 0 && foldCompact)
351 lev |= SC_FOLDLEVELWHITEFLAG;
352 if ((bAarea) && (visibleChars > 0) && !(nContainment & NOT_HEADER) && !bComment)
353 lev |= SC_FOLDLEVELHEADERFLAG;
354 if (lev != styler.LevelAt(lineCurrent)) {
355 styler.SetLevel(lineCurrent, lev);
356 }
357 if ((lev & SC_FOLDLEVELNUMBERMASK) <= (levelPrev & SC_FOLDLEVELNUMBERMASK)) {
358 // this level is at the same level or less than the previous line
359 // therefore these is nothing for the previous header to collapse, so remove the header
360 styler.SetLevel(lineCurrent - 1, levelPrev & ~SC_FOLDLEVELHEADERFLAG);
361 }
362 levelPrev = lev;
363 visibleChars = 0;
364 bAarea = false;
365 bNewLine = true;
366 lineCurrent++;
367 } else {
368 bNewLine = false;
369 }
370
371
372 if (!isspacechar(ch))
373 visibleChars++;
374 }
375
376 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
377 int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
378 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
379 }
380
381 const char * const COBOLWordListDesc[] = {
382 "A Keywords",
383 "B Keywords",
384 "Extended Keywords",
385 nullptr
386 };
387
388 }
389
390 extern const LexerModule lmCOBOL(SCLEX_COBOL, ColouriseCOBOLDoc, "COBOL", FoldCOBOLDoc, COBOLWordListDesc);