-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
hexlex.cpp
265 lines (230 loc) · 5.64 KB
/
hexlex.cpp
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
/** @file hexlex.cpp HLexical analyzer for Hexen definition/script syntax.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2007-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 1999 Activision
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "hexlex.h"
#include <cstdlib>
#include <cstdio>
#include <cstring>
#define T_COMMENT ';' ///< Single-line comment.
#define T_QUOTE '"'
void HexLex::checkOpen()
{
if(!_script) Con_Error("HexLex: No script to parse!");
}
bool HexLex::atEnd()
{
checkOpen();
return (_readPos >= Str_Length(_script));
}
HexLex::HexLex()
: _script(0)
, _readPos(0)
, _lineNumber(0)
, _tokenAsNumber(0)
, _alreadyGot(false)
, _multiline(false)
{
Str_InitStd(&_sourcePath);
Str_InitStd(&_token);
}
HexLex::~HexLex()
{
Str_Free(&_sourcePath);
Str_Free(&_token);
}
void HexLex::parse(Str const *script, Str const *sourcePath)
{
_script = script;
if(!sourcePath)
{
Str_Clear(&_sourcePath);
}
else
{
Str_Copy(&_sourcePath, sourcePath);
}
Str_Clear(&_token);
_readPos = 0;
_lineNumber = 1;
_alreadyGot = false;
}
bool HexLex::readToken()
{
checkOpen();
if(_alreadyGot)
{
_alreadyGot = false;
return true;
}
_multiline = false;
if(atEnd())
{
return false;
}
bool foundToken = false;
while(!foundToken)
{
while(Str_At(_script, _readPos) <= ' ')
{
if(atEnd())
{
return false;
}
if(Str_At(_script, _readPos++) == '\n')
{
_lineNumber++;
_multiline = true;
}
}
if(atEnd())
{
return false;
}
if(Str_At(_script, _readPos) != T_COMMENT)
{
// Found a token
foundToken = true;
}
else
{
// Skip comment.
while(Str_At(_script, _readPos++) != '\n')
{
if(atEnd())
{
return false;
}
}
_lineNumber++;
_multiline = true;
}
}
Str_Clear(&_token);
if(Str_At(_script, _readPos) == T_QUOTE)
{
// Quoted string.
_readPos++;
while(Str_At(_script, _readPos) != T_QUOTE)
{
Str_AppendChar(&_token, Str_At(_script, _readPos++));
if(atEnd())
{
break;
}
}
_readPos++;
}
else
{
// Normal string.
while(Str_At(_script, _readPos) > ' ' &&
Str_At(_script, _readPos) != T_COMMENT)
{
Str_AppendChar(&_token, Str_At(_script, _readPos++));
if(atEnd())
{
break;
}
}
}
return true;
}
Str const *HexLex::readString()
{
if(!readToken())
{
scriptError("Missing string");
}
return &_token;
}
int HexLex::readNumber()
{
checkOpen();
if(!readToken())
{
scriptError("Missing integer");
}
char *stopper;
_tokenAsNumber = strtol(Str_Text(&_token), &stopper, 0);
if(*stopper != 0)
{
Con_Error("HexLex: Non-numeric constant '%s' in \"%s\" on line #%i",
Str_Text(&_token), F_PrettyPath(Str_Text(&_sourcePath)), _lineNumber);
}
return _tokenAsNumber;
}
Uri *HexLex::readTextureUri(char const *defaultScheme)
{
if(!readToken()) // Name.
{
scriptError("Missing texture Uri");
}
Uri *uri = Uri_SetScheme(Uri_New(), defaultScheme);
AutoStr *path = Str_PercentEncode(Str_Copy(AutoStr_NewStd(), &_token));
Uri_SetPath(uri, Str_Text(path));
return uri;
}
AutoStr *HexLex::readLumpName()
{
return AutoStr_FromText(Str_Text(readString()));
}
uint HexLex::readMapNumber()
{
uint num = readNumber();
return num > 0? num - 1 : num;
}
int HexLex::readSoundId()
{
return Def_Get(DD_DEF_SOUND_BY_NAME, Str_Text(readString()), 0);
}
int HexLex::readSoundIndex()
{
char const *name = Str_Text(readString());
int i = Def_Get(DD_DEF_SOUND_BY_NAME, name, 0);
if(!i)
{
AutoStr *msg = Str_Appendf(AutoStr_New(), "Unknown sound '%s'", name);
scriptError(Str_Text(msg));
}
return i;
}
// @note Assumes there is a valid string in sc_String.
void HexLex::unreadToken()
{
_alreadyGot = true;
}
Str const *HexLex::token()
{
return &_token;
}
int HexLex::lineNumber() const
{
return _lineNumber;
}
void HexLex::scriptError(char const *message)
{
Con_Error("HexLex: Error in \"%s\" on line #%i.\n%s",
F_PrettyPath(Str_Text(&_sourcePath)), _lineNumber, message);
}
void HexLex::scriptError()
{
AutoStr *msg = Str_Appendf(AutoStr_NewStd(), "Unexpected token '%s'", Str_Text(&_token));
scriptError(Str_Text(msg));
}