2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* Set of subroutines to (ultimately) return the next character to the
23 various matching subroutines. This file's job is to read files and
24 build up lines that are parsed by the parser. This means that we
25 handle continuation lines and "include" lines.
27 The first thing the scanner does is to load an entire file into
28 memory. We load the entire file into memory for a couple reasons.
29 The first is that we want to be able to deal with nonseekable input
30 (pipes, stdin) and there is a lot of backing up involved during
33 The second is that we want to be able to print the locus of errors,
34 and an error on line 999999 could conflict with something on line
35 one. Given nonseekable input, we've got to store the whole thing.
37 One thing that helps are the column truncation limits that give us
38 an upper bound on the size of individual lines. We don't store the
41 From the scanner's viewpoint, the higher level subroutines ask for
42 new characters and do a lot of jumping backwards. */
52 /* Structure for holding module and include file search path. */
53 typedef struct gfc_directorylist
56 struct gfc_directorylist
*next
;
60 /* List of include file search directories. */
61 static gfc_directorylist
*include_dirs
;
63 static gfc_file
*file_head
, *current_file
;
65 static int continue_flag
, end_flag
;
67 gfc_source_form gfc_current_form
;
68 static gfc_linebuf
*line_head
, *line_tail
;
70 locus gfc_current_locus
;
71 char *gfc_source_file
;
74 /* Main scanner initialization. */
77 gfc_scanner_init_1 (void)
87 /* Main scanner destructor. */
90 gfc_scanner_done_1 (void)
95 while(line_head
!= NULL
)
102 while(file_head
!= NULL
)
105 gfc_free(file_head
->filename
);
113 /* Adds path to the list pointed to by list. */
116 gfc_add_include_path (const char *path
)
118 gfc_directorylist
*dir
;
122 while (*p
== ' ' || *p
== '\t') /* someone might do 'gfortran "-I include"' */
129 dir
= include_dirs
= gfc_getmem (sizeof (gfc_directorylist
));
136 dir
->next
= gfc_getmem (sizeof (gfc_directorylist
));
141 dir
->path
= gfc_getmem (strlen (p
) + 2);
142 strcpy (dir
->path
, p
);
143 strcat (dir
->path
, "/"); /* make '/' last character */
147 /* Release resources allocated for options. */
150 gfc_release_include_path (void)
152 gfc_directorylist
*p
;
154 gfc_free (gfc_option
.module_dir
);
155 while (include_dirs
!= NULL
)
158 include_dirs
= include_dirs
->next
;
164 /* Opens file for reading, searching through the include directories
165 given if necessary. */
168 gfc_open_included_file (const char *name
)
170 char fullname
[PATH_MAX
];
171 gfc_directorylist
*p
;
174 f
= gfc_open_file (name
);
178 for (p
= include_dirs
; p
; p
= p
->next
)
180 if (strlen (p
->path
) + strlen (name
) + 1 > PATH_MAX
)
183 strcpy (fullname
, p
->path
);
184 strcat (fullname
, name
);
186 f
= gfc_open_file (fullname
);
194 /* Test to see if we're at the end of the main source file. */
204 /* Test to see if we're at the end of the current file. */
213 if (line_head
== NULL
)
214 return 1; /* Null file */
216 if (gfc_current_locus
.lb
== NULL
)
223 /* Test to see if we're at the beginning of a new line. */
231 return (gfc_current_locus
.nextc
== gfc_current_locus
.lb
->line
);
235 /* Test to see if we're at the end of a line. */
244 return (*gfc_current_locus
.nextc
== '\0');
248 /* Advance the current line pointer to the next line. */
251 gfc_advance_line (void)
256 if (gfc_current_locus
.lb
== NULL
)
262 gfc_current_locus
.lb
= gfc_current_locus
.lb
->next
;
264 if (gfc_current_locus
.lb
!= NULL
)
265 gfc_current_locus
.nextc
= gfc_current_locus
.lb
->line
;
268 gfc_current_locus
.nextc
= NULL
;
274 /* Get the next character from the input, advancing gfc_current_file's
275 locus. When we hit the end of the line or the end of the file, we
276 start returning a '\n' in order to complete the current statement.
277 No Fortran line conventions are implemented here.
279 Requiring explicit advances to the next line prevents the parse
280 pointer from being on the wrong line if the current statement ends
288 if (gfc_current_locus
.nextc
== NULL
)
291 c
= *gfc_current_locus
.nextc
++;
294 gfc_current_locus
.nextc
--; /* Remain on this line. */
301 /* Skip a comment. When we come here the parse pointer is positioned
302 immediately after the comment character. If we ever implement
303 compiler directives withing comments, here is where we parse the
307 skip_comment_line (void)
321 /* Comment lines are null lines, lines containing only blanks or lines
322 on which the first nonblank line is a '!'. */
325 skip_free_comments (void)
332 start
= gfc_current_locus
;
340 while (gfc_is_whitespace (c
));
350 skip_comment_line ();
357 gfc_current_locus
= start
;
361 /* Skip comment lines in fixed source mode. We have the same rules as
362 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
363 in column 1. and a '!' cannot be in* column 6. */
366 skip_fixed_comments (void)
374 start
= gfc_current_locus
;
385 if (c
== '!' || c
== 'c' || c
== 'C' || c
== '*')
387 skip_comment_line ();
397 while (gfc_is_whitespace (c
));
405 if (col
!= 6 && c
== '!')
407 skip_comment_line ();
414 gfc_current_locus
= start
;
418 /* Skips the current line if it is a comment. Assumes that we are at
419 the start of the current line. */
422 gfc_skip_comments (void)
425 if (!gfc_at_bol () || gfc_current_form
== FORM_FREE
)
426 skip_free_comments ();
428 skip_fixed_comments ();
432 /* Get the next character from the input, taking continuation lines
433 and end-of-line comments into account. This implies that comment
434 lines between continued lines must be eaten here. For higher-level
435 subroutines, this flattens continued lines into a single logical
436 line. The in_string flag denotes whether we're inside a character
440 gfc_next_char_literal (int in_string
)
452 if (gfc_current_form
== FORM_FREE
)
455 if (!in_string
&& c
== '!')
457 /* This line can't be continued */
470 /* If the next nonblank character is a ! or \n, we've got a
471 continuation line. */
472 old_loc
= gfc_current_locus
;
475 while (gfc_is_whitespace (c
))
478 /* Character constants to be continued cannot have commentary
481 if (in_string
&& c
!= '\n')
483 gfc_current_locus
= old_loc
;
488 if (c
!= '!' && c
!= '\n')
490 gfc_current_locus
= old_loc
;
497 skip_comment_line ();
501 /* We've got a continuation line and need to find where it continues.
502 First eat any comment lines. */
503 gfc_skip_comments ();
505 /* Now that we have a non-comment line, probe ahead for the
506 first non-whitespace character. If it is another '&', then
507 reading starts at the next character, otherwise we must back
508 up to where the whitespace started and resume from there. */
510 old_loc
= gfc_current_locus
;
513 while (gfc_is_whitespace (c
))
517 gfc_current_locus
= old_loc
;
522 /* Fixed form continuation. */
523 if (!in_string
&& c
== '!')
525 /* Skip comment at end of line. */
537 old_loc
= gfc_current_locus
;
540 gfc_skip_comments ();
542 /* See if this line is a continuation line. */
543 for (i
= 0; i
< 5; i
++)
547 goto not_continuation
;
551 if (c
== '0' || c
== ' ')
552 goto not_continuation
;
555 /* Ready to read first character of continuation line, which might
556 be another continuation line! */
561 gfc_current_locus
= old_loc
;
569 /* Get the next character of input, folded to lowercase. In fixed
570 form mode, we also ignore spaces. When matcher subroutines are
571 parsing character literals, they have to call
572 gfc_next_char_literal(). */
581 c
= gfc_next_char_literal (0);
583 while (gfc_current_form
== FORM_FIXED
&& gfc_is_whitespace (c
));
595 old_loc
= gfc_current_locus
;
596 c
= gfc_next_char ();
597 gfc_current_locus
= old_loc
;
603 /* Recover from an error. We try to get past the current statement
604 and get lined up for the next. The next statement follows a '\n'
605 or a ';'. We also assume that we are not within a character
606 constant, and deal with finding a '\'' or '"'. */
609 gfc_error_recovery (void)
618 c
= gfc_next_char ();
619 if (c
== '\n' || c
== ';')
622 if (c
!= '\'' && c
!= '"')
655 /* Read ahead until the next character to be read is not whitespace. */
658 gfc_gobble_whitespace (void)
665 old_loc
= gfc_current_locus
;
666 c
= gfc_next_char_literal (0);
668 while (gfc_is_whitespace (c
));
670 gfc_current_locus
= old_loc
;
674 /* Load a single line into pbuf.
676 If pbuf points to a NULL pointer, it is allocated.
677 We truncate lines that are too long, unless we're dealing with
678 preprocessor lines or if the option -ffixed-line-length-none is set,
679 in which case we reallocate the buffer to fit the entire line, if
681 In fixed mode, we expand a tab that occurs within the statement
682 label region to expand to spaces that leave the next character in
683 the source region. */
686 load_line (FILE * input
, char **pbuf
, char *filename
, int linenum
)
688 int c
, maxlen
, i
, trunc_flag
, preprocessor_flag
;
689 static int buflen
= 0;
692 /* Determine the maximum allowed line length. */
693 if (gfc_current_form
== FORM_FREE
)
694 maxlen
= GFC_MAX_LINE
;
696 maxlen
= gfc_option
.fixed_line_length
;
700 /* Allocate the line buffer, storing its length into buflen. */
704 buflen
= GFC_MAX_LINE
;
706 *pbuf
= gfc_getmem (buflen
+ 1);
712 preprocessor_flag
= 0;
715 /* In order to not truncate preprocessor lines, we have to
716 remember that this is one. */
717 preprocessor_flag
= 1;
730 continue; /* Gobble characters. */
736 /* Ctrl-Z ends the file. */
737 while (fgetc (input
) != EOF
);
741 if (gfc_current_form
== FORM_FIXED
&& c
== '\t' && i
<= 6)
742 { /* Tab expansion. */
755 if (i
>= buflen
&& (maxlen
== 0 || preprocessor_flag
))
757 /* Reallocate line buffer to double size to hold the
760 *pbuf
= xrealloc (*pbuf
, buflen
);
763 else if (i
>= buflen
)
765 /* Truncate the rest of the line. */
771 if (c
== '\n' || c
== EOF
)
774 if (gfc_option
.warn_line_truncation
776 && !gfc_is_whitespace (c
))
778 gfc_warning_now ("%s:%d: Line is being truncated",
784 ungetc ('\n', input
);
788 /* Pad lines to the selected line length in fixed form. */
789 if (gfc_current_form
== FORM_FIXED
790 && gfc_option
.fixed_line_length
> 0
791 && !preprocessor_flag
800 /* Get a gfc_file structure, initialize it and add it to
804 get_file (char *name
, enum lc_reason reason ATTRIBUTE_UNUSED
)
808 f
= gfc_getmem (sizeof (gfc_file
));
810 f
->filename
= gfc_getmem (strlen (name
) + 1);
811 strcpy (f
->filename
, name
);
816 f
->included_by
= current_file
;
817 if (current_file
!= NULL
)
818 f
->inclusion_line
= current_file
->line
;
820 #ifdef USE_MAPPED_LOCATION
821 linemap_add (&line_table
, reason
, false, f
->filename
, 1);
827 /* Deal with a line from the C preprocessor. The
828 initial octothorp has already been seen. */
831 preprocessor_line (char *c
)
840 while (*c
== ' ' || *c
== '\t')
843 if (*c
< '0' || *c
> '9')
848 /* Set new line number. */
849 current_file
->line
= line
;
853 /* No file name given. */
859 while (*c
== ' ' || *c
== '\t')
869 /* Make filename end at quote. */
871 while (*c
&& ! (! escaped
&& *c
== '"'))
876 escaped
= *c
== '\\';
881 /* Preprocessor line has no closing quote. */
890 flag
[1] = flag
[2] = flag
[3] = flag
[4] = flag
[5] = false;
901 if (1 <= i
&& i
<= 4)
905 /* Interpret flags. */
907 if (flag
[1] || flag
[3]) /* Starting new file. */
909 f
= get_file (filename
, LC_RENAME
);
910 f
->up
= current_file
;
914 if (flag
[2]) /* Ending current file. */
916 current_file
= current_file
->up
;
919 /* The name of the file can be a temporary file produced by
920 cpp. Replace the name if it is different. */
922 if (strcmp (current_file
->filename
, filename
) != 0)
924 gfc_free (current_file
->filename
);
925 current_file
->filename
= gfc_getmem (strlen (filename
) + 1);
926 strcpy (current_file
->filename
, filename
);
932 gfc_warning_now ("%s:%d: Illegal preprocessor directive",
933 current_file
->filename
, current_file
->line
);
934 current_file
->line
++;
938 static try load_file (char *, bool);
940 /* include_line()-- Checks a line buffer to see if it is an include
941 line. If so, we call load_file() recursively to load the included
942 file. We never return a syntax error because a statement like
943 "include = 5" is perfectly legal. We return false if no include was
944 processed or true if we matched an include. */
947 include_line (char *line
)
949 char quote
, *c
, *begin
, *stop
;
952 while (*c
== ' ' || *c
== '\t')
955 if (strncasecmp (c
, "include", 7))
959 while (*c
== ' ' || *c
== '\t')
962 /* Find filename between quotes. */
965 if (quote
!= '"' && quote
!= '\'')
970 while (*c
!= quote
&& *c
!= '\0')
978 while (*c
== ' ' || *c
== '\t')
981 if (*c
!= '\0' && *c
!= '!')
984 /* We have an include line at this point. */
986 *stop
= '\0'; /* It's ok to trash the buffer, as this line won't be
987 read by anything else. */
989 load_file (begin
, false);
993 /* Load a file into memory by calling load_line until the file ends. */
996 load_file (char *filename
, bool initial
)
1004 for (f
= current_file
; f
; f
= f
->up
)
1005 if (strcmp (filename
, f
->filename
) == 0)
1007 gfc_error_now ("File '%s' is being included recursively", filename
);
1013 input
= gfc_open_file (filename
);
1016 gfc_error_now ("Can't open file '%s'", filename
);
1022 input
= gfc_open_included_file (filename
);
1025 gfc_error_now ("Can't open included file '%s'", filename
);
1030 /* Load the file. */
1032 f
= get_file (filename
, initial
? LC_RENAME
: LC_ENTER
);
1033 f
->up
= current_file
;
1035 current_file
->line
= 1;
1040 load_line (input
, &line
, filename
, current_file
->line
);
1042 len
= strlen (line
);
1043 if (feof (input
) && len
== 0)
1046 /* There are three things this line can be: a line of Fortran
1047 source, an include line or a C preprocessor directive. */
1051 preprocessor_line (line
);
1055 if (include_line (line
))
1057 current_file
->line
++;
1063 b
= gfc_getmem (gfc_linebuf_header_size
+ len
+ 1);
1065 #ifdef USE_MAPPED_LOCATION
1067 = linemap_line_start (&line_table
, current_file
->line
++, 120);
1069 b
->linenum
= current_file
->line
++;
1071 b
->file
= current_file
;
1072 strcpy (b
->line
, line
);
1074 if (line_head
== NULL
)
1077 line_tail
->next
= b
;
1082 /* Release the line buffer allocated in load_line. */
1087 current_file
= current_file
->up
;
1088 #ifdef USE_MAPPED_LOCATION
1089 linemap_add (&line_table
, LC_LEAVE
, 0, NULL
, 0);
1095 /* Determine the source form from the filename extension. We assume
1096 case insensitivity. */
1098 static gfc_source_form
1099 form_from_filename (const char *filename
)
1104 const char *extension
;
1105 gfc_source_form form
;
1123 }; /* sentinel value */
1125 gfc_source_form f_form
;
1126 const char *fileext
;
1129 /* Find end of file name. */
1131 while ((i
< PATH_MAX
) && (filename
[i
] != '\0'))
1134 /* Improperly terminated or too-long filename. */
1136 return FORM_UNKNOWN
;
1138 /* Find last period. */
1139 while (i
>= 0 && (filename
[i
] != '.'))
1142 /* Did we see a file extension? */
1144 return FORM_UNKNOWN
; /* Nope */
1146 /* Get file extension and compare it to others. */
1147 fileext
= &(filename
[i
]);
1150 f_form
= FORM_UNKNOWN
;
1154 if (strcasecmp (fileext
, exttype
[i
].extension
) == 0)
1156 f_form
= exttype
[i
].form
;
1160 while (exttype
[i
].form
!= FORM_UNKNOWN
);
1166 /* Open a new file and start scanning from that file. Returns SUCCESS
1167 if everything went OK, FAILURE otherwise. If form == FORM_UKNOWN
1168 it tries to determine the source form from the filename, defaulting
1172 gfc_new_file (const char *filename
, gfc_source_form form
)
1176 if (filename
!= NULL
)
1178 gfc_source_file
= gfc_getmem (strlen (filename
) + 1);
1179 strcpy (gfc_source_file
, filename
);
1182 gfc_source_file
= NULL
;
1184 /* Decide which form the file will be read in as. */
1186 if (form
!= FORM_UNKNOWN
)
1187 gfc_current_form
= form
;
1190 gfc_current_form
= form_from_filename (filename
);
1192 if (gfc_current_form
== FORM_UNKNOWN
)
1194 gfc_current_form
= FORM_FREE
;
1195 gfc_warning_now ("Reading file '%s' as free form.",
1196 (filename
[0] == '\0') ? "<stdin>" : filename
);
1200 result
= load_file (gfc_source_file
, true);
1202 gfc_current_locus
.lb
= line_head
;
1203 gfc_current_locus
.nextc
= (line_head
== NULL
) ? NULL
: line_head
->line
;
1205 #if 0 /* Debugging aid. */
1206 for (; line_head
; line_head
= line_head
->next
)
1207 gfc_status ("%s:%3d %s\n", line_head
->file
->filename
,
1208 #ifdef USE_MAPPED_LOCATION
1209 LOCATION_LINE (line_head
->location
),