| |
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. |
| |
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. |
| |
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 #define __PARSER_CXX__ |
| |
42 |
| |
43 #include <stdio.h> |
| |
44 #include <stdlib.h> |
| |
45 #include "common.h" |
| |
46 #include "str.h" |
| |
47 #include "scriptreader.h" |
| |
48 |
| |
49 /* Since the preprocessor is *called* from ReadChar and I don't want |
| |
50 * to worry about recursive preprocessing, the preprocessor uses its |
| |
51 * own bare-bones variant of the function for file reading. |
| |
52 */ |
| |
53 char ScriptReader::PPReadChar () { |
| |
54 char* c = (char*)malloc (sizeof (char)); |
| |
55 if (!fread (c, sizeof (char), 1, fp[fc])) |
| |
56 return 0; |
| |
57 curchar[fc]++; |
| |
58 return c[0]; |
| |
59 } |
| |
60 |
| |
61 void ScriptReader::PPMustChar (char c) { |
| |
62 char d = PPReadChar (); |
| |
63 if (c != d) { |
| |
64 ParserError ("expected `%c`, got `%d`", c, d); |
| |
65 } |
| |
66 } |
| |
67 |
| |
68 // Reads a word until whitespace |
| |
69 str ScriptReader::PPReadWord (char &term) { |
| |
70 str word; |
| |
71 while (1) { |
| |
72 char c = PPReadChar(); |
| |
73 if (feof (fp[fc]) || (IsCharWhitespace (c) && word.len ())) { |
| |
74 term = c; |
| |
75 break; |
| |
76 } |
| |
77 word += c; |
| |
78 } |
| |
79 return word; |
| |
80 } |
| |
81 |
| |
82 void ScriptReader::PreprocessDirectives () { |
| |
83 size_t spos = ftell (fp[fc]); |
| |
84 if (!DoDirectivePreprocessing ()) |
| |
85 fseek (fp[fc], spos, SEEK_SET); |
| |
86 } |
| |
87 |
| |
88 /* Returns true if the pre-processing was successful, false if not. |
| |
89 * If pre-processing was successful, the file pointer remains where |
| |
90 * it was, if not, it's pushed back to where it was before preprocessing |
| |
91 * took place and is parsed normally. |
| |
92 */ |
| |
93 bool ScriptReader::DoDirectivePreprocessing () { |
| |
94 char trash; |
| |
95 // Directives start with a pound sign |
| |
96 if (PPReadChar() != '#') |
| |
97 return false; |
| |
98 |
| |
99 // Read characters until next whitespace to |
| |
100 // build the name of the directive |
| |
101 str directive = PPReadWord (trash); |
| |
102 |
| |
103 // Now check the directive name against known names |
| |
104 if (!directive.icompare ("include")) { |
| |
105 // #include-directive |
| |
106 char terminator; |
| |
107 str file = PPReadWord (terminator); |
| |
108 |
| |
109 if (!file.len()) |
| |
110 ParserError ("expected file name for #include, got nothing instead"); |
| |
111 OpenFile (file); |
| |
112 return true; |
| |
113 } |
| |
114 |
| |
115 ParserError ("unknown directive `#%s`!", directive.chars()); |
| |
116 return false; |
| |
117 } |