forked from universal-ctags/ctags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ada.c
2230 lines (1986 loc) · 68.4 KB
/
ada.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* File: Ada.c
* Description: Enables extended Ada parsing support in Exuberant Ctags
* Version: 0.6
* Date: October 26, 2006
* Author: A. Aaron Cornelius (ADotAaronDotCorneliusAtgmailDotcom)
*
* Installation:
* You must have the Exuberant Ctags source to install this parser. Once you
* have the source, place this file into the directory with the rest of the
* ctags source. After ada.c is in the correct directory you need to make the
* following changes so that the ada parser is included when you compile and
* install ctags:
*
* to file source.mak add the line
* ada.c \
*
* after
* SOURCES = \
*
* then add the line
* ada.$(OBJECT) \
*
* after
* OBJECTS = \
*
* to file parsers.h add the line
* AdaParser, \
*
* after
* #define PARSER_LIST \
*
* Then compile and install ctags as normal (usually: './configure', './make',
* './make install').
*
* Changelog:
*
* 11/02/2006 - Completed implementation of file scope info and qualified tags
* information gathering.
* 11/02/2006 - Added recognition of private flag in a token for file scope
* checking purposes.
* 10/27/2006 - Added full package scope name when --extra=+q is set.
* 10/27/2006 - Fixed file scope setting, and added check to verify that tags
* with file scope should be included in the tag file.
* 10/26/2006 - Fixed error which caused infinite loop when parsing some
* files.
* 0.5 - Bugfixes
* 10/20/2006 - Cleaned up freeAdaTokenList.
* 10/20/2006 - Fixed error in freeAdaToken that caused the child token lists
* to become corrupted when "separate" tokens were deleted.
* 0.4 - Third Revision - 09/25/2006
* 09/25/2006 - Fixed error in newAdaToken which could cause an error on some
* systems when a separate token (which is temporary) gets
* created.
* 09/25/2006 - Change matchFilePos initialization in the findAdaTags
* function.
* 0.3 - Second Revision
* 06/02/2006 - Added missing EOF checks to prevent infinite loops in the case
* of an incomplete Ada (or non-Ada) file being parsed.
* 06/02/2006 - Added Copyright notice.
* 0.2 - First Revision
* 05/26/2006 - Fixed an error where tagging the proper scope of something
* declared in an anonymous block or anonymous loop was not
* working properly.
* 05/26/2006 - Fixed an error capturing the name of a 'separate' tag.
* 05/26/2006 - Fixed the cmp() function so that it finds matches correctly.
* 05/26/2006 - Fixed some spelling errors.
* 05/26/2006 - Added explicit skipping of use and with clauses.
* 0.1 - Initial Release
*
* Future Changes:
* TODO: Add inheritance information?
* TODO: Add signature gathering?
*
* Copyright (C) 2006 A. Aaron Cornelius
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*/
#include "general.h" /* always include first */
#include <string.h> /* to declare strxxx() functions */
#include <ctype.h> /* to define isxxx() macros */
#include <setjmp.h>
#include "parse.h" /* always include */
#include "read.h" /* to define file fileReadLine() */
#include "entry.h" /* for the tag entry manipulation */
#include "routines.h" /* for generic malloc/realloc/free routines */
#include "options.h" /* for the Option structure */
#include "debug.h" /* for Assert */
typedef enum eAdaException
{
EXCEPTION_NONE,
EXCEPTION_EOF
} adaException;
static adaException exception;
typedef enum eAdaParseMode
{
ADA_ROOT,
ADA_DECLARATIONS,
ADA_CODE,
ADA_EXCEPTIONS,
ADA_GENERIC
} adaParseMode;
typedef enum eAdaKinds
{
ADA_KIND_SEPARATE = -2, /* for defining the parent token name of a child
* sub-unit */
ADA_KIND_UNDEFINED = -1, /* for default/initialization values */
ADA_KIND_PACKAGE_SPEC,
ADA_KIND_PACKAGE,
ADA_KIND_TYPE_SPEC,
ADA_KIND_TYPE,
ADA_KIND_SUBTYPE_SPEC,
ADA_KIND_SUBTYPE,
ADA_KIND_RECORD_COMPONENT,
ADA_KIND_ENUM_LITERAL,
ADA_KIND_VARIABLE_SPEC,
ADA_KIND_VARIABLE,
ADA_KIND_FORMAL,
ADA_KIND_CONSTANT,
ADA_KIND_EXCEPTION,
ADA_KIND_SUBPROGRAM_SPEC,
ADA_KIND_SUBPROGRAM,
ADA_KIND_TASK_SPEC,
ADA_KIND_TASK,
ADA_KIND_PROTECTED_SPEC,
ADA_KIND_PROTECTED,
ADA_KIND_ENTRY_SPEC,
ADA_KIND_ENTRY,
ADA_KIND_LABEL,
ADA_KIND_IDENTIFIER,
ADA_KIND_AUTOMATIC_VARIABLE,
ADA_KIND_ANONYMOUS, /* for non-identified loops and blocks */
ADA_KIND_COUNT /* must be last */
} adaKind;
static kindOption AdaKinds[] =
{
{ TRUE, 'P', "packspec", "package specifications" },
{ TRUE, 'p', "package", "packages" },
{ FALSE, 'T', "typespec", "type specifications" },
{ TRUE, 't', "type", "types" },
{ FALSE, 'U', "subspec", "subtype specifications" },
{ TRUE, 'u', "subtype", "subtypes" },
{ TRUE, 'c', "component", "record type components" },
{ TRUE, 'l', "literal", "enum type literals" },
{ FALSE, 'V', "varspec", "variable specifications" },
{ TRUE, 'v', "variable", "variables" },
{ TRUE, 'f', "formal", "generic formal parameters" },
{ TRUE, 'n', "constant", "constants" },
{ TRUE, 'x', "exception", "user defined exceptions" },
{ TRUE, 'R', "subprogspec", "subprogram specifications" },
{ TRUE, 'r', "subprogram", "subprograms" },
{ TRUE, 'K', "taskspec", "task specifications" },
{ TRUE, 'k', "task", "tasks" },
{ TRUE, 'O', "protectspec", "protected data specifications" },
{ TRUE, 'o', "protected", "protected data" },
{ FALSE, 'E', "entryspec", "task/protected data entry specifications" },
{ TRUE, 'e', "entry", "task/protected data entries" },
{ TRUE, 'b', "label", "labels" },
{ TRUE, 'i', "identifier", "loop/declare identifiers"},
{ FALSE, 'a', "autovar", "automatic variables" },
{ FALSE, 'y', "annon", "loops and blocks with no identifier" }
};
typedef struct sAdaTokenList
{
int numTokens;
struct sAdaTokenInfo *head;
struct sAdaTokenInfo *tail;
} adaTokenList;
typedef struct sAdaTokenInfo
{
adaKind kind;
boolean isSpec;
boolean isPrivate;
char *name;
tagEntryInfo tag;
struct sAdaTokenInfo *parent;
struct sAdaTokenInfo *prev;
struct sAdaTokenInfo *next;
adaTokenList children;
} adaTokenInfo;
typedef enum eAdaKeywords
{
ADA_KEYWORD_ACCEPT,
ADA_KEYWORD_BEGIN,
ADA_KEYWORD_BODY,
ADA_KEYWORD_CASE,
ADA_KEYWORD_CONSTANT,
ADA_KEYWORD_DECLARE,
ADA_KEYWORD_DO,
ADA_KEYWORD_ELSE,
ADA_KEYWORD_ELSIF,
ADA_KEYWORD_END,
ADA_KEYWORD_ENTRY,
ADA_KEYWORD_EXCEPTION,
ADA_KEYWORD_FOR,
ADA_KEYWORD_FUNCTION,
ADA_KEYWORD_GENERIC,
ADA_KEYWORD_IF,
ADA_KEYWORD_IN,
ADA_KEYWORD_IS,
ADA_KEYWORD_LOOP,
ADA_KEYWORD_NEW,
ADA_KEYWORD_OR,
ADA_KEYWORD_PACKAGE,
ADA_KEYWORD_PRAGMA,
ADA_KEYWORD_PRIVATE,
ADA_KEYWORD_PROCEDURE,
ADA_KEYWORD_PROTECTED,
ADA_KEYWORD_RECORD,
ADA_KEYWORD_RENAMES,
ADA_KEYWORD_SELECT,
ADA_KEYWORD_SEPARATE,
ADA_KEYWORD_SUBTYPE,
ADA_KEYWORD_TASK,
ADA_KEYWORD_THEN,
ADA_KEYWORD_TYPE,
ADA_KEYWORD_UNTIL,
ADA_KEYWORD_USE,
ADA_KEYWORD_WHEN,
ADA_KEYWORD_WHILE,
ADA_KEYWORD_WITH
} adaKeyword;
static const char *AdaKeywords[] =
{
"accept",
"begin",
"body",
"case",
"constant",
"declare",
"do",
"else",
"elsif",
"end",
"entry",
"exception",
"for",
"function",
"generic",
"if",
"in",
"is",
"loop",
"new",
"or",
"package",
"pragma",
"private",
"procedure",
"protected",
"record",
"renames",
"select",
"separate",
"subtype",
"task",
"then",
"type",
"until",
"use",
"when",
"while",
"with"
};
/* a jump buffer for fail-safe error prevention */
static jmp_buf eofError;
/* a simple var to keep track of how many times we hit EOF... If we hit it
* say, about 1000 times, we will print an error, jump to the end of the
* program, and store what tags we have */
static int eofCount;
/* variables for managing the input string, position as well as input line
* number and position */
static const char *line;
static int lineLen;
static int pos;
static unsigned long matchLineNum;
static fpos_t matchFilePos;
/* utility functions */
static void makeSpec(adaKind *kind);
/* prototypes of functions for manipulating the Ada tokens */
static adaTokenInfo *newAdaToken(const char *name, int len,
adaKind kind, boolean isSpec,
adaTokenInfo *parent);
static void freeAdaToken(adaTokenList *list, adaTokenInfo *token);
static void appendAdaToken(adaTokenInfo *parent, adaTokenInfo *token);
/* token list processing function prototypes */
static void initAdaTokenList(adaTokenList *list);
static void freeAdaTokenList(adaTokenList *list);
static void appendAdaTokenList(adaTokenInfo *parent, adaTokenList *children);
/* prototypes of functions for moving through the DEFINED text */
static void readNewLine(void);
static void movePos(int amount);
static boolean cmp(const char *buf, int len, const char *match);
static boolean adaCmp(const char *match);
static boolean adaKeywordCmp(adaKeyword keyword);
static void skipUntilWhiteSpace(void);
static void skipWhiteSpace(void);
static void skipPast(const char *past);
static void skipPastKeyword(adaKeyword keyword);
static void skipPastWord(void);
/* prototypes of functions for parsing the high-level Ada constructs */
static adaTokenInfo *adaParseBlock(adaTokenInfo *parent, adaKind kind);
static adaTokenInfo *adaParseSubprogram(adaTokenInfo *parent, adaKind kind);
static adaTokenInfo *adaParseType(adaTokenInfo *parent, adaKind kind);
static adaTokenInfo *adaParseVariables(adaTokenInfo *parent, adaKind kind);
static adaTokenInfo *adaParseLoopVar(adaTokenInfo *parent);
static adaTokenInfo *adaParse(adaParseMode mode, adaTokenInfo *parent);
/* prototypes of the functions used by ctags */
static void storeAdaTags(adaTokenInfo *token, const char *parentScope);
static void findAdaTags(void);
extern parserDefinition* AdaParser(void);
static void makeSpec(adaKind *kind)
{
switch(*kind)
{
case ADA_KIND_PACKAGE:
*kind = ADA_KIND_PACKAGE_SPEC;
break;
case ADA_KIND_TYPE:
*kind = ADA_KIND_TYPE_SPEC;
break;
case ADA_KIND_SUBTYPE:
*kind = ADA_KIND_SUBTYPE_SPEC;
break;
case ADA_KIND_VARIABLE:
*kind = ADA_KIND_VARIABLE_SPEC;
break;
case ADA_KIND_SUBPROGRAM:
*kind = ADA_KIND_SUBPROGRAM_SPEC;
break;
case ADA_KIND_TASK:
*kind = ADA_KIND_TASK_SPEC;
break;
case ADA_KIND_PROTECTED:
*kind = ADA_KIND_PROTECTED_SPEC;
break;
case ADA_KIND_ENTRY:
*kind = ADA_KIND_ENTRY_SPEC;
break;
default:
printf("Warning, non-spec type trying to be 'spec'ified\n");
*kind = ADA_KIND_UNDEFINED;
break;
}
} /* static void makeSpec(adaKind *kind) */
static adaTokenInfo *newAdaToken(const char *name, int len, adaKind kind,
boolean isSpec, adaTokenInfo *parent)
{
char *tmpName = NULL;
adaTokenInfo *token = xMalloc(1, adaTokenInfo);
token->name = NULL;
if(name != NULL && len != 0)
{
tmpName = xMalloc(len + 1, char);
strncpy(tmpName, name, len);
tmpName[len] = '\0';
}
/* init the tag */
initTagEntry(&token->tag, tmpName);
token->kind = kind;
token->isSpec = isSpec;
token->isPrivate = FALSE;
/* set the token data */
token->name = tmpName;
token->parent = parent;
/* Now set the file scope for this tag. A tag has file scope if its direct
* parent is a package/subprogram/protected/task spec, or if it it's parent
* is UNDEFINED (a 'root' token), and if this is not in a 'private' section
* of that spec. */
if((parent != NULL) && (parent->isPrivate == FALSE) &&
((parent->kind == ADA_KIND_UNDEFINED) ||
(parent->kind == ADA_KIND_SEPARATE) ||
((parent->isSpec == TRUE) && ((parent->kind == ADA_KIND_PACKAGE) ||
(parent->kind == ADA_KIND_SUBPROGRAM) ||
(parent->kind == ADA_KIND_PROTECTED) ||
(parent->kind == ADA_KIND_TASK)))))
{
token->tag.isFileScope = FALSE;
}
else
{
token->tag.isFileScope = TRUE;
}
/* add the kind info - unless this is a SEPARATE kind, in which case keep
* them blank because they get filled in later. */
if(kind > ADA_KIND_UNDEFINED)
{
token->tag.kindName = AdaKinds[kind].name;
token->tag.kind = AdaKinds[kind].letter;
}
else
{
token->tag.kindName = "";
token->tag.kind = '\0';
}
/* setup the parent and children pointers */
initAdaTokenList(&token->children);
appendAdaToken(parent, token);
return token;
} /* static adaTokenInfo *newAdaToken(const char *name, int len, ... ) */
static void freeAdaToken(adaTokenList *list, adaTokenInfo *token)
{
if(token != NULL)
{
if(token->name != NULL)
{
eFree((void *) token->name);
token->name = NULL;
}
/* before we delete this token, clean up it's children */
freeAdaTokenList(&token->children);
/* move the next token in the list to this token's spot */
if(token->prev != NULL)
{
token->prev->next = token->next;
}
else if(list != NULL && token->prev == NULL)
{
list->head = token->next;
}
/* move the previous token in the list to this token's spot */
if(token->next != NULL)
{
token->next->prev = token->prev;
}
else if(list != NULL && token->next == NULL)
{
list->tail = token->prev;
}
/* decrement the list count */
if(list != NULL)
{
list->numTokens--;
}
/* now that this node has had everything hanging off of it rearranged,
* delete this node */
eFree(token);
} /* if(token != NULL) */
} /* static void freeAdaToken(adaTokenList *list, adaTokenInfo *token) */
static void appendAdaToken(adaTokenInfo *parent, adaTokenInfo *token)
{
/* if the parent or newChild is NULL there is nothing to be done */
if(parent != NULL && token != NULL)
{
/* we just need to add this to the list and set a parent pointer */
parent->children.numTokens++;
token->parent = parent;
token->prev = parent->children.tail;
token->next = NULL;
if(parent->children.tail != NULL)
{
parent->children.tail->next = token;
}
/* the token that was just added always becomes the last token int the
* list */
parent->children.tail = token;
if(parent->children.head == NULL)
{
parent->children.head = token;
}
} /* if(parent != NULL && token != NULL) */
} /* static void appendAdaToken(adaTokenInfo *parent, adaTokenInfo *token) */
static void initAdaTokenList(adaTokenList *list)
{
if(list != NULL)
{
list->numTokens = 0;
list->head = NULL;
list->tail = NULL;
}
}
static void freeAdaTokenList(adaTokenList *list)
{
if(list != NULL)
{
while(list->head != NULL)
{
freeAdaToken(list, list->head);
}
}
}
static void appendAdaTokenList(adaTokenInfo *parent, adaTokenList *children)
{
adaTokenInfo *tmp = NULL;
if(parent != NULL && children != NULL)
{
while(children->head != NULL)
{
tmp = children->head->next;
appendAdaToken(parent, children->head);
/* we just need to worry about setting the head pointer properly during
* the list iteration. The node's pointers will get set properly by the
* appendAdaToken() function */
children->head = tmp;
}
/* now that we have added all nodes from the children list to the parent
* node, zero out the children list */
initAdaTokenList(children);
}
} /* static void appendAdaTokenList(adaTokenInfo *parent, ... ) */
static void readNewLine(void)
{
while(TRUE)
{
line = (const char *) fileReadLine();
pos = 0;
if(line == NULL)
{
lineLen = 0;
exception = EXCEPTION_EOF;
eofCount++;
if(eofCount >= 1000)
{
printf("ERROR: Reached EOF %d times in file %s\n", eofCount,
getInputFileName());
longjmp(eofError, exception);
}
else
{
return;
}
} /* if(line == NULL) */
lineLen = strlen(line);
if(lineLen > 0)
{
return;
}
} /* while(TRUE) */
} /* static void readNewLine(void) */
static void movePos(int amount)
{
pos += amount;
if(exception != EXCEPTION_EOF && pos >= lineLen)
{
readNewLine();
}
}
/* a macro for checking for comments... This isn't the same as the check in
* cmp() because comments don't have to have whitespace or separation-type
* characters following the "--" */
#define isAdaComment(buf, pos, len) \
(((pos) == 0 || (!isalnum((buf)[(pos) - 1]) && (buf)[(pos) - 1] != '_')) && \
(pos) < (len) && \
strncasecmp(&(buf)[(pos)], "--", strlen("--")) == 0)
static boolean cmp(const char *buf, int len, const char *match)
{
boolean status = FALSE;
/* if we are trying to match nothing, that is always true */
if(match == NULL)
{
return TRUE;
}
/* first check to see if the buffer is empty, if it is, return false */
if(buf == NULL)
{
return status;
}
/* A match only happens the number of chars in the matching string match,
* and whitespace follows... Which means we also must check to see if the
* end of the line is after the matching string. Also check for some
* separation characters such as (, ), :, or ; */
if((strncasecmp(buf, match, strlen(match)) == 0) &&
(strlen(match) == len ||
(strlen(match) < len &&
(isspace(buf[strlen(match)]) || buf[strlen(match)] == '(' ||
buf[strlen(match)] == ')' || buf[strlen(match)] == ':' ||
buf[strlen(match)] == ';'))))
{
status = TRUE;
}
return status;
} /* static boolean cmp(char *buf, int len, char *match) */
static boolean adaCmp(const char *match)
{
boolean status = FALSE;
/* first check to see if line is empty, if it is, throw an exception */
if(line == NULL)
{
exception = EXCEPTION_EOF;
return status;
}
status = cmp(&line[pos], lineLen - pos, match);
/* if we match, increment the position pointer */
if(status == TRUE && match != NULL)
{
matchLineNum = getSourceLineNumber();
matchFilePos = getInputFilePosition();
movePos((strlen(match)));
}
return status;
} /* static boolean adaCmp(char *match) */
/* just a version of adaCmp that is a bit more optimized for keywords */
static boolean adaKeywordCmp(adaKeyword keyword)
{
boolean status = FALSE;
/* first check to see if line is empty, if it is, throw an exception */
if(line == NULL)
{
exception = EXCEPTION_EOF;
return status;
}
status = cmp(&line[pos], lineLen - pos, AdaKeywords[keyword]);
/* if we match, increment the position pointer */
if(status == TRUE)
{
matchLineNum = getSourceLineNumber();
matchFilePos = getInputFilePosition();
movePos((strlen(AdaKeywords[keyword])));
}
return status;
} /* static boolean adaKeywordCmp(adaKeyword keyword) */
static void skipUntilWhiteSpace(void)
{
/* first check for a comment line, because this would cause the isspace
* check to be true immediately */
while(exception != EXCEPTION_EOF && isAdaComment(line, pos, lineLen))
{
readNewLine();
}
while(exception != EXCEPTION_EOF && !isspace(line[pos]))
{
/* don't use movePos() because if we read in a new line with this function
* we need to stop */
pos++;
/* the newline counts as whitespace so read in the newline and return
* immediately */
if(pos >= lineLen)
{
line = (const char *) fileReadLine();
pos = 0;
if(line == NULL)
{
lineLen = 0;
exception = EXCEPTION_EOF;
return;
}
lineLen = strlen(line);
return;
} /* if(pos >= lineLen) */
/* now check for comments here */
while(exception != EXCEPTION_EOF && isAdaComment(line, pos, lineLen))
{
readNewLine();
}
} /* while(!isspace(line[pos])) */
} /* static void skipUntilWhiteSpace(void) */
static void skipWhiteSpace(void)
{
/* first check for a comment line, because this would cause the isspace
* check to fail immediately */
while(exception != EXCEPTION_EOF && isAdaComment(line, pos, lineLen))
{
readNewLine();
}
while(exception != EXCEPTION_EOF && isspace(line[pos]))
{
movePos(1);
/* now check for comments here */
while(exception != EXCEPTION_EOF && isAdaComment(line, pos, lineLen))
{
readNewLine();
}
} /* while(isspace(line[pos])) */
} /* static void skipWhiteSpace(void) */
static void skipPast(const char *past)
{
/* first check for a comment line, because this would cause the isspace
* check to fail immediately */
while(exception != EXCEPTION_EOF && isAdaComment(line, pos, lineLen))
{
readNewLine();
}
/* now look for the keyword */
while(exception != EXCEPTION_EOF && !adaCmp(past))
{
movePos(1);
/* now check for comments here */
while(exception != EXCEPTION_EOF && isAdaComment(line, pos, lineLen))
{
readNewLine();
}
}
} /* static void skipPast(char *past) */
static void skipPastKeyword(adaKeyword keyword)
{
/* first check for a comment line, because this would cause the isspace
* check to fail immediately */
while(exception != EXCEPTION_EOF && isAdaComment(line, pos, lineLen))
{
readNewLine();
}
/* now look for the keyword */
while(exception != EXCEPTION_EOF && !adaKeywordCmp(keyword))
{
movePos(1);
/* now check for comments here */
while(exception != EXCEPTION_EOF && isAdaComment(line, pos, lineLen))
{
readNewLine();
}
}
} /* static void skipPastKeyword(adaKeyword keyword) */
static void skipPastWord(void)
{
/* first check for a comment line, because this would cause the isspace
* check to fail immediately */
while(exception != EXCEPTION_EOF && isAdaComment(line, pos, lineLen))
{
readNewLine();
}
/* now increment until we hit a non-word character... Specifically,
* whitespace, '(', ')', ':', and ';' */
while(exception != EXCEPTION_EOF && !isspace(line[pos]) &&
line[pos] != '(' && line[pos] != ')' && line[pos] != ':' &&
line[pos] != ';')
{
/* don't use movePos() because if we read in a new line with this function
* we need to stop */
pos++;
/* the newline counts as whitespace so read in the newline and return
* immediately */
if(pos >= lineLen)
{
line = (const char *) fileReadLine();
pos = 0;
if(line == NULL)
{
lineLen = 0;
exception = EXCEPTION_EOF;
return;
}
lineLen = strlen(line);
return;
} /* if(pos >= lineLen) */
/* now check for comments here */
while(exception != EXCEPTION_EOF && isAdaComment(line, pos, lineLen))
{
readNewLine();
}
} /* while(!isspace(line[pos])) */
} /* static void skipPastWord(void) */
static adaTokenInfo *adaParseBlock(adaTokenInfo *parent, adaKind kind)
{
int i;
adaTokenInfo *token;
boolean isSpec = TRUE;
skipWhiteSpace();
/* if the next word is body, this is not a package spec */
if(adaKeywordCmp(ADA_KEYWORD_BODY))
{
isSpec = FALSE;
}
/* if the next word is "type" then this has to be a task or protected spec */
else if(adaKeywordCmp(ADA_KEYWORD_TYPE) &&
(kind != ADA_KIND_PROTECTED && kind != ADA_KIND_TASK))
{
/* if this failed to validate then we should just fail */
return NULL;
}
skipWhiteSpace();
/* we are at the start of what should be the tag now... But we have to get
* it's length. So loop until we hit whitespace, init the counter to 1
* since we know that the current position is not whitespace */
for(i = 1; (pos + i) < lineLen && !isspace(line[pos + i]) &&
line[pos + i] != '(' && line[pos + i] != ';'; i++);
/* we have reached the tag of the package, so create the tag */
token = newAdaToken(&line[pos], i, kind, isSpec, parent);
movePos(i);
skipWhiteSpace();
/* task and protected types are allowed to have discriminants */
if(exception != EXCEPTION_EOF && line[pos] == '(')
{
while(exception != EXCEPTION_EOF && line[pos] != ')')
{
movePos(1);
adaParseVariables(token, ADA_KIND_AUTOMATIC_VARIABLE);
}
movePos(1);
}
/* we must parse until we hit the "is" string to reach the end of
* this package declaration, or a "reanames" keyword */
while(token != NULL)
{
skipWhiteSpace();
if(adaKeywordCmp(ADA_KEYWORD_IS))
{
skipWhiteSpace();
if(adaKeywordCmp(ADA_KEYWORD_SEPARATE))
{
/* if the next word is the keyword "separate", don't create the tag
* since it will be defined elsewhere */
freeAdaToken(&parent->children, token);
token = NULL;
/* move past the ";" ending this declaration */
skipPast(";");
}
else if(adaKeywordCmp(ADA_KEYWORD_NEW))
{
/* if this is a "new" something then no need to parse */
skipPast(";");
}
else
{
adaParse(ADA_DECLARATIONS, token);
}
break;
} /* if(adaKeywordCmp(ADA_KEYWORD_IS)) */
else if(adaKeywordCmp(ADA_KEYWORD_RENAMES))
{
skipPast(";");
break;
}
else if(adaCmp(";"))
{
token->isSpec = TRUE;
break;
}
else
{
/* nothing found, move to the next word */
skipUntilWhiteSpace();
}
if (exception == EXCEPTION_EOF)
{
freeAdaToken(&parent->children, token);
token = NULL;
}
} /* while(token != NULL) - while the end of spec, or beginning of body not found */
return token;
} /* static adaTokenInfo *adaParseBlock(adaTokenInfo *parent, adaKind kind) */
static adaTokenInfo *adaParseSubprogram(adaTokenInfo *parent, adaKind kind)
{
int i;
adaTokenInfo *token;
adaTokenInfo *tmpToken = NULL;
skipWhiteSpace();
/* we are at the start of what should be the tag now... But we have to get
* it's length. So loop until we hit whitespace or the beginning of the
* parameter list. Init the counter to 1 * since we know that the current
* position is not whitespace */
for(i = 1; (pos + i) < lineLen && !isspace(line[pos + i]) &&
line[pos + i] != '(' && line[pos + i] != ';'; i++);
/* we have reached the tag of the subprogram, so create the tag... Init the
* isSpec flag to false and we will adjust it when we see if there is an
* "is", "do" or a ";" following the tag */
token = newAdaToken(&line[pos], i, kind, FALSE, parent);
/* move the line position */
movePos(i);
skipWhiteSpace();
/* if we find a '(' grab any parameters */
if(exception != EXCEPTION_EOF && line[pos] == '(' && token != NULL)
{
while(exception != EXCEPTION_EOF && line[pos] != ')')
{
movePos(1);
tmpToken = adaParseVariables(token, ADA_KIND_AUTOMATIC_VARIABLE);
}
movePos(1);
/* check to see if anything was received... If this is an entry this may
* have a 'discriminant' and not have any parameters in the first
* parenthesis pair, so check again if this was the case*/
if(kind == ADA_KIND_ENTRY && tmpToken == NULL)