src.c 11.6 KB
Newer Older
Jeff Law committed
1 2
/* src.c -- Implementation File
   Copyright (C) 1995 Free Software Foundation, Inc.
3
   Contributed by James Craig Burley.
Jeff Law committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

This file is part of GNU Fortran.

GNU Fortran is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Fortran is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Fortran; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.

   Related Modules:

   Description:
      Source-file functions to handle various combinations of case sensitivity
      and insensitivity at run time.

   Modifications:
*/

#include "proj.h"
#include "src.h"
#include "top.h"

/* This array is set up so that, given a source-mapped character, the result
   of indexing into this array will match an upper-cased character depending
   on the source-mapped character's case and the established ffe_case_match()
   setting.  So the uppercase cells contain identies (e.g. ['A'] == 'A')
   as long as uppercase matching is permitted (!FFE_caseLOWER) and the
   lowercase cells contain uppercased identities (e.g. ['a'] == 'A') as long
   as lowercase matching is permitted (!FFE_caseUPPER).	 Else the case
   cells contain -1.  _init_ is for the first character of a keyword,
   and _noninit_ is for other characters.  */

char ffesrc_char_match_init_[256];
char ffesrc_char_match_noninit_[256];

/* This array is used to map input source according to the established
   ffe_case_source() setting: for FFE_caseNONE, the array is all
   identities; for FFE_caseUPPER, the lowercase cells contain
   uppercased identities; and vice versa for FFE_caseLOWER.  */

char ffesrc_char_source_[256];

/* This array is used to map an internally generated character so that it
   will be accepted as an initial character in a keyword.  The assumption
   is that the incoming character is uppercase.  */

char ffesrc_char_internal_init_[256];

/* This array is used to determine if a particular character is valid in
   a symbol name according to the established ffe_case_symbol() setting:
   for FFE_caseNONE, the array is all FFEBAD; for FFE_caseUPPER, the
   lowercase cells contain a non-FFEBAD error code (FFEBAD_SYMBOL_UPPER_CASE);
   and vice versa for FFE_caseLOWER.  _init_ and _noninit_ distinguish
   between initial and subsequent characters for the caseINITCAP case,
   and their error codes are different for appropriate messages --
   specifically, _noninit_ contains a non-FFEBAD error code for all
   except lowercase characters for the caseINITCAP case.

   See ffesrc_check_symbol_, it must be TRUE if this array is not all
   FFEBAD.  */

ffebad ffesrc_bad_symbol_init_[256];
ffebad ffesrc_bad_symbol_noninit_[256];

/* Set TRUE if any element in ffesrc_bad_symbol (with an index representing
   a character that can also be in the text of a token passed to
   ffename_find, strictly speaking) is not FFEBAD.  I.e., TRUE if it is
   necessary to check token characters against the ffesrc_bad_symbol_
   array.  */

bool ffesrc_check_symbol_;

/* These are set TRUE if the kind of character (upper/lower) is ok as a match
   in the context (initial/noninitial character of keyword).  */

bool ffesrc_ok_match_init_upper_;
bool ffesrc_ok_match_init_lower_;
bool ffesrc_ok_match_noninit_upper_;
bool ffesrc_ok_match_noninit_lower_;

/* Initialize table of alphabetic matches. */

