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
26 HostType hostTypesC[] =
28 { "char", 0, true, CHAR_MIN, CHAR_MAX, sizeof(char) },
29 { "unsigned", "char", false, 0, UCHAR_MAX, sizeof(unsigned char) },
30 { "short", 0, true, SHRT_MIN, SHRT_MAX, sizeof(short) },
31 { "unsigned", "short", false, 0, USHRT_MAX, sizeof(unsigned short) },
32 { "int", 0, true, INT_MIN, INT_MAX, sizeof(int) },
33 { "unsigned", "int", false, 0, UINT_MAX, sizeof(unsigned int) },
34 { "long", 0, true, LONG_MIN, LONG_MAX, sizeof(long) },
35 { "unsigned", "long", false, 0, ULONG_MAX, sizeof(unsigned long) }
38 HostType hostTypesD[] =
40 { "byte", 0, true, CHAR_MIN, CHAR_MAX, 1 },
41 { "ubyte", 0, false, 0, UCHAR_MAX, 1 },
42 { "char", 0, false, 0, UCHAR_MAX, 1 },
43 { "short", 0, true, SHRT_MIN, SHRT_MAX, 2 },
44 { "ushort", 0, false, 0, USHRT_MAX, 2 },
45 { "wchar", 0, false, 0, USHRT_MAX, 2 },
46 { "int", 0, true, INT_MIN, INT_MAX, 4 },
47 { "uint", 0, false, 0, UINT_MAX, 4 },
48 { "dchar", 0, false, 0, UINT_MAX, 4 }
51 HostType hostTypesJava[] =
53 { "byte", 0, true, CHAR_MIN, CHAR_MAX, 1 },
54 { "short", 0, true, SHRT_MIN, SHRT_MAX, 2 },
55 { "char", 0, false, 0, USHRT_MAX, 2 },
56 { "int", 0, true, INT_MIN, INT_MAX, 4 },
59 HostType hostTypesRuby[] =
61 { "byte", 0, true, CHAR_MIN, CHAR_MAX, 1 },
62 { "short", 0, true, SHRT_MIN, SHRT_MAX, 2 },
63 { "char", 0, false, 0, USHRT_MAX, 2 },
64 { "int", 0, true, INT_MIN, INT_MAX, 4 },
67 HostLang hostLangC = { hostTypesC, 8, hostTypesC+0, true };
68 HostLang hostLangD = { hostTypesD, 9, hostTypesD+2, true };
69 HostLang hostLangJava = { hostTypesJava, 4, hostTypesJava+2, false };
70 HostLang hostLangRuby = { hostTypesRuby, 4, hostTypesRuby+2, false };
72 HostLang *hostLang = &hostLangC;
73 HostLangType hostLangType = CCode;
75 /* Construct a new parameter checker with for paramSpec. */
76 ParamCheck::ParamCheck(char *paramSpec, int argc, char **argv)
88 /* Check a single option. Returns the index of the next parameter. Sets p to
89 * the arg character if valid, 0 otherwise. Sets parg to the parameter arg if
90 * there is one, NULL otherwise. */
91 bool ParamCheck::check()
95 if ( iCurArg >= argc ) { /* Off the end of the arg list. */
100 if ( argOffset != 0 && *argOffset == 0 ) {
101 /* We are at the end of an arg string. */
103 if ( iCurArg >= argc ) {
110 if ( argOffset == 0 ) {
111 /* Set the current arg. */
112 curArg = argv[iCurArg];
114 /* We are at the beginning of an arg string. */
115 if ( argv[iCurArg] == 0 || /* Argv[iCurArg] is null. */
116 argv[iCurArg][0] != '-' || /* Not a param. */
117 argv[iCurArg][1] == 0 ) { /* Only a dash. */
125 argOffset = argv[iCurArg] + 1;
128 /* Get the arg char. */
129 char argChar = *argOffset;
131 /* Loop over all the parms and look for a match. */
132 char *pSpec = paramSpec;
133 while ( *pSpec != 0 ) {
134 char pSpecChar = *pSpec;
136 /* If there is a ':' following the char then
137 * it requires a parm. If a parm is required
138 * then move ahead two in the parmspec. Otherwise
139 * move ahead one in the parm spec. */
140 if ( pSpec[1] == ':' ) {
141 requiresParam = true;
145 requiresParam = false;
149 /* Do we have a match. */
150 if ( argChar == pSpecChar ) {
151 if ( requiresParam ) {
152 if ( argOffset[1] == 0 ) {
153 /* The param must follow. */
154 if ( iCurArg + 1 == argc ) {
155 /* We are the last arg so there
156 * cannot be a parameter to it. */
165 /* the parameter to the arg is the next arg. */
166 parameter = pSpecChar;
167 parameterArg = argv[iCurArg + 1];
175 /* The param for the arg is built in. */
176 parameter = pSpecChar;
177 parameterArg = argOffset + 1;
185 /* Good, we matched the parm and no
186 * arg is required. */
187 parameter = pSpecChar;
196 /* We did not find a match. Bad Argument. */
204 /* Counts newlines before sending sync. */
205 int output_filter::sync( )
208 return std::filebuf::sync();
211 /* Counts newlines before sending data out to file. */
212 std::streamsize output_filter::xsputn( const char *s, std::streamsize n )
214 for ( int i = 0; i < n; i++ ) {
218 return std::filebuf::xsputn( s, n );
221 /* Scans a string looking for the file extension. If there is a file
222 * extension then pointer returned points to inside the string
223 * passed in. Otherwise returns null. */
224 char *findFileExtension( char *stemFile )
226 char *ppos = stemFile + strlen(stemFile) - 1;
228 /* Scan backwards from the end looking for the first dot.
229 * If we encounter a '/' before the first dot, then stop the scan. */
231 /* If we found a dot or got to the beginning of the string then
233 if ( ppos == stemFile || *ppos == '.' )
236 /* If we hit a / then there is no extension. Done. */
237 if ( *ppos == '/' ) {
244 /* If we got to the front of the string then bail we
245 * did not find an extension */
246 if ( ppos == stemFile )
252 /* Make a file name from a stem. Removes the old filename suffix and
253 * replaces it with a new one. Returns a newed up string. */
254 char *fileNameFromStem( char *stemFile, char *suffix )
256 long len = strlen( stemFile );
259 /* Get the extension. */
260 char *ppos = findFileExtension( stemFile );
262 /* If an extension was found, then shorten what we think the len is. */
264 len = ppos - stemFile;
266 /* Make the return string from the stem and the suffix. */
267 char *retVal = new char[ len + strlen( suffix ) + 1 ];
268 strncpy( retVal, stemFile, len );
269 strcpy( retVal + len, suffix );