OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / gcc / scan.c
1 /* scan.c - Utility functions for scan-decls and patch-header programs.
2    Copyright (C) 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 #include "scan.h"
19 #include <ctype.h>
20
21 int lineno = 1;
22 int source_lineno = 1;
23 sstring source_filename;
24
25 void
26 make_sstring_space (str, count)
27      sstring *str;
28      int count;
29 {
30   int cur_pos = str->ptr - str->base;
31   int cur_size = str->limit - str->base;
32   int new_size = cur_pos + count + 100;
33
34   if (new_size <= cur_size)
35     return;
36   
37   if (str->base == NULL)
38     str->base = xmalloc (new_size);
39   else
40     str->base = xrealloc (str->base, new_size);
41   str->ptr = str->base + cur_size;
42   str->limit = str->base + new_size;
43 }
44
45 void
46 sstring_append (dst, src)
47      sstring *dst;
48      sstring *src;
49 {
50   register char *d, *s;
51   register count = SSTRING_LENGTH(src);
52   MAKE_SSTRING_SPACE(dst, count + 1);
53   d = dst->ptr;
54   s = src->base;
55   while (--count >= 0) *d++ = *s++;
56   dst->ptr = d;
57   *d = 0;  
58 }
59
60 memory_full ()
61 {
62   abort();
63 }
64
65 char *
66 xmalloc (size)
67      unsigned size;
68 {
69   register char *ptr = (char *) malloc (size);
70   if (ptr != 0) return (ptr);
71   memory_full ();
72   /*NOTREACHED*/
73   return 0;
74 }
75
76
77 char *
78 xrealloc (old, size)
79      char *old;
80      unsigned size;
81 {
82   register char *ptr = (char *) realloc (old, size);
83   if (ptr != 0) return (ptr);
84   memory_full ();
85   /*NOTREACHED*/
86   return 0;
87 }
88
89 int
90 scan_ident (fp, s, c)
91      register FILE *fp;
92      register sstring *s;
93      int c;
94 {
95   s->ptr = s->base;
96   if (isalpha(c) || c == '_')
97     {
98       for (;;)
99         {
100           SSTRING_PUT(s, c);
101           c = getc (fp);
102           if (c == EOF || !(isalnum(c) || c == '_'))
103             break;
104         }
105     }
106   MAKE_SSTRING_SPACE(s, 1);
107   *s->ptr = 0;
108   return c;
109 }
110
111 int scan_string (fp, s, init)
112      register FILE *fp;
113      register sstring *s;
114 {
115   int c;
116   for (;;)
117     {
118       c = getc (fp);
119       if (c == EOF || c == '\n')
120         break;
121       if (c == init)
122         {
123           c = getc (fp);
124           break;
125         }
126       if (c == '\\')
127         {
128           c = getc (fp);
129           if (c == EOF)
130             break;
131           if (c == '\n')
132             continue;
133         }
134       SSTRING_PUT(s, c);
135     }
136   MAKE_SSTRING_SPACE(s, 1);
137   *s->ptr = 0;
138   return c;
139 }
140
141 /* Skip horizontal white spaces (spaces, tabs, and C-style comments). */
142
143 int skip_spaces (fp, c)
144      register FILE *fp;
145      int c;
146 {
147   for (;;)
148     {
149       if (c == ' ' || c == '\t')
150         c = getc (fp);
151       else if (c == '/')
152         {
153           c = getc (fp);
154           if (c != '*')
155             {
156               ungetc (c, fp);
157               return '/';
158             }
159           c = getc (fp);
160           for (;;)
161             {
162               if (c == EOF)
163                 return EOF;
164               else if (c != '*')
165                 {
166                   if (c == '\n')
167                     source_lineno++, lineno++;
168                   c = getc (fp);
169                 }
170               else if ((c = getc (fp)) == '/')
171                 return getc (fp);
172             }
173         }
174       else
175         break;
176     }
177   return c;
178 }
179
180 int
181 read_upto (fp, str, delim)
182      FILE *fp;
183      sstring *str;
184      int delim;
185 {
186   int ch;
187   for (;;)
188     {
189       ch = getc (fp);
190       if (ch == EOF || ch == delim)
191         break;
192       SSTRING_PUT(str, ch);
193     }
194   MAKE_SSTRING_SPACE(str, 1);
195   *str->ptr = 0;
196   return ch;
197 }
198
199 int
200 get_token (fp, s)
201      register FILE *fp;
202      register sstring *s;
203 {
204   int c;
205   s->ptr = s->base;
206  retry:
207   c = ' ';
208  again:
209   c = skip_spaces (fp, c);
210   if (c == '\n')
211     {
212       source_lineno++;
213       lineno++;
214       goto retry;
215     }
216   if (c == '#')
217     {
218       c = get_token (fp, s);
219       if (c == INT_TOKEN)
220         {
221           source_lineno = atoi (s->base);
222           get_token (fp, &source_filename);
223         }
224       for (;;)
225         {
226           c = getc (fp);
227           if (c == EOF)
228             return EOF;
229           if (c == '\n')
230             goto retry;
231         }
232     }
233   if (c == EOF)
234     return EOF;
235   if (isdigit (c))
236     {
237       do
238         {
239           SSTRING_PUT(s, c);
240           c = getc (fp);
241         } while (c != EOF && isdigit(c));
242       ungetc (c, fp);
243       c = INT_TOKEN;
244       goto done;
245     }
246   if (isalpha (c) || c == '_')
247     {
248       c = scan_ident (fp, s, c);
249       ungetc (c, fp);
250       return IDENTIFIER_TOKEN;
251     }
252   if (c == '\'' || c == '"')
253     {
254       int quote = c;
255       c = scan_string (fp, s, c);
256       ungetc (c, fp);
257       return c == '\'' ? CHAR_TOKEN : STRING_TOKEN;
258     }
259   SSTRING_PUT(s, c);
260  done:
261   MAKE_SSTRING_SPACE(s, 1);
262   *s->ptr = 0;
263   return c;
264 }
265
266 unsigned long
267 hash (str)
268      char *str;
269 {
270   int h = 0;
271   /* Replace this with something faster/better! FIXME! */
272   while (*str) h = (h << 3) + *str++;
273   return h & 0x7FFFFFFF;
274 }