2 * Copyright 2006 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
24 HostType hostTypesC[] =
26 { "char", 0, true, CHAR_MIN, CHAR_MAX, sizeof(char) },
27 { "unsigned", "char", false, 0, UCHAR_MAX, sizeof(unsigned char) },
28 { "short", 0, true, SHRT_MIN, SHRT_MAX, sizeof(short) },
29 { "unsigned", "short", false, 0, USHRT_MAX, sizeof(unsigned short) },
30 { "int", 0, true, INT_MIN, INT_MAX, sizeof(int) },
31 { "unsigned", "int", false, 0, UINT_MAX, sizeof(unsigned int) },
32 { "long", 0, true, LONG_MIN, LONG_MAX, sizeof(long) },
33 { "unsigned", "long", false, 0, ULONG_MAX, sizeof(unsigned long) }
36 HostType hostTypesD[] =
38 { "byte", 0, true, CHAR_MIN, CHAR_MAX, 1 },
39 { "ubyte", 0, false, 0, UCHAR_MAX, 1 },
40 { "char", 0, false, 0, UCHAR_MAX, 1 },
41 { "short", 0, true, SHRT_MIN, SHRT_MAX, 2 },
42 { "ushort", 0, false, 0, USHRT_MAX, 2 },
43 { "wchar", 0, false, 0, USHRT_MAX, 2 },
44 { "int", 0, true, INT_MIN, INT_MAX, 4 },
45 { "uint", 0, false, 0, UINT_MAX, 4 },
46 { "dchar", 0, false, 0, UINT_MAX, 4 }
49 HostType hostTypesJava[] =
51 { "byte", 0, true, CHAR_MIN, CHAR_MAX, 1 },
52 { "short", 0, true, SHRT_MIN, SHRT_MAX, 2 },
53 { "char", 0, false, 0, USHRT_MAX, 2 },
54 { "int", 0, true, INT_MIN, INT_MAX, 4 },
57 HostLang hostLangC = { hostTypesC, 8, hostTypesC+0, true };
58 HostLang hostLangD = { hostTypesD, 9, hostTypesD+2, true };
59 HostLang hostLangJava = { hostTypesJava, 4, hostTypesJava+2, false };
61 HostLang *hostLang = &hostLangC;
62 HostLangType hostLangType = CCode;
64 /* Construct a new parameter checker with for paramSpec. */
65 ParamCheck::ParamCheck(char *paramSpec, int argc, char **argv)
77 /* Check a single option. Returns the index of the next parameter. Sets p to
78 * the arg character if valid, 0 otherwise. Sets parg to the parameter arg if
79 * there is one, NULL otherwise. */
80 bool ParamCheck::check()
84 if ( iCurArg >= argc ) { /* Off the end of the arg list. */
89 if ( argOffset != 0 && *argOffset == 0 ) {
90 /* We are at the end of an arg string. */
92 if ( iCurArg >= argc ) {
99 if ( argOffset == 0 ) {
100 /* Set the current arg. */
101 curArg = argv[iCurArg];
103 /* We are at the beginning of an arg string. */
104 if ( argv[iCurArg] == 0 || /* Argv[iCurArg] is null. */
105 argv[iCurArg][0] != '-' || /* Not a param. */
106 argv[iCurArg][1] == 0 ) { /* Only a dash. */
114 argOffset = argv[iCurArg] + 1;
117 /* Get the arg char. */
118 char argChar = *argOffset;
120 /* Loop over all the parms and look for a match. */
121 char *pSpec = paramSpec;
122 while ( *pSpec != 0 ) {
123 char pSpecChar = *pSpec;
125 /* If there is a ':' following the char then
126 * it requires a parm. If a parm is required
127 * then move ahead two in the parmspec. Otherwise
128 * move ahead one in the parm spec. */
129 if ( pSpec[1] == ':' ) {
130 requiresParam = true;
134 requiresParam = false;
138 /* Do we have a match. */
139 if ( argChar == pSpecChar ) {
140 if ( requiresParam ) {
141 if ( argOffset[1] == 0 ) {
142 /* The param must follow. */
143 if ( iCurArg + 1 == argc ) {
144 /* We are the last arg so there
145 * cannot be a parameter to it. */
154 /* the parameter to the arg is the next arg. */
155 parameter = pSpecChar;
156 parameterArg = argv[iCurArg + 1];
164 /* The param for the arg is built in. */
165 parameter = pSpecChar;
166 parameterArg = argOffset + 1;
174 /* Good, we matched the parm and no
175 * arg is required. */
176 parameter = pSpecChar;
185 /* We did not find a match. Bad Argument. */