File: common.cpp

package info (click to toggle)
ragel 5.14-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,628 kB
  • ctags: 4,619
  • sloc: cpp: 15,187; ansic: 3,490; yacc: 1,921; makefile: 411; sh: 146; awk: 6
file content (193 lines) | stat: -rw-r--r-- 5,432 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 *  Copyright 2006 Adrian Thurston <thurston@cs.queensu.ca>
 */

/*  This file is part of Ragel.
 *
 *  Ragel is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  Ragel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with Ragel; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

#include "common.h"

HostType hostTypesC[] =
{
	{ "char",     0,       true,   CHAR_MIN,  CHAR_MAX,   sizeof(char) },
	{ "unsigned", "char",  false,  0,         UCHAR_MAX,  sizeof(unsigned char) },
	{ "short",    0,       true,   SHRT_MIN,  SHRT_MAX,   sizeof(short) },
	{ "unsigned", "short", false,  0,         USHRT_MAX,  sizeof(unsigned short) },
	{ "int",      0,       true,   INT_MIN,   INT_MAX,    sizeof(int) },
	{ "unsigned", "int",   false,  0,         UINT_MAX,   sizeof(unsigned int) },
	{ "long",     0,       true,   LONG_MIN,  LONG_MAX,   sizeof(long) },
	{ "unsigned", "long",  false,  0,         ULONG_MAX,  sizeof(unsigned long) }
};

HostType hostTypesD[] =
{
	{ "byte",     0,  true,   CHAR_MIN,  CHAR_MAX,    1 },
	{ "ubyte",    0,  false,  0,         UCHAR_MAX,   1 },
	{ "char",     0,  false,  0,         UCHAR_MAX,   1 },
	{ "short",    0,  true,   SHRT_MIN,  SHRT_MAX,    2 },
	{ "ushort",   0,  false,  0,         USHRT_MAX,   2 },
	{ "wchar",    0,  false,  0,         USHRT_MAX,   2 },
	{ "int",      0,  true,   INT_MIN,   INT_MAX,     4 },
	{ "uint",     0,  false,  0,         UINT_MAX,    4 },
	{ "dchar",    0,  false,  0,         UINT_MAX,    4 }
};

HostType hostTypesJava[] = 
{
	{ "byte",     0,  true,   CHAR_MIN,  CHAR_MAX,    1 },
	{ "short",    0,  true,   SHRT_MIN,  SHRT_MAX,    2 },
	{ "char",     0,  false,  0,         USHRT_MAX,   2 },
	{ "int",      0,  true,   INT_MIN,   INT_MAX,     4 },
};

HostLang hostLangC =    { hostTypesC,    8, hostTypesC+0,    true };
HostLang hostLangD =    { hostTypesD,    9, hostTypesD+2,    true };
HostLang hostLangJava = { hostTypesJava, 4, hostTypesJava+2, false };

HostLang *hostLang = &hostLangC;
HostLangType hostLangType = CCode;

/* Construct a new parameter checker with for paramSpec. */
ParamCheck::ParamCheck(char *paramSpec, int argc, char **argv)
:
	state(noparam),
	argOffset(0),
	curArg(0),
	iCurArg(1),
	paramSpec(paramSpec), 
	argc(argc), 
	argv(argv)
{
}

/* Check a single option. Returns the index of the next parameter.  Sets p to
 * the arg character if valid, 0 otherwise.  Sets parg to the parameter arg if
 * there is one, NULL otherwise. */
bool ParamCheck::check()
{
	bool requiresParam;

	if ( iCurArg >= argc ) {            /* Off the end of the arg list. */
		state = noparam;
		return false;
	}

	if ( argOffset != 0 && *argOffset == 0 ) {
		/* We are at the end of an arg string. */
		iCurArg += 1;
		if ( iCurArg >= argc ) {
			state = noparam;
			return false;
		}
		argOffset = 0;
	}

	if ( argOffset == 0 ) {
		/* Set the current arg. */
		curArg = argv[iCurArg];

		/* We are at the beginning of an arg string. */
		if ( argv[iCurArg] == 0 ||        /* Argv[iCurArg] is null. */
			 argv[iCurArg][0] != '-' ||   /* Not a param. */
			 argv[iCurArg][1] == 0 ) {    /* Only a dash. */
			parameter = 0;
			parameterArg = 0;

			iCurArg += 1;
			state = noparam;
			return true;
		}
		argOffset = argv[iCurArg] + 1;
	}

	/* Get the arg char. */
	char argChar = *argOffset;
	
	/* Loop over all the parms and look for a match. */
	char *pSpec = paramSpec;
	while ( *pSpec != 0 ) {
		char pSpecChar = *pSpec;

		/* If there is a ':' following the char then
		 * it requires a parm.  If a parm is required
		 * then move ahead two in the parmspec. Otherwise
		 * move ahead one in the parm spec. */
		if ( pSpec[1] == ':' ) {
			requiresParam = true;
			pSpec += 2;
		}
		else {
			requiresParam = false;
			pSpec += 1;
		}

		/* Do we have a match. */
		if ( argChar == pSpecChar ) {
			if ( requiresParam ) {
				if ( argOffset[1] == 0 ) {
					/* The param must follow. */
					if ( iCurArg + 1 == argc ) {
						/* We are the last arg so there
						 * cannot be a parameter to it. */
						parameter = argChar;
						parameterArg = 0;
						iCurArg += 1;
						argOffset = 0;
						state = invalid;
						return true;
					}
					else {
						/* the parameter to the arg is the next arg. */
						parameter = pSpecChar;
						parameterArg = argv[iCurArg + 1];
						iCurArg += 2;
						argOffset = 0;
						state = match;
						return true;
					}
				}
				else {
					/* The param for the arg is built in. */
					parameter = pSpecChar;
					parameterArg = argOffset + 1;
					iCurArg += 1;
					argOffset = 0;
					state = match;
					return true;
				}
			}
			else {
				/* Good, we matched the parm and no
				 * arg is required. */
				parameter = pSpecChar;
				parameterArg = 0;
				argOffset += 1;
				state = match;
				return true;
			}
		}
	}

	/* We did not find a match. Bad Argument. */
	parameter = argChar;
	parameterArg = 0;
	argOffset += 1;
	state = invalid;
	return true;
}