summaryrefslogtreecommitdiff
path: root/support/tools/Burg/lex.c
blob: 85eb8a738faf8e8f303272c87e00a65244ca854c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
char rcsid_lex[] = "$Id$";

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "b.h"
#include "fe.h"
#include "gram.tab.h"

static char buf[BUFSIZ];

static int yyline = 1;

typedef int (*ReadFn) ARGS((void));

static char *StrCopy ARGS((char *));
static int code_get ARGS((void));
static int simple_get ARGS((void));
static void ReadCharString ARGS((ReadFn, int));
static void ReadCodeBlock ARGS((void));
static void ReadOldComment ARGS((ReadFn));

static char *
StrCopy(s) char *s;
{
	char *t = (char *)zalloc(strlen(s) + 1);
	strcpy(t,s);
	return t;
}

static int
simple_get()
{
	int ch;
	if ((ch = getchar()) == '\n') {
		yyline++;
	}
	return ch;
}

static int
code_get()
{
	int ch;
	if ((ch = getchar()) == '\n') {
		yyline++;
	}
	if (ch != EOF) {
		fputc(ch, outfile);
	}
	return ch;
}

void
yypurge()
{
	while (code_get() != EOF) ;
}


static void
ReadCharString(rdfn, which) ReadFn rdfn; int which;
{
	int ch;
	int backslash = 0;
	int firstline = yyline;

	while ((ch = rdfn()) != EOF) {
		if (ch == which && !backslash) {
			return;
		}
		if (ch == '\\' && !backslash) {
			backslash = 1;
		} else {
			backslash = 0;
		}
	}
	yyerror1("Unexpected EOF in string on line ");
	fprintf(stderr, "%d\n", firstline);
	exit(1);
}

static void
ReadOldComment(rdfn) ReadFn rdfn;
{
	/* will not work for comments delimiter in string */

	int ch;
	int starred = 0;
	int firstline = yyline;

	while ((ch = rdfn()) != EOF) {
		if (ch == '*') {
			starred = 1;
		} else if (ch == '/' && starred) {
			return;
		} else {
			starred = 0;
		}
	}
	yyerror1("Unexpected EOF in comment on line ");
	fprintf(stderr, "%d\n", firstline);
	exit(1);
}

static void
ReadCodeBlock()
{
	int ch;
	int firstline = yyline;

	while ((ch = getchar()) != EOF) {
		if (ch == '%') {
			ch = getchar();
			if (ch != '}') {
				yyerror("bad %%");
			}
			return;
		}
		fputc(ch, outfile);
		if (ch == '\n') {
			yyline++;
		}
		if (ch == '"' || ch == '\'') {
			ReadCharString(code_get, ch);
		} else if (ch == '/') {
			ch = getchar();
			if (ch == '*') {
				fputc(ch, outfile);
				ReadOldComment(code_get);
				continue;
			} else {
				ungetc(ch, stdin);
			}
		}
	}
	yyerror1("Unclosed block of C code started on line ");
	fprintf(stderr, "%d\n", firstline);
	exit(1);
}

static int done;
void
yyfinished()
{
	done = 1;
}

int
yylex()
{
	int ch;
	char *ptr = buf;

	if (done) return 0;
	while ((ch = getchar()) != EOF) {
		switch (ch) {
		case ' ':
		case '\f':
		case '\t':
			continue;
		case '\n':
			yyline++;
			continue;
		case '(':
		case ')':
		case ',':
		case ':':
		case ';':
		case '=':
			return(ch);
		case '/':
			ch = getchar();
			if (ch == '*') {
				ReadOldComment(simple_get);
				continue;
			} else {
				ungetc(ch, stdin);
				yyerror("illegal char /");
				continue;
			}
		case '%':
			ch = getchar();
			switch (ch) {
			case '%':
				return (K_PPERCENT);
			case '{':
				ReadCodeBlock();
				continue;
			case 's':
			case 'g':
			case 't':
				do {
					if (ptr >= &buf[BUFSIZ]) {
						yyerror("ID too long");
						return(ERROR);
					} else {
						*ptr++ = ch;
					}
					ch = getchar();
				} while (isalpha(ch) || isdigit(ch) || ch == '_');
				ungetc(ch, stdin);
				*ptr = '\0';
				if (!strcmp(buf, "term")) return K_TERM;
				if (!strcmp(buf, "start")) return K_START;
				if (!strcmp(buf, "gram")) return K_GRAM;
				yyerror("illegal character after %%");
				continue;
			default:
				yyerror("illegal character after %%");
				continue;
			}
		default:
			if (isalpha(ch) ) {
				do {
					if (ptr >= &buf[BUFSIZ]) {
						yyerror("ID too long");
						return(ERROR);
					} else {
						*ptr++ = ch;
					}
					ch = getchar();
				} while (isalpha(ch) || isdigit(ch) || ch == '_');
				ungetc(ch, stdin);
				*ptr = '\0';
				yylval.y_string = StrCopy(buf);
				return(ID);
			} 
			if (isdigit(ch)) {
				int val=0;
				do {
					val *= 10;
					val += (ch - '0');
					ch = getchar();
				} while (isdigit(ch));
				ungetc(ch, stdin);
				yylval.y_int = val;
				return(INT);
			}
			yyerror1("illegal char ");
			fprintf(stderr, "(\\%03o)\n", ch);
			exit(1);
		}
	}
	return(0);
}

void yyerror1(const char *str)
{
	fprintf(stderr, "line %d: %s", yyline, str);
}

void
yyerror(const char *str)
{
	yyerror1(str);
	fprintf(stderr, "\n");
	exit(1);
}