The common code is now a library (no more #include "common.cpp"). Improvements
[external/ragel.git] / common / common.cpp
1 /*
2  *  Copyright 2006-2007 Adrian Thurston <thurston@cs.queensu.ca>
3  */
4
5 /*  This file is part of Ragel.
6  *
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.
11  * 
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.
16  * 
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 
20  */
21
22 #include "pcheck.h"
23 #include "common.h"
24
25 HostType hostTypesC[] =
26 {
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) }
35 };
36
37 HostType hostTypesD[] =
38 {
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 }
48 };
49
50 HostType hostTypesJava[] = 
51 {
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 },
56 };
57
58 HostLang hostLangC =    { hostTypesC,    8, hostTypesC+0,    true };
59 HostLang hostLangD =    { hostTypesD,    9, hostTypesD+2,    true };
60 HostLang hostLangJava = { hostTypesJava, 4, hostTypesJava+2, false };
61
62 HostLang *hostLang = &hostLangC;
63 HostLangType hostLangType = CCode;
64
65 /* Construct a new parameter checker with for paramSpec. */
66 ParamCheck::ParamCheck(char *paramSpec, int argc, char **argv)
67 :
68         state(noparam),
69         argOffset(0),
70         curArg(0),
71         iCurArg(1),
72         paramSpec(paramSpec), 
73         argc(argc), 
74         argv(argv)
75 {
76 }
77
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()
82 {
83         bool requiresParam;
84
85         if ( iCurArg >= argc ) {            /* Off the end of the arg list. */
86                 state = noparam;
87                 return false;
88         }
89
90         if ( argOffset != 0 && *argOffset == 0 ) {
91                 /* We are at the end of an arg string. */
92                 iCurArg += 1;
93                 if ( iCurArg >= argc ) {
94                         state = noparam;
95                         return false;
96                 }
97                 argOffset = 0;
98         }
99
100         if ( argOffset == 0 ) {
101                 /* Set the current arg. */
102                 curArg = argv[iCurArg];
103
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. */
108                         parameter = 0;
109                         parameterArg = 0;
110
111                         iCurArg += 1;
112                         state = noparam;
113                         return true;
114                 }
115                 argOffset = argv[iCurArg] + 1;
116         }
117
118         /* Get the arg char. */
119         char argChar = *argOffset;
120         
121         /* Loop over all the parms and look for a match. */
122         char *pSpec = paramSpec;
123         while ( *pSpec != 0 ) {
124                 char pSpecChar = *pSpec;
125
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;
132                         pSpec += 2;
133                 }
134                 else {
135                         requiresParam = false;
136                         pSpec += 1;
137                 }
138
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. */
147                                                 parameter = argChar;
148                                                 parameterArg = 0;
149                                                 iCurArg += 1;
150                                                 argOffset = 0;
151                                                 state = invalid;
152                                                 return true;
153                                         }
154                                         else {
155                                                 /* the parameter to the arg is the next arg. */
156                                                 parameter = pSpecChar;
157                                                 parameterArg = argv[iCurArg + 1];
158                                                 iCurArg += 2;
159                                                 argOffset = 0;
160                                                 state = match;
161                                                 return true;
162                                         }
163                                 }
164                                 else {
165                                         /* The param for the arg is built in. */
166                                         parameter = pSpecChar;
167                                         parameterArg = argOffset + 1;
168                                         iCurArg += 1;
169                                         argOffset = 0;
170                                         state = match;
171                                         return true;
172                                 }
173                         }
174                         else {
175                                 /* Good, we matched the parm and no
176                                  * arg is required. */
177                                 parameter = pSpecChar;
178                                 parameterArg = 0;
179                                 argOffset += 1;
180                                 state = match;
181                                 return true;
182                         }
183                 }
184         }
185
186         /* We did not find a match. Bad Argument. */
187         parameter = argChar;
188         parameterArg = 0;
189         argOffset += 1;
190         state = invalid;
191         return true;
192 }
193
194