1 /* Copyright (C) 2002-2003 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Libgfortran; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* format.c-- parse a FORMAT string into a binary format suitable for
23 * interpretation during I/O statements */
28 #include "libgfortran.h"
33 /* Number of format nodes that we can store statically before we have
34 * to resort to dynamic allocation. The root node is array[0]. */
36 #define FARRAY_SIZE 200
38 static fnode *avail, array[FARRAY_SIZE];
40 /* Local variables for checking format strings. The saved_token is
41 * used to back up by a single format token during the parsing process. */
43 static char *format_string, *string;
44 static const char *error;
45 static format_token saved_token;
46 static int value, format_string_len, reversion_ok;
48 static fnode *saved_format, colon_node = { FMT_COLON };
52 static char posint_required[] = "Positive width required in format",
53 period_required[] = "Period required in format",
54 nonneg_required[] = "Nonnegative width required in format",
55 unexpected_element[] = "Unexpected element in format",
56 unexpected_end[] = "Unexpected end of format string",
57 bad_string[] = "Unterminated character constant in format",
58 bad_hollerith[] = "Hollerith constant extends past the end of the format",
59 reversion_error[] = "Exhausted data descriptors in format";
62 /* next_char()-- Return the next character in the format string.
63 * Returns -1 when the string is done. If the literal flag is set,
64 * spaces are significant, otherwise they are not. */
67 next_char (int literal)
73 if (format_string_len == 0)
77 c = toupper (*format_string++);
79 while (c == ' ' && !literal);
85 /* unget_char()-- Back up one character position. */
87 #define unget_char() { format_string--; format_string_len++; }
90 /* get_fnode()-- Allocate a new format node, inserting it into the
91 * current singly linked list. These are initially allocated from the
95 get_fnode (fnode ** head, fnode ** tail, format_token t)
99 if (avail - array >= FARRAY_SIZE)
100 f = get_mem (sizeof (fnode));
104 memset (f, '\0', sizeof (fnode));
117 f->source = format_string;
122 /* free_fnode()-- Recursive function to free the given fnode and
123 * everything it points to. We only have to actually free something
124 * if it is outside of the static array. */
127 free_fnode (fnode * f)
135 if (f->format == FMT_LPAREN)
136 free_fnode (f->u.child);
137 if (f < array || f >= array + FARRAY_SIZE)
143 /* free_fnodes()-- Free the current tree of fnodes. We only have to
144 * traverse the tree if some nodes were allocated dynamically. */
150 if (avail - array >= FARRAY_SIZE)
151 free_fnode (&array[0]);
154 memset(array, 0, sizeof(avail[0]) * FARRAY_SIZE);
158 /* format_lex()-- Simple lexical analyzer for getting the next token
159 * in a FORMAT string. We support a one-level token pushback in the
160 * saved_token variable. */
169 if (saved_token != FMT_NONE)
172 saved_token = FMT_NONE;
201 value = 10 * value + c - '0';
208 token = FMT_SIGNED_INT;
229 value = 10 * value + c - '0';
233 token = (value == 0) ? FMT_ZERO : FMT_POSINT;
257 switch (next_char (0))
286 switch (next_char (0))
303 switch (next_char (0))
323 string = format_string;
324 value = 0; /* This is the length of the string */
331 token = FMT_BADSTRING;
342 token = FMT_BADSTRING;
381 switch (next_char (0))
430 /* parse_format_list()-- Parse a format list. Assumes that a left
431 * paren has already been seen. Returns a list representing the
432 * parenthesis node which contains the rest of the list. */
435 parse_format_list (void)
438 format_token t, u, t2;
443 /* Get the next format item */
456 get_fnode (&head, &tail, FMT_LPAREN);
457 tail->repeat = repeat;
458 tail->u.child = parse_format_list ();
465 get_fnode (&head, &tail, FMT_SLASH);
466 tail->repeat = repeat;
470 get_fnode (&head, &tail, FMT_X);
483 get_fnode (&head, &tail, FMT_LPAREN);
485 tail->u.child = parse_format_list ();
491 case FMT_SIGNED_INT: /* Signed integer can only precede a P format. */
492 case FMT_ZERO: /* Same for zero. */
496 error = "Expected P edit descriptor in format";
501 get_fnode (&head, &tail, FMT_P);
505 if (t == FMT_F || t == FMT_EN || t == FMT_ES || t == FMT_D
506 || t == FMT_G || t == FMT_E)
515 case FMT_P: /* P and X require a prior number */
516 error = "P descriptor requires leading scale factor";
523 If we would be pedantic in the library, we would have to reject
524 an X descriptor without an integer prefix:
526 error = "X descriptor requires leading space count";
529 However, this is an extension supported by many Fortran compilers,
530 including Cray, HP, AIX, and IRIX. Therefore, we allow it in the
531 runtime library, and make the front end reject it if the compiler
532 is in pedantic mode. The interpretation of 'X' is '1X'.
534 get_fnode (&head, &tail, FMT_X);
540 get_fnode (&head, &tail, FMT_STRING);
542 tail->u.string.p = string;
543 tail->u.string.length = value;
552 get_fnode (&head, &tail, t);
556 get_fnode (&head, &tail, FMT_COLON);
560 get_fnode (&head, &tail, FMT_SLASH);
566 get_fnode (&head, &tail, FMT_DOLLAR);
573 if (t2 != FMT_POSINT)
575 error = posint_required;
578 get_fnode (&head, &tail, t);
599 get_fnode (&head, &tail, FMT_STRING);
601 if (format_string_len < 1)
603 error = bad_hollerith;
607 tail->u.string.p = format_string;
608 tail->u.string.length = 1;
617 error = unexpected_end;
627 error = unexpected_element;
631 /* In this state, t must currently be a data descriptor. Deal with
632 * things that can/must follow the descriptor */
641 error = "Repeat count cannot follow P descriptor";
646 get_fnode (&head, &tail, FMT_P);
654 error = posint_required;
658 get_fnode (&head, &tail, FMT_L);
660 tail->repeat = repeat;
668 value = -1; /* Width not present */
671 get_fnode (&head, &tail, FMT_A);
672 tail->repeat = repeat;
682 get_fnode (&head, &tail, t);
683 tail->repeat = repeat;
686 if (t == FMT_F || g.mode == WRITING)
688 if (u != FMT_POSINT && u != FMT_ZERO)
690 error = nonneg_required;
698 error = posint_required;
703 tail->u.real.w = value;
708 error = period_required;
713 if (t != FMT_ZERO && t != FMT_POSINT)
715 error = nonneg_required;
719 tail->u.real.d = value;
721 if (t == FMT_D || t == FMT_F)
726 /* Look for optional exponent */
736 error = "Positive exponent width required in format";
740 tail->u.real.e = value;
746 if (repeat > format_string_len)
748 error = bad_hollerith;
752 get_fnode (&head, &tail, FMT_STRING);
754 tail->u.string.p = format_string;
755 tail->u.string.length = repeat;
758 format_string += value;
759 format_string_len -= repeat;
767 get_fnode (&head, &tail, t);
768 tail->repeat = repeat;
772 if (g.mode == READING)
776 error = posint_required;
782 if (t != FMT_ZERO && t != FMT_POSINT)
784 error = nonneg_required;
789 tail->u.integer.w = value;
790 tail->u.integer.m = -1;
800 if (t != FMT_ZERO && t != FMT_POSINT)
802 error = nonneg_required;
806 tail->u.integer.m = value;
809 if (tail->u.integer.w != 0 && tail->u.integer.m > tail->u.integer.w)
811 error = "Minimum digits exceeds field width";
818 error = unexpected_element;
822 /* Between a descriptor and what comes next */
834 get_fnode (&head, &tail, FMT_SLASH);
843 error = unexpected_end;
847 error = "Missing comma in format";
851 /* Optional comma is a weird between state where we've just finished
852 * reading a colon, slash or P descriptor. */
864 default: /* Assume that we have another format item */
876 /* format_error()-- Generate an error message for a format statement.
877 * If the node that gives the location of the error is NULL, the error
878 * is assumed to happen at parse time, and the current location of the
881 * After freeing any dynamically allocated fnodes, generate a message
882 * showing where the problem is. We take extra care to print only the
883 * relevant part of the format if it is longer than a standard 80
887 format_error (fnode * f, const char *message)
889 int width, i, j, offset;
890 char *p, buffer[300];
893 format_string = f->source;
897 st_sprintf (buffer, "%s\n", message);
899 j = format_string - ioparm.format;
901 offset = (j > 60) ? j - 40 : 0;
904 width = ioparm.format_len - offset;
909 /* Show the format */
911 p = strchr (buffer, '\0');
913 memcpy (p, ioparm.format + offset, width);
918 /* Show where the problem is */
920 for (i = 1; i < j; i++)
926 generate_error (ERROR_FORMAT, buffer);
930 /* parse_format()-- Parse a format string. */
936 format_string = ioparm.format;
937 format_string_len = ioparm.format_len;
939 saved_token = FMT_NONE;
942 /* Initialize variables used during traversal of the tree */
945 g.reversion_flag = 0;
948 /* Allocate the first format node as the root of the tree */
952 avail->format = FMT_LPAREN;
956 if (format_lex () == FMT_LPAREN)
957 array[0].u.child = parse_format_list ();
959 error = "Missing initial left parenthesis in format";
962 format_error (NULL, error);
966 /* revert()-- Do reversion of the format. Control reverts to the left
967 * parenthesis that matches the rightmost right parenthesis. From our
968 * tree structure, we are looking for the rightmost parenthesis node
969 * at the second level, the first level always being a single
970 * parenthesis node. If this node doesn't exit, we use the top
978 g.reversion_flag = 1;
982 for (f = array[0].u.child; f; f = f->next)
983 if (f->format == FMT_LPAREN)
986 /* If r is NULL because no node was found, the whole tree will be used */
988 array[0].current = r;
993 /* next_format0()-- Get the next format node without worrying about
994 * reversion. Returns NULL when we hit the end of the list.
995 * Parenthesis nodes are incremented after the list has been
996 * exhausted, other nodes are incremented before they are returned. */
999 next_format0 (fnode * f)
1006 if (f->format != FMT_LPAREN)
1009 if (f->count <= f->repeat)
1016 /* Deal with a parenthesis node */
1018 for (; f->count < f->repeat; f->count++)
1020 if (f->current == NULL)
1021 f->current = f->u.child;
1023 for (; f->current != NULL; f->current = f->current->next)
1025 r = next_format0 (f->current);
1036 /* next_format()-- Return the next format node. If the format list
1037 * ends up being exhausted, we do reversion. Reversion is only
1038 * allowed if the we've seen a data descriptor since the
1039 * initialization or the last reversion. We return NULL if the there
1040 * are no more data descriptors to return (which is an error
1049 if (saved_format != NULL)
1050 { /* Deal with a pushed-back format node */
1052 saved_format = NULL;
1056 f = next_format0 (&array[0]);
1067 f = next_format0 (&array[0]);
1070 format_error (NULL, reversion_error);
1074 /* Push the first reverted token and return a colon node in case
1075 * there are no more data items. */
1081 /* If this is a data edit descriptor, then reversion has become OK. */
1086 if (!reversion_ok &&
1087 (t == FMT_I || t == FMT_B || t == FMT_O || t == FMT_Z || t == FMT_F ||
1088 t == FMT_E || t == FMT_EN || t == FMT_ES || t == FMT_G || t == FMT_L ||
1089 t == FMT_A || t == FMT_D))
1095 /* unget_format()-- Push the given format back so that it will be
1096 * returned on the next call to next_format() without affecting
1097 * counts. This is necessary when we've encountered a data
1098 * descriptor, but don't know what the data item is yet. The format
1099 * node is pushed back, and we return control to the main program,
1100 * which calls the library back with the data item (or not). */
1103 unget_format (fnode * f)
1114 static void dump_format1 (fnode * f);
1116 /* dump_format0()-- Dump a single format node */
1119 dump_format0 (fnode * f)
1130 st_printf (" %d/", f->u.r);
1136 st_printf (" T%d", f->u.n);
1139 st_printf (" TR%d", f->u.n);
1142 st_printf (" TL%d", f->u.n);
1145 st_printf (" %dX", f->u.n);
1161 st_printf (" %d(", f->repeat);
1163 dump_format1 (f->u.child);
1170 for (i = f->u.string.length; i > 0; i--)
1171 st_printf ("%c", *p++);
1177 st_printf (" %dP", f->u.k);
1180 st_printf (" %dI%d.%d", f->repeat, f->u.integer.w, f->u.integer.m);
1184 st_printf (" %dB%d.%d", f->repeat, f->u.integer.w, f->u.integer.m);
1188 st_printf (" %dO%d.%d", f->repeat, f->u.integer.w, f->u.integer.m);
1192 st_printf (" %dZ%d.%d", f->repeat, f->u.integer.w, f->u.integer.m);
1202 st_printf (" %dD%d.%d", f->repeat, f->u.real.w, f->u.real.d);
1206 st_printf (" %dEN%d.%dE%d", f->repeat, f->u.real.w, f->u.real.d,
1211 st_printf (" %dES%d.%dE%d", f->repeat, f->u.real.w, f->u.real.d,
1216 st_printf (" %dF%d.%d", f->repeat, f->u.real.w, f->u.real.d);
1220 st_printf (" %dE%d.%dE%d", f->repeat, f->u.real.w, f->u.real.d,
1225 st_printf (" %dG%d.%dE%d", f->repeat, f->u.real.w, f->u.real.d,
1230 st_printf (" %dL%d", f->repeat, f->u.w);
1233 st_printf (" %dA%d", f->repeat, f->u.w);
1243 /* dump_format1()-- Dump a string of format nodes */
1246 dump_format1 (fnode * f)
1249 for (; f; f = f->next)
1253 /* dump_format()-- Dump the whole format node tree */
1259 st_printf ("format = ");
1260 dump_format0 (&array[0]);
1271 for (i = 0; i < 20; i++)
1276 st_printf ("No format!\n");