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 HostType hostTypesRuby[] =
60 { "byte", 0, true, CHAR_MIN, CHAR_MAX, 1 },
61 { "short", 0, true, SHRT_MIN, SHRT_MAX, 2 },
62 { "char", 0, false, 0, USHRT_MAX, 2 },
63 { "int", 0, true, INT_MIN, INT_MAX, 4 },
66 HostLang hostLangC = { hostTypesC, 8, hostTypesC+0, true };
67 HostLang hostLangD = { hostTypesD, 9, hostTypesD+2, true };
68 HostLang hostLangJava = { hostTypesJava, 4, hostTypesJava+2, false };
69 HostLang hostLangRuby = { hostTypesRuby, 4, hostTypesRuby+2, false };
71 HostLang *hostLang = &hostLangC;
72 HostLangType hostLangType = CCode;
74 /* Construct a new parameter checker with for paramSpec. */
75 ParamCheck::ParamCheck(char *paramSpec, int argc, char **argv)
87 /* Check a single option. Returns the index of the next parameter. Sets p to
88 * the arg character if valid, 0 otherwise. Sets parg to the parameter arg if
89 * there is one, NULL otherwise. */
90 bool ParamCheck::check()
94 if ( iCurArg >= argc ) { /* Off the end of the arg list. */
99 if ( argOffset != 0 && *argOffset == 0 ) {
100 /* We are at the end of an arg string. */
102 if ( iCurArg >= argc ) {
109 if ( argOffset == 0 ) {
110 /* Set the current arg. */
111 curArg = argv[iCurArg];
113 /* We are at the beginning of an arg string. */
114 if ( argv[iCurArg] == 0 || /* Argv[iCurArg] is null. */
115 argv[iCurArg][0] != '-' || /* Not a param. */
116 argv[iCurArg][1] == 0 ) { /* Only a dash. */
124 argOffset = argv[iCurArg] + 1;
127 /* Get the arg char. */
128 char argChar = *argOffset;
130 /* Loop over all the parms and look for a match. */
131 char *pSpec = paramSpec;
132 while ( *pSpec != 0 ) {
133 char pSpecChar = *pSpec;
135 /* If there is a ':' following the char then
136 * it requires a parm. If a parm is required
137 * then move ahead two in the parmspec. Otherwise
138 * move ahead one in the parm spec. */
139 if ( pSpec[1] == ':' ) {
140 requiresParam = true;
144 requiresParam = false;
148 /* Do we have a match. */
149 if ( argChar == pSpecChar ) {
150 if ( requiresParam ) {
151 if ( argOffset[1] == 0 ) {
152 /* The param must follow. */
153 if ( iCurArg + 1 == argc ) {
154 /* We are the last arg so there
155 * cannot be a parameter to it. */
164 /* the parameter to the arg is the next arg. */
165 parameter = pSpecChar;
166 parameterArg = argv[iCurArg + 1];
174 /* The param for the arg is built in. */
175 parameter = pSpecChar;
176 parameterArg = argOffset + 1;
184 /* Good, we matched the parm and no
185 * arg is required. */
186 parameter = pSpecChar;
195 /* We did not find a match. Bad Argument. */