Initial revision
[official-gcc.git] / gcc / scan.c
blob4f575b38a9fe8d135f732be787cdb3e0e340ee24
1 /* Utility functions for scan-decls and fix-header programs.
2 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
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.
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.
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. */
18 #include "scan.h"
19 #include "hconfig.h"
20 #include <ctype.h>
22 int lineno = 1;
23 int source_lineno = 1;
24 sstring source_filename;
26 void
27 make_sstring_space (str, count)
28 sstring *str;
29 int count;
31 int cur_pos = str->ptr - str->base;
32 int cur_size = str->limit - str->base;
33 int new_size = cur_pos + count + 100;
35 if (new_size <= cur_size)
36 return;
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;
46 void
47 sstring_append (dst, src)
48 sstring *dst;
49 sstring *src;
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;
61 int
62 scan_ident (fp, s, c)
63 register FILE *fp;
64 register sstring *s;
65 int c;
67 s->ptr = s->base;
68 if (isalpha(c) || c == '_')
70 for (;;)
72 SSTRING_PUT(s, c);
73 c = getc (fp);
74 if (c == EOF || !(isalnum(c) || c == '_'))
75 break;
78 MAKE_SSTRING_SPACE(s, 1);
79 *s->ptr = 0;
80 return c;
83 int
84 scan_string (fp, s, init)
85 register FILE *fp;
86 register sstring *s;
88 int c;
89 for (;;)
91 c = getc (fp);
92 if (c == EOF || c == '\n')
93 break;
94 if (c == init)
96 c = getc (fp);
97 break;
99 if (c == '\\')
101 c = getc (fp);
102 if (c == EOF)
103 break;
104 if (c == '\n')
105 continue;
107 SSTRING_PUT(s, c);
109 MAKE_SSTRING_SPACE(s, 1);
110 *s->ptr = 0;
111 return c;
114 /* Skip horizontal white spaces (spaces, tabs, and C-style comments). */
117 skip_spaces (fp, c)
118 register FILE *fp;
119 int c;
121 for (;;)
123 if (c == ' ' || c == '\t')
124 c = getc (fp);
125 else if (c == '/')
127 c = getc (fp);
128 if (c != '*')
130 ungetc (c, fp);
131 return '/';
133 c = getc (fp);
134 for (;;)
136 if (c == EOF)
137 return EOF;
138 else if (c != '*')
140 if (c == '\n')
141 source_lineno++, lineno++;
142 c = getc (fp);
144 else if ((c = getc (fp)) == '/')
145 return getc (fp);
148 else
149 break;
151 return c;
155 read_upto (fp, str, delim)
156 FILE *fp;
157 sstring *str;
158 int delim;
160 int ch;
161 for (;;)
163 ch = getc (fp);
164 if (ch == EOF || ch == delim)
165 break;
166 SSTRING_PUT(str, ch);
168 MAKE_SSTRING_SPACE(str, 1);
169 *str->ptr = 0;
170 return ch;
174 get_token (fp, s)
175 register FILE *fp;
176 register sstring *s;
178 int c;
179 s->ptr = s->base;
180 retry:
181 c = ' ';
182 c = skip_spaces (fp, c);
183 if (c == '\n')
185 source_lineno++;
186 lineno++;
187 goto retry;
189 if (c == '#')
191 c = get_token (fp, s);
192 if (c == INT_TOKEN)
194 source_lineno = atoi (s->base) - 1; /* '\n' will add 1 */
195 get_token (fp, &source_filename);
197 for (;;)
199 c = getc (fp);
200 if (c == EOF)
201 return EOF;
202 if (c == '\n')
204 source_lineno++;
205 lineno++;
206 goto retry;
210 if (c == EOF)
211 return EOF;
212 if (isdigit (c))
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;
223 if (isalpha (c) || c == '_')
225 c = scan_ident (fp, s, c);
226 ungetc (c, fp);
227 return IDENTIFIER_TOKEN;
229 if (c == '\'' || c == '"')
231 c = scan_string (fp, s, c);
232 ungetc (c, fp);
233 return c == '\'' ? CHAR_TOKEN : STRING_TOKEN;
235 SSTRING_PUT(s, c);
236 done:
237 MAKE_SSTRING_SPACE(s, 1);
238 *s->ptr = 0;
239 return c;