2 Copyright (C) 2000, 2001, 2002, 2003, 2004 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. */
48 /* Structure for holding module and include file search path. */
49 typedef struct gfc_directorylist
52 struct gfc_directorylist *next;
56 /* List of include file search directories. */
57 static gfc_directorylist *include_dirs;
59 static gfc_file *file_head, *current_file;
61 static int continue_flag, end_flag;
63 gfc_source_form gfc_current_form;
64 static gfc_linebuf *line_head, *line_tail;
66 locus gfc_current_locus;
67 char *gfc_source_file;
70 /* Main scanner initialization. */
73 gfc_scanner_init_1 (void)
83 /* Main scanner destructor. */
86 gfc_scanner_done_1 (void)
91 while(line_head != NULL)
98 while(file_head != NULL)
101 gfc_free(file_head->filename);
109 /* Adds path to the list pointed to by list. */
112 gfc_add_include_path (const char *path)
114 gfc_directorylist *dir;
118 while (*p == ' ' || *p == '\t') /* someone might do 'gfortran "-I include"' */
125 dir = include_dirs = gfc_getmem (sizeof (gfc_directorylist));
132 dir->next = gfc_getmem (sizeof (gfc_directorylist));
137 dir->path = gfc_getmem (strlen (p) + 2);
138 strcpy (dir->path, p);
139 strcat (dir->path, "/"); /* make '/' last character */
143 /* Release resources allocated for options. */
146 gfc_release_include_path (void)
148 gfc_directorylist *p;
150 gfc_free (gfc_option.module_dir);
151 while (include_dirs != NULL)
154 include_dirs = include_dirs->next;
160 /* Opens file for reading, searching through the include directories
161 given if necessary. */
164 gfc_open_included_file (const char *name)
166 char fullname[PATH_MAX];
167 gfc_directorylist *p;
170 f = gfc_open_file (name);
174 for (p = include_dirs; p; p = p->next)
176 if (strlen (p->path) + strlen (name) + 1 > PATH_MAX)
179 strcpy (fullname, p->path);
180 strcat (fullname, name);
182 f = gfc_open_file (fullname);
190 /* Test to see if we're at the end of the main source file. */
200 /* Test to see if we're at the end of the current file. */
209 if (line_head == NULL)
210 return 1; /* Null file */
212 if (gfc_current_locus.lb == NULL)
219 /* Test to see if we're at the beginning of a new line. */
227 return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
231 /* Test to see if we're at the end of a line. */
240 return (*gfc_current_locus.nextc == '\0');
244 /* Advance the current line pointer to the next line. */
247 gfc_advance_line (void)
252 if (gfc_current_locus.lb == NULL)
258 gfc_current_locus.lb = gfc_current_locus.lb->next;
260 if (gfc_current_locus.lb != NULL)
261 gfc_current_locus.nextc = gfc_current_locus.lb->line;
264 gfc_current_locus.nextc = NULL;
270 /* Get the next character from the input, advancing gfc_current_file's
271 locus. When we hit the end of the line or the end of the file, we
272 start returning a '\n' in order to complete the current statement.
273 No Fortran line conventions are implemented here.
275 Requiring explicit advances to the next line prevents the parse
276 pointer from being on the wrong line if the current statement ends
284 if (gfc_current_locus.nextc == NULL)
287 c = *gfc_current_locus.nextc++;
290 gfc_current_locus.nextc--; /* Remain on this line. */
297 /* Skip a comment. When we come here the parse pointer is positioned
298 immediately after the comment character. If we ever implement
299 compiler directives withing comments, here is where we parse the
303 skip_comment_line (void)
317 /* Comment lines are null lines, lines containing only blanks or lines
318 on which the first nonblank line is a '!'. */
321 skip_free_comments (void)
328 start = gfc_current_locus;
336 while (gfc_is_whitespace (c));
346 skip_comment_line ();
353 gfc_current_locus = start;
357 /* Skip comment lines in fixed source mode. We have the same rules as
358 in skip_free_comment(), except that we can have a 'c', 'C' or '*'
359 in column 1, and a '!' cannot be in column 6. */
362 skip_fixed_comments (void)
370 start = gfc_current_locus;
381 if (c == '!' || c == 'c' || c == 'C' || c == '*')
383 skip_comment_line ();
393 while (gfc_is_whitespace (c));
401 if (col != 6 && c == '!')
403 skip_comment_line ();
410 gfc_current_locus = start;
414 /* Skips the current line if it is a comment. Assumes that we are at
415 the start of the current line. */
418 gfc_skip_comments (void)
421 if (!gfc_at_bol () || gfc_current_form == FORM_FREE)
422 skip_free_comments ();
424 skip_fixed_comments ();
428 /* Get the next character from the input, taking continuation lines
429 and end-of-line comments into account. This implies that comment
430 lines between continued lines must be eaten here. For higher-level
431 subroutines, this flattens continued lines into a single logical
432 line. The in_string flag denotes whether we're inside a character
436 gfc_next_char_literal (int in_string)
448 if (gfc_current_form == FORM_FREE)
451 if (!in_string && c == '!')
453 /* This line can't be continued */
466 /* If the next nonblank character is a ! or \n, we've got a
467 continuation line. */
468 old_loc = gfc_current_locus;
471 while (gfc_is_whitespace (c))
474 /* Character constants to be continued cannot have commentary
477 if (in_string && c != '\n')
479 gfc_current_locus = old_loc;
484 if (c != '!' && c != '\n')
486 gfc_current_locus = old_loc;
493 skip_comment_line ();
497 /* We've got a continuation line and need to find where it continues.
498 First eat any comment lines. */
499 gfc_skip_comments ();
501 /* Now that we have a non-comment line, probe ahead for the
502 first non-whitespace character. If it is another '&', then
503 reading starts at the next character, otherwise we must back
504 up to where the whitespace started and resume from there. */
506 old_loc = gfc_current_locus;
509 while (gfc_is_whitespace (c))
513 gfc_current_locus = old_loc;
518 /* Fixed form continuation. */
519 if (!in_string && c == '!')
521 /* Skip comment at end of line. */
533 old_loc = gfc_current_locus;
536 gfc_skip_comments ();
538 /* See if this line is a continuation line. */
539 for (i = 0; i < 5; i++)
543 goto not_continuation;
547 if (c == '0' || c == ' ')
548 goto not_continuation;
551 /* Ready to read first character of continuation line, which might
552 be another continuation line! */
557 gfc_current_locus = old_loc;
565 /* Get the next character of input, folded to lowercase. In fixed
566 form mode, we also ignore spaces. When matcher subroutines are
567 parsing character literals, they have to call
568 gfc_next_char_literal(). */
577 c = gfc_next_char_literal (0);
579 while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
591 old_loc = gfc_current_locus;
592 c = gfc_next_char ();
593 gfc_current_locus = old_loc;
599 /* Recover from an error. We try to get past the current statement
600 and get lined up for the next. The next statement follows a '\n'
601 or a ';'. We also assume that we are not within a character
602 constant, and deal with finding a '\'' or '"'. */
605 gfc_error_recovery (void)
614 c = gfc_next_char ();
615 if (c == '\n' || c == ';')
618 if (c != '\'' && c != '"')
651 /* Read ahead until the next character to be read is not whitespace. */
654 gfc_gobble_whitespace (void)
661 old_loc = gfc_current_locus;
662 c = gfc_next_char_literal (0);
664 while (gfc_is_whitespace (c));
666 gfc_current_locus = old_loc;
670 /* Load a single line into pbuf.
672 If pbuf points to a NULL pointer, it is allocated.
673 We truncate lines that are too long, unless we're dealing with
674 preprocessor lines or if the option -ffixed-line-length-none is set,
675 in which case we reallocate the buffer to fit the entire line, if
677 In fixed mode, we expand a tab that occurs within the statement
678 label region to expand to spaces that leave the next character in
679 the source region. */
682 load_line (FILE * input, char **pbuf, char *filename, int linenum)
684 int c, maxlen, i, trunc_flag, preprocessor_flag;
685 static int buflen = 0;
688 /* Determine the maximum allowed line length. */
689 if (gfc_current_form == FORM_FREE)
690 maxlen = GFC_MAX_LINE;
692 maxlen = gfc_option.fixed_line_length;
696 /* Allocate the line buffer, storing its length into buflen. */
700 buflen = GFC_MAX_LINE;
702 *pbuf = gfc_getmem (buflen + 1);
708 preprocessor_flag = 0;
711 /* In order to not truncate preprocessor lines, we have to
712 remember that this is one. */
713 preprocessor_flag = 1;
726 continue; /* Gobble characters. */
732 /* Ctrl-Z ends the file. */
733 while (fgetc (input) != EOF);
737 if (gfc_current_form == FORM_FIXED && c == '\t' && i <= 6)
738 { /* Tab expansion. */
751 if (i >= buflen && (maxlen == 0 || preprocessor_flag))
753 /* Reallocate line buffer to double size to hold the
756 *pbuf = xrealloc (*pbuf, buflen);
759 else if (i >= buflen)
761 /* Truncate the rest of the line. */
767 if (c == '\n' || c == EOF)
770 if (gfc_option.warn_line_truncation
772 && !gfc_is_whitespace (c))
774 gfc_warning_now ("%s:%d: Line is being truncated",
780 ungetc ('\n', input);
784 /* Pad lines to the selected line length in fixed form. */
785 if (gfc_current_form == FORM_FIXED
786 && gfc_option.fixed_line_length > 0
787 && !preprocessor_flag
796 /* Get a gfc_file structure, initialize it and add it to
800 get_file (char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
804 f = gfc_getmem (sizeof (gfc_file));
806 f->filename = gfc_getmem (strlen (name) + 1);
807 strcpy (f->filename, name);
812 f->included_by = current_file;
813 if (current_file != NULL)
814 f->inclusion_line = current_file->line;
816 #ifdef USE_MAPPED_LOCATION
817 linemap_add (&line_table, reason, false, f->filename, 1);
823 /* Deal with a line from the C preprocessor. The
824 initial octothorp has already been seen. */
827 preprocessor_line (char *c)
836 while (*c == ' ' || *c == '\t')
839 if (*c < '0' || *c > '9')
844 /* Set new line number. */
845 current_file->line = line;
849 /* No file name given. */
855 while (*c == ' ' || *c == '\t')
865 /* Make filename end at quote. */
867 while (*c && ! (! escaped && *c == '"'))
872 escaped = *c == '\\';
877 /* Preprocessor line has no closing quote. */
886 flag[1] = flag[2] = flag[3] = flag[4] = flag[5] = false;
897 if (1 <= i && i <= 4)
901 /* Interpret flags. */
903 if (flag[1] || flag[3]) /* Starting new file. */
905 f = get_file (filename, LC_RENAME);
906 f->up = current_file;
910 if (flag[2]) /* Ending current file. */
912 current_file = current_file->up;
915 /* The name of the file can be a temporary file produced by
916 cpp. Replace the name if it is different. */
918 if (strcmp (current_file->filename, filename) != 0)
920 gfc_free (current_file->filename);
921 current_file->filename = gfc_getmem (strlen (filename) + 1);
922 strcpy (current_file->filename, filename);
928 gfc_warning_now ("%s:%d: Illegal preprocessor directive",
929 current_file->filename, current_file->line);
930 current_file->line++;
934 static try load_file (char *, bool);
936 /* include_line()-- Checks a line buffer to see if it is an include
937 line. If so, we call load_file() recursively to load the included
938 file. We never return a syntax error because a statement like
939 "include = 5" is perfectly legal. We return false if no include was
940 processed or true if we matched an include. */
943 include_line (char *line)
945 char quote, *c, *begin, *stop;
948 while (*c == ' ' || *c == '\t')
951 if (strncasecmp (c, "include", 7))
955 while (*c == ' ' || *c == '\t')
958 /* Find filename between quotes. */
961 if (quote != '"' && quote != '\'')
966 while (*c != quote && *c != '\0')
974 while (*c == ' ' || *c == '\t')
977 if (*c != '\0' && *c != '!')
980 /* We have an include line at this point. */
982 *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
983 read by anything else. */
985 load_file (begin, false);
989 /* Load a file into memory by calling load_line until the file ends. */
992 load_file (char *filename, bool initial)
1000 for (f = current_file; f; f = f->up)
1001 if (strcmp (filename, f->filename) == 0)
1003 gfc_error_now ("File '%s' is being included recursively", filename);
1009 input = gfc_open_file (filename);
1012 gfc_error_now ("Can't open file '%s'", filename);
1018 input = gfc_open_included_file (filename);
1021 gfc_error_now ("Can't open included file '%s'", filename);
1026 /* Load the file. */
1028 f = get_file (filename, initial ? LC_RENAME : LC_ENTER);
1029 f->up = current_file;
1031 current_file->line = 1;
1036 load_line (input, &line, filename, current_file->line);
1038 len = strlen (line);
1039 if (feof (input) && len == 0)
1042 /* There are three things this line can be: a line of Fortran
1043 source, an include line or a C preprocessor directive. */
1047 preprocessor_line (line);
1051 if (include_line (line))
1053 current_file->line++;
1059 b = gfc_getmem (gfc_linebuf_header_size + len + 1);
1061 #ifdef USE_MAPPED_LOCATION
1063 = linemap_line_start (&line_table, current_file->line++, 120);
1065 b->linenum = current_file->line++;
1067 b->file = current_file;
1068 strcpy (b->line, line);
1070 if (line_head == NULL)
1073 line_tail->next = b;
1078 /* Release the line buffer allocated in load_line. */
1083 current_file = current_file->up;
1084 #ifdef USE_MAPPED_LOCATION
1085 linemap_add (&line_table, LC_LEAVE, 0, NULL, 0);
1091 /* Determine the source form from the filename extension. We assume
1092 case insensitivity. */
1094 static gfc_source_form
1095 form_from_filename (const char *filename)
1100 const char *extension;
1101 gfc_source_form form;
1119 }; /* sentinel value */
1121 gfc_source_form f_form;
1122 const char *fileext;
1125 /* Find end of file name. */
1127 while ((i < PATH_MAX) && (filename[i] != '\0'))
1130 /* Improperly terminated or too-long filename. */
1132 return FORM_UNKNOWN;
1134 /* Find last period. */
1135 while (i >= 0 && (filename[i] != '.'))
1138 /* Did we see a file extension? */
1140 return FORM_UNKNOWN; /* Nope */
1142 /* Get file extension and compare it to others. */
1143 fileext = &(filename[i]);
1146 f_form = FORM_UNKNOWN;
1150 if (strcasecmp (fileext, exttype[i].extension) == 0)
1152 f_form = exttype[i].form;
1156 while (exttype[i].form != FORM_UNKNOWN);
1162 /* Open a new file and start scanning from that file. Returns SUCCESS
1163 if everything went OK, FAILURE otherwise. If form == FORM_UKNOWN
1164 it tries to determine the source form from the filename, defaulting
1168 gfc_new_file (const char *filename, gfc_source_form form)
1172 if (filename != NULL)
1174 gfc_source_file = gfc_getmem (strlen (filename) + 1);
1175 strcpy (gfc_source_file, filename);
1178 gfc_source_file = NULL;
1180 /* Decide which form the file will be read in as. */
1182 if (form != FORM_UNKNOWN)
1183 gfc_current_form = form;
1186 gfc_current_form = form_from_filename (filename);
1188 if (gfc_current_form == FORM_UNKNOWN)
1190 gfc_current_form = FORM_FREE;
1191 gfc_warning_now ("Reading file '%s' as free form.",
1192 (filename[0] == '\0') ? "<stdin>" : filename);
1196 result = load_file (gfc_source_file, true);
1198 gfc_current_locus.lb = line_head;
1199 gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
1201 #if 0 /* Debugging aid. */
1202 for (; line_head; line_head = line_head->next)
1203 gfc_status ("%s:%3d %s\n", line_head->file->filename,
1204 #ifdef USE_MAPPED_LOCATION
1205 LOCATION_LINE (line_head->location),