1 /* Copyright (C) 2002, 2003, 2004 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 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with Libgfortran; see the file COPYING. If not, write to
27 the Free Software Foundation, 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
31 /* format.c-- parse a FORMAT string into a binary format suitable for
32 * interpretation during I/O statements */
37 #include "libgfortran.h"
42 /* Number of format nodes that we can store statically before we have
43 * to resort to dynamic allocation. The root node is array[0]. */
45 #define FARRAY_SIZE 200
47 static fnode *avail, array[FARRAY_SIZE];
49 /* Local variables for checking format strings. The saved_token is
50 * used to back up by a single format token during the parsing process. */
52 static char *format_string, *string;
53 static const char *error;
54 static format_token saved_token;
55 static int value, format_string_len, reversion_ok;
57 static fnode *saved_format, colon_node = { FMT_COLON };
61 static char posint_required[] = "Positive width required in format",
62 period_required[] = "Period required in format",
63 nonneg_required[] = "Nonnegative width required in format",
64 unexpected_element[] = "Unexpected element in format",
65 unexpected_end[] = "Unexpected end of format string",
66 bad_string[] = "Unterminated character constant in format",
67 bad_hollerith[] = "Hollerith constant extends past the end of the format",
68 reversion_error[] = "Exhausted data descriptors in format";
71 /* next_char()-- Return the next character in the format string.
72 * Returns -1 when the string is done. If the literal flag is set,
73 * spaces are significant, otherwise they are not. */
76 next_char (int literal)
82 if (format_string_len == 0)
86 c = toupper (*format_string++);
88 while (c == ' ' && !literal);
94 /* unget_char()-- Back up one character position. */
96 #define unget_char() { format_string--; format_string_len++; }
99 /* get_fnode()-- Allocate a new format node, inserting it into the
100 * current singly linked list. These are initially allocated from the
104 get_fnode (fnode ** head, fnode ** tail, format_token t)
108 if (avail - array >= FARRAY_SIZE)
109 f = get_mem (sizeof (fnode));
113 memset (f, '\0', sizeof (fnode));
126 f->source = format_string;
131 /* free_fnode()-- Recursive function to free the given fnode and
132 * everything it points to. We only have to actually free something
133 * if it is outside of the static array. */
136 free_fnode (fnode * f)
144 if (f->format == FMT_LPAREN)
145 free_fnode (f->u.child);
146 if (f < array || f >= array + FARRAY_SIZE)
152 /* free_fnodes()-- Free the current tree of fnodes. We only have to
153 * traverse the tree if some nodes were allocated dynamically. */
158 if (avail - array >= FARRAY_SIZE)
159 free_fnode (&array[0]);
162 memset(array, 0, sizeof(avail[0]) * FARRAY_SIZE);
166 /* format_lex()-- Simple lexical analyzer for getting the next token
167 * in a FORMAT string. We support a one-level token pushback in the
168 * saved_token variable. */
178 if (saved_token != FMT_NONE)
181 saved_token = FMT_NONE;
210 value = 10 * value + c - '0';
217 token = FMT_SIGNED_INT;
238 value = 10 * value + c - '0';
242 token = (value == 0) ? FMT_ZERO : FMT_POSINT;
266 switch (next_char (0))
295 switch (next_char (0))
312 switch (next_char (0))
332 string = format_string;
333 value = 0; /* This is the length of the string */
340 token = FMT_BADSTRING;
351 token = FMT_BADSTRING;
390 switch (next_char (0))
439 /* parse_format_list()-- Parse a format list. Assumes that a left
440 * paren has already been seen. Returns a list representing the
441 * parenthesis node which contains the rest of the list. */
444 parse_format_list (void)
447 format_token t, u, t2;
452 /* Get the next format item */
464 get_fnode (&head, &tail, FMT_LPAREN);
465 tail->repeat = repeat;
466 tail->u.child = parse_format_list ();
473 get_fnode (&head, &tail, FMT_SLASH);
474 tail->repeat = repeat;
478 get_fnode (&head, &tail, FMT_X);
491 get_fnode (&head, &tail, FMT_LPAREN);
493 tail->u.child = parse_format_list ();
499 case FMT_SIGNED_INT: /* Signed integer can only precede a P format. */
500 case FMT_ZERO: /* Same for zero. */
504 error = "Expected P edit descriptor in format";
509 get_fnode (&head, &tail, FMT_P);
514 if (t == FMT_F || t == FMT_EN || t == FMT_ES || t == FMT_D
515 || t == FMT_G || t == FMT_E)
524 case FMT_P: /* P and X require a prior number */
525 error = "P descriptor requires leading scale factor";
532 If we would be pedantic in the library, we would have to reject
533 an X descriptor without an integer prefix:
535 error = "X descriptor requires leading space count";
538 However, this is an extension supported by many Fortran compilers,
539 including Cray, HP, AIX, and IRIX. Therefore, we allow it in the
540 runtime library, and make the front end reject it if the compiler
541 is in pedantic mode. The interpretation of 'X' is '1X'.
543 get_fnode (&head, &tail, FMT_X);
549 get_fnode (&head, &tail, FMT_STRING);
551 tail->u.string.p = string;
552 tail->u.string.length = value;
561 get_fnode (&head, &tail, t);
566 get_fnode (&head, &tail, FMT_COLON);
570 get_fnode (&head, &tail, FMT_SLASH);
576 get_fnode (&head, &tail, FMT_DOLLAR);
583 if (t2 != FMT_POSINT)
585 error = posint_required;
588 get_fnode (&head, &tail, t);
609 get_fnode (&head, &tail, FMT_STRING);
611 if (format_string_len < 1)
613 error = bad_hollerith;
617 tail->u.string.p = format_string;
618 tail->u.string.length = 1;
627 error = unexpected_end;
637 error = unexpected_element;
641 /* In this state, t must currently be a data descriptor. Deal with
642 things that can/must follow the descriptor */
650 error = "Repeat count cannot follow P descriptor";
655 get_fnode (&head, &tail, FMT_P);
663 error = posint_required;
667 get_fnode (&head, &tail, FMT_L);
669 tail->repeat = repeat;
677 value = -1; /* Width not present */
680 get_fnode (&head, &tail, FMT_A);
681 tail->repeat = repeat;
691 get_fnode (&head, &tail, t);
692 tail->repeat = repeat;
695 if (t == FMT_F || g.mode == WRITING)
697 if (u != FMT_POSINT && u != FMT_ZERO)
699 error = nonneg_required;
707 error = posint_required;
712 tail->u.real.w = value;
717 error = period_required;
722 if (t != FMT_ZERO && t != FMT_POSINT)
724 error = nonneg_required;
728 tail->u.real.d = value;
730 if (t == FMT_D || t == FMT_F)
735 /* Look for optional exponent */
744 error = "Positive exponent width required in format";
748 tail->u.real.e = value;
754 if (repeat > format_string_len)
756 error = bad_hollerith;
760 get_fnode (&head, &tail, FMT_STRING);
762 tail->u.string.p = format_string;
763 tail->u.string.length = repeat;
766 format_string += value;
767 format_string_len -= repeat;
775 get_fnode (&head, &tail, t);
776 tail->repeat = repeat;
780 if (g.mode == READING)
784 error = posint_required;
790 if (t != FMT_ZERO && t != FMT_POSINT)
792 error = nonneg_required;
797 tail->u.integer.w = value;
798 tail->u.integer.m = -1;
808 if (t != FMT_ZERO && t != FMT_POSINT)
810 error = nonneg_required;
814 tail->u.integer.m = value;
817 if (tail->u.integer.w != 0 && tail->u.integer.m > tail->u.integer.w)
819 error = "Minimum digits exceeds field width";
826 error = unexpected_element;
830 /* Between a descriptor and what comes next */
842 get_fnode (&head, &tail, FMT_SLASH);
851 error = unexpected_end;
855 error = "Missing comma in format";
859 /* Optional comma is a weird between state where we've just finished
860 reading a colon, slash or P descriptor. */
871 default: /* Assume that we have another format item */
883 /* format_error()-- Generate an error message for a format statement.
884 * If the node that gives the location of the error is NULL, the error
885 * is assumed to happen at parse time, and the current location of the
888 * After freeing any dynamically allocated fnodes, generate a message
889 * showing where the problem is. We take extra care to print only the
890 * relevant part of the format if it is longer than a standard 80
894 format_error (fnode * f, const char *message)
896 int width, i, j, offset;
897 char *p, buffer[300];
900 format_string = f->source;
904 st_sprintf (buffer, "%s\n", message);
906 j = format_string - ioparm.format;
908 offset = (j > 60) ? j - 40 : 0;
911 width = ioparm.format_len - offset;
916 /* Show the format */
918 p = strchr (buffer, '\0');
920 memcpy (p, ioparm.format + offset, width);
925 /* Show where the problem is */
927 for (i = 1; i < j; i++)
933 generate_error (ERROR_FORMAT, buffer);
937 /* parse_format()-- Parse a format string. */
942 format_string = ioparm.format;
943 format_string_len = ioparm.format_len;
945 saved_token = FMT_NONE;
948 /* Initialize variables used during traversal of the tree */
951 g.reversion_flag = 0;
954 /* Allocate the first format node as the root of the tree */
958 avail->format = FMT_LPAREN;
962 if (format_lex () == FMT_LPAREN)
963 array[0].u.child = parse_format_list ();
965 error = "Missing initial left parenthesis in format";
968 format_error (NULL, error);
972 /* revert()-- Do reversion of the format. Control reverts to the left
973 * parenthesis that matches the rightmost right parenthesis. From our
974 * tree structure, we are looking for the rightmost parenthesis node
975 * at the second level, the first level always being a single
976 * parenthesis node. If this node doesn't exit, we use the top
984 g.reversion_flag = 1;
988 for (f = array[0].u.child; f; f = f->next)
989 if (f->format == FMT_LPAREN)
992 /* If r is NULL because no node was found, the whole tree will be used */
994 array[0].current = r;
999 /* next_format0()-- Get the next format node without worrying about
1000 * reversion. Returns NULL when we hit the end of the list.
1001 * Parenthesis nodes are incremented after the list has been
1002 * exhausted, other nodes are incremented before they are returned. */
1005 next_format0 (fnode * f)
1012 if (f->format != FMT_LPAREN)
1015 if (f->count <= f->repeat)
1022 /* Deal with a parenthesis node */
1024 for (; f->count < f->repeat; f->count++)
1026 if (f->current == NULL)
1027 f->current = f->u.child;
1029 for (; f->current != NULL; f->current = f->current->next)
1031 r = next_format0 (f->current);
1042 /* next_format()-- Return the next format node. If the format list
1043 * ends up being exhausted, we do reversion. Reversion is only
1044 * allowed if the we've seen a data descriptor since the
1045 * initialization or the last reversion. We return NULL if the there
1046 * are no more data descriptors to return (which is an error
1055 if (saved_format != NULL)
1056 { /* Deal with a pushed-back format node */
1058 saved_format = NULL;
1062 f = next_format0 (&array[0]);
1073 f = next_format0 (&array[0]);
1076 format_error (NULL, reversion_error);
1080 /* Push the first reverted token and return a colon node in case
1081 * there are no more data items. */
1087 /* If this is a data edit descriptor, then reversion has become OK. */
1091 if (!reversion_ok &&
1092 (t == FMT_I || t == FMT_B || t == FMT_O || t == FMT_Z || t == FMT_F ||
1093 t == FMT_E || t == FMT_EN || t == FMT_ES || t == FMT_G || t == FMT_L ||
1094 t == FMT_A || t == FMT_D))
1100 /* unget_format()-- Push the given format back so that it will be
1101 * returned on the next call to next_format() without affecting
1102 * counts. This is necessary when we've encountered a data
1103 * descriptor, but don't know what the data item is yet. The format
1104 * node is pushed back, and we return control to the main program,
1105 * which calls the library back with the data item (or not). */
1108 unget_format (fnode * f)
1118 static void dump_format1 (fnode * f);
1120 /* dump_format0()-- Dump a single format node */
1123 dump_format0 (fnode * f)
1134 st_printf (" %d/", f->u.r);
1140 st_printf (" T%d", f->u.n);
1143 st_printf (" TR%d", f->u.n);
1146 st_printf (" TL%d", f->u.n);
1149 st_printf (" %dX", f->u.n);
1165 st_printf (" %d(", f->repeat);
1167 dump_format1 (f->u.child);
1174 for (i = f->u.string.length; i > 0; i--)
1175 st_printf ("%c", *p++);
1181 st_printf (" %dP", f->u.k);
1184 st_printf (" %dI%d.%d", f->repeat, f->u.integer.w, f->u.integer.m);
1188 st_printf (" %dB%d.%d", f->repeat, f->u.integer.w, f->u.integer.m);
1192 st_printf (" %dO%d.%d", f->repeat, f->u.integer.w, f->u.integer.m);
1196 st_printf (" %dZ%d.%d", f->repeat, f->u.integer.w, f->u.integer.m);
1206 st_printf (" %dD%d.%d", f->repeat, f->u.real.w, f->u.real.d);
1210 st_printf (" %dEN%d.%dE%d", f->repeat, f->u.real.w, f->u.real.d,
1215 st_printf (" %dES%d.%dE%d", f->repeat, f->u.real.w, f->u.real.d,
1220 st_printf (" %dF%d.%d", f->repeat, f->u.real.w, f->u.real.d);
1224 st_printf (" %dE%d.%dE%d", f->repeat, f->u.real.w, f->u.real.d,
1229 st_printf (" %dG%d.%dE%d", f->repeat, f->u.real.w, f->u.real.d,
1234 st_printf (" %dL%d", f->repeat, f->u.w);
1237 st_printf (" %dA%d", f->repeat, f->u.w);
1247 /* dump_format1()-- Dump a string of format nodes */
1250 dump_format1 (fnode * f)
1252 for (; f; f = f->next)
1256 /* dump_format()-- Dump the whole format node tree */
1261 st_printf ("format = ");
1262 dump_format0 (&array[0]);
1273 for (i = 0; i < 20; i++)
1278 st_printf ("No format!\n");