namedenums/NamedEnumerations.cc

changeset 109
6572803cd0ca
child 110
7a7a53f1d51b
equal deleted inserted replaced
108:6409ece8297c 109:6572803cd0ca
1 /*
2 Copyright 2014 Santeri Piippo
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. The name of the author may not be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <string>
30 #include <deque>
31 #include <algorithm>
32 #include <cerrno>
33 #include <cstdio>
34 #include <cstdlib>
35 #include <cstring>
36 #include <cstdarg>
37
38 using std::string;
39 using std::deque;
40
41 static int gLineNumber;
42 static std::string gCurrentFile;
43
44 // =============================================================================
45 //
46 struct NamedEnumInfo
47 {
48 string name;
49 deque<string> enumerators;
50 };
51
52 // =============================================================================
53 //
54 void SkipWhitespace (char*& cp)
55 {
56 while (isspace (*cp))
57 {
58 if (*cp == '\n')
59 gLineNumber++;
60
61 ++cp;
62 }
63
64 if (strncmp (cp, "//", 2) == 0)
65 {
66 while (*(++cp) != '\n')
67 ;
68
69 gLineNumber++;
70 SkipWhitespace (cp);
71 }
72 }
73
74 // =============================================================================
75 //
76 void Error (const char* fmt, ...)
77 {
78 char buf[1024];
79 va_list va;
80 va_start (va, fmt);
81 vsprintf (buf, fmt, va);
82 va_end (va);
83 throw std::string (buf);
84 }
85
86 // =============================================================================
87 //
88 int main (int argc, char* argv[])
89 {
90 try
91 {
92 deque<NamedEnumInfo> namedEnumerations;
93 deque<string> filesToInclude;
94
95 if (argc < 3)
96 {
97 fprintf (stderr, "usage: %s input [input [input [...]]] output\n", argv[0]);
98 return EXIT_FAILURE;
99 }
100
101 for (int i = 1; i < argc - 1; ++ i)
102 {
103 gCurrentFile = argv[i];
104 FILE* fp = fopen (argv[i], "r");
105 char* buf;
106 gLineNumber = 1;
107
108 if (fp == NULL)
109 {
110 fprintf (stderr, "could not open %s for writing: %s\n",
111 argv[i], strerror (errno));
112 exit (EXIT_FAILURE);
113 }
114
115 fseek (fp, 0, SEEK_END);
116 long int filesize = ftell (fp);
117 rewind (fp);
118
119 try
120 {
121 buf = new char[filesize];
122 }
123 catch (std::bad_alloc)
124 {
125 Error ("could not allocate %ld bytes for %s\n", filesize, argv[i]);
126 }
127
128 if (static_cast<long> (fread (buf, 1, filesize, fp)) < filesize)
129 Error ("could not read %ld bytes from %s\n", filesize, argv[i]);
130
131 char* const end = &buf[0] + filesize;
132
133 for (char* cp = &buf[0]; cp < end; ++cp)
134 {
135 SkipWhitespace (cp);
136
137 if (strncmp (cp, "#define ", strlen ("#define ")) == 0)
138 {
139 while (cp < end && *cp != '\n')
140 cp++;
141
142 continue;
143 }
144
145 if ((cp != &buf[0] && isspace (* (cp - 1)) == false) ||
146 strncmp (cp, "named_enum ", strlen ("named_enum ")) != 0)
147 {
148 continue;
149 }
150
151 cp += strlen ("named_enum ");
152 SkipWhitespace (cp);
153
154 NamedEnumInfo nenum;
155 auto& enumname = nenum.name;
156 auto& enumerators = nenum.enumerators;
157
158 if (isalpha (*cp) == false && *cp != '_')
159 Error ("anonymous named_enum");
160
161 while (isalnum (*cp) || *cp == '_')
162 enumname += *cp++;
163
164 SkipWhitespace (cp);
165
166 if (*cp++ != '{')
167 Error ("expected '{' after named_enum");
168
169 for (;;)
170 {
171 SkipWhitespace (cp);
172
173 if (*cp == '}')
174 {
175 cp++;
176 break;
177 }
178
179 if (isalpha (*cp) == false && *cp != '_')
180 Error ("expected identifier, got '%c'", *cp);
181
182 std::string enumerator;
183
184 while (isalnum (*cp) || *cp == '_')
185 enumerator += *cp++;
186
187 SkipWhitespace (cp);
188
189 if (*cp == ',')
190 SkipWhitespace (++cp);
191
192 if (*cp == '=')
193 Error ("named enums must not have defined values");
194
195 enumerators.push_back (enumerator);
196 }
197
198 SkipWhitespace (cp);
199
200 if (*cp != ';')
201 Error ("expected ';'");
202
203 if (enumerators.size() > 0)
204 {
205 namedEnumerations.push_back (nenum);
206 filesToInclude.push_back (argv[i]);
207 }
208 }
209 }
210
211 FILE* fp;
212
213 if ((fp = fopen (argv[argc - 1], "w")) == NULL)
214 Error ("couldn't open %s for writing: %s", argv[argc - 1], strerror (errno));
215
216 fprintf (fp, "#pragma once\n");
217
218 std::sort (filesToInclude.begin(), filesToInclude.end());
219 auto pos = std::unique (filesToInclude.begin(), filesToInclude.end());
220 filesToInclude.resize (std::distance (filesToInclude.begin(), pos));
221
222 for (const string & a : filesToInclude)
223 fprintf (fp, "#include \"%s\"\n", basename (a.c_str()));
224
225 for (NamedEnumInfo & e : namedEnumerations)
226 {
227 fprintf (fp, "\nstatic const char* g%sNames[] =\n{\n", e.name.c_str());
228
229 for (const string & a : e.enumerators)
230 fprintf (fp, "\t\"%s\",\n", a.c_str());
231
232 fprintf (fp, "};\n\n");
233
234 fprintf (fp, "inline const char* Get%sString( %s a )\n"
235 "{\n"
236 "\treturn g%sNames[a];\n"
237 "}\n",
238 e.name.c_str(), e.name.c_str(), e.name.c_str());
239 }
240
241 printf ("Wrote named enumerations to %s\n", argv[argc - 1]);
242 fclose (fp);
243 }
244 catch (std::string a)
245 {
246 fprintf (stderr, "%s:%d: error: %s\n", gCurrentFile.c_str(), gLineNumber, a.c_str());
247 return 1;
248 }
249
250 return 0;
251 }

mercurial