OSDN Git Service

2001-07-30 H.J. Lu (hjl@gnu.org)
[pf3gnuchains/gcc-fork.git] / gcc / scan.c
1 /* Utility functions for scan-decls and fix-header programs.
2    Copyright (C) 1993, 1994, 1998 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 "hconfig.h"
19 #include "system.h"
20 #include "scan.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   str->base = xrealloc (str->base, new_size);
39   str->ptr = str->base + cur_size;
40   str->limit = str->base + new_size;
41 }
42
43 void
44 sstring_append (dst, src)
45      sstring *dst;
46      sstring *src;
47 {
48   register char *d, *s;
49   register int count = SSTRING_LENGTH(src);
50   MAKE_SSTRING_SPACE(dst, count + 1);
51   d = dst->ptr;
52   s = src->base;
53   while (--count >= 0) *d++ = *s++;
54   dst->ptr = d;
55   *d = 0;  
56 }
57
58 int
59 scan_ident (fp, s, c)
60      register FILE *fp;
61      register sstring *s;
62      int c;
63 {
64   s->ptr = s->base;
65   if (ISALPHA(c) || c == '_')
66     {
67       for (;;)
68         {
69           SSTRING_PUT(s, c);
70           c = getc (fp);
71           if (c == EOF || !(ISALNUM(c) || c == '_'))
72             break;
73         }
74     }
75   MAKE_SSTRING_SPACE(s, 1);
76   *s->ptr = 0;
77   return c;
78 }
79
80 int
81 scan_string (fp, s, init)
82      register FILE *fp;
83      register sstring *s;
84      int init;
85 {
86   int c;
87   for (;;)
88     {
89       c = getc (fp);
90       if (c == EOF || c == '\n')
91         break;
92       if (c == init)
93         {
94           c = getc (fp);
95           break;
96         }
97       if (c == '\\')
98         {
99           c = getc (fp);
100           if (c == EOF)
101             break;
102           if (c == '\n')
103             continue;
104         }
105       SSTRING_PUT(s, c);
106     }
107   MAKE_SSTRING_SPACE(s, 1);
108   *s->ptr = 0;
109   return c;
110 }
111
112 /* Skip horizontal white spaces (spaces, tabs, and C-style comments).  */
113
114 int
115 skip_spaces (fp, c)
116      register FILE *fp;
117      int c;
118 {
119   for (;;)
120     {
121       if (c == ' ' || c == '\t')
122         c = getc (fp);
123       else if (c == '/')
124         {
125           c = getc (fp);
126           if (c != '*')
127             {
128               ungetc (c, fp);
129               return '/';
130             }
131           c = getc (fp);
132           for (;;)
133             {
134               if (c == EOF)
135                 return EOF;
136               else if (c != '*')
137                 {
138                   if (c == '\n')
139                     source_lineno++, lineno++;
140                   c = getc (fp);
141                 }
142               else if ((c = getc (fp)) == '/')
143                 return getc (fp);
144             }
145         }
146       else
147         break;
148     }
149   return c;
150 }
151
152 int
153 read_upto (fp, str, delim)
154      FILE *fp;
155      sstring *str;
156      int delim;
157 {
158   int ch;
159   for (;;)
160     {
161       ch = getc (fp);
162       if (ch == EOF || ch == delim)
163         break;
164       SSTRING_PUT(str, ch);
165     }
166   MAKE_SSTRING_SPACE(str, 1);
167   *str->ptr = 0;
168   return ch;
169 }
170
171 int
172 get_token (fp, s)
173      register FILE *fp;
174      register sstring *s;
175 {
176   int c;
177   s->ptr = s->base;
178  retry:
179   c = ' ';
180   c = skip_spaces (fp, c);
181   if (c == '\n')
182     {
183       source_lineno++;
184       lineno++;
185       goto retry;
186     }
187   if (c == '#')
188     {
189       c = get_token (fp, s);
190       if (c == INT_TOKEN)
191         {
192           source_lineno = atoi (s->base) - 1; /* '\n' will add 1 */
193           get_token (fp, &source_filename);
194         }
195       for (;;)
196         {
197           c = getc (fp);
198           if (c == EOF)
199             return EOF;
200           if (c == '\n')
201             {
202             source_lineno++;
203             lineno++;
204             goto retry;
205             }
206         }
207     }
208   if (c == EOF)
209     return EOF;
210   if (ISDIGIT (c))
211     {
212       do
213         {
214           SSTRING_PUT(s, c);
215           c = getc (fp);
216         } while (c != EOF && ISDIGIT(c));
217       ungetc (c, fp);
218       c = INT_TOKEN;
219       goto done;
220     }
221   if (ISALPHA (c) || c == '_')
222     {
223       c = scan_ident (fp, s, c);
224       ungetc (c, fp);
225       return IDENTIFIER_TOKEN;
226     }
227   if (c == '\'' || c == '"')
228     {
229       c = scan_string (fp, s, c);
230       ungetc (c, fp);
231       return c == '\'' ? CHAR_TOKEN : STRING_TOKEN;
232     }
233   SSTRING_PUT(s, c);
234  done:
235   MAKE_SSTRING_SPACE(s, 1);
236   *s->ptr = 0;
237   return c;
238 }
239
240 unsigned int
241 hashstr (str, len)
242      const char *str;
243      unsigned int len;
244 {
245   unsigned int n = len;
246   unsigned int r = 0;
247   const unsigned char *s = (const unsigned char *)str;
248
249   do
250     r = r * 67 + (*s++ - 113);
251   while (--n);
252   return r + len;
253 }