home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / gcc / scan.c < prev    next >
C/C++ Source or Header  |  1995-06-15  |  4KB  |  241 lines

  1. /* Utility functions for scan-decls and fix-header programs.
  2.    Copyright (C) 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  17.  
  18. #include "scan.h"
  19. #include "hconfig.h"
  20. #include <ctype.h>
  21.  
  22. int lineno = 1;
  23. int source_lineno = 1;
  24. sstring source_filename;
  25.  
  26. void
  27. make_sstring_space (str, count)
  28.      sstring *str;
  29.      int count;
  30. {
  31.   int cur_pos = str->ptr - str->base;
  32.   int cur_size = str->limit - str->base;
  33.   int new_size = cur_pos + count + 100;
  34.  
  35.   if (new_size <= cur_size)
  36.     return;
  37.   
  38.   if (str->base == NULL)
  39.     str->base = xmalloc (new_size);
  40.   else
  41.     str->base = xrealloc (str->base, new_size);
  42.   str->ptr = str->base + cur_size;
  43.   str->limit = str->base + new_size;
  44. }
  45.  
  46. void
  47. sstring_append (dst, src)
  48.      sstring *dst;
  49.      sstring *src;
  50. {
  51.   register char *d, *s;
  52.   register count = SSTRING_LENGTH(src);
  53.   MAKE_SSTRING_SPACE(dst, count + 1);
  54.   d = dst->ptr;
  55.   s = src->base;
  56.   while (--count >= 0) *d++ = *s++;
  57.   dst->ptr = d;
  58.   *d = 0;  
  59. }
  60.  
  61. int
  62. scan_ident (fp, s, c)
  63.      register FILE *fp;
  64.      register sstring *s;
  65.      int c;
  66. {
  67.   s->ptr = s->base;
  68.   if (isalpha(c) || c == '_')
  69.     {
  70.       for (;;)
  71.     {
  72.       SSTRING_PUT(s, c);
  73.       c = getc (fp);
  74.       if (c == EOF || !(isalnum(c) || c == '_'))
  75.         break;
  76.     }
  77.     }
  78.   MAKE_SSTRING_SPACE(s, 1);
  79.   *s->ptr = 0;
  80.   return c;
  81. }
  82.  
  83. int
  84. scan_string (fp, s, init)
  85.      register FILE *fp;
  86.      register sstring *s;
  87. {
  88.   int c;
  89.   for (;;)
  90.     {
  91.       c = getc (fp);
  92.       if (c == EOF || c == '\n')
  93.     break;
  94.       if (c == init)
  95.     {
  96.       c = getc (fp);
  97.       break;
  98.     }
  99.       if (c == '\\')
  100.     {
  101.       c = getc (fp);
  102.       if (c == EOF)
  103.         break;
  104.       if (c == '\n')
  105.         continue;
  106.     }
  107.       SSTRING_PUT(s, c);
  108.     }
  109.   MAKE_SSTRING_SPACE(s, 1);
  110.   *s->ptr = 0;
  111.   return c;
  112. }
  113.  
  114. /* Skip horizontal white spaces (spaces, tabs, and C-style comments). */
  115.  
  116. int
  117. skip_spaces (fp, c)
  118.      register FILE *fp;
  119.      int c;
  120. {
  121.   for (;;)
  122.     {
  123.       if (c == ' ' || c == '\t')
  124.     c = getc (fp);
  125.       else if (c == '/')
  126.     {
  127.       c = getc (fp);
  128.       if (c != '*')
  129.         {
  130.           ungetc (c, fp);
  131.           return '/';
  132.         }
  133.       c = getc (fp);
  134.       for (;;)
  135.         {
  136.           if (c == EOF)
  137.         return EOF;
  138.           else if (c != '*')
  139.         {
  140.           if (c == '\n')
  141.             source_lineno++, lineno++;
  142.           c = getc (fp);
  143.         }
  144.           else if ((c = getc (fp)) == '/')
  145.         return getc (fp);
  146.         }
  147.     }
  148.       else
  149.     break;
  150.     }
  151.   return c;
  152. }
  153.  
  154. int
  155. read_upto (fp, str, delim)
  156.      FILE *fp;
  157.      sstring *str;
  158.      int delim;
  159. {
  160.   int ch;
  161.   for (;;)
  162.     {
  163.       ch = getc (fp);
  164.       if (ch == EOF || ch == delim)
  165.     break;
  166.       SSTRING_PUT(str, ch);
  167.     }
  168.   MAKE_SSTRING_SPACE(str, 1);
  169.   *str->ptr = 0;
  170.   return ch;
  171. }
  172.  
  173. int
  174. get_token (fp, s)
  175.      register FILE *fp;
  176.      register sstring *s;
  177. {
  178.   int c;
  179.   s->ptr = s->base;
  180.  retry:
  181.   c = ' ';
  182.   c = skip_spaces (fp, c);
  183.   if (c == '\n')
  184.     {
  185.       source_lineno++;
  186.       lineno++;
  187.       goto retry;
  188.     }
  189.   if (c == '#')
  190.     {
  191.       c = get_token (fp, s);
  192.       if (c == INT_TOKEN)
  193.     {
  194.       source_lineno = atoi (s->base) - 1; /* '\n' will add 1 */
  195.       get_token (fp, &source_filename);
  196.     }
  197.       for (;;)
  198.     {
  199.       c = getc (fp);
  200.       if (c == EOF)
  201.         return EOF;
  202.       if (c == '\n')
  203.         {
  204.         source_lineno++;
  205.         lineno++;
  206.         goto retry;
  207.         }
  208.     }
  209.     }
  210.   if (c == EOF)
  211.     return EOF;
  212.   if (isdigit (c))
  213.     {
  214.       do
  215.     {
  216.       SSTRING_PUT(s, c);
  217.       c = getc (fp);
  218.     } while (c != EOF && isdigit(c));
  219.       ungetc (c, fp);
  220.       c = INT_TOKEN;
  221.       goto done;
  222.     }
  223.   if (isalpha (c) || c == '_')
  224.     {
  225.       c = scan_ident (fp, s, c);
  226.       ungetc (c, fp);
  227.       return IDENTIFIER_TOKEN;
  228.     }
  229.   if (c == '\'' || c == '"')
  230.     {
  231.       c = scan_string (fp, s, c);
  232.       ungetc (c, fp);
  233.       return c == '\'' ? CHAR_TOKEN : STRING_TOKEN;
  234.     }
  235.   SSTRING_PUT(s, c);
  236.  done:
  237.   MAKE_SSTRING_SPACE(s, 1);
  238.   *s->ptr = 0;
  239.   return c;
  240. }
  241.