File: uclex.ll

package info (click to toggle)
exult 1.2-15.3
  • links: PTS, VCS
  • area: contrib
  • in suites: jessie, jessie-kfreebsd
  • size: 8,656 kB
  • ctags: 10,524
  • sloc: cpp: 99,373; sh: 7,324; ansic: 4,659; makefile: 991; yacc: 769; lex: 313; xml: 19
file content (313 lines) | stat: -rw-r--r-- 6,590 bytes parent folder | download | duplicates (8)
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
310
311
312
313
%{
/**
 **	Uclex.ll - Usecode lexical scanner.
 **
 **	Written: 12/30/2000 - JSF
 **/

/*
Copyright (C) 2000 The Exult Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include <string>
#include <vector>
#include "ucparse.h"
#include "ucloc.h"

using std::string;

extern std::vector<char *> include_dirs;	// -I directories.

/*
 *	Want a stack of files for implementing '#include'.
 */
std::vector<Uc_location *> locstack;
std::vector<YY_BUFFER_STATE> bufstack;

/*
 *	Parse out a name in quotes.
 *
 *	Output:	->name, with a null replacing the ending quote.
 *		0 if not found.
 */

static char *Find_name
	(
	char *name,
	char *& ename			// ->null at end of name returned.
	)
	{
	while (*name && *name != '"')	// Find start of filename.
		name++;
	if (!*name)
		return 0;
	name++;				// Point to name.
	ename = name;			// Find end.
	while (*ename && *ename != '"')
		ename++;
	if (!*ename)
		return 0;
	*ename = 0;
	return name;
	}

/*
 *	Set location from a preprocessor string.
 */

static void Set_location
	(
	char *text			// ->first digit of line #.
	)
	{
	char *name;
	int line = strtol(text, &name, 10);
	char *ename;
	name = Find_name(name, ename);
	if (!name)
		return;
//cout << "Setting location at line " << line - 1 << endl;
					// We're 0-based.
	Uc_location::set_cur(name, line - 1);
	*name = '"';			// Restore text.
	}

/*
 *	Include another source.
 */

static void Include
	(
	char *yytext			// ->text containing name.
	)
	{
	char msg[180];
	if (bufstack.size() > 20)
		{
		Uc_location::yyerror("#includes are nested too deeply");
		exit(1);
		}
	char *ename;
	char *name = Find_name(yytext, ename);
	if (!name)
		{
		Uc_location::yyerror("No file in #include");
		return;
		}
	locstack.push_back(new Uc_location());
	bufstack.push_back(YY_CURRENT_BUFFER);
	yyin = fopen(name, "r");
					// Look in -I list if not found here.
	for (std::vector<char *>::const_iterator it = include_dirs.begin();
				!yyin && it != include_dirs.end(); ++it)
		{
		string path(*it);
		path += '/';
		path += name;
		yyin = fopen(path.c_str(), "r");
		}
	if (!yyin)
		{
		sprintf(msg, "Can't open '%s'", name);
		Uc_location::yyerror(msg);
		exit(1);
		}
					// Set location to new file.
	Uc_location::set_cur(name, 0);
	yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
	}

/*
 *	Make a copy of a string, interpreting '\' codes.
 *
 *	Output:	->allocated string.
 */

char *Handle_string
	(
	const char *from			// Ends with a '"'.
	)
	{
	char *to = new char[1 + strlen(from)];	// (Bigger than needed.)
	char *str = to;

	while (*from && *from != '\"')
		{
		if (*from != '\\')
			{
			*to++ = *from++;
			continue;
			}
		switch (*++from)
			{
		case 'n':
			*to++ = '\n'; break;
		default:
			*to++ = *from; break;
			}
		++from;
		}
	*to = 0;
	return str;
	}

extern "C" int yywrap() { return 1; }		/* Stop at EOF. */

%}

%x comment

%%

