forked from universal-ctags/ctags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcobol.c
523 lines (452 loc) · 11.7 KB
/
cobol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
* Copyright (c) 2000-2003, Darren Hiebert
*
* This source code is released for free distribution under the terms of the
* GNU General Public License version 2 or (at your option) any later version.
*
* This module contains functions for generating tags for COBOL language
* files.
*/
/* Some references:
* - https://www.cs.vu.nl/grammarware/browsable/cobol/
* - https://www.cs.vu.nl/grammarware/browsable/vs-cobol-ii/
* - https://open-cobol.sourceforge.io/guides/grammar.pdf
* - http://mapage.noos.fr/~bpinon/a_cobol_parser.htm
* - https://en.wikipedia.org/wiki/COBOL
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include "debug.h"
#include "entry.h"
#include "keyword.h"
#include "nestlevel.h"
#include "parse.h"
#include "read.h"
#include "routines.h"
typedef enum {
K_FILE,
K_GROUP,
K_PROGRAM,
K_SECTION,
K_DIVISION,
K_PARAGRAPH,
K_DATA,
K_SOURCEFILE,
} cobolKind;
typedef enum {
COBOL_SOURCEFILE_COPIED,
} cobolSourcefileRole;
static roleDefinition CobolSourcefileRoles [] = {
{ true, "copied", "copied in source file" },
};
static kindDefinition CobolKinds[] = {
{ true, 'f', "fd", "file descriptions (FD, SD, RD)" },
{ true, 'g', "group", "group items" },
{ true, 'P', "program", "program ids" },
{ true, 's', "section", "sections" },
{ true, 'D', "division", "divisions" },
{ true, 'p', "paragraph", "paragraphs" },
{ true, 'd', "data", "data items" },
{ true, 'S', "sourcefile", "source code file",
.referenceOnly = true, ATTACH_ROLES(CobolSourcefileRoles)},
};
static langType Lang_cobol;
enum {
KEYWORD_FD,
KEYWORD_SD,
KEYWORD_RD,
KEYWORD_SECTION,
KEYWORD_DIVISION,
KEYWORD_CONTINUE,
KEYWORD_END_EXEC,
KEYWORD_FILLER,
KEYWORD_BLANK,
KEYWORD_OCCURS,
KEYWORD_IS,
KEYWORD_JUST,
KEYWORD_PIC,
KEYWORD_REDEFINES,
KEYWORD_RENAMES,
KEYWORD_SIGN,
KEYWORD_SYNC,
KEYWORD_USAGE,
KEYWORD_VALUE,
KEYWORD_PROGRAM_ID,
KEYWORD_EXIT,
KEYWORD_COPY,
};
static const keywordTable cobolKeywordTable[] = {
#define DEFINE_KEYWORD(n) { #n, KEYWORD_##n }
DEFINE_KEYWORD (FD),
DEFINE_KEYWORD (SD),
DEFINE_KEYWORD (RD),
DEFINE_KEYWORD (SECTION),
DEFINE_KEYWORD (DIVISION),
DEFINE_KEYWORD (CONTINUE),
{ "END-EXEC", KEYWORD_END_EXEC },
DEFINE_KEYWORD (EXIT),
DEFINE_KEYWORD (FILLER),
DEFINE_KEYWORD (BLANK),
DEFINE_KEYWORD (OCCURS),
DEFINE_KEYWORD (IS),
DEFINE_KEYWORD (JUST),
DEFINE_KEYWORD (PIC),
{ "PICTURE", KEYWORD_PIC },
DEFINE_KEYWORD (REDEFINES),
DEFINE_KEYWORD (RENAMES),
DEFINE_KEYWORD (SIGN),
DEFINE_KEYWORD (SYNC),
DEFINE_KEYWORD (USAGE),
DEFINE_KEYWORD (VALUE),
{ "VALUES", KEYWORD_VALUE },
{ "PROGRAM-ID", KEYWORD_PROGRAM_ID },
DEFINE_KEYWORD (COPY),
};
#define INDICATOR_COLUMN 7
#define PROGRAM_NAME_AREA_COLUMN 73
#define isIdentifierChar(c) (isalnum(c) || (c) == '-')
#define isQuote(c) ((c) == '\'' || (c) == '"')
typedef enum {
/* Fixed: program starts at column 8, ends at column 72 */
FORMAT_FIXED = 0x1,
/* Free: program starts at column 1, no specific end */
FORMAT_FREE = 0x2,
/* Variable: program starts at column 8, no specific end */
FORMAT_VARIABLE = FORMAT_FIXED | FORMAT_FREE
} CobolFormat;
static struct {
vString *line;
unsigned long int lineNumber;
MIOPos filePosition;
const char *nextLine;
CobolFormat format;
} CblInputState;
static void cblppInit (const CobolFormat format)
{
CblInputState.line = vStringNew ();
CblInputState.lineNumber = 0;
CblInputState.nextLine = NULL;
CblInputState.format = format;
}
static void cblppDeinit (void)
{
vStringDelete (CblInputState.line);
}
static const char *cblppGetColumn (const char *line,
const unsigned int column)
{
unsigned int col = 0;
for (; *line; line++)
{
col += (*line == '\t') ? 8 : 1;
if (col >= column)
return line;
}
return NULL;
}
static void cblppAppendLine (vString *buffer,
const char *line)
{
if (CblInputState.format & FORMAT_FIXED)
{
const char *indicator = cblppGetColumn (line, INDICATOR_COLUMN);
if (indicator && *indicator && *indicator != '*' && *indicator != '/')
{
const char *lineStart = indicator + 1;
const char *lineEnd = cblppGetColumn (line, PROGRAM_NAME_AREA_COLUMN);
if (*indicator == '-')
{
vStringStripTrailing (buffer);
while (isspace (*lineStart))
lineStart++;
}
if (CblInputState.format == FORMAT_FIXED)
vStringNCatS (buffer, lineStart, lineEnd - lineStart);
else
vStringCatS (buffer, lineStart);
}
}
else if (line[0] != '*' && line[0] != '/')
vStringCatS (buffer, line);
}
/* TODO: skip *> comments */
static const char *cblppGetLine (void)
{
const char *line;
if (CblInputState.nextLine)
{
line = CblInputState.nextLine;
CblInputState.nextLine = NULL;
}
else
line = (const char *) readLineFromInputFile ();
CblInputState.lineNumber = getInputLineNumber ();
CblInputState.filePosition = getInputFilePosition ();
if (!line)
return NULL;
vStringClear (CblInputState.line);
cblppAppendLine (CblInputState.line, line);
/* check for continuation lines */
if (CblInputState.format & FORMAT_FIXED)
{
while (true)
{
const char *indicator;
line = (const char *) readLineFromInputFile ();
if (! line)
break;
indicator = cblppGetColumn (line, INDICATOR_COLUMN);
if (indicator && *indicator == '-')
cblppAppendLine (CblInputState.line, line);
else
break;
}
CblInputState.nextLine = line;
}
return vStringValue (CblInputState.line);
}
static void initCOBOLRefTagEntry (tagEntryInfo *e, const char *name,
const cobolKind kind, const int role)
{
initRefTagEntry (e, name, kind, role);
e->lineNumber = CblInputState.lineNumber;
e->filePosition = CblInputState.filePosition;
}
static void initCOBOLTagEntry (tagEntryInfo *e, const char *name, const cobolKind kind)
{
initCOBOLRefTagEntry (e, name, kind, ROLE_DEFINITION_INDEX);
}
static int makeCOBOLRefTag (const char *name, const cobolKind kind, const int role)
{
if (CobolKinds[kind].enabled)
{
tagEntryInfo e;
initCOBOLRefTagEntry (&e, name, kind, role);
return makeTagEntry (&e);
}
return CORK_NIL;
}
static int makeCOBOLTag (const char *name, const cobolKind kind)
{
return makeCOBOLRefTag (name, kind, ROLE_DEFINITION_INDEX);
}
#define CBL_NL(nl) (*((unsigned int *) (nestingLevelGetUserData (nl))))
static NestingLevel *popNestingLevelsToLevelNumber (NestingLevels *levels, const unsigned int levelNumber)
{
NestingLevel *nl;
while (true)
{
nl = nestingLevelsGetCurrent (levels);
if (! nl || CBL_NL (nl) < levelNumber)
break;
nestingLevelsPop (levels);
}
return nl;
}
static bool isNumeric (const char *nptr, unsigned long int *num)
{
char *endptr;
unsigned long int v;
v = strtoul (nptr, &endptr, 10);
if (nptr != endptr && *endptr == 0)
{
if (num)
*num = v;
return true;
}
return false;
}
static void findCOBOLTags (const CobolFormat format)
{
NestingLevels *levels;
const char *line;
cblppInit (format);
levels = nestingLevelsNew (sizeof (unsigned int));
while ((line = cblppGetLine ()) != NULL)
{
char word[64];
int keyword;
unsigned long int levelNumber;
#define READ_WHILE(word, cond) \
do { \
unsigned int i; \
for (i = 0; i < (ARRAY_SIZE (word) - 1) && *line && (cond); line++) \
word[i++] = *line; \
word[i] = 0; \
} while (0)
#define READ_LITERAL(word) \
do { \
const char READ_LITERAL__q = isQuote (*line) ? *line++ : 0; \
READ_WHILE (word, (READ_LITERAL__q && READ_LITERAL__q != *line) || \
isIdentifierChar (*line)); \
if (READ_LITERAL__q && READ_LITERAL__q == *line) \
line++; \
keyword = lookupCaseKeyword (word, Lang_cobol); \
} while (0)
#define READ_WORD(word, keyword) \
do { \
READ_WHILE (word, isIdentifierChar (*line)); \
keyword = lookupCaseKeyword (word, Lang_cobol); \
} while (0)
#define READ_KEYWORD(keyword) \
do { \
char READ_KEYWORD__word[64]; \
READ_WORD (READ_KEYWORD__word, keyword); \
} while (0)
#define SKIP_SPACES() do { while (isspace (*line)) line++; } while (0)
SKIP_SPACES ();
READ_WORD (word, keyword);
SKIP_SPACES ();
switch (keyword)
{
case KEYWORD_FD:
case KEYWORD_SD:
case KEYWORD_RD:
READ_WORD (word, keyword);
SKIP_SPACES ();
if (*word && *line == '.')
makeCOBOLTag (word, K_FILE);
break;
case KEYWORD_PROGRAM_ID:
if (*line == '.')
{
line++;
SKIP_SPACES ();
}
READ_LITERAL (word);
if (*word)
makeCOBOLTag (word, K_PROGRAM);
break;
case KEYWORD_COPY:
READ_WORD (word, keyword); // FIXME: also allow LITERAL
if (*word)
makeCOBOLRefTag (word, K_SOURCEFILE, COBOL_SOURCEFILE_COPIED);
break;
case KEYWORD_CONTINUE:
case KEYWORD_END_EXEC:
case KEYWORD_EXIT:
case KEYWORD_FILLER:
/* nothing, just ignore those in following cases */;
break;
default:
if (isNumeric (word, &levelNumber))
{
READ_WORD (word, keyword);
SKIP_SPACES ();
if (*word && keyword != KEYWORD_FILLER)
{
int kind = KIND_GHOST_INDEX;
if (*line == '.')
kind = K_GROUP;
else
{
int keyword2;
READ_KEYWORD (keyword2);
switch (keyword2)
{
case KEYWORD_BLANK:
case KEYWORD_OCCURS:
case KEYWORD_IS:
case KEYWORD_JUST:
case KEYWORD_PIC:
case KEYWORD_REDEFINES:
case KEYWORD_RENAMES:
case KEYWORD_SIGN:
case KEYWORD_SYNC:
case KEYWORD_USAGE:
case KEYWORD_VALUE:
kind = K_DATA;
}
}
if (kind != KIND_GHOST_INDEX)
{
NestingLevel *nl;
tagEntryInfo entry;
int r;
unsigned int nestingLevelNumber;
/* for nesting purposes, level 77 is identical to 1,
* and 66 to 2 */
switch (levelNumber)
{
default: nestingLevelNumber = levelNumber; break;
case 77: nestingLevelNumber = 1; break;
case 66: nestingLevelNumber = 2; break;
}
nl = popNestingLevelsToLevelNumber (levels, nestingLevelNumber);
initCOBOLTagEntry (&entry, word, kind);
if (nl && CBL_NL (nl) < nestingLevelNumber)
entry.extensionFields.scopeIndex = nl->corkIndex;
r = makeTagEntry (&entry);
if (levelNumber < 50 /* exclude special levels */)
{
nl = nestingLevelsPush (levels, r);
CBL_NL (nl) = levelNumber;
}
}
}
}
else if (*word && *line == '.')
makeCOBOLTag (word, K_PARAGRAPH);
else
{
int keyword2;
READ_KEYWORD (keyword2);
SKIP_SPACES ();
if (keyword2 == KEYWORD_DIVISION && *line == '.')
makeCOBOLTag (word, K_DIVISION);
else if (keyword2 == KEYWORD_SECTION && *line == '.')
makeCOBOLTag (word, K_SECTION);
}
}
}
nestingLevelsFree (levels);
cblppDeinit ();
}
static void findCOBOLFixedTags (void)
{
findCOBOLTags (FORMAT_FIXED);
}
static void findCOBOLFreeTags (void)
{
findCOBOLTags (FORMAT_FREE);
}
static void findCOBOLVariableTags (void)
{
findCOBOLTags (FORMAT_VARIABLE);
}
static void initializeCobolParser (langType language)
{
Lang_cobol = language;
}
static parserDefinition* commonCobolParserDefinition (const char *name,
simpleParser parser)
{
parserDefinition* def = parserNew (name);
def->initialize = initializeCobolParser;
def->parser = parser;
def->kindTable = CobolKinds;
def->kindCount = ARRAY_SIZE(CobolKinds);
def->keywordTable = cobolKeywordTable;
def->keywordCount = ARRAY_SIZE(cobolKeywordTable);
def->useCork = CORK_QUEUE;
return def;
}
extern parserDefinition* CobolParser (void)
{
static const char *const extensions [] = {
"cbl", "cob", "CBL", "COB", NULL };
parserDefinition* def = commonCobolParserDefinition ("Cobol",
findCOBOLFixedTags);
def->extensions = extensions;
return def;
}
extern parserDefinition* CobolFreeParser (void)
{
return commonCobolParserDefinition ("CobolFree", findCOBOLFreeTags);
}
extern parserDefinition* CobolVariableParser (void)
{
return commonCobolParserDefinition ("CobolVariable", findCOBOLVariableTags);
}