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_locus1
;
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
);
195 /* Return a pointer to the current locus. */
198 gfc_current_locus (void)
201 return &gfc_current_locus1
;
206 /* Let a caller move the current read pointer (backwards). */
209 gfc_set_locus (locus
* lp
)
212 gfc_current_locus1
= *lp
;
216 /* Test to see if we're at the end of the main source file. */
226 /* Test to see if we're at the end of the current file. */
235 if (line_head
== NULL
)
236 return 1; /* Null file */
238 if (gfc_current_locus1
.lb
== NULL
)
245 /* Test to see if we're at the beginning of a new line. */
253 return (gfc_current_locus1
.nextc
== gfc_current_locus1
.lb
->line
);
257 /* Test to see if we're at the end of a line. */
266 return (*gfc_current_locus1
.nextc
== '\0');
270 /* Advance the current line pointer to the next line. */
273 gfc_advance_line (void)
278 if (gfc_current_locus1
.lb
== NULL
)
284 gfc_current_locus1
.lb
= gfc_current_locus1
.lb
->next
;
286 if (gfc_current_locus1
.lb
!= NULL
)
287 gfc_current_locus1
.nextc
= gfc_current_locus1
.lb
->line
;
290 gfc_current_locus1
.nextc
= NULL
;
296 /* Get the next character from the input, advancing gfc_current_file's
297 locus. When we hit the end of the line or the end of the file, we
298 start returning a '\n' in order to complete the current statement.
299 No Fortran line conventions are implemented here.
301 Requiring explicit advances to the next line prevents the parse
302 pointer from being on the wrong line if the current statement ends
310 if (gfc_current_locus1
.nextc
== NULL
)
313 c
= *gfc_current_locus1
.nextc
++;
316 gfc_current_locus1
.nextc
--; /* Remain on this line. */
323 /* Skip a comment. When we come here the parse pointer is positioned
324 immediately after the comment character. If we ever implement
325 compiler directives withing comments, here is where we parse the
329 skip_comment_line (void)
343 /* Comment lines are null lines, lines containing only blanks or lines
344 on which the first nonblank line is a '!'. */
347 skip_free_comments (void)
354 start
= gfc_current_locus1
;
362 while (gfc_is_whitespace (c
));
372 skip_comment_line ();
379 gfc_set_locus (&start
);
383 /* Skip comment lines in fixed source mode. We have the same rules as
384 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
385 in column 1. and a '!' cannot be in* column 6. */
388 skip_fixed_comments (void)
396 start
= gfc_current_locus1
;
407 if (c
== '!' || c
== 'c' || c
== 'C' || c
== '*')
409 skip_comment_line ();
419 while (gfc_is_whitespace (c
));
427 if (col
!= 6 && c
== '!')
429 skip_comment_line ();
436 gfc_set_locus (&start
);
440 /* Skips the current line if it is a comment. Assumes that we are at
441 the start of the current line. */
444 gfc_skip_comments (void)
447 if (!gfc_at_bol () || gfc_current_form
== FORM_FREE
)
448 skip_free_comments ();
450 skip_fixed_comments ();
454 /* Get the next character from the input, taking continuation lines
455 and end-of-line comments into account. This implies that comment
456 lines between continued lines must be eaten here. For higher-level
457 subroutines, this flattens continued lines into a single logical
458 line. The in_string flag denotes whether we're inside a character
462 gfc_next_char_literal (int in_string
)
474 if (gfc_current_form
== FORM_FREE
)
477 if (!in_string
&& c
== '!')
479 /* This line can't be continued */
492 /* If the next nonblank character is a ! or \n, we've got a
493 continuation line. */
494 old_loc
= gfc_current_locus1
;
497 while (gfc_is_whitespace (c
))
500 /* Character constants to be continued cannot have commentary
503 if (in_string
&& c
!= '\n')
505 gfc_set_locus (&old_loc
);
510 if (c
!= '!' && c
!= '\n')
512 gfc_set_locus (&old_loc
);
519 skip_comment_line ();
523 /* We've got a continuation line and need to find where it continues.
524 First eat any comment lines. */
525 gfc_skip_comments ();
527 /* Now that we have a non-comment line, probe ahead for the
528 first non-whitespace character. If it is another '&', then
529 reading starts at the next character, otherwise we must back
530 up to where the whitespace started and resume from there. */
532 old_loc
= *gfc_current_locus ();
535 while (gfc_is_whitespace (c
))
539 gfc_set_locus (&old_loc
);
544 /* Fixed form continuation. */
545 if (!in_string
&& c
== '!')
547 /* Skip comment at end of line. */
559 old_loc
= *gfc_current_locus ();
562 gfc_skip_comments ();
564 /* See if this line is a continuation line. */
565 for (i
= 0; i
< 5; i
++)
569 goto not_continuation
;
573 if (c
== '0' || c
== ' ')
574 goto not_continuation
;
577 /* Ready to read first character of continuation line, which might
578 be another continuation line! */
583 gfc_set_locus (&old_loc
);
591 /* Get the next character of input, folded to lowercase. In fixed
592 form mode, we also ignore spaces. When matcher subroutines are
593 parsing character literals, they have to call
594 gfc_next_char_literal(). */
603 c
= gfc_next_char_literal (0);
605 while (gfc_current_form
== FORM_FIXED
&& gfc_is_whitespace (c
));
617 old_loc
= gfc_current_locus1
;
618 c
= gfc_next_char ();
619 gfc_set_locus (&old_loc
);
625 /* Recover from an error. We try to get past the current statement
626 and get lined up for the next. The next statement follows a '\n'
627 or a ';'. We also assume that we are not within a character
628 constant, and deal with finding a '\'' or '"'. */
631 gfc_error_recovery (void)
640 c
= gfc_next_char ();
641 if (c
== '\n' || c
== ';')
644 if (c
!= '\'' && c
!= '"')
677 /* Read ahead until the next character to be read is not whitespace. */
680 gfc_gobble_whitespace (void)
687 old_loc
= gfc_current_locus1
;
688 c
= gfc_next_char_literal (0);
690 while (gfc_is_whitespace (c
));
692 gfc_set_locus (&old_loc
);
696 /* Load a single line into the buffer. We truncate lines that are too
697 long. In fixed mode, we expand a tab that occurs within the
698 statement label region to expand to spaces that leave the next
699 character in the source region. */
702 load_line (FILE * input
, char *buffer
, char *filename
, int linenum
)
704 int c
, maxlen
, i
, trunc_flag
;
706 maxlen
= (gfc_current_form
== FORM_FREE
)
708 : gfc_option
.fixed_line_length
;
722 continue; /* Gobble characters. */
728 /* Ctrl-Z ends the file. */
729 while (fgetc (input
) != EOF
);
733 if (gfc_current_form
== FORM_FIXED
&& c
== '\t' && i
<= 6)
734 { /* Tab expandsion. */
748 { /* Truncate the rest of the line. */
754 if (c
== '\n' || c
== EOF
)
757 if (gfc_option
.warn_line_truncation
759 && !gfc_is_whitespace (c
))
761 gfc_warning_now ("Line %d of %s is being truncated",
767 ungetc ('\n', input
);
775 /* Get a gfc_file structure, initialize it and add it to
779 get_file (char *name
)
783 f
= gfc_getmem (sizeof (gfc_file
));
785 f
->filename
= gfc_getmem (strlen (name
) + 1);
786 strcpy (f
->filename
, name
);
791 f
->included_by
= current_file
;
792 if (current_file
!= NULL
)
793 f
->inclusion_line
= current_file
->line
;
798 /* Deal with a line from the C preprocessor. The
799 initial octothorp has already been seen. */
802 preprocessor_line (char *c
)
810 while (*c
== ' ' || *c
== '\t')
813 if (*c
< '0' || *c
> '9')
815 gfc_warning_now ("%s:%d Unknown preprocessor directive",
816 current_file
->filename
, current_file
->line
);
817 current_file
->line
++;
823 c
= strchr (c
, ' ') + 2; /* Skip space and quote. */
826 c
= strchr (c
, '"'); /* Make filename end at quote. */
831 flag
[1] = flag
[2] = flag
[3] = flag
[4] = flag
[5] = false;
842 if (1 <= i
&& i
<= 4)
846 /* Interpret flags. */
848 if (flag
[1] || flag
[3]) /* Starting new file. */
850 f
= get_file (filename
);
851 f
->up
= current_file
;
855 if (flag
[2]) /* Ending current file. */
857 current_file
= current_file
->up
;
860 current_file
->line
= line
;
862 /* The name of the file can be a temporary file produced by
863 cpp. Replace the name if it is different. */
865 if (strcmp (current_file
->filename
, filename
) != 0)
867 gfc_free (current_file
->filename
);
868 current_file
->filename
= gfc_getmem (strlen (filename
) + 1);
869 strcpy (current_file
->filename
, filename
);
874 static try load_file (char *, bool);
876 /* include_line()-- Checks a line buffer to see if it is an include
877 line. If so, we call load_file() recursively to load the included
878 file. We never return a syntax error because a statement like
879 "include = 5" is perfectly legal. We return false if no include was
880 processed or true if we matched an include. */
883 include_line (char *line
)
885 char quote
, *c
, *begin
, *stop
;
888 while (*c
== ' ' || *c
== '\t')
891 if (strncasecmp (c
, "include", 7))
895 while (*c
== ' ' || *c
== '\t')
898 /* Find filename between quotes. */
901 if (quote
!= '"' && quote
!= '\'')
906 while (*c
!= quote
&& *c
!= '\0')
914 while (*c
== ' ' || *c
== '\t')
917 if (*c
!= '\0' && *c
!= '!')
920 /* We have an include line at this point. */
922 *stop
= '\0'; /* It's ok to trash the buffer, as this line won't be
923 read by anything else. */
925 load_file (begin
, false);
929 /* Load a file into memory by calling load_line until the file ends. */
932 load_file (char *filename
, bool initial
)
934 char line
[GFC_MAX_LINE
+1];
940 for (f
= current_file
; f
; f
= f
->up
)
941 if (strcmp (filename
, f
->filename
) == 0)
943 gfc_error_now ("File '%s' is being included recursively", filename
);
949 input
= gfc_open_file (filename
);
952 gfc_error_now ("Can't open file '%s'", filename
);
958 input
= gfc_open_included_file (filename
);
961 gfc_error_now ("Can't open included file '%s'", filename
);
968 f
= get_file (filename
);
969 f
->up
= current_file
;
971 current_file
->line
= 1;
975 load_line (input
, line
, filename
, current_file
->line
);
978 if (feof (input
) && len
== 0)
981 /* There are three things this line can be: a line of Fortran
982 source, an include line or a C preprocessor directive. */
986 preprocessor_line (line
);
990 if (include_line (line
))
992 current_file
->line
++;
998 b
= gfc_getmem (sizeof (gfc_linebuf
) + len
+ 1);
1000 b
->linenum
= current_file
->line
++;
1001 b
->file
= current_file
;
1002 strcpy (b
->line
, line
);
1004 if (line_head
== NULL
)
1007 line_tail
->next
= b
;
1014 current_file
= current_file
->up
;
1019 /* Determine the source form from the filename extension. We assume
1020 case insensitivity. */
1022 static gfc_source_form
1023 form_from_filename (const char *filename
)
1028 const char *extension
;
1029 gfc_source_form form
;
1047 }; /* sentinel value */
1049 gfc_source_form f_form
;
1050 const char *fileext
;
1053 /* Find end of file name. */
1055 while ((i
< PATH_MAX
) && (filename
[i
] != '\0'))
1058 /* Improperly terminated or too-long filename. */
1060 return FORM_UNKNOWN
;
1062 /* Find last period. */
1063 while (i
>= 0 && (filename
[i
] != '.'))
1066 /* Did we see a file extension? */
1068 return FORM_UNKNOWN
; /* Nope */
1070 /* Get file extension and compare it to others. */
1071 fileext
= &(filename
[i
]);
1074 f_form
= FORM_UNKNOWN
;
1078 if (strcasecmp (fileext
, exttype
[i
].extension
) == 0)
1080 f_form
= exttype
[i
].form
;
1084 while (exttype
[i
].form
!= FORM_UNKNOWN
);
1090 /* Open a new file and start scanning from that file. Returns SUCCESS
1091 if everything went OK, FAILURE otherwise. If form == FORM_UKNOWN
1092 it tries to determine the source form from the filename, defaulting
1096 gfc_new_file (const char *filename
, gfc_source_form form
)
1100 if (filename
!= NULL
)
1102 gfc_source_file
= gfc_getmem (strlen (filename
) + 1);
1103 strcpy (gfc_source_file
, filename
);
1106 gfc_source_file
= NULL
;
1108 /* Decide which form the file will be read in as. */
1110 if (form
!= FORM_UNKNOWN
)
1111 gfc_current_form
= form
;
1114 gfc_current_form
= form_from_filename (filename
);
1116 if (gfc_current_form
== FORM_UNKNOWN
)
1118 gfc_current_form
= FORM_FREE
;
1119 gfc_warning_now ("Reading file '%s' as free form.",
1120 (filename
[0] == '\0') ? "<stdin>" : filename
);
1124 result
= load_file (gfc_source_file
, true);
1126 gfc_current_locus1
.lb
= line_head
;
1127 gfc_current_locus1
.nextc
= (line_head
== NULL
) ? NULL
: line_head
->line
;
1129 #if 0 /* Debugging aid. */
1130 for (; line_head
; line_head
= line_head
->next
)
1131 gfc_status ("%s:%3d %s\n", line_head
->file
->filename
,
1132 line_head
->linenum
, line_head
->line
);