src.c 12.3 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

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 does a toupper (), but any valid char type is valid as an
   index and returns identity if not a lower-case character.  */

char ffesrc_toupper_[256];

/* This array does a tolower (), but any valid char type is valid as an
   index and returns identity if not an upper-case character.  */

char ffesrc_tolower_[256];

/* 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_toupper_[i] = i;
      ffesrc_tolower_[i] = i;
      ffesrc_bad_symbol_init_[i] = FFEBAD;
      ffesrc_bad_symbol_noninit_[i] = FFEBAD;
    }

  for (i = 'A'; i <= 'Z'; ++i)
123
    ffesrc_tolower_[i] = TOLOWER (i);
Jeff Law committed
124 125

  for (i = 'a'; i <= 'z'; ++i)
126
    ffesrc_toupper_[i] = TOUPPER (i);
Jeff Law committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

  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)
156
      ffesrc_char_match_init_[i] = TOUPPER (i);
Jeff Law committed
157 158 159 160 161 162 163 164 165 166
  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)
167
      ffesrc_char_match_noninit_[i] = TOUPPER (i);
Jeff Law committed
168 169 170 171 172 173
  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)
174
      ffesrc_char_source_[i] = TOLOWER (i);
Jeff Law committed
175 176
  else if (ffe_case_source () == FFE_caseUPPER)
    for (i = 'a'; i <= 'z'; ++i)
177
      ffesrc_char_source_[i] = TOUPPER (i);
Jeff Law committed
178 179 180

  if (ffe_case_match () == FFE_caseLOWER)
    for (i = 'A'; i <= 'Z'; ++i)
181
      ffesrc_char_internal_init_[i] = TOLOWER (i);
Jeff Law committed
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

  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. */
	  c = ffesrc_toupper (c);	/* Upcase source. */
	  d = ffesrc_toupper (*str_ic);	/* Upcase InitialCaps char. */
	  if (c != d)
236 237 238 239 240 241
	    {
	      if ((d != '\0') && (c < d))
		return -1;
	      else
		return 1;
	    }
Jeff Law committed
242 243 244 245 246 247 248 249 250
	}
      break;

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

    case FFE_caseLOWER:
      for (; len > 0; --len, ++var, ++str_ic)
	{
	  c = ffesrc_char_source (*var);	/* Transform source. */
	  d = ffesrc_tolower (*str_ic);	/* Transform InitialCaps char. */
	  if (c != d)
266 267 268 269 270 271
	    {
	      if ((d != '\0') && (c < d))
		return -1;
	      else
		return 1;
	    }
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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	}
      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)
	    {
	      c = ffesrc_toupper (c);
	      d = ffesrc_toupper (d);
	      while ((len > 0) && (c == d))
		{		/* Skip past equivalent (case-ins) chars. */
		  --len, ++var, ++str_ic;
		  if (len > 0)
		    c = ffesrc_toupper (*var);
		  d = ffesrc_toupper (*str_ic);
		}
	      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)
	{
	  c = ffesrc_toupper (*var);	/* Upcase source. */
	  if (c != *str_uc)
328 329 330 331 332 333
	    {
	      if ((*str_uc != '\0') && (c < *str_uc))
		return -1;
	      else
		return 1;
	    }
Jeff Law committed
334 335 336 337 338 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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
	}
      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)
	    {
	      c = ffesrc_toupper (*var);
	      while ((c != '\0') && (c == *str_uc))
		{		/* Skip past equivalent (case-ins) chars. */
		  ++var, ++str_uc;
		  c = ffesrc_toupper (*var);
		}
	      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)
	{
	  c = ffesrc_toupper (*var);	/* Upcase source. */
	  if (c != *str_uc)
398 399 400 401 402 403
	    {
	      if (c < *str_uc)
		return -1;
	      else
		return 1;
	    }
Jeff Law committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	}
      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)
	    {
	      c = ffesrc_toupper (*var);
	      while ((len > 0) && (c == *str_uc))
		{		/* Skip past equivalent (case-ins) chars. */
		  --len, ++var, ++str_uc;
		  if (len > 0)
		    c = ffesrc_toupper (*var);
		}
	      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;
}