Mon, 16 Jul 2012 04:26:27 +0300
Negative numbers are now considered numbers too...
0 | 1 | /* |
2 | * botc source code | |
3 | * Copyright (C) 2012 Santeri `Dusk` Piippo | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright notice, | |
10 | * this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
3 | 14 | * 3. Neither the name of the developer nor the names of its contributors may |
15 | * be used to endorse or promote products derived from this software without | |
16 | * specific prior written permission. | |
0 | 17 | * 4. Redistributions in any form must be accompanied by information on how to |
18 | * obtain complete source code for the software and any accompanying | |
19 | * software that uses the software. The source code must either be included | |
20 | * in the distribution or be available for no more than the cost of | |
21 | * distribution plus a nominal fee, and must be freely redistributable | |
22 | * under reasonable conditions. For an executable file, complete source | |
23 | * code means the source code for all modules it contains. It does not | |
24 | * include source code for modules or files that typically accompany the | |
25 | * major components of the operating system on which the executable file | |
26 | * runs. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
34 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
35 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
36 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
37 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
38 | * POSSIBILITY OF SUCH DAMAGE. | |
39 | */ | |
40 | ||
41 | #include <stdio.h> | |
42 | #include <stdlib.h> | |
43 | #include "string.h" | |
44 | #include "str.h" | |
45 | #include "common.h" | |
46 | #include "scriptreader.h" | |
47 | ||
19
66993500719f
Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
12
diff
changeset
|
48 | static bool IsWhitespace (char c) { |
0 | 49 | // These characters are invisible, thus considered whitespace |
50 | if (c <= 32 || c == 127 || c == 255) | |
51 | return true; | |
52 | ||
53 | return false; | |
54 | } | |
55 | ||
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
56 | ScriptReader::ScriptReader (str path) { |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
57 | if (!(fp = fopen (path, "r"))) { |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
58 | error ("couldn't open %s for reading!\n", path.chars ()); |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
59 | exit (1); |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
60 | } |
0 | 61 | |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
62 | filepath = path; |
0 | 63 | curline = 1; |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
64 | curchar = 1; |
0 | 65 | pos = 0; |
66 | token = ""; | |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
67 | atnewline = false; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
68 | commentmode = 0; |
0 | 69 | } |
70 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
71 | ScriptReader::~ScriptReader () { |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
72 | FinalChecks (); |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
73 | fclose (fp); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
74 | } |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
75 | |
0 | 76 | char ScriptReader::ReadChar () { |
77 | char* c = (char*)malloc (sizeof (char)); | |
78 | if (!fread (c, sizeof (char), 1, fp)) | |
79 | return 0; | |
80 | ||
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
81 | // We're at a newline, thus next char read will begin the next line |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
82 | if (atnewline) { |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
83 | atnewline = false; |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
84 | curline++; |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
85 | curchar = 0; // gets incremented to 1 |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
86 | } |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
87 | |
0 | 88 | if (c[0] == '\n') |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
89 | atnewline = true; |
0 | 90 | |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
91 | curchar++; |
0 | 92 | return c[0]; |
93 | } | |
94 | ||
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
95 | char ScriptReader::PeekChar (int offset) { |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
96 | // Store current position |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
97 | long curpos = ftell (fp); |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
98 | |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
99 | // Forward by offset |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
100 | fseek (fp, offset, SEEK_CUR); |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
101 | |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
102 | // Read the character |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
103 | char* c = (char*)malloc (sizeof (char)); |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
104 | if (!fread (c, sizeof (char), 1, fp)) |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
105 | return 0; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
106 | |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
107 | // Rewind back |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
108 | fseek (fp, curpos, SEEK_SET); |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
109 | |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
110 | return c[0]; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
111 | } |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
112 | |
0 | 113 | // true if was found, false if not. |
114 | bool ScriptReader::Next () { | |
115 | str tmp = ""; | |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
116 | |
0 | 117 | while (!feof (fp)) { |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
118 | // Check if the next token possibly starts a comment. |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
119 | if (PeekChar () == '/' && !tmp.len()) { |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
120 | char c2 = PeekChar (1); |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
121 | // C++-style comment |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
122 | if (c2 == '/') |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
123 | commentmode = 1; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
124 | else if (c2 == '*') |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
125 | commentmode = 2; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
126 | |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
127 | // We don't need to actually read in the |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
128 | // comment characters, since they will get |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
129 | // ignored due to comment mode anyway. |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
130 | } |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
131 | |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
132 | c = ReadChar (); |
0 | 133 | |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
134 | // If this is a comment we're reading, check if this character |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
135 | // gets the comment terminated, otherwise ignore it. |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
136 | if (commentmode > 0) { |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
137 | if (commentmode == 1 && c == '\n') { |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
138 | // C++-style comments are terminated by a newline |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
139 | commentmode = 0; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
140 | continue; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
141 | } else if (commentmode == 2 && c == '*') { |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
142 | // C-style comments are terminated by a `*/` |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
143 | if (PeekChar() == '/') { |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
144 | commentmode = 0; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
145 | // Now the char has to be read in since we |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
146 | // no longer are reading a comment |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
147 | ReadChar (); |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
148 | } |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
149 | } |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
150 | |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
151 | // Otherwise, ignore it. |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
152 | continue; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
153 | } |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
154 | |
11
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
155 | // Non-alphanumber characters (sans underscore) break the word too. |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
156 | // If there was prior data, the delimeter pushes the cursor back so |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
157 | // that the next character will be the same delimeter. If there isn't, |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
158 | // the delimeter itself is included (and thus becomes a token itself.) |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
159 | if ((c >= 33 && c <= 47) || |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
160 | (c >= 58 && c <= 64) || |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
161 | (c >= 91 && c <= 96 && c != '_') || |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
162 | (c >= 123 && c <= 126)) { |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
163 | if (tmp.len()) |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
164 | fseek (fp, ftell (fp) - 1, SEEK_SET); |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
165 | else |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
166 | tmp += c; |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
167 | break; |
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
168 | } |
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
169 | |
19
66993500719f
Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
12
diff
changeset
|
170 | if (IsWhitespace (c)) { |
0 | 171 | // Don't break if we haven't gathered anything yet. |
172 | if (tmp.len()) | |
173 | break; | |
174 | } else { | |
175 | tmp += c; | |
176 | } | |
177 | } | |
178 | ||
19
66993500719f
Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
12
diff
changeset
|
179 | // If we got nothing here, read failed. This should |
66993500719f
Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
12
diff
changeset
|
180 | // only hapen in the case of EOF. |
0 | 181 | if (!tmp.len()) { |
182 | token = ""; | |
183 | return false; | |
184 | } | |
185 | ||
186 | pos++; | |
187 | token = tmp; | |
188 | return true; | |
189 | } | |
190 | ||
191 | // Returns the next token without advancing the cursor. | |
192 | str ScriptReader::PeekNext () { | |
193 | // Store current position | |
194 | int cpos = ftell (fp); | |
195 | ||
196 | // Advance on the token. | |
197 | if (!Next ()) | |
198 | return ""; | |
199 | ||
200 | str tmp = token; | |
201 | ||
202 | // Restore position | |
203 | fseek (fp, cpos, SEEK_SET); | |
204 | pos--; | |
205 | ||
206 | return tmp; | |
207 | } | |
208 | ||
209 | void ScriptReader::Seek (unsigned int n, int origin) { | |
210 | switch (origin) { | |
211 | case SEEK_SET: | |
212 | fseek (fp, 0, SEEK_SET); | |
213 | pos = 0; | |
214 | break; | |
215 | case SEEK_CUR: | |
216 | break; | |
217 | case SEEK_END: | |
218 | printf ("ScriptReader::Seek: SEEK_END not yet supported.\n"); | |
219 | break; | |
220 | } | |
221 | ||
222 | for (unsigned int i = 0; i < n+1; i++) | |
223 | Next(); | |
224 | } | |
225 | ||
226 | void ScriptReader::MustNext (const char* c) { | |
227 | if (!Next()) { | |
228 | if (strlen (c)) | |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
229 | ParserError ("expected `%s`, reached end of file instead\n", c); |
0 | 230 | else |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
231 | ParserError ("expected a token, reached end of file instead\n"); |
0 | 232 | } |
233 | ||
234 | if (strlen (c) && token.compare (c) != 0) { | |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
235 | ParserError ("expected `%s`, got `%s` instead", c, token.chars()); |
0 | 236 | } |
237 | } | |
238 | ||
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
239 | void ScriptReader::ParserError (const char* message, ...) { |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
240 | PERFORM_FORMAT (message, outmessage); |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
241 | ParserMessage ("\nParse error\n", outmessage); |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
242 | exit (1); |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
243 | } |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
244 | |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
245 | void ScriptReader::ParserWarning (const char* message, ...) { |
0 | 246 | PERFORM_FORMAT (message, outmessage); |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
247 | ParserMessage ("Warning: ", outmessage); |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
248 | } |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
249 | |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
250 | void ScriptReader::ParserMessage (const char* header, char* message) { |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
251 | fprintf (stderr, "%sIn file %s, at line %u, col %u: %s\n", |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
252 | header, filepath.chars(), curline, curchar, message); |
9
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
253 | } |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
254 | |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
255 | void ScriptReader::MustString () { |
9
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
256 | MustNext ("\""); |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
257 | |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
258 | str string; |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
259 | // Keep reading characters until we find a terminating quote. |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
260 | while (1) { |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
261 | // can't end here! |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
262 | if (feof (fp)) |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
263 | ParserError ("unterminated string"); |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
264 | |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
265 | char c = ReadChar (); |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
266 | if (c == '"') |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
267 | break; |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
268 | |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
269 | string += c; |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
270 | } |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
271 | |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
272 | token = string; |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
273 | } |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
274 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
275 | void ScriptReader::MustNumber () { |
30
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
276 | str num; |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
277 | MustNext (); |
30
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
278 | num += token; |
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
279 | |
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
280 | // Cater for a possible minus sign, since it |
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
281 | // breaks the token, read a new one with the number. |
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
282 | if (!token.compare ("-")) { |
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
283 | MustNext (); |
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
284 | num += token; |
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
285 | } |
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
286 | |
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
287 | if (!num.isnumber()) |
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
288 | ParserError ("expected a number, got `%s`", num.chars()); |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
289 | } |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
290 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
291 | void ScriptReader::MustBool () { |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
292 | MustNext(); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
293 | if (!token.compare ("0") || !token.compare ("1") || |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
294 | !token.compare ("true") || !token.compare ("false") || |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
295 | !token.compare ("yes") || !token.compare ("no")) { |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
296 | return; |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
297 | } |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
298 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
299 | ParserError ("expected a boolean value, got `%s`", token.chars()); |
20
d7b13805d1e0
Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
300 | } |
d7b13805d1e0
Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
301 | |
d7b13805d1e0
Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
302 | bool ScriptReader::BoolValue () { |
d7b13805d1e0
Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
303 | return (!token.compare ("1") || !token.compare ("true") || !token.compare ("yes")); |
22
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
304 | } |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
305 | |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
306 | void ScriptReader::MustValue (int type) { |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
307 | switch (type) { |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
308 | case RETURNVAL_INT: MustNumber (); break; |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
309 | case RETURNVAL_STRING: MustString (); break; |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
310 | case RETURNVAL_BOOLEAN: MustBool (); break; |
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
311 | } |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
312 | } |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
313 | |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
314 | // Checks to be performed at the end of file |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
315 | void ScriptReader::FinalChecks () { |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
316 | // If comment mode is 2 by the time the file ended, the |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
317 | // comment was left unterminated. 1 is no problem, since |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
318 | // it's terminated by newlines anyway. |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
319 | if (commentmode == 2) |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
320 | ParserError ("unterminated `/*`-style comment"); |
0 | 321 | } |