StreamCommandParser.h
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Kernel parse.h */
40 /* */
41 /* Data areas for parse.c */
42 /* */
43 /******************************************************************************/
44 #ifndef StreamCommandParser_Included
45 #define StreamCommandParser_Included
46 
47 #include <cctype>
48 #include <ctype.h>
49 #include "Utilities.hpp"
50  /* return code for gettoken at the end of input string */
51 #define no_token 1
52 
53  /************************************************************************/
54  /* Bit values to be used in the actions field of the actiontablestruct */
55  /************************************************************************/
56 typedef enum
57 {
58  NoAction, // table terminator element
59  BitOr, // or a value into an integer
60  BitAnd, // and a value into an integer
61  MF, // Mutual exclusion field
62  ME, // Mutual exclusion flag
63  MI, // Mutual inclusion flag
64  MEB, // Mutual exclusion bool
65  MIB, // Mutual inclusion bool
66  SetBool, // set a boolean item
67  SetItem, // set an int item to a value
68  CallItem, // additional processing required
69 } ActionType;
70 
71  /************************************************************************/
72  /* Single token structure */
73  /************************************************************************/
74 
75 // single parsing token
77 {
78 public:
79  StreamToken(const char *data)
80  {
81  sourceData = data;
82  string = NULL;
83  length = 0;
84  offset = 0;
85  }
86 
87  bool nextToken();
88  void previousToken();
89  inline void skipBlanks()
90  {
91  while(sourceData[offset] == ' ')
92  {
93  offset++;
94  }
95  }
96 
97 
98  inline bool equals(const char *token)
99  {
100  return Utilities::memicmp(token, string, length) == 0;
101  }
102 
103  inline bool atEnd() { return sourceData[offset] == '\0'; }
104 
105  inline bool toNumber(int64_t &num)
106  {
107  int64_t off = 0;
108 
109  /* convert string into long for later*/
110  for (size_t i = 0; i < length; i++)
111  {
112  char ch = string[i];
113 
114  if (!isdigit(ch))
115  {
116  return false;
117  }
118 
119  off = (off * 10) + (ch - '0');
120  }
121  num = off;
122  return true;
123  }
124 
125 
126  inline bool toNumber(int &num)
127  {
128  int off = 0;
129 
130  /* convert string into long for later*/
131  for (size_t i = 0; i < length; i++)
132  {
133  char ch = string[i];
134 
135  if (!isdigit(ch))
136  {
137  return false;
138  }
139 
140  off = (off * 10) + (ch - '0');
141  }
142  num = off;
143  return true;
144  }
145 
146  inline size_t getLength() { return length; }
147 
148 protected:
149 
150  const char *sourceData;// the source parsing data
151  const char *string; // token data
152  size_t length; // length of the token
153  size_t offset; // offset into the input string for this token
154 };
155 
156 
157 class ParseAction;
158  /***********************************************************************/
159  /* token table - holds parameter token and pointer to the action table */
160  /* and the address of an unknown token function */
161  /***********************************************************************/
162 
164 {
165 public:
166  inline TokenDefinition(const char *t, size_t l, ParseAction *a)
167  {
168  token = t;
169  minlength = l;
170  actions = a;
171  actionRoutine = NULL;
172  }
173 
174  inline TokenDefinition(int (*a)(TokenDefinition *, StreamToken &, void *))
175  {
176  token = NULL;
177  minlength = 0;
178  actions = NULL;
179  actionRoutine = a;
180  }
181 
182  bool isValid() { return token != NULL; }
183  int callUnknown(StreamToken &tokenizer, void *parms)
184  {
185  return (*actionRoutine)(this, tokenizer, parms);
186  }
187  const char *token; // token value
188  size_t minlength; // minimum length for token to be a valid match with the input token
189  ParseAction *actions; // token action definition
190 
191  // the action routine for processing this token.
193 };
194 
195  /************************************************************************/
196  /* action table - information of what to do with what and to who */
197  /************************************************************************/
198 
200 {
201 public:
202  inline ParseAction()
203  {
204  action = NoAction;
205  int_output = NULL;
206  int_value = 0;
207  bool_output = NULL;
208  bool_value = false;
209  afp = NULL;
210  actionParm = NULL;
211  }
212 
213  inline ParseAction(ActionType a, int &target, int source)
214  {
215  action = a;
216  int_output = &target;
217  int_value = source;
218  bool_output = NULL;
219  bool_value = false;
220  afp = NULL;
221  actionParm = NULL;
222  }
223 
224  inline ParseAction(ActionType a, int &target)
225  {
226  action = a;
227  int_output = &target;
228  int_value = 0;
229  bool_output = NULL;
230  bool_value = false;
231  afp = NULL;
232  actionParm = NULL;
233  }
234 
235  inline ParseAction(ActionType a, bool &target, bool source)
236  {
237  action = a;
238  bool_output = &target;
239  bool_value = source;
240  int_output = NULL;
241  int_value = false;
242  afp = NULL;
243  actionParm = NULL;
244  }
245 
246  inline ParseAction(ActionType a, bool &target)
247  {
248  action = a;
249  bool_output = &target;
250  bool_value = false;
251  int_value = 0;
252  int_output = NULL;
253  afp = NULL;
254  actionParm = NULL;
255  }
256 
257  inline ParseAction(ActionType a, int (*act)(TokenDefinition *, StreamToken &, void *), void *parm)
258  {
259  action = a;
260  int_output = NULL;
261  int_value = 0;
262  bool_output = NULL;
263  bool_value = false;
264  afp = act;
265  actionParm = parm;
266  }
267 
268  inline bool isValid() { return action != NoAction; }
269  int applyAction(TokenDefinition *def, StreamToken &token, void *userparms);
270 
271 protected:
272 
273  ActionType action; // the actions to process (defined by bit flags)
274  size_t itemlength; // length of the output item
275  int *int_output; // address of the filled in item
276  bool *bool_output; // address of the filled in item
277  int int_value; // value used for any int manipulations
278  bool bool_value; // integer valued output
279  // type cast for a call action-call
280  int (*afp)(TokenDefinition *, StreamToken &, void *);
281  void *actionParm; // opaque value passed to action
282 };
283 
284  /************************************************************************/
285  /* parse routine prototype */
286  /************************************************************************/
287 int parser(TokenDefinition *ttsp, const char *TokenString, void *userparms);
288 
289 
290 #endif
291 
292 
@ BitAnd
@ CallItem
@ NoAction
@ SetBool
@ SetItem
int parser(TokenDefinition *ttsp, const char *TokenString, void *userparms)
ParseAction(ActionType a, int(*act)(TokenDefinition *, StreamToken &, void *), void *parm)
int(* afp)(TokenDefinition *, StreamToken &, void *)
ParseAction(ActionType a, int &target, int source)
ParseAction(ActionType a, bool &target)
ParseAction(ActionType a, int &target)
ParseAction(ActionType a, bool &target, bool source)
int applyAction(TokenDefinition *def, StreamToken &token, void *userparms)
bool equals(const char *token)
const char * string
const char * sourceData
bool toNumber(int &num)
StreamToken(const char *data)
bool toNumber(int64_t &num)
ParseAction * actions
TokenDefinition(int(*a)(TokenDefinition *, StreamToken &, void *))
int(* actionRoutine)(TokenDefinition *, StreamToken &, void *)
int callUnknown(StreamToken &tokenizer, void *parms)
TokenDefinition(const char *t, size_t l, ParseAction *a)
static int memicmp(const void *opt1, const void *opt2, size_t len)
Definition: Utilities.cpp:127
signed __int64 int64_t