2 * Copyright 2006-2007 Adrian Thurston <thurston@cs.queensu.ca>
5 /* This file is part of Ragel.
7 * Ragel is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Ragel is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Ragel; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 HostType hostTypesC[] =
29 { "char", 0, true, CHAR_MIN, CHAR_MAX, sizeof(char) },
30 { "unsigned", "char", false, 0, UCHAR_MAX, sizeof(unsigned char) },
31 { "short", 0, true, SHRT_MIN, SHRT_MAX, sizeof(short) },
32 { "unsigned", "short", false, 0, USHRT_MAX, sizeof(unsigned short) },
33 { "int", 0, true, INT_MIN, INT_MAX, sizeof(int) },
34 { "unsigned", "int", false, 0, UINT_MAX, sizeof(unsigned int) },
35 { "long", 0, true, LONG_MIN, LONG_MAX, sizeof(long) },
36 { "unsigned", "long", false, 0, ULONG_MAX, sizeof(unsigned long) }
39 HostType hostTypesD[] =
41 { "byte", 0, true, CHAR_MIN, CHAR_MAX, 1 },
42 { "ubyte", 0, false, 0, UCHAR_MAX, 1 },
43 { "char", 0, false, 0, UCHAR_MAX, 1 },
44 { "short", 0, true, SHRT_MIN, SHRT_MAX, 2 },
45 { "ushort", 0, false, 0, USHRT_MAX, 2 },
46 { "wchar", 0, false, 0, USHRT_MAX, 2 },
47 { "int", 0, true, INT_MIN, INT_MAX, 4 },
48 { "uint", 0, false, 0, UINT_MAX, 4 },
49 { "dchar", 0, false, 0, UINT_MAX, 4 }
52 HostType hostTypesJava[] =
54 { "byte", 0, true, CHAR_MIN, CHAR_MAX, 1 },
55 { "short", 0, true, SHRT_MIN, SHRT_MAX, 2 },
56 { "char", 0, false, 0, USHRT_MAX, 2 },
57 { "int", 0, true, INT_MIN, INT_MAX, 4 },
60 HostType hostTypesRuby[] =
62 { "byte", 0, true, CHAR_MIN, CHAR_MAX, 1 },
63 { "short", 0, true, SHRT_MIN, SHRT_MAX, 2 },
64 { "char", 0, false, 0, USHRT_MAX, 2 },
65 { "int", 0, true, INT_MIN, INT_MAX, 4 },
68 HostLang hostLangC = { hostTypesC, 8, hostTypesC+0, true };
69 HostLang hostLangD = { hostTypesD, 9, hostTypesD+2, true };
70 HostLang hostLangJava = { hostTypesJava, 4, hostTypesJava+2, false };
71 HostLang hostLangRuby = { hostTypesRuby, 4, hostTypesRuby+2, false };
73 HostLang *hostLang = &hostLangC;
74 HostLangType hostLangType = CCode;
76 /* Construct a new parameter checker with for paramSpec. */
77 ParamCheck::ParamCheck(char *paramSpec, int argc, char **argv)
89 /* Check a single option. Returns the index of the next parameter. Sets p to
90 * the arg character if valid, 0 otherwise. Sets parg to the parameter arg if
91 * there is one, NULL otherwise. */
92 bool ParamCheck::check()
96 if ( iCurArg >= argc ) { /* Off the end of the arg list. */
101 if ( argOffset != 0 && *argOffset == 0 ) {
102 /* We are at the end of an arg string. */
104 if ( iCurArg >= argc ) {
111 if ( argOffset == 0 ) {
112 /* Set the current arg. */
113 curArg = argv[iCurArg];
115 /* We are at the beginning of an arg string. */
116 if ( argv[iCurArg] == 0 || /* Argv[iCurArg] is null. */
117 argv[iCurArg][0] != '-' || /* Not a param. */
118 argv[iCurArg][1] == 0 ) { /* Only a dash. */
126 argOffset = argv[iCurArg] + 1;
129 /* Get the arg char. */
130 char argChar = *argOffset;
132 /* Loop over all the parms and look for a match. */
133 char *pSpec = paramSpec;
134 while ( *pSpec != 0 ) {
135 char pSpecChar = *pSpec;
137 /* If there is a ':' following the char then
138 * it requires a parm. If a parm is required
139 * then move ahead two in the parmspec. Otherwise
140 * move ahead one in the parm spec. */
141 if ( pSpec[1] == ':' ) {
142 requiresParam = true;
146 requiresParam = false;
150 /* Do we have a match. */
151 if ( argChar == pSpecChar ) {
152 if ( requiresParam ) {
153 if ( argOffset[1] == 0 ) {
154 /* The param must follow. */
155 if ( iCurArg + 1 == argc ) {
156 /* We are the last arg so there
157 * cannot be a parameter to it. */
166 /* the parameter to the arg is the next arg. */
167 parameter = pSpecChar;
168 parameterArg = argv[iCurArg + 1];
176 /* The param for the arg is built in. */
177 parameter = pSpecChar;
178 parameterArg = argOffset + 1;
186 /* Good, we matched the parm and no
187 * arg is required. */
188 parameter = pSpecChar;
197 /* We did not find a match. Bad Argument. */
205 /* Counts newlines before sending sync. */
206 int output_filter::sync( )
209 return std::filebuf::sync();
212 /* Counts newlines before sending data out to file. */
213 std::streamsize output_filter::xsputn( const char *s, std::streamsize n )
215 for ( int i = 0; i < n; i++ ) {
219 return std::filebuf::xsputn( s, n );
222 /* Scans a string looking for the file extension. If there is a file
223 * extension then pointer returned points to inside the string
224 * passed in. Otherwise returns null. */
225 char *findFileExtension( char *stemFile )
227 char *ppos = stemFile + strlen(stemFile) - 1;
229 /* Scan backwards from the end looking for the first dot.
230 * If we encounter a '/' before the first dot, then stop the scan. */
232 /* If we found a dot or got to the beginning of the string then
234 if ( ppos == stemFile || *ppos == '.' )
237 /* If we hit a / then there is no extension. Done. */
238 if ( *ppos == '/' ) {
245 /* If we got to the front of the string then bail we
246 * did not find an extension */
247 if ( ppos == stemFile )
253 /* Make a file name from a stem. Removes the old filename suffix and
254 * replaces it with a new one. Returns a newed up string. */
255 char *fileNameFromStem( char *stemFile, char *suffix )
257 long len = strlen( stemFile );
260 /* Get the extension. */
261 char *ppos = findFileExtension( stemFile );
263 /* If an extension was found, then shorten what we think the len is. */
265 len = ppos - stemFile;
267 /* Make the return string from the stem and the suffix. */
268 char *retVal = new char[ len + strlen( suffix ) + 1 ];
269 strncpy( retVal, stemFile, len );
270 strcpy( retVal + len, suffix );
277 void operator<<( std::ostream &out, exit_object & )