1 /* src.c -- Implementation File
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
25 Source-file functions to handle various combinations of case sensitivity
26 and insensitivity at run time.
35 /* This array does a toupper (), but any valid char type is valid as an
36 index and returns identity if not a lower-case character. */
38 char ffesrc_toupper_
[256];
40 /* This array does a tolower (), but any valid char type is valid as an
41 index and returns identity if not an upper-case character. */
43 char ffesrc_tolower_
[256];
45 /* This array is set up so that, given a source-mapped character, the result
46 of indexing into this array will match an upper-cased character depending
47 on the source-mapped character's case and the established ffe_case_match()
48 setting. So the uppercase cells contain identies (e.g. ['A'] == 'A')
49 as long as uppercase matching is permitted (!FFE_caseLOWER) and the
50 lowercase cells contain uppercased identities (e.g. ['a'] == 'A') as long
51 as lowercase matching is permitted (!FFE_caseUPPER). Else the case
52 cells contain -1. _init_ is for the first character of a keyword,
53 and _noninit_ is for other characters. */
55 char ffesrc_char_match_init_
[256];
56 char ffesrc_char_match_noninit_
[256];
58 /* This array is used to map input source according to the established
59 ffe_case_source() setting: for FFE_caseNONE, the array is all
60 identities; for FFE_caseUPPER, the lowercase cells contain
61 uppercased identities; and vice versa for FFE_caseLOWER. */
63 char ffesrc_char_source_
[256];
65 /* This array is used to map an internally generated character so that it
66 will be accepted as an initial character in a keyword. The assumption
67 is that the incoming character is uppercase. */
69 char ffesrc_char_internal_init_
[256];
71 /* This array is used to determine if a particular character is valid in
72 a symbol name according to the established ffe_case_symbol() setting:
73 for FFE_caseNONE, the array is all FFEBAD; for FFE_caseUPPER, the
74 lowercase cells contain a non-FFEBAD error code (FFEBAD_SYMBOL_UPPER_CASE);
75 and vice versa for FFE_caseLOWER. _init_ and _noninit_ distinguish
76 between initial and subsequent characters for the caseINITCAP case,
77 and their error codes are different for appropriate messages --
78 specifically, _noninit_ contains a non-FFEBAD error code for all
79 except lowercase characters for the caseINITCAP case.
81 See ffesrc_check_symbol_, it must be TRUE if this array is not all
84 ffebad ffesrc_bad_symbol_init_
[256];
85 ffebad ffesrc_bad_symbol_noninit_
[256];
87 /* Set TRUE if any element in ffesrc_bad_symbol (with an index representing
88 a character that can also be in the text of a token passed to
89 ffename_find, strictly speaking) is not FFEBAD. I.e., TRUE if it is
90 necessary to check token characters against the ffesrc_bad_symbol_
93 bool ffesrc_check_symbol_
;
95 /* These are set TRUE if the kind of character (upper/lower) is ok as a match
96 in the context (initial/noninitial character of keyword). */
98 bool ffesrc_ok_match_init_upper_
;
99 bool ffesrc_ok_match_init_lower_
;
100 bool ffesrc_ok_match_noninit_upper_
;
101 bool ffesrc_ok_match_noninit_lower_
;
103 /* Initialize table of alphabetic matches. */
110 for (i
= 0; i
< 256; ++i
)
112 ffesrc_char_match_init_
[i
] = i
;
113 ffesrc_char_match_noninit_
[i
] = i
;
114 ffesrc_char_source_
[i
] = i
;
115 ffesrc_char_internal_init_
[i
] = i
;
116 ffesrc_toupper_
[i
] = i
;
117 ffesrc_tolower_
[i
] = i
;
118 ffesrc_bad_symbol_init_
[i
] = FFEBAD
;
119 ffesrc_bad_symbol_noninit_
[i
] = FFEBAD
;
122 for (i
= 'A'; i
<= 'Z'; ++i
)
123 ffesrc_tolower_
[i
] = TOLOWER (i
);
125 for (i
= 'a'; i
<= 'z'; ++i
)
126 ffesrc_toupper_
[i
] = TOUPPER (i
);
128 ffesrc_check_symbol_
= (ffe_case_symbol () != FFE_caseNONE
);
130 ffesrc_ok_match_init_upper_
= (ffe_case_match () != FFE_caseLOWER
);
131 ffesrc_ok_match_init_lower_
= (ffe_case_match () != FFE_caseUPPER
)
132 && (ffe_case_match () != FFE_caseINITCAP
);
133 ffesrc_ok_match_noninit_upper_
= (ffe_case_match () != FFE_caseLOWER
)
134 && (ffe_case_match () != FFE_caseINITCAP
);
135 ffesrc_ok_match_noninit_lower_
= (ffe_case_match () != FFE_caseUPPER
);
137 /* Note that '-' is used to flag an invalid match character. '-' is
138 somewhat arbitrary, actually. -1 was used, but that's not wise on a
139 system with unsigned chars as default -- it'd turn into 255 or some such
140 large positive number, which would sort higher than the alphabetics and
141 thus possibly cause problems. So '-' is picked just because it's never
142 likely to be a symbol character in Fortran and because it's "less than"
143 any alphabetic character. EBCDIC might see things differently, I don't
144 remember it well enough, but that's just tough -- lots of other things
145 might have to change to support EBCDIC -- anyway, some other character
146 could easily be picked. */
148 #define FFESRC_INVALID_SYMBOL_CHAR_ '-'
150 if (!ffesrc_ok_match_init_upper_
)
151 for (i
= 'A'; i
<= 'Z'; ++i
)
152 ffesrc_char_match_init_
[i
] = FFESRC_INVALID_SYMBOL_CHAR_
;
154 if (ffesrc_ok_match_init_lower_
)
155 for (i
= 'a'; i
<= 'z'; ++i
)
156 ffesrc_char_match_init_
[i
] = TOUPPER (i
);
158 for (i
= 'a'; i
<= 'z'; ++i
)
159 ffesrc_char_match_init_
[i
] = FFESRC_INVALID_SYMBOL_CHAR_
;
161 if (!ffesrc_ok_match_noninit_upper_
)
162 for (i
= 'A'; i
<= 'Z'; ++i
)
163 ffesrc_char_match_noninit_
[i
] = FFESRC_INVALID_SYMBOL_CHAR_
;
165 if (ffesrc_ok_match_noninit_lower_
)
166 for (i
= 'a'; i
<= 'z'; ++i
)
167 ffesrc_char_match_noninit_
[i
] = TOUPPER (i
);
169 for (i
= 'a'; i
<= 'z'; ++i
)
170 ffesrc_char_match_noninit_
[i
] = FFESRC_INVALID_SYMBOL_CHAR_
;
172 if (ffe_case_source () == FFE_caseLOWER
)
173 for (i
= 'A'; i
<= 'Z'; ++i
)
174 ffesrc_char_source_
[i
] = TOLOWER (i
);
175 else if (ffe_case_source () == FFE_caseUPPER
)
176 for (i
= 'a'; i
<= 'z'; ++i
)
177 ffesrc_char_source_
[i
] = TOUPPER (i
);
179 if (ffe_case_match () == FFE_caseLOWER
)
180 for (i
= 'A'; i
<= 'Z'; ++i
)
181 ffesrc_char_internal_init_
[i
] = TOLOWER (i
);
183 switch (ffe_case_symbol ())
186 for (i
= 'A'; i
<= 'Z'; ++i
)
188 ffesrc_bad_symbol_init_
[i
] = FFEBAD_SYMBOL_UPPER_CASE
;
189 ffesrc_bad_symbol_noninit_
[i
] = FFEBAD_SYMBOL_UPPER_CASE
;
194 for (i
= 'a'; i
<= 'z'; ++i
)
196 ffesrc_bad_symbol_init_
[i
] = FFEBAD_SYMBOL_LOWER_CASE
;
197 ffesrc_bad_symbol_noninit_
[i
] = FFEBAD_SYMBOL_LOWER_CASE
;
201 case FFE_caseINITCAP
:
202 for (i
= 0; i
< 256; ++i
)
203 ffesrc_bad_symbol_noninit_
[i
] = FFEBAD_SYMBOL_NOLOWER_INITCAP
;
204 for (i
= 'a'; i
<= 'z'; ++i
)
206 ffesrc_bad_symbol_init_
[i
] = FFEBAD_SYMBOL_LOWER_INITCAP
;
207 ffesrc_bad_symbol_noninit_
[i
] = FFEBAD
;
216 /* Compare two strings a la strcmp, the first being a source string with its
217 length passed, and the second being a constant string passed
218 in InitialCaps form. Also, the return value is always -1, 0, or 1. */
221 ffesrc_strcmp_1ns2i (ffeCase mcase
, const char *var
, int len
,
230 for (; len
> 0; --len
, ++var
, ++str_ic
)
232 c
= ffesrc_char_source (*var
); /* Transform source. */
233 c
= ffesrc_toupper (c
); /* Upcase source. */
234 d
= ffesrc_toupper (*str_ic
); /* Upcase InitialCaps char. */
237 if ((d
!= '\0') && (c
< d
))
246 for (; len
> 0; --len
, ++var
, ++str_ic
)
248 c
= ffesrc_char_source (*var
); /* Transform source. */
249 d
= ffesrc_toupper (*str_ic
); /* Transform InitialCaps char. */
252 if ((d
!= '\0') && (c
< d
))
261 for (; len
> 0; --len
, ++var
, ++str_ic
)
263 c
= ffesrc_char_source (*var
); /* Transform source. */
264 d
= ffesrc_tolower (*str_ic
); /* Transform InitialCaps char. */
267 if ((d
!= '\0') && (c
< d
))
275 case FFE_caseINITCAP
:
276 for (; len
> 0; --len
, ++var
, ++str_ic
)
278 c
= ffesrc_char_source (*var
); /* Transform source. */
279 d
= *str_ic
; /* No transform of InitialCaps char. */
282 c
= ffesrc_toupper (c
);
283 d
= ffesrc_toupper (d
);
284 while ((len
> 0) && (c
== d
))
285 { /* Skip past equivalent (case-ins) chars. */
286 --len
, ++var
, ++str_ic
;
288 c
= ffesrc_toupper (*var
);
289 d
= ffesrc_toupper (*str_ic
);
291 if ((d
!= '\0') && (c
< d
))
300 assert ("bad case value" == NULL
);
309 /* Compare two strings a la strcmp, the second being a constant string passed
310 in both uppercase and lowercase form. If not equal, the uppercase string
311 is used to determine the sign of the return value. Also, the return
312 value is always -1, 0, or 1. */
315 ffesrc_strcmp_2c (ffeCase mcase
, const char *var
, const char *str_uc
,
316 const char *str_lc
, const char *str_ic
)
324 for (; *var
!= '\0'; ++var
, ++str_uc
)
326 c
= ffesrc_toupper (*var
); /* Upcase source. */
329 if ((*str_uc
!= '\0') && (c
< *str_uc
))
340 i
= strcmp (var
, str_uc
);
344 i
= strcmp (var
, str_lc
);
347 case FFE_caseINITCAP
:
348 for (; *var
!= '\0'; ++var
, ++str_ic
, ++str_uc
)
352 c
= ffesrc_toupper (*var
);
353 while ((c
!= '\0') && (c
== *str_uc
))
354 { /* Skip past equivalent (case-ins) chars. */
356 c
= ffesrc_toupper (*var
);
358 if ((*str_uc
!= '\0') && (c
< *str_uc
))
369 assert ("bad case value" == NULL
);
380 /* Compare two strings a la strncmp, the second being a constant string passed
381 in uppercase, lowercase, and InitialCaps form. If not equal, the
382 uppercase string is used to determine the sign of the return value. */
385 ffesrc_strncmp_2c (ffeCase mcase
, const char *var
, const char *str_uc
,
386 const char *str_lc
, const char *str_ic
, int len
)
394 for (; len
> 0; ++var
, ++str_uc
, --len
)
396 c
= ffesrc_toupper (*var
); /* Upcase source. */
408 i
= strncmp (var
, str_uc
, len
);
412 i
= strncmp (var
, str_lc
, len
);
415 case FFE_caseINITCAP
:
416 for (; len
> 0; ++var
, ++str_ic
, ++str_uc
, --len
)
420 c
= ffesrc_toupper (*var
);
421 while ((len
> 0) && (c
== *str_uc
))
422 { /* Skip past equivalent (case-ins) chars. */
423 --len
, ++var
, ++str_uc
;
425 c
= ffesrc_toupper (*var
);
427 if ((len
> 0) && (c
< *str_uc
))
436 assert ("bad case value" == NULL
);