summaryrefslogtreecommitdiff
path: root/Xenogears/compil-2.lex
blob: 9ad13f831ad5567c3e1311191cd5da65888ef2a5 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    /* In text block */
%s I
    /* Outside of text block */
%s O
    /* Waiting for Width argument */
%s W
    /* Waiting for OpCode arguments */
%s Op

    #include <string.h>

    int num_blocks = 0;
    int num_lines = 0;
    int cur_num_lines = 0;
    int cur_num_block = 0;
    int line_width = 0;
    int max_line_width = 0;
    int block_lines = 0;
    int block_width = 0;
    int errstate = 0;
    void yputc(int c, int a);
    void unputs(char *);

%%

<INITIAL>"<Blocks:"\ *[[:digit:]]+">"\n	{
	num_blocks = atoi(yytext + 8);
	fprintf(stderr, "Number of blocks: %i\n", num_blocks);
	
	num_lines = 2;
	
	BEGIN(O);
    }

<INITIAL>.*	{
	fprintf(stderr, "Invalid file, must begin with <Blocks: XX>\n");
	errstate = 1;
	num_lines = 2;
	num_blocks = 9999;
    }

<O>"<Text_block lines:"\ *[[:digit:]]+\ +	{
	if (cur_num_block >= num_blocks) {
	    fprintf(stderr, "Error: too much blocks at line %i\n", num_lines);
	    errstate = 1;
	}
	block_lines = atoi(yytext + 18);
	BEGIN(W);
    }

<W>"width:"\ *[[:digit:]]+">\n"	{
	block_width = atoi(yytext + 6);
	fprintf(stderr, "Begin of block %i at line %i -- lines = %i, width = %i\n", cur_num_block, num_lines, block_lines, block_width);
	num_lines++;
	line_width = 0;
	max_line_width = 0;
	cur_num_lines = 1;
	BEGIN(I);
    }

<W>.*	{
	fprintf(stderr, "Error: invalid arguments to command Text_block at line %i: '%s'\n", num_lines, yytext);
	errstate = 1;
	BEGIN(O);
    }

<I>\n	{
	if (line_width > block_width) {
	    fprintf(stderr, "Warning: line %i too long (%i > %i).\n", num_lines, line_width, block_width);
	}
	if (line_width > max_line_width)
	    max_line_width = line_width;
	line_width = 0;
	num_lines++;
	cur_num_lines++;
	if (cur_num_lines > block_lines) {
	    fprintf(stderr, "Warning: too much lines for text block %i at line %i (%i > %i)\n", cur_num_block, num_lines, cur_num_lines, block_lines);
	}
	putc('\n', yyout);
    }

<I>"<New>\n"	{
	if (line_width > block_width) {
	    fprintf(stderr, "Warning: line %i too long (%i > %i).\n", num_lines, line_width, block_width);
	}
	if (line_width > max_line_width)
	    max_line_width = line_width;
	line_width = 0;
	num_lines++;
	cur_num_lines = 1;
	putc('\n', yyout);
    }

<I>"<Wait>\n"	{
	if (line_width > block_width) {
	    fprintf(stderr, "Warning: line %i too long (%i > %i).\n", num_lines, line_width, block_width);
	}
	if (line_width > max_line_width)
	    max_line_width = line_width;
	line_width = 0;
	num_lines++;
	cur_num_lines = 1;
	putc('\n', yyout);
    }

<I>"<Wait>"	{
	cur_num_lines = 1;
    }

<I>"<Delay"\ +[[:digit:]]+">"	{
	int d = atoi(yytext + 7);
	if (d > 255) {
	    fprintf(stderr, "Error: delay too important: %i\n", d);
	    errstate = 1;
	}
    }

<I>"<Gear"\ +[[:digit:]]+">" {
	int d = atoi(yytext + 5);
	if (d > 255) {
	    fprintf(stderr, "Error: gear number too important: %i\n", d);
	    errstate = 1;
	} else {
	    putc(0xf, yyout);
	    putc(5, yyout);
	    putc(d, yyout);
	    line_width += 16;
	}
    }

<I>"<Opcode"\ +/[[:digit:]]+\ +[[:digit:]]+">"	{
	BEGIN(Op);
    }

<Op>[[:digit:]]+\ +[[:digit:]]+">"	{
	int o1, o2;
	o1 = atoi(yytext);
	o2 = atoi(yytext + (o1 < 10 ? 2 : o1 < 100 ? 3 : 4));
	BEGIN(I);
    }

<Op>[^<]*">"	{
	yytext[strlen(yytext - 1)] = 0;
	fprintf(stderr, "Error: invalid OpCode '%s'\n", yytext);
	errstate = 1;
	BEGIN(I);
    }

<I>"<"[[:digit:]]">"	unput(*(yytext + 1));
<I>"<".">"	unput(*(yytext + 1));
<I>"<..>"	unput('.'); unput('.');
<I>"<...>"	unput('.'); unput('.'); unput('.');

<I>"<Extra1"\ +[[:digit:]]+">"	{
	int d = atoi(yytext + 7);
	if (d > 255) {
	    fprintf(stderr, "Error: Extra1 number too important: %i\n", d);
	    errstate = 1;
	} else {
	    putc(0xfe, yyout);
	    putc(d, yyout);
	}
    }

