Fri, 13 Jul 2012 20:14:46 +0300
oh blerb.
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 | ||
48 | bool IsWhitespace (char c) { | |
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 | ||
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
56 | ScriptReader::ScriptReader (char* path) { |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
57 | if (!(fp = fopen (path, "r"))) { |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
58 | error ("couldn't open %s for reading!\n", path); |
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 | |
62 | curline = 1; | |
63 | pos = 0; | |
64 | token = ""; | |
65 | } | |
66 | ||
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
67 | ScriptReader::~ScriptReader () { |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
68 | fclose (fp); |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
69 | } |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
70 | |
0 | 71 | char ScriptReader::ReadChar () { |
72 | char* c = (char*)malloc (sizeof (char)); | |
73 | if (!fread (c, sizeof (char), 1, fp)) | |
74 | return 0; | |
75 | ||
76 | // If we just read a new line, increase current line number counter. | |
77 | if (c[0] == '\n') | |
78 | curline++; | |
79 | ||
80 | return c[0]; | |
81 | } | |
82 | ||
83 | // true if was found, false if not. | |
84 | bool ScriptReader::Next () { | |
85 | str tmp = ""; | |
86 | bool quote = false; | |
87 | tokenquoted = false; | |
88 | while (!feof (fp)) { | |
89 | char c = ReadChar (); | |
90 | ||
91 | // Quotation mark. | |
92 | if (c == '"') { | |
93 | // End-quote ends the token. | |
94 | if (quote) | |
95 | break; | |
96 | ||
97 | tokenquoted = true; | |
98 | quote = !quote; | |
99 | continue; | |
100 | } | |
101 | ||
102 | if (IsWhitespace (c) && !quote) { | |
103 | // Don't break if we haven't gathered anything yet. | |
104 | if (tmp.len()) | |
105 | break; | |
106 | } else { | |
107 | tmp += c; | |
108 | } | |
109 | } | |
110 | ||
111 | // If we got nothing here, read failed. | |
112 | if (!tmp.len()) { | |
113 | token = ""; | |
114 | return false; | |
115 | } | |
116 | ||
117 | pos++; | |
118 | token = tmp; | |
119 | return true; | |
120 | } | |
121 | ||
122 | // Returns the next token without advancing the cursor. | |
123 | str ScriptReader::PeekNext () { | |
124 | // Store current position | |
125 | int cpos = ftell (fp); | |
126 | ||
127 | // Advance on the token. | |
128 | if (!Next ()) | |
129 | return ""; | |
130 | ||
131 | str tmp = token; | |
132 | ||
133 | // Restore position | |
134 | fseek (fp, cpos, SEEK_SET); | |
135 | pos--; | |
136 | ||
137 | return tmp; | |
138 | } | |
139 | ||
140 | void ScriptReader::Seek (unsigned int n, int origin) { | |
141 | switch (origin) { | |
142 | case SEEK_SET: | |
143 | fseek (fp, 0, SEEK_SET); | |
144 | pos = 0; | |
145 | break; | |
146 | case SEEK_CUR: | |
147 | break; | |
148 | case SEEK_END: | |
149 | printf ("ScriptReader::Seek: SEEK_END not yet supported.\n"); | |
150 | break; | |
151 | } | |
152 | ||
153 | for (unsigned int i = 0; i < n+1; i++) | |
154 | Next(); | |
155 | } | |
156 | ||
157 | void ScriptReader::MustNext (const char* c) { | |
158 | if (!Next()) { | |
159 | if (strlen (c)) | |
160 | ParseError ("expected `%s`, reached end of file instead\n", c); | |
161 | else | |
162 | ParseError ("expected a token, reached end of file instead\n"); | |
163 | } | |
164 | ||
165 | if (strlen (c) && token.compare (c) != 0) { | |
166 | ParseError ("expected `%s`, got `%s` instead", c, token.chars()); | |
167 | } | |
168 | } | |
169 | ||
170 | void ScriptReader::ParseError (const char* message, ...) { | |
171 | PERFORM_FORMAT (message, outmessage); | |
172 | error ("Parse error on line %d:\n%s\n", curline, outmessage); | |
173 | } |