void
ffesrc_init_1 ()
{
  int i;

  for (i = 0; i < 256; ++i)
    {
      ffesrc_char_match_init_[i] = i;
      ffesrc_char_match_noninit_[i] = i;
      ffesrc_char_source_[i] = i;
      ffesrc_char_internal_init_[i] = i;
      ffesrc_bad_symbol_init_[i] = FFEBAD;
      ffesrc_bad_symbol_noninit_[i] = FFEBAD;
    }

  ffesrc_check_symbol_ = (ffe_case_symbol () != FFE_caseNONE);

  ffesrc_ok_match_init_upper_ = (ffe_case_match () != FFE_caseLOWER);
  ffesrc_ok_match_init_lower_ = (ffe_case_match () != FFE_caseUPPER)
    && (ffe_case_match () != FFE_caseINITCAP);
  ffesrc_ok_match_noninit_upper_ = (ffe_case_match () != FFE_caseLOWER)
    && (ffe_case_match () != FFE_caseINITCAP);
  ffesrc_ok_match_noninit_lower_ = (ffe_case_match () != FFE_caseUPPER);

  /* Note that '-' is used to flag an invalid match character.	'-' is
     somewhat arbitrary, actually.  -1 was used, but that's not wise on a
     system with unsigned chars as default -- it'd turn into 255 or some such
     large positive number, which would sort higher than the alphabetics and
     thus possibly cause problems.  So '-' is picked just because it's never
     likely to be a symbol character in Fortran and because it's "less than"
     any alphabetic character.	EBCDIC might see things differently, I don't
     remember it well enough, but that's just tough -- lots of other things
     might have to change to support EBCDIC -- anyway, some other character
     could easily be picked.  */

#define FFESRC_INVALID_SYMBOL_CHAR_ '-'

  if (!ffesrc_ok_match_init_upper_)
    for (i = 'A'; i <= 'Z'; ++i)
      ffesrc_char_match_init_[i] = FFESRC_INVALID_SYMBOL_CHAR_;

  if (ffesrc_ok_match_init_lower_)
    for (i = 'a'; i <= 'z'; ++i)
138
      ffesrc_char_match_init_[i] = TOUPPER (i);
Jeff Law committed
139 140 141 142 143 144 145 146 147 148
  else
    for (i = 'a'; i <= 'z'; ++i)
      ffesrc_char_match_init_[i] = FFESRC_INVALID_SYMBOL_CHAR_;

  if (!ffesrc_ok_match_noninit_upper_)
    for (i = 'A'; i <= 'Z'; ++i)
      ffesrc_char_match_noninit_[i] = FFESRC_INVALID_SYMBOL_CHAR_;

  if (ffesrc_ok_match_noninit_lower_)
    for (i = 'a'; i <= 'z'; ++i)
149
      ffesrc_char_match_noninit_[i] = TOUPPER (i);
Jeff Law committed
150 151 152 153 154 155
  else
    for (i = 'a'; i <= 'z'; ++i)
      ffesrc_char_match_noninit_[i] = FFESRC_INVALID_SYMBOL_CHAR_;

  if (ffe_case_source () == FFE_caseLOWER)
    for (i = 'A'; i <= 'Z'; ++i)
156
      ffesrc_char_source_[i] = TOLOWER (i);
Jeff Law committed
157 158
  else if (ffe_case_source () == FFE_caseUPPER)
    for (i = 'a'; i <= 'z'; ++i)
159
      ffesrc_char_source_[i] = TOUPPER (i);
Jeff Law committed
160 161 162

  if (ffe_case_match () == FFE_caseLOWER)
    for (i = 'A'; i <= 'Z'; ++i)
163
      ffesrc_char_internal_init_[i] = TOLOWER (i);
Jeff Law committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

  switch (ffe_case_symbol ())
    {
    case FFE_caseLOWER:
      for (i = 'A'; i <= 'Z'; ++i)
	{
	  ffesrc_bad_symbol_init_[i] = FFEBAD_SYMBOL_UPPER_CASE;
	  ffesrc_bad_symbol_noninit_[i] = FFEBAD_SYMBOL_UPPER_CASE;
	}
      break;

    case FFE_caseUPPER:
      for (i = 'a'; i <= 'z'; ++i)
	{
	  ffesrc_bad_symbol_init_[i] = FFEBAD_SYMBOL_LOWER_CASE;
	  ffesrc_bad_symbol_noninit_[i] = FFEBAD_SYMBOL_LOWER_CASE;
	}
      break;

    case FFE_caseINITCAP:
      for (i = 0; i < 256; ++i)
	ffesrc_bad_symbol_noninit_[i] = FFEBAD_SYMBOL_NOLOWER_INITCAP;
      for (i = 'a'; i <= 'z'; ++i)
	{
	  ffesrc_bad_symbol_init_[i] = FFEBAD_SYMBOL_LOWER_INITCAP;
	  ffesrc_bad_symbol_noninit_[i] = FFEBAD;
	}
      break;

    default:
      break;
    }
}

/* Compare two strings a la strcmp, the first being a source string with its
   length passed, and the second being a constant string passed
   in InitialCaps form.	 Also, the return value is always -1, 0, or 1. */

int
ffesrc_strcmp_1ns2i (ffeCase mcase, const char *var, int len,
		     const char *str_ic)
{
  char c;
  char d;

  switch (mcase)
    {
    case FFE_caseNONE:
      for (; len > 0; --len, ++var, ++str_ic)
	{
	  c = ffesrc_char_source (*var);	/* Transform source. */
Zack Weinberg committed
215 216
	  c = TOUPPER (c);			/* Upcase source. */
	  d = TOUPPER (*str_ic);		/* Upcase InitialCaps char. */
Jeff Law committed
217
	  if (c != d)
218 219 220 221 222 223
	    {
	      if ((d != '\0') && (c < d))
		return -1;
	      else
		return 1;
	    }
Jeff Law committed
224 225 226 227 228 229 230
	}
      break;

    case FFE_caseUPPER:
      for (; len > 0; --len, ++var, ++str_ic)
	{
	  c = ffesrc_char_source (*var);	/* Transform source. */
Zack Weinberg committed
231
	  d = TOUPPER (*str_ic);	/* Transform InitialCaps char. */
Jeff Law committed
232
	  if (c != d)
233 234 235 236 237 238
	    {
	      if ((d != '\0') && (c < d))
		return -1;
	      else
		return 1;
	    }
Jeff Law committed
239 240 241 242 243 244 245
	}
      break;

    case FFE_caseLOWER:
      for (; len > 0; --len, ++var, ++str_ic)
	{
	  c = ffesrc_char_source (*var);	/* Transform source. */
Zack Weinberg committed
246
	  d = TOLOWER (*str_ic);	/* Transform InitialCaps char. */
Jeff Law committed
247
	  if (c != d)
248 249 250 251 252 253
	    {
	      if ((d != '\0') && (c < d))
		return -1;
	      else
		return 1;
	    }
Jeff Law committed
254 255 256 257 258 259 260 261 262 263
	}
      break;

    case FFE_caseINITCAP:
      for (; len > 0; --len, ++var, ++str_ic)
	{
	  c = ffesrc_char_source (*var);	/* Transform source. */
	  d = *str_ic;		/* No transform of InitialCaps char. */
	  if (c != d)
	    {
Zack Weinberg committed
264 265
	      c = TOUPPER (c);
	      d = TOUPPER (d);
Jeff Law committed
266 267 268 269
	      while ((len > 0) && (c == d))
		{		/* Skip past equivalent (case-ins) chars. */
		  --len, ++var, ++str_ic;
		  if (len > 0)
Zack Weinberg committed
270 271
		    c = TOUPPER (*var);
		  d = TOUPPER (*str_ic);
Jeff Law committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
		}
	      if ((d != '\0') && (c < d))
		return -1;
	      else
		return 1;
	    }
	}
      break;

    default:
      assert ("bad case value" == NULL);
      return -1;
    }

  if (*str_ic == '\0')
    return 0;
  return -1;
}

