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