1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4 F2003 I/O support contributed by Jerry DeLisle
6 This file is part of the GNU Fortran 95 runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
22 Libgfortran is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with Libgfortran; see the file COPYING. If not, write to
29 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
30 Boston, MA 02110-1301, USA. */
33 /* format.c-- parse a FORMAT string into a binary format suitable for
34 * interpretation during I/O statements */
40 #define FARRAY_SIZE 64
42 typedef struct fnode_array
44 struct fnode_array *next;
45 fnode array[FARRAY_SIZE];
49 typedef struct format_data
51 char *format_string, *string;
53 format_token saved_token;
54 int value, format_string_len, reversion_ok;
56 const fnode *saved_format;
62 static const fnode colon_node = { FMT_COLON, 0, NULL, NULL, {{ 0, 0, 0 }}, 0,
67 static const char posint_required[] = "Positive width required in format",
68 period_required[] = "Period required in format",
69 nonneg_required[] = "Nonnegative width required in format",
70 unexpected_element[] = "Unexpected element in format",
71 unexpected_end[] = "Unexpected end of format string",
72 bad_string[] = "Unterminated character constant in format",
73 bad_hollerith[] = "Hollerith constant extends past the end of the format",
74 reversion_error[] = "Exhausted data descriptors in format";
77 /* next_char()-- Return the next character in the format string.
78 * Returns -1 when the string is done. If the literal flag is set,
79 * spaces are significant, otherwise they are not. */
82 next_char (format_data *fmt, int literal)
88 if (fmt->format_string_len == 0)
91 fmt->format_string_len--;
92 c = toupper (*fmt->format_string++);
94 while ((c == ' ' || c == '\t') && !literal);
100 /* unget_char()-- Back up one character position. */
102 #define unget_char(fmt) \
103 { fmt->format_string--; fmt->format_string_len++; }
106 /* get_fnode()-- Allocate a new format node, inserting it into the
107 * current singly linked list. These are initially allocated from the
111 get_fnode (format_data *fmt, fnode **head, fnode **tail, format_token t)
115 if (fmt->avail == &fmt->last->array[FARRAY_SIZE])
117 fmt->last->next = get_mem (sizeof (fnode_array));
118 fmt->last = fmt->last->next;
119 fmt->last->next = NULL;
120 fmt->avail = &fmt->last->array[0];
123 memset (f, '\0', sizeof (fnode));
135 f->source = fmt->format_string;
140 /* free_format_data()-- Free all allocated format data. */
143 free_format_data (st_parameter_dt *dtp)
145 fnode_array *fa, *fa_next;
146 format_data *fmt = dtp->u.p.fmt;
151 for (fa = fmt->array.next; fa; fa = fa_next)
162 /* format_lex()-- Simple lexical analyzer for getting the next token
163 * in a FORMAT string. We support a one-level token pushback in the
164 * fmt->saved_token variable. */
167 format_lex (format_data *fmt)
174 if (fmt->saved_token != FMT_NONE)
176 token = fmt->saved_token;
177 fmt->saved_token = FMT_NONE;
182 c = next_char (fmt, 0);
191 c = next_char (fmt, 0);
198 fmt->value = c - '0';
202 c = next_char (fmt, 0);
206 fmt->value = 10 * fmt->value + c - '0';
212 fmt->value = -fmt->value;
213 token = FMT_SIGNED_INT;
226 fmt->value = c - '0';
230 c = next_char (fmt, 0);
234 fmt->value = 10 * fmt->value + c - '0';
238 token = (fmt->value == 0) ? FMT_ZERO : FMT_POSINT;
262 switch (next_char (fmt, 0))
291 switch (next_char (fmt, 0))
308 switch (next_char (fmt, 0))
328 fmt->string = fmt->format_string;
329 fmt->value = 0; /* This is the length of the string */
333 c = next_char (fmt, 1);
336 token = FMT_BADSTRING;
337 fmt->error = bad_string;
343 c = next_char (fmt, 1);
347 token = FMT_BADSTRING;
348 fmt->error = bad_string;
386 switch (next_char (fmt, 0))
418 switch (next_char (fmt, 0))
446 /* parse_format_list()-- Parse a format list. Assumes that a left
447 * paren has already been seen. Returns a list representing the
448 * parenthesis node which contains the rest of the list. */
451 parse_format_list (st_parameter_dt *dtp)
454 format_token t, u, t2;
456 format_data *fmt = dtp->u.p.fmt;
460 /* Get the next format item */
462 t = format_lex (fmt);
469 t = format_lex (fmt);
473 get_fnode (fmt, &head, &tail, FMT_LPAREN);
474 tail->repeat = repeat;
475 tail->u.child = parse_format_list (dtp);
476 if (fmt->error != NULL)
482 get_fnode (fmt, &head, &tail, FMT_SLASH);
483 tail->repeat = repeat;
487 get_fnode (fmt, &head, &tail, FMT_X);
489 tail->u.k = fmt->value;
500 get_fnode (fmt, &head, &tail, FMT_LPAREN);
502 tail->u.child = parse_format_list (dtp);
503 if (fmt->error != NULL)
508 case FMT_SIGNED_INT: /* Signed integer can only precede a P format. */
509 case FMT_ZERO: /* Same for zero. */
510 t = format_lex (fmt);
513 fmt->error = "Expected P edit descriptor in format";
518 get_fnode (fmt, &head, &tail, FMT_P);
519 tail->u.k = fmt->value;
522 t = format_lex (fmt);
523 if (t == FMT_F || t == FMT_EN || t == FMT_ES || t == FMT_D
524 || t == FMT_G || t == FMT_E)
530 fmt->saved_token = t;
533 case FMT_P: /* P and X require a prior number */
534 fmt->error = "P descriptor requires leading scale factor";
541 If we would be pedantic in the library, we would have to reject
542 an X descriptor without an integer prefix:
544 fmt->error = "X descriptor requires leading space count";
547 However, this is an extension supported by many Fortran compilers,
548 including Cray, HP, AIX, and IRIX. Therefore, we allow it in the
549 runtime library, and make the front end reject it if the compiler
550 is in pedantic mode. The interpretation of 'X' is '1X'.
552 get_fnode (fmt, &head, &tail, FMT_X);
558 get_fnode (fmt, &head, &tail, FMT_STRING);
560 tail->u.string.p = fmt->string;
561 tail->u.string.length = fmt->value;
567 notify_std (&dtp->common, GFC_STD_F2003, "Fortran 2003: DC or DP "
568 "descriptor not allowed");
575 get_fnode (fmt, &head, &tail, t);
580 get_fnode (fmt, &head, &tail, FMT_COLON);
585 get_fnode (fmt, &head, &tail, FMT_SLASH);
591 get_fnode (fmt, &head, &tail, FMT_DOLLAR);
593 notify_std (&dtp->common, GFC_STD_GNU, "Extension: $ descriptor");
600 t2 = format_lex (fmt);
601 if (t2 != FMT_POSINT)
603 fmt->error = posint_required;
606 get_fnode (fmt, &head, &tail, t);
607 tail->u.n = fmt->value;
627 get_fnode (fmt, &head, &tail, FMT_STRING);
629 if (fmt->format_string_len < 1)
631 fmt->error = bad_hollerith;
635 tail->u.string.p = fmt->format_string;
636 tail->u.string.length = 1;
639 fmt->format_string++;
640 fmt->format_string_len--;
645 fmt->error = unexpected_end;
655 fmt->error = unexpected_element;
659 /* In this state, t must currently be a data descriptor. Deal with
660 things that can/must follow the descriptor */
665 t = format_lex (fmt);
668 fmt->error = "Repeat count cannot follow P descriptor";
672 fmt->saved_token = t;
673 get_fnode (fmt, &head, &tail, FMT_P);
678 t = format_lex (fmt);
681 if (notification_std(GFC_STD_GNU) == ERROR)
683 fmt->error = posint_required;
688 fmt->saved_token = t;
689 fmt->value = 1; /* Default width */
690 notify_std (&dtp->common, GFC_STD_GNU, posint_required);
694 get_fnode (fmt, &head, &tail, FMT_L);
695 tail->u.n = fmt->value;
696 tail->repeat = repeat;
700 t = format_lex (fmt);
703 fmt->saved_token = t;
704 fmt->value = -1; /* Width not present */
707 get_fnode (fmt, &head, &tail, FMT_A);
708 tail->repeat = repeat;
709 tail->u.n = fmt->value;
718 get_fnode (fmt, &head, &tail, t);
719 tail->repeat = repeat;
721 u = format_lex (fmt);
722 if (t == FMT_F || dtp->u.p.mode == WRITING)
724 if (u != FMT_POSINT && u != FMT_ZERO)
726 fmt->error = nonneg_required;
734 fmt->error = posint_required;
739 tail->u.real.w = fmt->value;
741 t = format_lex (fmt);
744 /* We treat a missing decimal descriptor as 0. Note: This is only
745 allowed if -std=legacy, otherwise an error occurs. */
746 if (compile_options.warn_std != 0)
748 fmt->error = period_required;
751 fmt->saved_token = t;
756 t = format_lex (fmt);
757 if (t != FMT_ZERO && t != FMT_POSINT)
759 fmt->error = nonneg_required;
763 tail->u.real.d = fmt->value;
765 if (t == FMT_D || t == FMT_F)
770 /* Look for optional exponent */
771 t = format_lex (fmt);
773 fmt->saved_token = t;
776 t = format_lex (fmt);
779 fmt->error = "Positive exponent width required in format";
783 tail->u.real.e = fmt->value;
789 if (repeat > fmt->format_string_len)
791 fmt->error = bad_hollerith;
795 get_fnode (fmt, &head, &tail, FMT_STRING);
797 tail->u.string.p = fmt->format_string;
798 tail->u.string.length = repeat;
801 fmt->format_string += fmt->value;
802 fmt->format_string_len -= repeat;
810 get_fnode (fmt, &head, &tail, t);
811 tail->repeat = repeat;
813 t = format_lex (fmt);
815 if (dtp->u.p.mode == READING)
819 fmt->error = posint_required;
825 if (t != FMT_ZERO && t != FMT_POSINT)
827 fmt->error = nonneg_required;
832 tail->u.integer.w = fmt->value;
833 tail->u.integer.m = -1;
835 t = format_lex (fmt);
838 fmt->saved_token = t;
842 t = format_lex (fmt);
843 if (t != FMT_ZERO && t != FMT_POSINT)
845 fmt->error = nonneg_required;
849 tail->u.integer.m = fmt->value;
852 if (tail->u.integer.w != 0 && tail->u.integer.m > tail->u.integer.w)
854 fmt->error = "Minimum digits exceeds field width";
861 fmt->error = unexpected_element;
865 /* Between a descriptor and what comes next */
867 t = format_lex (fmt);
878 get_fnode (fmt, &head, &tail, t);
883 fmt->error = unexpected_end;
887 /* Assume a missing comma, this is a GNU extension */
891 /* Optional comma is a weird between state where we've just finished
892 reading a colon, slash or P descriptor. */
894 t = format_lex (fmt);
903 default: /* Assume that we have another format item */
904 fmt->saved_token = t;
915 /* format_error()-- Generate an error message for a format statement.
916 * If the node that gives the location of the error is NULL, the error
917 * is assumed to happen at parse time, and the current location of the
920 * We generate a message showing where the problem is. We take extra
921 * care to print only the relevant part of the format if it is longer
922 * than a standard 80 column display. */
925 format_error (st_parameter_dt *dtp, const fnode *f, const char *message)
927 int width, i, j, offset;
928 char *p, buffer[300];
929 format_data *fmt = dtp->u.p.fmt;
932 fmt->format_string = f->source;
934 sprintf (buffer, "%s\n", message);
936 j = fmt->format_string - dtp->format;
938 offset = (j > 60) ? j - 40 : 0;
941 width = dtp->format_len - offset;
946 /* Show the format */
948 p = strchr (buffer, '\0');
950 memcpy (p, dtp->format + offset, width);
955 /* Show where the problem is */
957 for (i = 1; i < j; i++)
963 generate_error (&dtp->common, LIBERROR_FORMAT, buffer);
967 /* parse_format()-- Parse a format string. */
970 parse_format (st_parameter_dt *dtp)
974 dtp->u.p.fmt = fmt = get_mem (sizeof (format_data));
975 fmt->format_string = dtp->format;
976 fmt->format_string_len = dtp->format_len;
979 fmt->saved_token = FMT_NONE;
983 /* Initialize variables used during traversal of the tree */
985 fmt->reversion_ok = 0;
986 fmt->saved_format = NULL;
988 /* Allocate the first format node as the root of the tree */
990 fmt->last = &fmt->array;
991 fmt->last->next = NULL;
992 fmt->avail = &fmt->array.array[0];
994 memset (fmt->avail, 0, sizeof (*fmt->avail));
995 fmt->avail->format = FMT_LPAREN;
996 fmt->avail->repeat = 1;
999 if (format_lex (fmt) == FMT_LPAREN)
1000 fmt->array.array[0].u.child = parse_format_list (dtp);
1002 fmt->error = "Missing initial left parenthesis in format";
1005 format_error (dtp, NULL, fmt->error);
1009 /* revert()-- Do reversion of the format. Control reverts to the left
1010 * parenthesis that matches the rightmost right parenthesis. From our
1011 * tree structure, we are looking for the rightmost parenthesis node
1012 * at the second level, the first level always being a single
1013 * parenthesis node. If this node doesn't exit, we use the top
1017 revert (st_parameter_dt *dtp)
1020 format_data *fmt = dtp->u.p.fmt;
1022 dtp->u.p.reversion_flag = 1;
1026 for (f = fmt->array.array[0].u.child; f; f = f->next)
1027 if (f->format == FMT_LPAREN)
1030 /* If r is NULL because no node was found, the whole tree will be used */
1032 fmt->array.array[0].current = r;
1033 fmt->array.array[0].count = 0;
1037 /* next_format0()-- Get the next format node without worrying about
1038 * reversion. Returns NULL when we hit the end of the list.
1039 * Parenthesis nodes are incremented after the list has been
1040 * exhausted, other nodes are incremented before they are returned. */
1042 static const fnode *
1043 next_format0 (fnode * f)
1050 if (f->format != FMT_LPAREN)
1053 if (f->count <= f->repeat)
1060 /* Deal with a parenthesis node */
1062 for (; f->count < f->repeat; f->count++)
1064 if (f->current == NULL)
1065 f->current = f->u.child;
1067 for (; f->current != NULL; f->current = f->current->next)
1069 r = next_format0 (f->current);
1080 /* next_format()-- Return the next format node. If the format list
1081 * ends up being exhausted, we do reversion. Reversion is only
1082 * allowed if the we've seen a data descriptor since the
1083 * initialization or the last reversion. We return NULL if there
1084 * are no more data descriptors to return (which is an error
1088 next_format (st_parameter_dt *dtp)
1092 format_data *fmt = dtp->u.p.fmt;
1094 if (fmt->saved_format != NULL)
1095 { /* Deal with a pushed-back format node */
1096 f = fmt->saved_format;
1097 fmt->saved_format = NULL;
1101 f = next_format0 (&fmt->array.array[0]);
1104 if (!fmt->reversion_ok)
1107 fmt->reversion_ok = 0;
1110 f = next_format0 (&fmt->array.array[0]);
1113 format_error (dtp, NULL, reversion_error);
1117 /* Push the first reverted token and return a colon node in case
1118 * there are no more data items. */
1120 fmt->saved_format = f;
1124 /* If this is a data edit descriptor, then reversion has become OK. */
1128 if (!fmt->reversion_ok &&
1129 (t == FMT_I || t == FMT_B || t == FMT_O || t == FMT_Z || t == FMT_F ||
1130 t == FMT_E || t == FMT_EN || t == FMT_ES || t == FMT_G || t == FMT_L ||
1131 t == FMT_A || t == FMT_D))
1132 fmt->reversion_ok = 1;
1137 /* unget_format()-- Push the given format back so that it will be
1138 * returned on the next call to next_format() without affecting
1139 * counts. This is necessary when we've encountered a data
1140 * descriptor, but don't know what the data item is yet. The format
1141 * node is pushed back, and we return control to the main program,
1142 * which calls the library back with the data item (or not). */
1145 unget_format (st_parameter_dt *dtp, const fnode *f)
1147 dtp->u.p.fmt->saved_format = f;