2 Copyright (C) 1995 Free Software Foundation, Inc.
3 Contributed by James Craig Burley.
5 This file is part of GNU Fortran.
7 GNU Fortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Fortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Fortran; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 #define MAXNAMELEN 100
30 typedef struct _name_
*name
;
40 char kwname
[MAXNAMELEN
];
41 char name_uc
[MAXNAMELEN
];
42 char name_lc
[MAXNAMELEN
];
43 char name_ic
[MAXNAMELEN
];
62 static char prefix
[32];
63 static char postfix
[32];
64 static char storage
[32];
65 static const char *const xspaces
[]
101 "\t\t\t\t ", /* 33 */
102 "\t\t\t\t ", /* 34 */
103 "\t\t\t\t ", /* 35 */
104 "\t\t\t\t ", /* 36 */
105 "\t\t\t\t ", /* 37 */
106 "\t\t\t\t ", /* 38 */
107 "\t\t\t\t ", /* 39 */
108 "\t\t\t\t\t", /* 40 */
109 "\t\t\t\t\t ", /* 41 */
110 "\t\t\t\t\t ", /* 42 */
111 "\t\t\t\t\t ", /* 43 */
112 "\t\t\t\t\t ", /* 44 */
113 "\t\t\t\t\t ", /* 45 */
114 "\t\t\t\t\t ", /* 46 */
115 "\t\t\t\t\t ", /* 47 */
116 "\t\t\t\t\t\t", /* 48 */
117 "\t\t\t\t\t\t ", /* 49 */
118 "\t\t\t\t\t\t ", /* 50 */
119 "\t\t\t\t\t\t ", /* 51 */
120 "\t\t\t\t\t\t ", /* 52 */
121 "\t\t\t\t\t\t ", /* 53 */
122 "\t\t\t\t\t\t ", /* 54 */
123 "\t\t\t\t\t\t ", /* 55 */
124 "\t\t\t\t\t\t\t", /* 56 */
125 "\t\t\t\t\t\t\t ", /* 57 */
126 "\t\t\t\t\t\t\t ", /* 58 */
127 "\t\t\t\t\t\t\t ", /* 59 */
128 "\t\t\t\t\t\t\t ", /* 60 */
129 "\t\t\t\t\t\t\t ", /* 61 */
130 "\t\t\t\t\t\t\t ", /* 62 */
131 "\t\t\t\t\t\t\t ", /* 63 */
132 "\t\t\t\t\t\t\t\t", /* 64 */
133 "\t\t\t\t\t\t\t\t ", /* 65 */
134 "\t\t\t\t\t\t\t\t ", /* 66 */
135 "\t\t\t\t\t\t\t\t ", /* 67 */
136 "\t\t\t\t\t\t\t\t ", /* 68 */
137 "\t\t\t\t\t\t\t\t ", /* 69 */
138 "\t\t\t\t\t\t\t\t ", /* 70 */
139 "\t\t\t\t\t\t\t\t ", /* 71 */
140 "\t\t\t\t\t\t\t\t\t", /* 72 */
141 "\t\t\t\t\t\t\t\t\t ", /* 73 */
142 "\t\t\t\t\t\t\t\t\t ", /* 74 */
143 "\t\t\t\t\t\t\t\t\t ", /* 75 */
144 "\t\t\t\t\t\t\t\t\t ", /* 76 */
145 "\t\t\t\t\t\t\t\t\t ", /* 77 */
146 "\t\t\t\t\t\t\t\t\t ", /* 78 */
147 "\t\t\t\t\t\t\t\t\t ", /* 79 */
148 "\t\t\t\t\t\t\t\t\t\t", /* 80 */
149 "\t\t\t\t\t\t\t\t\t\t ", /* 81 */
150 "\t\t\t\t\t\t\t\t\t\t ", /* 82 */
151 "\t\t\t\t\t\t\t\t\t\t ", /* 83 */
152 "\t\t\t\t\t\t\t\t\t\t ", /* 84 */
153 "\t\t\t\t\t\t\t\t\t\t ", /* 85 */
154 "\t\t\t\t\t\t\t\t\t\t ", /* 86 */
155 "\t\t\t\t\t\t\t\t\t\t ",/* 87 */
156 "\t\t\t\t\t\t\t\t\t\t\t", /* 88 */
157 "\t\t\t\t\t\t\t\t\t\t\t ", /* 89 */
158 "\t\t\t\t\t\t\t\t\t\t\t ", /* 90 */
159 "\t\t\t\t\t\t\t\t\t\t\t ", /* 91 */
160 "\t\t\t\t\t\t\t\t\t\t\t ", /* 92 */
161 "\t\t\t\t\t\t\t\t\t\t\t ",/* 93 */
162 "\t\t\t\t\t\t\t\t\t\t\t ", /* 94 */
163 "\t\t\t\t\t\t\t\t\t\t\t ", /* 95 */
164 "\t\t\t\t\t\t\t\t\t\t\t\t", /* 96 */
165 "\t\t\t\t\t\t\t\t\t\t\t\t ", /* 97 */
166 "\t\t\t\t\t\t\t\t\t\t\t\t ", /* 98 */
167 "\t\t\t\t\t\t\t\t\t\t\t\t ",/* 99 */
168 "\t\t\t\t\t\t\t\t\t\t\t\t ", /* 100 */
169 "\t\t\t\t\t\t\t\t\t\t\t\t ", /* 101 */
170 "\t\t\t\t\t\t\t\t\t\t\t\t ", /* 102 */
171 "\t\t\t\t\t\t\t\t\t\t\t\t ", /* 103 */
172 "\t\t\t\t\t\t\t\t\t\t\t\t\t", /* 104 */
173 "\t\t\t\t\t\t\t\t\t\t\t\t\t ",/* 105 */
174 "\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 106 */
175 "\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 107 */
176 "\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 108 */
177 "\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 109 */
178 "\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 110 */
179 "\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 111 */
180 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t", /* 112 */
181 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 113 */
182 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 114 */
183 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 115 */
184 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 116 */
185 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 117 */
186 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 118 */
187 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 119 */
188 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t", /* 120 */
189 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 121 */
190 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 122 */
191 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 123 */
192 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 124 */
193 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 125 */
194 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 126 */
195 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 127 */
196 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t", /* 128 */
197 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 129 */
198 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 130 */
199 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 131 */
200 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 132 */
201 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 133 */
202 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 134 */
203 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 135 */
204 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t", /* 136 */
205 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 137 */
206 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 138 */
207 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 139 */
208 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 140 */
209 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 141 */
210 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 142 */
211 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 143 */
212 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t", /* 144 */
213 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 145 */
214 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 146 */
215 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 147 */
216 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 148 */
217 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 149 */
218 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 150 */
219 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 151 */
220 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t", /* 152 */
221 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 153 */
222 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 154 */
223 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 155 */
224 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 156 */
225 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 157 */
226 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 158 */
227 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ", /* 159 */
230 void testname (bool nested
, int indent
, name first
, name last
);
231 void testnames (bool nested
, int indent
, int len
, name first
, name last
);
234 main (int argc
, char **argv
)
236 char buf
[MAXNAMELEN
];
237 char last_buf
[MAXNAMELEN
];
238 char kwname
[MAXNAMELEN
];
244 struct _name_root_ names
[200];
245 struct _name_alpha_ names_alpha
;
254 int do_name
; /* TRUE if token may be NAME. */
255 int do_names
; /* TRUE if token may be NAMES. */
257 bool do_exit
= FALSE
;
261 for (i
= 0; ((size_t) i
) < ARRAY_SIZE (names
); ++i
)
262 { /* Initialize length/name ordered list roots. */
263 names
[i
].first
= (name
) &names
[i
];
264 names
[i
].last
= (name
) &names
[i
];
266 names_alpha
.first
= (name
) &names_alpha
; /* Initialize name order. */
267 names_alpha
.last
= (name
) &names_alpha
;
271 fprintf (stderr
, "Command form: fini input output-code output-include\n");
275 input_name
= argv
[1];
276 output_name
= argv
[2];
277 include_name
= argv
[3];
279 in
= fopen (input_name
, "r");
282 fprintf (stderr
, "Cannot open \"%s\"\n", input_name
);
285 out
= fopen (output_name
, "w");
289 fprintf (stderr
, "Cannot open \"%s\"\n", output_name
);
292 incl
= fopen (include_name
, "w");
296 fprintf (stderr
, "Cannot open \"%s\"\n", include_name
);
300 /* Get past the initial block-style comment (man, this parsing code is just
301 _so_ lame, but I'm too lazy to improve it). */
308 while (((cc
= getc (in
)) != '}') && (cc
!= EOF
))
313 while (((cc
= getc (in
)) != EOF
) && (! ISALNUM (cc
)))
320 assert ("EOF too soon!" == NULL
);
325 fscanf (in
, "%s %s %s %s %s %d %d", prefix
, postfix
, storage
, type
, routine
,
326 &do_name
, &do_names
);
328 if (storage
[0] == '\0')
331 /* Assume string is quoted somehow, replace ending quote with space. */
333 if (storage
[2] == '\0')
336 storage
[strlen (storage
) - 1] = ' ';
339 if (postfix
[0] == '\0')
341 else /* Assume string is quoted somehow, strip off
343 postfix
[strlen (postfix
) - 1] = '\0';
345 for (i
= 1; storage
[i
] != '\0'; ++i
)
346 storage
[i
- 1] = storage
[i
];
347 storage
[i
- 1] = '\0';
349 for (i
= 1; postfix
[i
] != '\0'; ++i
)
350 postfix
[i
- 1] = postfix
[i
];
351 postfix
[i
- 1] = '\0';
353 fixlengths
= strlen (prefix
) + strlen (postfix
);
357 count
= fscanf (in
, "%s %s", buf
, kwname
);
362 continue; /* Skip empty lines. */
364 continue; /* Skip commented-out lines. */
365 for (i
= strlen (buf
) - 1; i
> 0; --i
)
368 /* Make new name object to store name and its keyword. */
370 newname
= (name
) xmalloc (sizeof (*newname
));
371 newname
->namelen
= strlen (buf
);
372 newname
->kwlen
= strlen (kwname
);
373 total_length
= newname
->kwlen
+ fixlengths
;
374 if (total_length
>= 32) /* Else resulting keyword name too long. */
376 fprintf (stderr
, "%s: %s%s%s is 31+%d chars long\n", input_name
,
377 prefix
, kwname
, postfix
, total_length
- 31);
380 strcpy (newname
->kwname
, kwname
);
381 for (i
= 0; i
< newname
->namelen
; ++i
)
384 newname
->name_uc
[i
] = TOUPPER (cc
);
385 newname
->name_lc
[i
] = TOLOWER (cc
);
386 newname
->name_ic
[i
] = cc
;
388 newname
->name_uc
[i
] = newname
->name_lc
[i
] = newname
->name_ic
[i
] = '\0';
390 /* Warn user if names aren't alphabetically ordered. */
392 if ((last_buf
[0] != '\0')
393 && (strcmp (last_buf
, newname
->name_uc
) >= 0))
395 fprintf (stderr
, "%s: \"%s\" precedes \"%s\"\n", input_name
,
396 last_buf
, newname
->name_uc
);
399 strcpy (last_buf
, newname
->name_uc
);
401 /* Append name to end of alpha-sorted list (assumes names entered in
402 alpha order wrt name, not kwname, even though kwname is output from
405 n
= names_alpha
.last
;
406 newname
->next_alpha
= n
->next_alpha
;
407 newname
->previous_alpha
= n
;
408 n
->next_alpha
->previous_alpha
= newname
;
409 n
->next_alpha
= newname
;
411 /* Insert name in appropriate length/name ordered list. */
413 n
= (name
) &names
[len
];
414 while ((n
->next
!= (name
) &names
[len
])
415 && (strcmp (buf
, n
->next
->name_uc
) > 0))
417 if (strcmp (buf
, n
->next
->name_uc
) == 0)
419 fprintf (stderr
, "%s: extraneous \"%s\"\n", input_name
, buf
);
422 newname
->next
= n
->next
;
423 newname
->previous
= n
;
424 n
->next
->previous
= newname
;
429 for (len
= 0; len
< ARRAY_SIZE (name
); ++len
)
431 if (names
[len
].first
== (name
) &names
[len
])
433 printf ("Length %d:\n", len
);
434 for (n
= names
[len
].first
; n
!= (name
) &names
[len
]; n
= n
->next
)
435 printf (" %s %s %s\n", n
->name_uc
, n
->name_lc
, n
->name_ic
);
442 /* First output the #include file. */
444 for (n
= names_alpha
.first
; n
!= (name
) &names_alpha
; n
= n
->next_alpha
)
446 fprintf (incl
, "#define %sl%s%s %d\n", prefix
, n
->kwname
, postfix
,
457 type
, prefix
, postfix
);
459 for (n
= names_alpha
.first
; n
!= (name
) &names_alpha
; n
= n
->next_alpha
)
465 prefix
, n
->kwname
, postfix
);
472 typedef enum %s_ %s;\n\
474 prefix
, postfix
, type
, type
);
476 /* Now output the C program. */
481 %s (ffelexToken t)\n\
486 p = ffelex_token_text (t);\n\
489 storage
, type
, routine
, '{');
496 if (ffelex_token_type (t) == FFELEX_typeNAME)\n\
498 switch (ffelex_token_length (t))\n\
505 assert (ffelex_token_type (t) == FFELEX_typeNAME);\n\
507 switch (ffelex_token_length (t))\n\
512 /* Now output the length as a case, followed by the binary search within that length. */
514 for (len
= 0; ((size_t) len
) < ARRAY_SIZE (names
); ++len
)
516 if (names
[len
].first
!= (name
) &names
[len
])
530 testname (FALSE
, do_names
? 10 : 6, names
[len
].first
, names
[len
].last
);
569 assert (ffelex_token_type (t) == FFELEX_typeNAMES);\n\
571 switch (ffelex_token_length (t))\n\
577 /* Find greatest non-empty length list. */
579 for (len
= ARRAY_SIZE (names
) - 1;
580 names
[len
].first
== (name
) &names
[len
];
584 /* Now output the length as a case, followed by the binary search within that length. */
588 for (; len
!= 0; --len
)
595 if (names
[len
].first
!= (name
) &names
[len
])
596 testnames (FALSE
, 6, len
, names
[len
].first
, names
[len
].last
);
598 if (names
[1].first
== (name
) &names
[1])
603 ); /* Need empty statement after an empty case
627 testname (bool nested
, int indent
, name first
, name last
)
634 assert (!nested
|| indent
>= 2);
635 assert (((size_t) indent
) + 4 < ARRAY_SIZE (xspaces
));
639 for (n
= first
, nhalf
= first
; n
!= last
->next
; n
= n
->next
)
641 if ((++num
& 1) == 0)
653 xspaces
[indent
- 2]);
657 %sif ((c = ffesrc_strcmp_2c (ffe_case_match (), p, \"%s\", \"%s\", \"%s\")) == 0)\n\
660 xspaces
[indent
], nhalf
->name_uc
, nhalf
->name_lc
, nhalf
->name_ic
,
661 xspaces
[indent
+ 2], prefix
, nhalf
->kwname
, postfix
);
676 xspaces
[indent
+ 2]);
678 testname (TRUE
, indent
+ 4, first
, nhalf
->previous
);
680 if (num
- numhalf
> 1)
688 testname (TRUE
, indent
+ 4, nhalf
->next
, last
);
697 xspaces
[indent
- 2]);
701 testnames (bool nested
, int indent
, int len
, name first
, name last
)
708 assert (!nested
|| indent
>= 2);
709 assert (((size_t) indent
) + 4 < ARRAY_SIZE (xspaces
));
713 for (n
= first
, nhalf
= first
; n
!= last
->next
; n
= n
->next
)
715 if ((++num
& 1) == 0)
727 xspaces
[indent
- 2]);
731 %sif ((c = ffesrc_strncmp_2c (ffe_case_match (), p, \"%s\", \"%s\", \"%s\", %d)) == 0)\n\
734 xspaces
[indent
], nhalf
->name_uc
, nhalf
->name_lc
, nhalf
->name_ic
,
735 len
, xspaces
[indent
+ 2], prefix
, nhalf
->kwname
, postfix
);
750 xspaces
[indent
+ 2]);
752 testnames (TRUE
, indent
+ 4, len
, first
, nhalf
->previous
);
754 if (num
- numhalf
> 1)
762 testnames (TRUE
, indent
+ 4, len
, nhalf
->next
, last
);
771 xspaces
[indent
- 2]);