<I>"<Extra2"\ +[[:digit:]]+">"	{
	int d = atoi(yytext + 7);
	if (d > 255) {
	    fprintf(stderr, "Error: Extra2 number too important: %i\n", d);
	    errstate = 1;
	} else {
	    putc(0xff, yyout);
	    putc(d, yyout);
	}
    }

<I>"<Bare"\ +[[:xdigit:]]{1,2}">"	{
	char str[5] = {'0', 'x', 0, 0, 0};
	int d;
	char * _yytext = yytext;
	for (_yytext += 6; *_yytext == ' '; _yytext++);
	str[2] = *(_yytext++);
	str[3] = *(_yytext++);
	sscanf(str, "%i", &d);
	if (d > 255) {
	    fprintf(stderr, "Error: Bare number too important: %i\n", d);
	    errstate = 1;
	} else {
	    putc(d, yyout);
	}
    }

<I>"<oe>"	unput('e'); unput('o');
<I>"<ae>"	unput('e'); unput('a');

>"<Elly>"     unputs("<Gear 01>");
<I>"<Citan>"    unputs("<Gear 02>");
<I>"<Bart>"     unputs("<Gear 03>");
<I>"<Billy>"    unputs("<Gear 04>");
<I>"<Rico>"     unputs("<Gear 05>");
<I>"<Emeralda>" unputs("<Gear 06>");
<I>"<Chu-Chu>"  unputs("<Gear 07>");
<I>"<Maria>"    unputs("<Gear 08>");
<I>"<Citan-2>"  unputs("<Gear 09>");
<I>"<Emeralda-2>"       unputs("<Gear 10>");
<I>"<Weltall>"  unputs("<Gear 11>");
<I>"<Waltall-2>"        unputs("<Gear 12>");
<I>"<Vierge>"   unputs("<Gear 13>");
<I>"<Heimdal>"  unputs("<Gear 14>");
<I>"<Brigandier>"       unputs("<Gear 15>");
<I>"<Renmazuo>" unputs("<Gear 16>");
<I>"<Stier>"    unputs("<Gear 17>");
<I>"<BigChu-chu>"       unputs("<Gear 18>");
<I>"<Seibzehn>" unputs("<Gear 19>");
<I>"<Crescens>" unputs("<Gear 20>");
<I>"<Regurus>"  unputs("<Gear 21>");
<I>"<Fenrir>"   unputs("<Gear 22>");
<I>"<Andvari>"  unputs("<Gear 23>");
<I>"<Renmazuo>" unputs("<Gear 24>");
<I>"<Stier-2>"  unputs("<Gear 25>");
<I>"<Xenogears>"        unputs("<Gear 26>");
<I>"<BARTHOS>"  unputs("<Gear 27>");
<I>"<Yggdra>"   unputs("<Gear 29>");
<I>"<Perso1>"   unputs("<Gear 128>");
<I>"<Perso2>"   unputs("<Gear 129>");
<I>"<Perso3>"   unputs("<Gear 130>");


<I>"\n<End_of_block>"	{	
	if (line_width > max_line_width)
	    max_line_width = line_width;
	if (max_line_width < block_width)
	    fprintf(stderr, "Warning: block %i too large (%i < %i)\n", cur_num_block, max_line_width, block_width);
	num_lines++;
	cur_num_block++;
	putc('\n', yyout);
	putc('\n', yyout);
	BEGIN(O);
    }
    
<I>"<"[^\>\n]*">"	{
	yytext[strlen(yytext) - 1] = 0;
	fprintf(stderr, "Error: Invalid command at line %i: '%s'\n", num_lines, yytext + 1);
	errstate = 1;
    }

<O>\n	num_lines++;
<O>.	/* Eat up comments */

<I>\210 unputs("à");
<I>\211 unputs("â");
<I>\216 unputs("é");
<I>\217 unputs("è");
<I>\220 unputs("ê");
<I>\224 unputs("î");
<I>\225 unputs("ï");
<I>\231 unputs("ô");
<I>\235 unputs("ù");
<I>\236 unputs("û");
<I>\215 unputs("ç");

<I>.	{
	putc(*yytext, yyout);
    }

.	{
	fprintf(stderr, "Hu uh, something's wrong at line %i...\n", num_lines);
    }

%%

int	yywrap(void) {
    if (cur_num_block < num_blocks) {
	fprintf(stderr, "Warning: Too few blocks at end of file.\n");
    }
    exit(errstate ? -1 : 0);
}

int	main(int argc, char ** argv) {
    if ((argc < 2) || (argc > 3)) {
	fprintf(stderr, "Usage: %s <output> [input]\n", argv[0]);
	exit(-1);
    }
    if (!(yyout = fopen(argv[1], "wb"))) {
	fprintf(stderr, "Error: can't open file %s\n", argv[1]);
	exit(-1);
    }
    if (argc == 3) {
	if (!(yyin = fopen(argv[2], "rb"))) {
	    fprintf(stderr, "Error: can't open file %s\n", argv[2]);
	    exit(-1);
	}
    }
    fprintf(stderr, "Creating file %s\n", argv[1]);
    yylex();
    exit(errstate ? -1 : 0);
}

void	yputc(int c, int a) {
    line_width += a;
    putc(c, yyout);
}

void    unputs(char * s) {
    int l = strlen(s), i;

    for (i = l - 1; i >= 0; i--) {
        unput(s[i]);
    }
}