4 * Copyright (c) 1998-2003, Darren Hiebert
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
9 * This module contains functions for generating tags for Fortran language
16 #include "general.h" /* must always come first */
20 #include <ctype.h> /* to define tolower () */
35 #define isident(c) (isalnum(c) || (c) == '_')
36 #define isBlank(c) (boolean) (c == ' ' || c == '\t')
37 #define isType(token,t) (boolean) ((token)->type == (t))
38 #define isKeyword(token,k) (boolean) ((token)->keyword == (k))
39 #define isSecondaryKeyword(token,k) (boolean) ((token)->secondary == NULL ? \
40 FALSE : (token)->secondary->keyword == (k))
46 typedef enum eException
{
47 ExceptionNone
, ExceptionEOF
, ExceptionFixedFormat
, ExceptionLoop
50 /* Used to designate type of line read in fixed source form.
52 typedef enum eFortranLineType
{
62 /* Used to specify type of keyword.
64 typedef enum eKeywordId
{
136 /* Used to determine whether keyword is valid for the token language and
139 typedef struct sKeywordDesc
{
144 typedef enum eTokenType
{
160 typedef enum eTagType
{
176 TAG_COUNT
/* must be last */
179 typedef struct sTokenInfo
{
184 struct sTokenInfo
*secondary
;
185 unsigned long lineNumber
;
193 static langType Lang_fortran
;
194 static jmp_buf Exception
;
196 static unsigned int Column
;
197 static boolean FreeSourceForm
;
198 static boolean ParsingString
;
199 static tokenInfo
*Parent
;
201 /* indexed by tagType */
202 static kindOption FortranKinds
[] = {
203 { TRUE
, 'b', "block data", "block data"},
204 { TRUE
, 'c', "common", "common blocks"},
205 { TRUE
, 'e', "entry", "entry points"},
206 { TRUE
, 'f', "function", "functions"},
207 { FALSE
, 'i', "interface", "interface contents, generic names, and operators"},
208 { TRUE
, 'k', "component", "type and structure components"},
209 { TRUE
, 'l', "label", "labels"},
210 { FALSE
, 'L', "local", "local, common block, and namelist variables"},
211 { TRUE
, 'm', "module", "modules"},
212 { TRUE
, 'n', "namelist", "namelists"},
213 { TRUE
, 'p', "program", "programs"},
214 { TRUE
, 's', "subroutine", "subroutines"},
215 { TRUE
, 't', "type", "derived types and structures"},
216 { TRUE
, 'v', "variable", "program (global) and module variables"}
219 /* For a definition of Fortran 77 with extensions:
220 * http://scienide.uwaterloo.ca/MIPSpro7/007-2362-004/sgi_html/index.html
222 * For the Compaq Fortran Reference Manual:
223 * http://h18009.www1.hp.com/fortran/docs/lrm/dflrm.htm
226 static const keywordDesc FortranKeywordTable
[] = {
227 /* keyword keyword ID */
228 { "allocatable", KEYWORD_allocatable
},
229 { "assignment", KEYWORD_assignment
},
230 { "automatic", KEYWORD_automatic
},
231 { "block", KEYWORD_block
},
232 { "byte", KEYWORD_byte
},
233 { "cexternal", KEYWORD_cexternal
},
234 { "cglobal", KEYWORD_cglobal
},
235 { "character", KEYWORD_character
},
236 { "common", KEYWORD_common
},
237 { "complex", KEYWORD_complex
},
238 { "contains", KEYWORD_contains
},
239 { "data", KEYWORD_data
},
240 { "dimension", KEYWORD_dimension
},
241 { "dll_export", KEYWORD_dllexport
},
242 { "dll_import", KEYWORD_dllimport
},
243 { "do", KEYWORD_do
},
244 { "double", KEYWORD_double
},
245 { "elemental", KEYWORD_elemental
},
246 { "end", KEYWORD_end
},
247 { "entry", KEYWORD_entry
},
248 { "equivalence", KEYWORD_equivalence
},
249 { "external", KEYWORD_external
},
250 { "format", KEYWORD_format
},
251 { "function", KEYWORD_function
},
252 { "if", KEYWORD_if
},
253 { "implicit", KEYWORD_implicit
},
254 { "include", KEYWORD_include
},
255 { "inline", KEYWORD_inline
},
256 { "integer", KEYWORD_integer
},
257 { "intent", KEYWORD_intent
},
258 { "interface", KEYWORD_interface
},
259 { "intrinsic", KEYWORD_intrinsic
},
260 { "logical", KEYWORD_logical
},
261 { "map", KEYWORD_map
},
262 { "module", KEYWORD_module
},
263 { "namelist", KEYWORD_namelist
},
264 { "operator", KEYWORD_operator
},
265 { "optional", KEYWORD_optional
},
266 { "parameter", KEYWORD_parameter
},
267 { "pascal", KEYWORD_pascal
},
268 { "pexternal", KEYWORD_pexternal
},
269 { "pglobal", KEYWORD_pglobal
},
270 { "pointer", KEYWORD_pointer
},
271 { "precision", KEYWORD_precision
},
272 { "private", KEYWORD_private
},
273 { "program", KEYWORD_program
},
274 { "public", KEYWORD_public
},
275 { "pure", KEYWORD_pure
},
276 { "real", KEYWORD_real
},
277 { "record", KEYWORD_record
},
278 { "recursive", KEYWORD_recursive
},
279 { "save", KEYWORD_save
},
280 { "select", KEYWORD_select
},
281 { "sequence", KEYWORD_sequence
},
282 { "static", KEYWORD_static
},
283 { "stdcall", KEYWORD_stdcall
},
284 { "structure", KEYWORD_structure
},
285 { "subroutine", KEYWORD_subroutine
},
286 { "target", KEYWORD_target
},
287 { "then", KEYWORD_then
},
288 { "type", KEYWORD_type
},
289 { "union", KEYWORD_union
},
290 { "use", KEYWORD_use
},
291 { "value", KEYWORD_value
},
292 { "virtual", KEYWORD_virtual
},
293 { "volatile", KEYWORD_volatile
},
294 { "where", KEYWORD_where
},
295 { "while", KEYWORD_while
}
302 } Ancestors
= { 0, 0, NULL
};
305 * FUNCTION PROTOTYPES
307 static void parseStructureStmt (tokenInfo
*const token
);
308 static void parseUnionStmt (tokenInfo
*const token
);
309 static void parseDerivedTypeDef (tokenInfo
*const token
);
310 static void parseFunctionSubprogram (tokenInfo
*const token
);
311 static void parseSubroutineSubprogram (tokenInfo
*const token
);
314 * FUNCTION DEFINITIONS
317 static void ancestorPush (tokenInfo
*const token
)
319 enum { incrementalIncrease
= 10 };
320 if (Ancestors
.list
== NULL
)
322 Assert (Ancestors
.max
== 0);
324 Ancestors
.max
= incrementalIncrease
;
325 Ancestors
.list
= xMalloc (Ancestors
.max
, tokenInfo
);
327 else if (Ancestors
.count
== Ancestors
.max
)
329 Ancestors
.max
+= incrementalIncrease
;
330 Ancestors
.list
= xRealloc (Ancestors
.list
, Ancestors
.max
, tokenInfo
);
332 Ancestors
.list
[Ancestors
.count
] = *token
;
333 Ancestors
.list
[Ancestors
.count
].string
= vStringNewCopy (token
->string
);
337 static void ancestorPop (void)
339 Assert (Ancestors
.count
> 0);
341 vStringDelete (Ancestors
.list
[Ancestors
.count
].string
);
343 Ancestors
.list
[Ancestors
.count
].type
= TOKEN_UNDEFINED
;
344 Ancestors
.list
[Ancestors
.count
].keyword
= KEYWORD_NONE
;
345 Ancestors
.list
[Ancestors
.count
].secondary
= NULL
;
346 Ancestors
.list
[Ancestors
.count
].tag
= TAG_UNDEFINED
;
347 Ancestors
.list
[Ancestors
.count
].string
= NULL
;
348 Ancestors
.list
[Ancestors
.count
].lineNumber
= 0L;
351 static const tokenInfo
* ancestorScope (void)
353 tokenInfo
*result
= NULL
;
355 for (i
= Ancestors
.count
; i
> 0 && result
== NULL
; --i
)
357 tokenInfo
*const token
= Ancestors
.list
+ i
- 1;
358 if (token
->type
== TOKEN_IDENTIFIER
&&
359 token
->tag
!= TAG_UNDEFINED
&& token
->tag
!= TAG_INTERFACE
)
365 static const tokenInfo
* ancestorTop (void)
367 Assert (Ancestors
.count
> 0);
368 return &Ancestors
.list
[Ancestors
.count
- 1];
371 #define ancestorCount() (Ancestors.count)
373 static void ancestorClear (void)
375 while (Ancestors
.count
> 0)
377 if (Ancestors
.list
!= NULL
)
378 eFree (Ancestors
.list
);
379 Ancestors
.list
= NULL
;
384 static boolean
insideInterface (void)
386 boolean result
= FALSE
;
388 for (i
= 0 ; i
< Ancestors
.count
&& !result
; ++i
)
390 if (Ancestors
.list
[i
].tag
== TAG_INTERFACE
)
396 static void buildFortranKeywordHash (void)
399 sizeof (FortranKeywordTable
) / sizeof (FortranKeywordTable
[0]);
401 for (i
= 0 ; i
< count
; ++i
)
403 const keywordDesc
* const p
= &FortranKeywordTable
[i
];
404 addKeyword (p
->name
, Lang_fortran
, (int) p
->id
);
409 * Tag generation functions
412 static tokenInfo
*newToken (void)
414 tokenInfo
*const token
= xMalloc (1, tokenInfo
);
416 token
->type
= TOKEN_UNDEFINED
;
417 token
->keyword
= KEYWORD_NONE
;
418 token
->tag
= TAG_UNDEFINED
;
419 token
->string
= vStringNew ();
420 token
->secondary
= NULL
;
421 token
->lineNumber
= getSourceLineNumber ();
422 token
->filePosition
= getInputFilePosition ();
427 static tokenInfo
*newTokenFrom (tokenInfo
*const token
)
429 tokenInfo
*result
= newToken ();
431 result
->string
= vStringNewCopy (token
->string
);
432 token
->secondary
= NULL
;
436 static void deleteToken (tokenInfo
*const token
)
440 vStringDelete (token
->string
);
441 deleteToken (token
->secondary
);
442 token
->secondary
= NULL
;
447 static boolean
isFileScope (const tagType type
)
449 return (boolean
) (type
== TAG_LABEL
|| type
== TAG_LOCAL
);
452 static boolean
includeTag (const tagType type
)
455 Assert (type
!= TAG_UNDEFINED
);
456 include
= FortranKinds
[(int) type
].enabled
;
457 if (include
&& isFileScope (type
))
458 include
= Option
.include
.fileScope
;
462 static void makeFortranTag (tokenInfo
*const token
, tagType tag
)
465 if (includeTag (token
->tag
))
467 const char *const name
= vStringValue (token
->string
);
470 initTagEntry (&e
, name
);
472 if (token
->tag
== TAG_COMMON_BLOCK
)
473 e
.lineNumberEntry
= (boolean
) (Option
.locate
!= EX_PATTERN
);
475 e
.lineNumber
= token
->lineNumber
;
476 e
.filePosition
= token
->filePosition
;
477 e
.isFileScope
= isFileScope (token
->tag
);
478 e
.kindName
= FortranKinds
[token
->tag
].name
;
479 e
.kind
= FortranKinds
[token
->tag
].letter
;
480 e
.truncateLine
= (boolean
) (token
->tag
!= TAG_LABEL
);
482 if (ancestorCount () > 0)
484 const tokenInfo
* const scope
= ancestorScope ();
487 e
.extensionFields
.scope
[0] = FortranKinds
[scope
->tag
].name
;
488 e
.extensionFields
.scope
[1] = vStringValue (scope
->string
);
491 if (! insideInterface () || includeTag (TAG_INTERFACE
))
500 static int skipLine (void)
506 while (c
!= EOF
&& c
!= '\n');
511 static void makeLabelTag (vString
*const label
)
513 tokenInfo
*token
= newToken ();
514 token
->type
= TOKEN_LABEL
;
515 vStringCopy (token
->string
, label
);
516 makeFortranTag (token
, TAG_LABEL
);
520 static lineType
getLineType (void)
522 static vString
*label
= NULL
;
524 lineType type
= LTYPE_UNDETERMINED
;
527 label
= vStringNew ();
529 do /* read in first 6 "margin" characters */
533 /* 3.2.1 Comment_Line. A comment line is any line that contains
534 * a C or an asterisk in column 1, or contains only blank characters
535 * in columns 1 through 72. A comment line that contains a C or
536 * an asterisk in column 1 may contain any character capable of
537 * representation in the processor in columns 2 through 72.
539 /* EXCEPTION! Some compilers permit '!' as a commment character here.
541 * Treat # and $ in column 1 as comment to permit preprocessor directives.
542 * Treat D and d in column 1 as comment for HP debug statements.
544 if (column
== 0 && strchr ("*Cc!#$Dd", c
) != NULL
)
545 type
= LTYPE_COMMENT
;
546 else if (c
== '\t') /* EXCEPTION! Some compilers permit a tab here */
549 type
= LTYPE_INITIAL
;
551 else if (column
== 5)
553 /* 3.2.2 Initial_Line. An initial line is any line that is not
554 * a comment line and contains the character blank or the digit 0
555 * in column 6. Columns 1 through 5 may contain a statement label
556 * (3.4), or each of the columns 1 through 5 must contain the
559 if (c
== ' ' || c
== '0')
560 type
= LTYPE_INITIAL
;
562 /* 3.2.3 Continuation_Line. A continuation line is any line that
563 * contains any character of the FORTRAN character set other than
564 * the character blank or the digit 0 in column 6 and contains
565 * only blank characters in columns 1 through 5.
567 else if (vStringLength (label
) == 0)
568 type
= LTYPE_CONTINUATION
;
570 type
= LTYPE_INVALID
;
578 else if (isdigit (c
))
579 vStringPut (label
, c
);
581 type
= LTYPE_INVALID
;
584 } while (column
< 6 && type
== LTYPE_UNDETERMINED
);
586 Assert (type
!= LTYPE_UNDETERMINED
);
588 if (vStringLength (label
) > 0)
590 vStringTerminate (label
);
591 makeLabelTag (label
);
592 vStringClear (label
);
597 static int getFixedFormChar (void)
599 boolean newline
= FALSE
;
605 #ifdef STRICT_FIXED_FORM
606 /* EXCEPTION! Some compilers permit more than 72 characters per line.
618 newline
= TRUE
; /* need to check for continuation line */
621 else if (c
== '!' && ! ParsingString
)
624 newline
= TRUE
; /* need to check for continuation line */
627 else if (c
== '&') /* check for free source form */
629 const int c2
= fileGetc ();
631 longjmp (Exception
, (int) ExceptionFixedFormat
);
638 type
= getLineType ();
641 case LTYPE_UNDETERMINED
:
643 longjmp (Exception
, (int) ExceptionFixedFormat
);
646 case LTYPE_SHORT
: break;
647 case LTYPE_COMMENT
: skipLine (); break;
664 /* fall through to next case */
665 case LTYPE_CONTINUATION
:
671 } while (isBlank (c
));
682 Assert ("Unexpected line type" == NULL
);
688 static int skipToNextLine (void)
696 static int getFreeFormChar (void)
698 static boolean newline
= TRUE
;
699 boolean advanceLine
= FALSE
;
702 /* If the last nonblank, non-comment character of a FORTRAN 90
703 * free-format text line is an ampersand then the next non-comment
704 * line is a continuation line.
710 while (isspace (c
) && c
!= '\n');
724 else if (newline
&& (c
== '!' || c
== '#'))
730 if (c
== '!' || (newline
&& c
== '#'))
732 c
= skipToNextLine ();
741 newline
= (boolean
) (c
== '\n');
745 static int getChar (void)
754 else if (FreeSourceForm
)
755 c
= getFreeFormChar ();
757 c
= getFixedFormChar ();
761 static void ungetChar (const int c
)
766 /* If a numeric is passed in 'c', this is used as the first digit of the
767 * numeric being parsed.
769 static vString
*parseInteger (int c
)
771 static vString
*string
= NULL
;
774 string
= vStringNew ();
775 vStringClear (string
);
779 vStringPut (string
, c
);
782 else if (! isdigit (c
))
784 while (c
!= EOF
&& isdigit (c
))
786 vStringPut (string
, c
);
789 vStringTerminate (string
);
795 while (c
!= EOF
&& isalpha (c
));
802 static vString
*parseNumeric (int c
)
804 static vString
*string
= NULL
;
807 string
= vStringNew ();
808 vStringCopy (string
, parseInteger (c
));
813 vStringPut (string
, c
);
814 vStringCat (string
, parseInteger ('\0'));
817 if (tolower (c
) == 'e')
819 vStringPut (string
, c
);
820 vStringCat (string
, parseInteger ('\0'));
825 vStringTerminate (string
);
830 static void parseString (vString
*const string
, const int delimeter
)
832 const unsigned long inputLineNumber
= getInputLineNumber ();
834 ParsingString
= TRUE
;
836 while (c
!= delimeter
&& c
!= '\n' && c
!= EOF
)
838 vStringPut (string
, c
);
841 if (c
== '\n' || c
== EOF
)
843 verbose ("%s: unterminated character string at line %lu\n",
844 getInputFileName (), inputLineNumber
);
846 longjmp (Exception
, (int) ExceptionEOF
);
847 else if (! FreeSourceForm
)
848 longjmp (Exception
, (int) ExceptionFixedFormat
);
850 vStringTerminate (string
);
851 ParsingString
= FALSE
;
854 /* Read a C identifier beginning with "firstChar" and places it into "name".
856 static void parseIdentifier (vString
*const string
, const int firstChar
)
862 vStringPut (string
, c
);
864 } while (isident (c
));
866 vStringTerminate (string
);
867 ungetChar (c
); /* unget non-identifier character */
870 /* Analyzes the identifier contained in a statement described by the
871 * statement structure and adjusts the structure according the significance
874 static keywordId
analyzeToken (vString
*const name
)
876 static vString
*keyword
= NULL
;
880 keyword
= vStringNew ();
881 vStringCopyToLower (keyword
, name
);
882 id
= (keywordId
) lookupKeyword (vStringValue (keyword
), Lang_fortran
);
887 static void checkForLabel (void)
889 tokenInfo
* token
= NULL
;
897 for (length
= 0 ; isdigit (c
) && length
< 5 ; ++length
)
902 token
->type
= TOKEN_LABEL
;
904 vStringPut (token
->string
, c
);
909 Assert (token
!= NULL
);
910 vStringTerminate (token
->string
);
911 makeFortranTag (token
, TAG_LABEL
);
917 static void readIdentifier (tokenInfo
*const token
, const int c
)
919 parseIdentifier (token
->string
, c
);
920 token
->keyword
= analyzeToken (token
->string
);
921 if (! isKeyword (token
, KEYWORD_NONE
))
922 token
->type
= TOKEN_KEYWORD
;
925 token
->type
= TOKEN_IDENTIFIER
;
926 if (strncmp (vStringValue (token
->string
), "end", 3) == 0)
928 vString
*const sub
= vStringNewInit (vStringValue (token
->string
) + 3);
929 const keywordId kw
= analyzeToken (sub
);
931 if (kw
!= KEYWORD_NONE
)
933 token
->secondary
= newToken ();
934 token
->secondary
->type
= TOKEN_KEYWORD
;
935 token
->secondary
->keyword
= kw
;
936 token
->keyword
= KEYWORD_end
;
942 static void readToken (tokenInfo
*const token
)
946 deleteToken (token
->secondary
);
947 token
->type
= TOKEN_UNDEFINED
;
948 token
->tag
= TAG_UNDEFINED
;
949 token
->keyword
= KEYWORD_NONE
;
950 token
->secondary
= NULL
;
951 vStringClear (token
->string
);
956 token
->lineNumber
= getSourceLineNumber ();
957 token
->filePosition
= getInputFilePosition ();
961 case EOF
: longjmp (Exception
, (int) ExceptionEOF
); break;
962 case ' ': goto getNextChar
;
963 case '\t': goto getNextChar
;
964 case ',': token
->type
= TOKEN_COMMA
; break;
965 case '(': token
->type
= TOKEN_PAREN_OPEN
; break;
966 case ')': token
->type
= TOKEN_PAREN_CLOSE
; break;
967 case '%': token
->type
= TOKEN_PERCENT
; break;
977 const char *const operatorChars
= "*/+=<>";
979 vStringPut (token
->string
, c
);
981 } while (strchr (operatorChars
, c
) != NULL
);
983 vStringTerminate (token
->string
);
984 token
->type
= TOKEN_OPERATOR
;
1000 /* fall through to newline case */
1002 token
->type
= TOKEN_STATEMENT_END
;
1008 parseIdentifier (token
->string
, c
);
1012 vStringPut (token
->string
, c
);
1013 vStringTerminate (token
->string
);
1014 token
->type
= TOKEN_OPERATOR
;
1019 token
->type
= TOKEN_UNDEFINED
;
1025 parseString (token
->string
, c
);
1026 token
->type
= TOKEN_STRING
;
1030 token
->type
= TOKEN_STATEMENT_END
;
1036 token
->type
= TOKEN_DOUBLE_COLON
;
1040 token
->type
= TOKEN_UNDEFINED
;
1046 readIdentifier (token
, c
);
1047 else if (isdigit (c
))
1049 vStringCat (token
->string
, parseNumeric (c
));
1050 token
->type
= TOKEN_NUMERIC
;
1053 token
->type
= TOKEN_UNDEFINED
;
1058 static void readSubToken (tokenInfo
*const token
)
1060 if (token
->secondary
== NULL
)
1062 token
->secondary
= newToken ();
1063 readToken (token
->secondary
);
1065 Assert (token
->secondary
!= NULL
);
1069 * Scanning functions
1072 static void skipToToken (tokenInfo
*const token
, tokenType type
)
1074 while (! isType (token
, type
) && ! isType (token
, TOKEN_STATEMENT_END
) &&
1075 !(token
->secondary
!= NULL
&& isType (token
->secondary
, TOKEN_STATEMENT_END
)))
1079 static void skipPast (tokenInfo
*const token
, tokenType type
)
1081 skipToToken (token
, type
);
1082 if (! isType (token
, TOKEN_STATEMENT_END
))
1086 static void skipToNextStatement (tokenInfo
*const token
)
1090 skipToToken (token
, TOKEN_STATEMENT_END
);
1092 } while (isType (token
, TOKEN_STATEMENT_END
));
1095 /* skip over parenthesis enclosed contents starting at next token.
1096 * Token is left at the first token following closing parenthesis. If an
1097 * opening parenthesis is not found, `token' is moved to the end of the
1100 static void skipOverParens (tokenInfo
*const token
)
1104 if (isType (token
, TOKEN_STATEMENT_END
))
1106 else if (isType (token
, TOKEN_PAREN_OPEN
))
1108 else if (isType (token
, TOKEN_PAREN_CLOSE
))
1111 } while (level
> 0);
1114 static boolean
isTypeSpec (tokenInfo
*const token
)
1117 switch (token
->keyword
)
1120 case KEYWORD_integer
:
1122 case KEYWORD_double
:
1123 case KEYWORD_complex
:
1124 case KEYWORD_character
:
1125 case KEYWORD_logical
:
1126 case KEYWORD_record
:
1137 static boolean
isSubprogramPrefix (tokenInfo
*const token
)
1140 switch (token
->keyword
)
1142 case KEYWORD_elemental
:
1144 case KEYWORD_recursive
:
1145 case KEYWORD_stdcall
:
1156 * is INTEGER [kind-selector]
1157 * or REAL [kind-selector] is ( etc. )
1158 * or DOUBLE PRECISION
1159 * or COMPLEX [kind-selector]
1160 * or CHARACTER [kind-selector]
1161 * or LOGICAL [kind-selector]
1162 * or TYPE ( type-name )
1164 * Note that INTEGER and REAL may be followed by "*N" where "N" is an integer
1166 static void parseTypeSpec (tokenInfo
*const token
)
1168 /* parse type-spec, leaving `token' at first token following type-spec */
1169 Assert (isTypeSpec (token
));
1170 switch (token
->keyword
)
1172 case KEYWORD_character
:
1173 /* skip char-selector */
1175 if (isType (token
, TOKEN_OPERATOR
) &&
1176 strcmp (vStringValue (token
->string
), "*") == 0)
1178 if (isType (token
, TOKEN_PAREN_OPEN
))
1179 skipOverParens (token
);
1180 else if (isType (token
, TOKEN_NUMERIC
))
1186 case KEYWORD_complex
:
1187 case KEYWORD_integer
:
1188 case KEYWORD_logical
:
1191 if (isType (token
, TOKEN_PAREN_OPEN
))
1192 skipOverParens (token
); /* skip kind-selector */
1193 if (isType (token
, TOKEN_OPERATOR
) &&
1194 strcmp (vStringValue (token
->string
), "*") == 0)
1201 case KEYWORD_double
:
1203 if (isKeyword (token
, KEYWORD_complex
) ||
1204 isKeyword (token
, KEYWORD_precision
))
1207 skipToToken (token
, TOKEN_STATEMENT_END
);
1210 case KEYWORD_record
:
1212 if (isType (token
, TOKEN_OPERATOR
) &&
1213 strcmp (vStringValue (token
->string
), "/") == 0)
1215 readToken (token
); /* skip to structure name */
1216 readToken (token
); /* skip to '/' */
1217 readToken (token
); /* skip to variable name */
1223 if (isType (token
, TOKEN_PAREN_OPEN
))
1224 skipOverParens (token
); /* skip type-name */
1226 parseDerivedTypeDef (token
);
1230 skipToToken (token
, TOKEN_STATEMENT_END
);
1235 static boolean
skipStatementIfKeyword (tokenInfo
*const token
, keywordId keyword
)
1237 boolean result
= FALSE
;
1238 if (isKeyword (token
, keyword
))
1241 skipToNextStatement (token
);
1246 /* parse a list of qualifying specifiers, leaving `token' at first token
1247 * following list. Examples of such specifiers are:
1248 * [[, attr-spec] ::]
1249 * [[, component-attr-spec-list] ::]
1253 * or access-spec (is PUBLIC or PRIVATE)
1255 * or DIMENSION ( array-spec )
1257 * or INTENT ( intent-spec )
1264 * component-attr-spec
1266 * or DIMENSION ( component-array-spec )
1268 static void parseQualifierSpecList (tokenInfo
*const token
)
1272 readToken (token
); /* should be an attr-spec */
1273 switch (token
->keyword
)
1275 case KEYWORD_parameter
:
1276 case KEYWORD_allocatable
:
1277 case KEYWORD_external
:
1278 case KEYWORD_intrinsic
:
1279 case KEYWORD_optional
:
1280 case KEYWORD_private
:
1281 case KEYWORD_pointer
:
1282 case KEYWORD_public
:
1284 case KEYWORD_target
:
1288 case KEYWORD_dimension
:
1289 case KEYWORD_intent
:
1291 skipOverParens (token
);
1294 default: skipToToken (token
, TOKEN_STATEMENT_END
); break;
1296 } while (isType (token
, TOKEN_COMMA
));
1297 if (! isType (token
, TOKEN_DOUBLE_COLON
))
1298 skipToToken (token
, TOKEN_STATEMENT_END
);
1301 static tagType
variableTagType (void)
1303 tagType result
= TAG_VARIABLE
;
1304 if (ancestorCount () > 0)
1306 const tokenInfo
* const parent
= ancestorTop ();
1307 switch (parent
->tag
)
1309 case TAG_MODULE
: result
= TAG_VARIABLE
; break;
1310 case TAG_DERIVED_TYPE
: result
= TAG_COMPONENT
; break;
1311 case TAG_FUNCTION
: result
= TAG_LOCAL
; break;
1312 case TAG_SUBROUTINE
: result
= TAG_LOCAL
; break;
1313 default: result
= TAG_VARIABLE
; break;
1319 static void parseEntityDecl (tokenInfo
*const token
)
1321 Assert (isType (token
, TOKEN_IDENTIFIER
));
1322 makeFortranTag (token
, variableTagType ());
1324 if (isType (token
, TOKEN_PAREN_OPEN
))
1325 skipOverParens (token
);
1326 if (isType (token
, TOKEN_OPERATOR
) &&
1327 strcmp (vStringValue (token
->string
), "*") == 0)
1329 readToken (token
); /* read char-length */
1330 if (isType (token
, TOKEN_PAREN_OPEN
))
1331 skipOverParens (token
);
1335 if (isType (token
, TOKEN_OPERATOR
))
1337 if (strcmp (vStringValue (token
->string
), "/") == 0)
1338 { /* skip over initializations of structure field */
1340 skipPast (token
, TOKEN_OPERATOR
);
1342 else if (strcmp (vStringValue (token
->string
), "=") == 0)
1344 while (! isType (token
, TOKEN_COMMA
) &&
1345 ! isType (token
, TOKEN_STATEMENT_END
))
1348 if (isType (token
, TOKEN_PAREN_OPEN
))
1349 skipOverParens (token
);
1353 /* token left at either comma or statement end */
1356 static void parseEntityDeclList (tokenInfo
*const token
)
1358 if (isType (token
, TOKEN_PERCENT
))
1359 skipToNextStatement (token
);
1360 else while (isType (token
, TOKEN_IDENTIFIER
) ||
1361 (isType (token
, TOKEN_KEYWORD
) &&
1362 !isKeyword (token
, KEYWORD_function
) &&
1363 !isKeyword (token
, KEYWORD_subroutine
)))
1365 /* compilers accept keywoeds as identifiers */
1366 if (isType (token
, TOKEN_KEYWORD
))
1367 token
->type
= TOKEN_IDENTIFIER
;
1368 parseEntityDecl (token
);
1369 if (isType (token
, TOKEN_COMMA
))
1371 else if (isType (token
, TOKEN_STATEMENT_END
))
1373 skipToNextStatement (token
);
1379 /* type-declaration-stmt is
1380 * type-spec [[, attr-spec] ... ::] entity-decl-list
1382 static void parseTypeDeclarationStmt (tokenInfo
*const token
)
1384 Assert (isTypeSpec (token
));
1385 parseTypeSpec (token
);
1386 if (!isType (token
, TOKEN_STATEMENT_END
)) /* if not end of derived type... */
1388 if (isType (token
, TOKEN_COMMA
))
1389 parseQualifierSpecList (token
);
1390 if (isType (token
, TOKEN_DOUBLE_COLON
))
1392 parseEntityDeclList (token
);
1394 if (isType (token
, TOKEN_STATEMENT_END
))
1395 skipToNextStatement (token
);
1399 * NAMELIST /namelist-group-name/ namelist-group-object-list
1400 * [[,]/[namelist-group-name]/ namelist-block-object-list] ...
1402 * namelist-group-object is
1406 * COMMON [/[common-block-name]/] common-block-object-list
1407 * [[,]/[common-block-name]/ common-block-object-list] ...
1409 * common-block-object is
1410 * variable-name [ ( explicit-shape-spec-list ) ]
1412 static void parseCommonNamelistStmt (tokenInfo
*const token
, tagType type
)
1414 Assert (isKeyword (token
, KEYWORD_common
) ||
1415 isKeyword (token
, KEYWORD_namelist
));
1419 if (isType (token
, TOKEN_OPERATOR
) &&
1420 strcmp (vStringValue (token
->string
), "/") == 0)
1423 if (isType (token
, TOKEN_IDENTIFIER
))
1425 makeFortranTag (token
, type
);
1428 skipPast (token
, TOKEN_OPERATOR
);
1430 if (isType (token
, TOKEN_IDENTIFIER
))
1431 makeFortranTag (token
, TAG_LOCAL
);
1433 if (isType (token
, TOKEN_PAREN_OPEN
))
1434 skipOverParens (token
); /* skip explicit-shape-spec-list */
1435 if (isType (token
, TOKEN_COMMA
))
1437 } while (! isType (token
, TOKEN_STATEMENT_END
));
1438 skipToNextStatement (token
);
1441 static void parseFieldDefinition (tokenInfo
*const token
)
1443 if (isTypeSpec (token
))
1444 parseTypeDeclarationStmt (token
);
1445 else if (isKeyword (token
, KEYWORD_structure
))
1446 parseStructureStmt (token
);
1447 else if (isKeyword (token
, KEYWORD_union
))
1448 parseUnionStmt (token
);
1450 skipToNextStatement (token
);
1453 static void parseMap (tokenInfo
*const token
)
1455 Assert (isKeyword (token
, KEYWORD_map
));
1456 skipToNextStatement (token
);
1457 while (! isKeyword (token
, KEYWORD_end
))
1458 parseFieldDefinition (token
);
1459 readSubToken (token
);
1460 Assert (isSecondaryKeyword (token
, KEYWORD_map
));
1461 skipToNextStatement (token
);
1466 * [field-definition] [field-definition] ...
1469 * [field-definition] [field-definition] ...
1472 * [field-definition]
1473 * [field-definition] ...
1478 * Typed data declarations (variables or arrays) in structure declarations
1479 * have the form of normal Fortran typed data declarations. Data items with
1480 * different types can be freely intermixed within a structure declaration.
1482 * Unnamed fields can be declared in a structure by specifying the pseudo
1483 * name %FILL in place of an actual field name. You can use this mechanism to
1484 * generate empty space in a record for purposes such as alignment.
1486 * All mapped field declarations that are made within a UNION declaration
1487 * share a common location within the containing structure. When initializing
1488 * the fields within a UNION, the final initialization value assigned
1489 * overlays any value previously assigned to a field definition that shares
1492 static void parseUnionStmt (tokenInfo
*const token
)
1494 Assert (isKeyword (token
, KEYWORD_union
));
1495 skipToNextStatement (token
);
1496 while (isKeyword (token
, KEYWORD_map
))
1498 Assert (isKeyword (token
, KEYWORD_end
));
1499 readSubToken (token
);
1500 Assert (isSecondaryKeyword (token
, KEYWORD_union
));
1501 skipToNextStatement (token
);
1504 /* STRUCTURE [/structure-name/] [field-names]
1505 * [field-definition]
1506 * [field-definition] ...
1510 * identifies the structure in a subsequent RECORD statement.
1511 * Substructures can be established within a structure by means of either
1512 * a nested STRUCTURE declaration or a RECORD statement.
1515 * (for substructure declarations only) one or more names having the
1516 * structure of the substructure being defined.
1519 * can be one or more of the following:
1521 * Typed data declarations, which can optionally include one or more
1522 * data initialization values.
1524 * Substructure declarations (defined by either RECORD statements or
1525 * subsequent STRUCTURE statements).
1527 * UNION declarations, which are mapped fields defined by a block of
1528 * statements. The syntax of a UNION declaration is described below.
1530 * PARAMETER statements, which do not affect the form of the
1533 static void parseStructureStmt (tokenInfo
*const token
)
1536 Assert (isKeyword (token
, KEYWORD_structure
));
1538 if (isType (token
, TOKEN_OPERATOR
) &&
1539 strcmp (vStringValue (token
->string
), "/") == 0)
1540 { /* read structure name */
1542 if (isType (token
, TOKEN_IDENTIFIER
))
1543 makeFortranTag (token
, TAG_DERIVED_TYPE
);
1544 name
= newTokenFrom (token
);
1545 skipPast (token
, TOKEN_OPERATOR
);
1548 { /* fake out anonymous structure */
1550 name
->type
= TOKEN_IDENTIFIER
;
1551 name
->tag
= TAG_DERIVED_TYPE
;
1552 vStringCopyS (name
->string
, "anonymous");
1554 while (isType (token
, TOKEN_IDENTIFIER
))
1555 { /* read field names */
1556 makeFortranTag (token
, TAG_COMPONENT
);
1558 if (isType (token
, TOKEN_COMMA
))
1561 skipToNextStatement (token
);
1562 ancestorPush (name
);
1563 while (! isKeyword (token
, KEYWORD_end
))
1564 parseFieldDefinition (token
);
1565 readSubToken (token
);
1566 Assert (isSecondaryKeyword (token
, KEYWORD_structure
));
1567 skipToNextStatement (token
);
1572 /* specification-stmt
1573 * is access-stmt (is access-spec [[::] access-id-list)
1574 * or allocatable-stmt (is ALLOCATABLE [::] array-name etc.)
1575 * or common-stmt (is COMMON [ / [common-block-name] /] etc.)
1576 * or data-stmt (is DATA data-stmt-list [[,] data-stmt-set] ...)
1577 * or dimension-stmt (is DIMENSION [::] array-name etc.)
1578 * or equivalence-stmt (is EQUIVALENCE equivalence-set-list)
1579 * or external-stmt (is EXTERNAL etc.)
1580 * or intent-stmt (is INTENT ( intent-spec ) [::] etc.)
1581 * or instrinsic-stmt (is INTRINSIC etc.)
1582 * or namelist-stmt (is NAMELIST / namelist-group-name / etc.)
1583 * or optional-stmt (is OPTIONAL [::] etc.)
1584 * or pointer-stmt (is POINTER [::] object-name etc.)
1585 * or save-stmt (is SAVE etc.)
1586 * or target-stmt (is TARGET [::] object-name etc.)
1588 * access-spec is PUBLIC or PRIVATE
1590 static boolean
parseSpecificationStmt (tokenInfo
*const token
)
1592 boolean result
= TRUE
;
1593 switch (token
->keyword
)
1595 case KEYWORD_common
:
1596 parseCommonNamelistStmt (token
, TAG_COMMON_BLOCK
);
1599 case KEYWORD_namelist
:
1600 parseCommonNamelistStmt (token
, TAG_NAMELIST
);
1603 case KEYWORD_structure
:
1604 parseStructureStmt (token
);
1607 case KEYWORD_allocatable
:
1609 case KEYWORD_dimension
:
1610 case KEYWORD_equivalence
:
1611 case KEYWORD_external
:
1612 case KEYWORD_intent
:
1613 case KEYWORD_intrinsic
:
1614 case KEYWORD_optional
:
1615 case KEYWORD_pointer
:
1616 case KEYWORD_private
:
1617 case KEYWORD_public
:
1619 case KEYWORD_target
:
1620 skipToNextStatement (token
);
1630 /* component-def-stmt is
1631 * type-spec [[, component-attr-spec-list] ::] component-decl-list
1634 * component-name [ ( component-array-spec ) ] [ * char-length ]
1636 static void parseComponentDefStmt (tokenInfo
*const token
)
1638 Assert (isTypeSpec (token
));
1639 parseTypeSpec (token
);
1640 if (isType (token
, TOKEN_COMMA
))
1641 parseQualifierSpecList (token
);
1642 if (isType (token
, TOKEN_DOUBLE_COLON
))
1644 parseEntityDeclList (token
);
1647 /* derived-type-def is
1648 * derived-type-stmt is (TYPE [[, access-spec] ::] type-name
1649 * [private-sequence-stmt] ... (is PRIVATE or SEQUENCE)
1650 * component-def-stmt
1651 * [component-def-stmt] ...
1654 static void parseDerivedTypeDef (tokenInfo
*const token
)
1656 if (isType (token
, TOKEN_COMMA
))
1657 parseQualifierSpecList (token
);
1658 if (isType (token
, TOKEN_DOUBLE_COLON
))
1660 if (isType (token
, TOKEN_IDENTIFIER
))
1661 makeFortranTag (token
, TAG_DERIVED_TYPE
);
1662 ancestorPush (token
);
1663 skipToNextStatement (token
);
1664 if (isKeyword (token
, KEYWORD_private
) ||
1665 isKeyword (token
, KEYWORD_sequence
))
1667 skipToNextStatement (token
);
1669 while (! isKeyword (token
, KEYWORD_end
))
1671 if (isTypeSpec (token
))
1672 parseComponentDefStmt (token
);
1674 skipToNextStatement (token
);
1676 readSubToken (token
);
1677 Assert (isSecondaryKeyword (token
, KEYWORD_type
));
1678 skipToToken (token
, TOKEN_STATEMENT_END
);
1683 * interface-stmt (is INTERFACE [generic-spec])
1685 * [module-procedure-stmt] ...
1686 * end-interface-stmt (is END INTERFACE)
1690 * or OPERATOR ( defined-operator )
1691 * or ASSIGNMENT ( = )
1695 * [specification-part]
1697 * or subroutine-stmt
1698 * [specification-part]
1699 * end-subroutine-stmt
1701 * module-procedure-stmt is
1702 * MODULE PROCEDURE procedure-name-list
1704 static void parseInterfaceBlock (tokenInfo
*const token
)
1706 tokenInfo
*name
= NULL
;
1707 Assert (isKeyword (token
, KEYWORD_interface
));
1709 if (isType (token
, TOKEN_IDENTIFIER
))
1711 makeFortranTag (token
, TAG_INTERFACE
);
1712 name
= newTokenFrom (token
);
1714 else if (isKeyword (token
, KEYWORD_assignment
) ||
1715 isKeyword (token
, KEYWORD_operator
))
1718 if (isType (token
, TOKEN_PAREN_OPEN
))
1720 if (isType (token
, TOKEN_OPERATOR
))
1722 makeFortranTag (token
, TAG_INTERFACE
);
1723 name
= newTokenFrom (token
);
1729 name
->type
= TOKEN_IDENTIFIER
;
1730 name
->tag
= TAG_INTERFACE
;
1732 ancestorPush (name
);
1733 while (! isKeyword (token
, KEYWORD_end
))
1735 switch (token
->keyword
)
1737 case KEYWORD_function
: parseFunctionSubprogram (token
); break;
1738 case KEYWORD_subroutine
: parseSubroutineSubprogram (token
); break;
1741 if (isSubprogramPrefix (token
))
1743 else if (isTypeSpec (token
))
1744 parseTypeSpec (token
);
1746 skipToNextStatement (token
);
1750 readSubToken (token
);
1751 Assert (isSecondaryKeyword (token
, KEYWORD_interface
));
1752 skipToNextStatement (token
);
1758 * ENTRY entry-name [ ( dummy-arg-list ) ]
1760 static void parseEntryStmt (tokenInfo
*const token
)
1762 Assert (isKeyword (token
, KEYWORD_entry
));
1764 if (isType (token
, TOKEN_IDENTIFIER
))
1765 makeFortranTag (token
, TAG_ENTRY_POINT
);
1766 skipToNextStatement (token
);
1769 /* stmt-function-stmt is
1770 * function-name ([dummy-arg-name-list]) = scalar-expr
1772 static boolean
parseStmtFunctionStmt (tokenInfo
*const token
)
1774 boolean result
= FALSE
;
1775 Assert (isType (token
, TOKEN_IDENTIFIER
));
1776 #if 0 /* cannot reliably parse this yet */
1777 makeFortranTag (token
, TAG_FUNCTION
);
1780 if (isType (token
, TOKEN_PAREN_OPEN
))
1782 skipOverParens (token
);
1783 result
= (boolean
) (isType (token
, TOKEN_OPERATOR
) &&
1784 strcmp (vStringValue (token
->string
), "=") == 0);
1786 skipToNextStatement (token
);
1790 static boolean
isIgnoredDeclaration (tokenInfo
*const token
)
1793 switch (token
->keyword
)
1795 case KEYWORD_cexternal
:
1796 case KEYWORD_cglobal
:
1797 case KEYWORD_dllexport
:
1798 case KEYWORD_dllimport
:
1799 case KEYWORD_external
:
1800 case KEYWORD_format
:
1801 case KEYWORD_include
:
1802 case KEYWORD_inline
:
1803 case KEYWORD_parameter
:
1804 case KEYWORD_pascal
:
1805 case KEYWORD_pexternal
:
1806 case KEYWORD_pglobal
:
1807 case KEYWORD_static
:
1809 case KEYWORD_virtual
:
1810 case KEYWORD_volatile
:
1821 /* declaration-construct
1822 * [derived-type-def]
1824 * [type-declaration-stmt]
1825 * [specification-stmt]
1826 * [parameter-stmt] (is PARAMETER ( named-constant-def-list )
1827 * [format-stmt] (is FORMAT format-specification)
1829 * [stmt-function-stmt]
1831 static boolean
parseDeclarationConstruct (tokenInfo
*const token
)
1833 boolean result
= TRUE
;
1834 switch (token
->keyword
)
1836 case KEYWORD_entry
: parseEntryStmt (token
); break;
1837 case KEYWORD_interface
: parseInterfaceBlock (token
); break;
1838 case KEYWORD_stdcall
: readToken (token
); break;
1839 /* derived type handled by parseTypeDeclarationStmt(); */
1841 case KEYWORD_automatic
:
1843 if (isTypeSpec (token
))
1844 parseTypeDeclarationStmt (token
);
1846 skipToNextStatement (token
);
1851 if (isIgnoredDeclaration (token
))
1852 skipToNextStatement (token
);
1853 else if (isTypeSpec (token
))
1855 parseTypeDeclarationStmt (token
);
1858 else if (isType (token
, TOKEN_IDENTIFIER
))
1859 result
= parseStmtFunctionStmt (token
);
1861 result
= parseSpecificationStmt (token
);
1867 /* implicit-part-stmt
1868 * is [implicit-stmt] (is IMPLICIT etc.)
1869 * or [parameter-stmt] (is PARAMETER etc.)
1870 * or [format-stmt] (is FORMAT etc.)
1871 * or [entry-stmt] (is ENTRY entry-name etc.)
1873 static boolean
parseImplicitPartStmt (tokenInfo
*const token
)
1875 boolean result
= TRUE
;
1876 switch (token
->keyword
)
1878 case KEYWORD_entry
: parseEntryStmt (token
); break;
1880 case KEYWORD_implicit
:
1881 case KEYWORD_include
:
1882 case KEYWORD_parameter
:
1883 case KEYWORD_format
:
1884 skipToNextStatement (token
);
1887 default: result
= FALSE
; break;
1892 /* specification-part is
1893 * [use-stmt] ... (is USE module-name etc.)
1894 * [implicit-part] (is [implicit-part-stmt] ... [implicit-stmt])
1895 * [declaration-construct] ...
1897 static boolean
parseSpecificationPart (tokenInfo
*const token
)
1899 boolean result
= FALSE
;
1900 while (skipStatementIfKeyword (token
, KEYWORD_use
))
1902 while (parseImplicitPartStmt (token
))
1904 while (parseDeclarationConstruct (token
))
1910 * block-data-stmt (is BLOCK DATA [block-data-name]
1911 * [specification-part]
1912 * end-block-data-stmt (is END [BLOCK DATA [block-data-name]])
1914 static void parseBlockData (tokenInfo
*const token
)
1916 Assert (isKeyword (token
, KEYWORD_block
));
1918 if (isKeyword (token
, KEYWORD_data
))
1921 if (isType (token
, TOKEN_IDENTIFIER
))
1922 makeFortranTag (token
, TAG_BLOCK_DATA
);
1924 ancestorPush (token
);
1925 skipToNextStatement (token
);
1926 parseSpecificationPart (token
);
1927 while (! isKeyword (token
, KEYWORD_end
))
1928 skipToNextStatement (token
);
1929 readSubToken (token
);
1930 Assert (isSecondaryKeyword (token
, KEYWORD_NONE
) ||
1931 isSecondaryKeyword (token
, KEYWORD_block
));
1932 skipToNextStatement (token
);
1936 /* internal-subprogram-part is
1937 * contains-stmt (is CONTAINS)
1938 * internal-subprogram
1939 * [internal-subprogram] ...
1941 * internal-subprogram
1942 * is function-subprogram
1943 * or subroutine-subprogram
1945 static void parseInternalSubprogramPart (tokenInfo
*const token
)
1947 boolean done
= FALSE
;
1948 if (isKeyword (token
, KEYWORD_contains
))
1949 skipToNextStatement (token
);
1952 switch (token
->keyword
)
1954 case KEYWORD_function
: parseFunctionSubprogram (token
); break;
1955 case KEYWORD_subroutine
: parseSubroutineSubprogram (token
); break;
1956 case KEYWORD_end
: done
= TRUE
; break;
1959 if (isSubprogramPrefix (token
))
1961 else if (isTypeSpec (token
))
1962 parseTypeSpec (token
);
1971 * mudule-stmt (is MODULE module-name)
1972 * [specification-part]
1973 * [module-subprogram-part]
1974 * end-module-stmt (is END [MODULE [module-name]])
1976 * module-subprogram-part
1977 * contains-stmt (is CONTAINS)
1979 * [module-subprogram] ...
1982 * is function-subprogram
1983 * or subroutine-subprogram
1985 static void parseModule (tokenInfo
*const token
)
1987 Assert (isKeyword (token
, KEYWORD_module
));
1989 if (isType (token
, TOKEN_IDENTIFIER
))
1990 makeFortranTag (token
, TAG_MODULE
);
1991 ancestorPush (token
);
1992 skipToNextStatement (token
);
1993 parseSpecificationPart (token
);
1994 if (isKeyword (token
, KEYWORD_contains
))
1995 parseInternalSubprogramPart (token
);
1996 while (! isKeyword (token
, KEYWORD_end
))
1997 skipToNextStatement (token
);
1998 readSubToken (token
);
1999 Assert (isSecondaryKeyword (token
, KEYWORD_NONE
) ||
2000 isSecondaryKeyword (token
, KEYWORD_module
));
2001 skipToNextStatement (token
);
2006 * executable-construct
2008 * executable-contstruct is
2009 * execution-part-construct [execution-part-construct]
2011 * execution-part-construct
2012 * is executable-construct
2017 static boolean
parseExecutionPart (tokenInfo
*const token
)
2019 boolean result
= FALSE
;
2020 boolean done
= FALSE
;
2023 switch (token
->keyword
)
2026 if (isSubprogramPrefix (token
))
2029 skipToNextStatement (token
);
2034 parseEntryStmt (token
);
2038 case KEYWORD_contains
:
2039 case KEYWORD_function
:
2040 case KEYWORD_subroutine
:
2045 readSubToken (token
);
2046 if (isSecondaryKeyword (token
, KEYWORD_do
) ||
2047 isSecondaryKeyword (token
, KEYWORD_if
) ||
2048 isSecondaryKeyword (token
, KEYWORD_select
) ||
2049 isSecondaryKeyword (token
, KEYWORD_where
))
2051 skipToNextStatement (token
);
2062 static void parseSubprogram (tokenInfo
*const token
, const tagType tag
)
2064 Assert (isKeyword (token
, KEYWORD_program
) ||
2065 isKeyword (token
, KEYWORD_function
) ||
2066 isKeyword (token
, KEYWORD_subroutine
));
2068 if (isType (token
, TOKEN_IDENTIFIER
))
2069 makeFortranTag (token
, tag
);
2070 ancestorPush (token
);
2071 skipToNextStatement (token
);
2072 parseSpecificationPart (token
);
2073 parseExecutionPart (token
);
2074 if (isKeyword (token
, KEYWORD_contains
))
2075 parseInternalSubprogramPart (token
);
2076 Assert (isKeyword (token
, KEYWORD_end
));
2077 readSubToken (token
);
2078 Assert (isSecondaryKeyword (token
, KEYWORD_NONE
) ||
2079 isSecondaryKeyword (token
, KEYWORD_program
) ||
2080 isSecondaryKeyword (token
, KEYWORD_function
) ||
2081 isSecondaryKeyword (token
, KEYWORD_subroutine
));
2082 skipToNextStatement (token
);
2087 /* function-subprogram is
2088 * function-stmt (is [prefix] FUNCTION function-name etc.)
2089 * [specification-part]
2091 * [internal-subprogram-part]
2092 * end-function-stmt (is END [FUNCTION [function-name]])
2095 * is type-spec [RECURSIVE]
2096 * or [RECURSIVE] type-spec
2098 static void parseFunctionSubprogram (tokenInfo
*const token
)
2100 parseSubprogram (token
, TAG_FUNCTION
);
2103 /* subroutine-subprogram is
2104 * subroutine-stmt (is [RECURSIVE] SUBROUTINE subroutine-name etc.)
2105 * [specification-part]
2107 * [internal-subprogram-part]
2108 * end-subroutine-stmt (is END [SUBROUTINE [function-name]])
2110 static void parseSubroutineSubprogram (tokenInfo
*const token
)
2112 parseSubprogram (token
, TAG_SUBROUTINE
);
2116 * [program-stmt] (is PROGRAM program-name)
2117 * [specification-part]
2119 * [internal-subprogram-part ]
2122 static void parseMainProgram (tokenInfo
*const token
)
2124 parseSubprogram (token
, TAG_PROGRAM
);
2129 * or external-subprogram (is function-subprogram or subroutine-subprogram)
2133 static void parseProgramUnit (tokenInfo
*const token
)
2138 if (isType (token
, TOKEN_STATEMENT_END
))
2140 else switch (token
->keyword
)
2142 case KEYWORD_block
: parseBlockData (token
); break;
2143 case KEYWORD_end
: skipToNextStatement (token
); break;
2144 case KEYWORD_function
: parseFunctionSubprogram (token
); break;
2145 case KEYWORD_module
: parseModule (token
); break;
2146 case KEYWORD_program
: parseMainProgram (token
); break;
2147 case KEYWORD_subroutine
: parseSubroutineSubprogram (token
); break;
2150 if (isSubprogramPrefix (token
))
2154 boolean one
= parseSpecificationPart (token
);
2155 boolean two
= parseExecutionPart (token
);
2164 static boolean
findFortranTags (const unsigned int passCount
)
2167 exception_t exception
;
2170 Assert (passCount
< 3);
2171 Parent
= newToken ();
2172 token
= newToken ();
2173 FreeSourceForm
= (boolean
) (passCount
> 1);
2175 exception
= (exception_t
) setjmp (Exception
);
2176 if (exception
== ExceptionEOF
)
2178 else if (exception
== ExceptionFixedFormat
&& ! FreeSourceForm
)
2180 verbose ("%s: not fixed source form; retry as free source form\n",
2181 getInputFileName ());
2186 parseProgramUnit (token
);
2190 deleteToken (token
);
2191 deleteToken (Parent
);
2196 static void initialize (const langType language
)
2198 Lang_fortran
= language
;
2199 buildFortranKeywordHash ();
2202 extern parserDefinition
* FortranParser (void)
2204 static const char *const extensions
[] = {
2205 "f", "for", "ftn", "f77", "f90", "f95",
2206 #ifndef CASE_INSENSITIVE_FILENAMES
2207 "F", "FOR", "FTN", "F77", "F90", "F95",
2211 parserDefinition
* def
= parserNew ("Fortran");
2212 def
->kinds
= FortranKinds
;
2213 def
->kindCount
= KIND_COUNT (FortranKinds
);
2214 def
->extensions
= extensions
;
2215 def
->parser2
= findFortranTags
;
2216 def
->initialize
= initialize
;
2220 /* vi:set tabstop=4 shiftwidth=4: */