OSDN Git Service

2003-01-30 Ralf Corsepius <corsepiu@faw.uni-ulm.de>
[pf3gnuchains/gcc-fork.git] / gcc / scan.c
1 /* Utility functions for scan-decls and fix-header programs.
2    Copyright (C) 1993, 1994, 1998, 2002 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 "bconfig.h"
19 #include "system.h"
20 #include "coretypes.h"
21 #include "tm.h"
22 #include "scan.h"
23
24 int lineno = 1;
25 int source_lineno = 1;
26 sstring source_filename;
27
28 void
29 make_sstring_space (str, count)
30      sstring *str;
31      int count;
32 {
33   int cur_pos = str->ptr - str->base;
34   int cur_size = str->limit - str->base;
35   int new_size = cur_pos + count + 100;
36
37   if (new_size <= cur_size)
38     return;
39
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   char *d, *s;
51   int count = SSTRING_LENGTH (src);
52
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      FILE *fp;
64      sstring *s;
65      int c;
66 {
67   s->ptr = s->base;
68   if (ISIDST (c))
69     {
70       for (;;)
71         {
72           SSTRING_PUT (s, c);
73           c = getc (fp);
74           if (c == EOF || ! ISIDNUM (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      FILE *fp;
86      sstring *s;
87      int init;
88 {
89   int c;
90
91   for (;;)
92     {
93       c = getc (fp);
94       if (c == EOF || c == '\n')
95         break;
96       if (c == init)
97         {
98           c = getc (fp);
99           break;
100         }
101       if (c == '\\')
102         {
103           c = getc (fp);
104           if (c == EOF)
105             break;
106           if (c == '\n')
107             continue;
108         }
109       SSTRING_PUT (s, c);
110     }
111   MAKE_SSTRING_SPACE (s, 1);
112   *s->ptr = 0;
113   return c;
114 }
115
116 /* Skip horizontal white spaces (spaces, tabs, and C-style comments).  */
117
118 int
119 skip_spaces (fp, c)
120      FILE *fp;
121      int c;
122 {
123   for (;;)
124     {
125       if (c == ' ' || c == '\t')
126         c = getc (fp);
127       else if (c == '/')
128         {
129           c = getc (fp);
130           if (c != '*')
131             {
132               ungetc (c, fp);
133               return '/';
134             }
135           c = getc (fp);
136           for (;;)
137             {
138               if (c == EOF)
139                 return EOF;
140               else if (c != '*')
141                 {
142                   if (c == '\n')
143                     source_lineno++, lineno++;
144                   c = getc (fp);
145                 }
146               else if ((c = getc (fp)) == '/')
147                 return getc (fp);
148             }
149         }
150       else
151         break;
152     }
153   return c;
154 }
155
156 int
157 read_upto (fp, str, delim)
158      FILE *fp;
159      sstring *str;
160      int delim;
161 {
162   int ch;
163
164   for (;;)
165     {
166       ch = getc (fp);
167       if (ch == EOF || ch == delim)
168         break;
169       SSTRING_PUT (str, ch);
170     }
171   MAKE_SSTRING_SPACE (str, 1);
172   *str->ptr = 0;
173   return ch;
174 }
175
176 int
177 get_token (fp, s)
178      FILE *fp;
179      sstring *s;
180 {
181   int c;
182
183   s->ptr = s->base;
184  retry:
185   c = ' ';
186   c = skip_spaces (fp, c);
187   if (c == '\n')
188     {
189       source_lineno++;
190       lineno++;
191       goto retry;
192     }
193   if (c == '#')
194     {
195       c = get_token (fp, s);
196       if (c == INT_TOKEN)
197         {
198           source_lineno = atoi (s->base) - 1; /* '\n' will add 1 */
199           get_token (fp, &source_filename);
200         }
201       for (;;)
202         {
203           c = getc (fp);
204           if (c == EOF)
205             return EOF;
206           if (c == '\n')
207             {
208             source_lineno++;
209             lineno++;
210             goto retry;
211             }
212         }
213     }
214   if (c == EOF)
215     return EOF;
216   if (ISDIGIT (c))
217     {
218       do
219         {
220           SSTRING_PUT (s, c);
221           c = getc (fp);
222         } while (c != EOF && ISDIGIT (c));
223       ungetc (c, fp);
224       c = INT_TOKEN;
225       goto done;
226     }
227   if (ISIDST (c))
228     {
229       c = scan_ident (fp, s, c);
230       ungetc (c, fp);
231       return IDENTIFIER_TOKEN;
232     }
233   if (c == '\'' || c == '"')
234     {
235       c = scan_string (fp, s, c);
236       ungetc (c, fp);
237       return c == '\'' ? CHAR_TOKEN : STRING_TOKEN;
238     }
239   SSTRING_PUT (s, c);
240  done:
241   MAKE_SSTRING_SPACE (s, 1);
242   *s->ptr = 0;
243   return c;
244 }
245
246 unsigned int
247 hashstr (str, len)
248      const char *str;
249      unsigned int len;
250 {
251   unsigned int n = len;
252   unsigned int r = 0;
253   const unsigned char *s = (const unsigned char *) str;
254
255   do
256     r = r * 67 + (*s++ - 113);
257   while (--n);
258   return r + len;
259 }