71 char ScriptReader::ReadChar () { |
73 char ScriptReader::ReadChar () { |
72 char* c = (char*)malloc (sizeof (char)); |
74 char* c = (char*)malloc (sizeof (char)); |
73 if (!fread (c, sizeof (char), 1, fp)) |
75 if (!fread (c, sizeof (char), 1, fp)) |
74 return 0; |
76 return 0; |
75 |
77 |
76 // If we just read a new line, increase current line number counter. |
78 // We're at a newline, thus next char read will begin the next line |
|
79 if (atnewline) { |
|
80 atnewline = false; |
|
81 curline++; |
|
82 } |
|
83 |
77 if (c[0] == '\n') |
84 if (c[0] == '\n') |
78 curline++; |
85 atnewline = true; |
79 |
86 |
80 return c[0]; |
87 return c[0]; |
81 } |
88 } |
82 |
89 |
83 // true if was found, false if not. |
90 // true if was found, false if not. |
155 } |
162 } |
156 |
163 |
157 void ScriptReader::MustNext (const char* c) { |
164 void ScriptReader::MustNext (const char* c) { |
158 if (!Next()) { |
165 if (!Next()) { |
159 if (strlen (c)) |
166 if (strlen (c)) |
160 ParseError ("expected `%s`, reached end of file instead\n", c); |
167 ParserError ("expected `%s`, reached end of file instead\n", c); |
161 else |
168 else |
162 ParseError ("expected a token, reached end of file instead\n"); |
169 ParserError ("expected a token, reached end of file instead\n"); |
163 } |
170 } |
164 |
171 |
165 if (strlen (c) && token.compare (c) != 0) { |
172 if (strlen (c) && token.compare (c) != 0) { |
166 ParseError ("expected `%s`, got `%s` instead", c, token.chars()); |
173 ParserError ("expected `%s`, got `%s` instead", c, token.chars()); |
167 } |
174 } |
168 } |
175 } |
169 |
176 |
170 void ScriptReader::ParseError (const char* message, ...) { |
177 void ScriptReader::ParserError (const char* message, ...) { |
171 PERFORM_FORMAT (message, outmessage); |
178 PERFORM_FORMAT (message, outmessage); |
172 error ("Parse error on line %d:\n%s\n", curline, outmessage); |
179 ParserMessage ("\nParse error\n", outmessage); |
|
180 exit (1); |
173 } |
181 } |
|
182 |
|
183 void ScriptReader::ParserWarning (const char* message, ...) { |
|
184 PERFORM_FORMAT (message, outmessage); |
|
185 ParserMessage ("Warning: ", outmessage); |
|
186 } |
|
187 |
|
188 void ScriptReader::ParserMessage (const char* header, char* message) { |
|
189 fprintf (stderr, "%sIn file %s, on line %d: %s\n", |
|
190 header, filepath.chars(), curline, message); |
|
191 } |