if		return IF;
else		return ELSE;
return		return RETURN;
while		return WHILE;
for		return FOR;
in		return UCC_IN;
with		return WITH;
to		return TO;
var		return VAR;
int		return UCC_INT;
const		return UCC_CONST;
string		return STRING;
enum		return ENUM;
extern		return EXTERN;
true		return UCTRUE;
false		return UCFALSE;
break		return BREAK;
case		return CASE;
static		return STATIC_;

converse	return CONVERSE;
say		return SAY;
add		return ADD;
hide		return HIDE;
message		return MESSAGE;
response	return RESPONSE;
script		return SCRIPT;
after		return AFTER;
ticks		return TICKS;
hours		return HOURS;
event		return EVENT;
gflags		return FLAG;
item		return ITEM;
goto		return GOTO;
".original"	return ORIGINAL;

					/* Script commands. */
continue	return CONTINUE;
repeat		return REPEAT;
nop		return NOP;
nohalt		return NOHALT;
wait		return WAIT;
remove		return REMOVE;
rise		return RISE;
descent		return DESCEND;
frame		return FRAME;
hatch		return HATCH;
next		return NEXT;
previous	return PREVIOUS;
cycle		return CYCLE;
step		return STEP;
music		return MUSIC;
call		return CALL;
speech		return SPEECH;
sfx		return SFX;
face		return FACE;
hit		return HIT;
actor		return ACTOR;

north		return NORTH;
south		return SOUTH;
east		return EAST;
west		return WEST;
nw		return NW;
ne		return NE;
sw		return SW;
se		return SE;

"&&"		return AND;
"||"		return OR;
"!"		return NOT;

[a-zA-Z][a-zA-Z0-9_]*	{
			yylval.strval = strdup(yytext);
			return IDENTIFIER;
			}
\"([^"]|\\.)*\"		{
					// Remove ending quote.
			yylval.strval = Handle_string(yytext + 1);
			return STRING_LITERAL;
			}
\"([^"]|\\.)*\"\*		{
					// Remove ending quote and asterisk.
			yylval.strval = strdup(yytext + 1);
			yylval.strval[strlen(yylval.strval) - 2] = 0;
			return STRING_PREFIX;
			}
[0-9]+			{
			yylval.intval = atoi(yytext);
			return INT_LITERAL;
			}
0x[0-9a-fA-F]+		{
			yylval.intval = strtol(yytext + 2, 0, 16);
			return INT_LITERAL;
			}

"=="			{ return EQUALS; }
"!="			{ return NEQUALS; }
"<="			{ return LTEQUALS; }
">="			{ return GTEQUALS; }
"->"			{ return UCC_POINTS; }

"# "[0-9]+\ \"[^"]*\".*\n	{ Set_location(yytext + 2); }
"#line "[0-9]+\ \"[^"]*\".*\n	{ Set_location(yytext + 6); }
"#include"[ \t]+.*\n		{ Include(yytext + 8); }

\#.*			/* Ignore other cpp directives. */

[ \t\r]+					/* Ignore spaces. */
"//".*						/* Comments. */
"/*"			BEGIN(comment);
<comment>[^*\n]*				/* All but '*'. */
<comment>"*"+[^*/\n]*				/* *'s not followed by '/'. */
<comment>\n		{ Uc_location::increment_cur_line(); }
<comment>"*/"		BEGIN(INITIAL);
<comment><<EOF>>	{ Uc_location::yyerror("Comment not terminated");
			yyterminate(); }
\n			{ Uc_location::increment_cur_line(); }
.			return *yytext;		/* Being lazy. */
<<EOF>>			{
			if (locstack.empty())
				yyterminate();
			else		// Restore buffer and location.
				{
				Uc_location *loc = locstack.back();
				locstack.pop_back();
				const char *nm = loc->get_source();
				loc->set_cur(nm, loc->get_line());
				delete loc;
				yy_delete_buffer(YY_CURRENT_BUFFER);
				yy_switch_to_buffer(bufstack.back());
				bufstack.pop_back();
				}
			}


%%