/* Compare two strings a la strcmp, the second being a constant string passed
   in both uppercase and lowercase form.  If not equal, the uppercase string
   is used to determine the sign of the return value.  Also, the return
   value is always -1, 0, or 1. */

int
ffesrc_strcmp_2c (ffeCase mcase, const char *var, const char *str_uc,
		  const char *str_lc, const char *str_ic)
{
  int i;
  char c;

  switch (mcase)
    {
    case FFE_caseNONE:
      for (; *var != '\0'; ++var, ++str_uc)
	{
Zack Weinberg committed
308
	  c = TOUPPER (*var);	/* Upcase source. */
Jeff Law committed
309
	  if (c != *str_uc)
310 311 312 313 314 315
	    {
	      if ((*str_uc != '\0') && (c < *str_uc))
		return -1;
	      else
		return 1;
	    }
Jeff Law committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
	}
      if (*str_uc == '\0')
	return 0;
      return -1;

    case FFE_caseUPPER:
      i = strcmp (var, str_uc);
      break;

    case FFE_caseLOWER:
      i = strcmp (var, str_lc);
      break;

    case FFE_caseINITCAP:
      for (; *var != '\0'; ++var, ++str_ic, ++str_uc)
	{
	  if (*var != *str_ic)
	    {
Zack Weinberg committed
334
	      c = TOUPPER (*var);
Jeff Law committed
335 336 337
	      while ((c != '\0') && (c == *str_uc))
		{		/* Skip past equivalent (case-ins) chars. */
		  ++var, ++str_uc;
Zack Weinberg committed
338
		  c = TOUPPER (*var);
Jeff Law committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
		}
	      if ((*str_uc != '\0') && (c < *str_uc))
		return -1;
	      else
		return 1;
	    }
	}
      if (*str_ic == '\0')
	return 0;
      return -1;

    default:
      assert ("bad case value" == NULL);
      return -1;
    }

  if (i == 0)
    return 0;
  else if (i < 0)
    return -1;
  return 1;
}

/* Compare two strings a la strncmp, the second being a constant string passed
   in uppercase, lowercase, and InitialCaps form.  If not equal, the
   uppercase string is used to determine the sign of the return value.	*/

int
ffesrc_strncmp_2c (ffeCase mcase, const char *var, const char *str_uc,
		   const char *str_lc, const char *str_ic, int len)
{
  int i;
  char c;

  switch (mcase)
    {
    case FFE_caseNONE:
      for (; len > 0; ++var, ++str_uc, --len)
	{
Zack Weinberg committed
378
	  c = TOUPPER (*var);	/* Upcase source. */
Jeff Law committed
379
	  if (c != *str_uc)
380 381 382 383 384 385
	    {
	      if (c < *str_uc)
		return -1;
	      else
		return 1;
	    }
Jeff Law committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	}
      return 0;

    case FFE_caseUPPER:
      i = strncmp (var, str_uc, len);
      break;

    case FFE_caseLOWER:
      i = strncmp (var, str_lc, len);
      break;

    case FFE_caseINITCAP:
      for (; len > 0; ++var, ++str_ic, ++str_uc, --len)
	{
	  if (*var != *str_ic)
	    {
Zack Weinberg committed
402
	      c = TOUPPER (*var);
Jeff Law committed
403 404 405 406
	      while ((len > 0) && (c == *str_uc))
		{		/* Skip past equivalent (case-ins) chars. */
		  --len, ++var, ++str_uc;
		  if (len > 0)
Zack Weinberg committed
407
		    c = TOUPPER (*var);
Jeff Law committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
		}
	      if ((len > 0) && (c < *str_uc))
		return -1;
	      else
		return 1;
	    }
	}
      return 0;

    default:
      assert ("bad case value" == NULL);
      return -1;
    }

  if (i == 0)
    return 0;
  else if (i < 0)
    return -1;
  return 1;
}