ParseTrigger.cpp
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.oorexx.org/license.html */
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 Translator ParseTrigger.cpp */
40 /* */
41 /* Primitive Procedure Parse Trigger Class */
42 /* */
43 /******************************************************************************/
44 #include <stdlib.h>
45 #include "RexxCore.h"
46 #include "StringClass.hpp"
47 #include "QueueClass.hpp"
48 #include "RexxActivation.hpp"
49 #include "ParseTrigger.hpp"
50 #include "ParseTarget.hpp"
52 
54  int type, /* type of trigger */
55  RexxObject *_value, /* value to evaluatate */
56  size_t _variableCount, /* count of variables */
57  RexxQueue *_variables) /* array of trigger variables */
58 /******************************************************************************/
59 /* Function: Initialize a parse trigger translator object */
60 /******************************************************************************/
61 {
62  this->setType(type); /* set the type (and hashvalue) */
63  this->variableCount = _variableCount; /* set the number of variables also */
64  OrefSet(this, this->value, _value); /* save the associated value */
65  /* loop through the variable list */
66  while (_variableCount > 0) /* copying each variable */
67  {
68  OrefSet(this, this->variables[--_variableCount], (RexxVariableBase *)_variables->pop());
69  }
70 }
71 
72 
74  RexxObject *trigger) /* value to be converted */
75 /******************************************************************************/
76 /* Function: Convert a trigger value to an integer, with appopriate error */
77 /* reporting. */
78 /******************************************************************************/
79 {
80  stringsize_t result; /* converted result */
81  /* convert the value */
82  if (!trigger->requestUnsignedNumber(result, number_digits()))
83  {
84  /* report an exception */
86  }
87  return result; /* finished */
88 }
89 
90 
92  RexxObject *trigger) /* value to be converted */
93 /******************************************************************************/
94 /* Function: Convert a trigger expression to a String, with appopriate error */
95 /* reporting. */
96 /******************************************************************************/
97 {
98  /* force to string form */
99  return REQUEST_STRING(trigger);
100 }
101 
102 
104  RexxActivation *context, /* current execution context */
105  RexxExpressionStack *stack, /* current expression stack */
106  RexxTarget *target ) /* current parsing target string */
107 /******************************************************************************/
108 /* Function: Apply a parsing trigger against a parsing target */
109 /******************************************************************************/
110 {
111  RexxObject *_value = OREF_NULL;/* evaluated trigger part */
112  RexxString *stringvalue; /* new string value */
113  stringsize_t integer; /* target integer value */
114  size_t i; /* loop counter */
115  size_t size; /* size of variables array */
116  RexxVariableBase *variable; /* current variable processing */
117 
118  if (this->value != OREF_NULL)
119  { /* need a value processed? */
120  /* evaluate the expression part */
121  _value = this->value->evaluate(context, stack);
122  context->traceResult(_value); /* trace if necessary */
123  stack->pop(); /* Get rid of the value off the stack*/
124  }
125  switch (this->getType())
126  { /* perform the trigger operations */
127 
128  case TRIGGER_END: /* just match to the end */
129  target->moveToEnd(); /* move the pointers */
130  break;
131 
132  case TRIGGER_PLUS: /* positive relative target */
133  integer = this->integerTrigger(_value); /* get binary version of trigger */
134  target->forward(integer); /* move the position */
135  break;
136 
137  case TRIGGER_MINUS: /* negative relative target */
138  integer = this->integerTrigger(_value); /* get binary version of trigger */
139  target->backward(integer); /* move the position */
140  break;
141 
142  case TRIGGER_PLUS_LENGTH: /* positive length */
143  integer = this->integerTrigger(_value); /* get binary version of trigger */
144  target->forwardLength(integer); /* move the position */
145  break;
146 
147  case TRIGGER_MINUS_LENGTH: /* negative relative target */
148  integer = this->integerTrigger(_value); /* get binary version of trigger */
149  target->backwardLength(integer); /* move the position */
150  break;
151 
152  case TRIGGER_ABSOLUTE: /* absolute column position */
153  integer = this->integerTrigger(_value); /* get binary version of trigger */
154  target->absolute(integer); /* move the position */
155  break;
156 
157  case TRIGGER_STRING: /* string search */
158  /* force to string form */
159  stringvalue = this->stringTrigger(_value);
160  target->search(stringvalue); /* perform the search */
161  break;
162 
163  case TRIGGER_MIXED: /* string search */
164  /* force to string form */
165  stringvalue = this->stringTrigger(_value);
166  /* and go search */
167  target->caselessSearch(stringvalue);
168  break;
169  }
170  if (context->tracingResults())
171  { /* are we tracing? */
172  /* loop through the entire list */
173  for (i = 0, size = this->variableCount; i < size; i++)
174  {
175  if (i + 1 == size) /* last variable? */
176  {
177  _value = target->remainder(); /* extract the remainder */
178  }
179  else
180  {
181  _value = target->getWord(); /* just get the next word */
182  }
183  ProtectedObject p(_value); // needs protecting if the assignment is a compound var.
184  variable = this->variables[i]; /* get the next variable retriever */
185  if (variable != OREF_NULL)
186  { /* not a place holder dummy? */
187  /* set the value */
188  // NOTE: The different variable tpes handle their own assignment tracing
189 
190  variable->assign(context, stack, _value);
191  // if only tracing results and not intermediates, then we need to
192  // trace this value explicitly.
193  if (!context->tracingIntermediates())
194  {
195  context->traceResult(_value); /* trace if necessary */
196  }
197  }
198  else /* dummy variable, just trace it */
199  {
200  /* trace if necessary */
201  context->traceIntermediate(_value, TRACE_PREFIX_DUMMY);
202  }
203  }
204  }
205  else
206  { /* not tracing, can optimize */
207  /* loop through the entire list */
208  for (i = 0, size = this->variableCount; i < size; i++)
209  {
210  variable = this->variables[i]; /* get the next variable retriever */
211  if (variable != OREF_NULL)
212  { /* not a place holder dummy? */
213  if (i + 1 == size) /* last variable? */
214  {
215  _value = target->remainder(); /* extract the remainder */
216  }
217  else
218  {
219  _value = target->getWord(); /* just get the next word */
220  }
221  ProtectedObject p(_value); // needs protecting if the assignment is a compound var.
222  /* set the value */
223  variable->assign(context, stack, _value);
224  }
225  else
226  { /* dummy variable, just skip it */
227  if (i + 1 == size) /* last variable? */
228  {
229  target->skipRemainder(); /* skip the remainder */
230  }
231  else
232  {
233  target->skipWord(); /* just skip the next word */
234  }
235  }
236  }
237  }
238 }
239 
240 void RexxTrigger::live(size_t liveMark)
241 /******************************************************************************/
242 /* Function: Normal garbage collection live marking */
243 /******************************************************************************/
244 {
245  size_t i; /* loop counter */
246  size_t count; /* argument count */
247 
248  for (i = 0, count = this->variableCount; i < count; i++)
249  {
250  memory_mark(this->variables[i]);
251  }
252  memory_mark(this->value);
253 }
254 
255 void RexxTrigger::liveGeneral(int reason)
256 /******************************************************************************/
257 /* Function: Generalized object marking */
258 /******************************************************************************/
259 {
260  size_t i; /* loop counter */
261  size_t count; /* argument count */
262 
263  for (i = 0, count = this->variableCount; i < count; i++)
264  {
265  memory_mark_general(this->variables[i]);
266  }
267  memory_mark_general(this->value);
268 }
269 
271 /******************************************************************************/
272 /* Function: Flatten an object */
273 /******************************************************************************/
274 {
275  size_t i; /* loop counter */
276  size_t count; /* argument count */
277 
278  setUpFlatten(RexxTrigger) /* set up for the flatten */
279 
280  flatten_reference(newThis->value, envelope);
281  for (i = 0, count = this->variableCount; i < count; i++)
282  {
283  flatten_reference(newThis->variables[i], envelope);
284  }
285 
287 }
288 
289 void *RexxTrigger::operator new(size_t size,
290  int variableCount) /* list of variables */
291 /******************************************************************************/
292 /* Function: Create a new parsing trigger object */
293 /******************************************************************************/
294 {
295  /* Get new object */
296  return new_object(size + (variableCount - 1) * sizeof(RexxObject *), T_ParseTrigger);
297 }
298 
void reportException(wholenumber_t error)
@ T_ParseTrigger
size_t number_digits()
Definition: Numerics.hpp:147
#define TRIGGER_MIXED
#define TRIGGER_END
#define TRIGGER_PLUS_LENGTH
#define TRIGGER_MINUS
#define TRIGGER_MINUS_LENGTH
#define TRIGGER_STRING
#define TRIGGER_ABSOLUTE
#define TRIGGER_PLUS
@ TRACE_PREFIX_DUMMY
#define OREF_NULL
Definition: RexxCore.h:61
RexxString * REQUEST_STRING(RexxObject *object)
Definition: RexxCore.h:295
#define OrefSet(o, r, v)
Definition: RexxCore.h:101
#define Error_Invalid_whole_number_parse
#define memory_mark(oref)
Definition: RexxMemory.hpp:450
RexxObject * new_object(size_t s)
Definition: RexxMemory.hpp:436
#define flatten_reference(oref, envel)
Definition: RexxMemory.hpp:498
#define memory_mark_general(oref)
Definition: RexxMemory.hpp:451
#define cleanUpFlatten
Definition: RexxMemory.hpp:484
#define setUpFlatten(type)
Definition: RexxMemory.hpp:478
ssize_t integer(RexxObject *obj, const char *errorMessage)
Definition: TextClass.cpp:289
void traceResult(RexxObject *v)
bool tracingIntermediates()
void traceIntermediate(RexxObject *v, int p)
virtual RexxObject * evaluate(RexxActivation *, RexxExpressionStack *)
bool requestUnsignedNumber(stringsize_t &, size_t)
RexxObject * pop()
Definition: QueueClass.hpp:80
void search(RexxString *)
void backward(stringsize_t)
void absolute(stringsize_t)
void backwardLength(stringsize_t)
RexxString * getWord()
void skipRemainder()
Definition: ParseTarget.hpp:68
RexxString * remainder()
void forwardLength(stringsize_t)
void caselessSearch(RexxString *)
void forward(stringsize_t)
void moveToEnd()
void skipWord()
void parse(RexxActivation *, RexxExpressionStack *, RexxTarget *)
RexxObject * value
void setType(int v)
void liveGeneral(int reason)
size_t variableCount
void flatten(RexxEnvelope *)
void live(size_t)
RexxVariableBase * variables[1]
RexxString * stringTrigger(RexxObject *)
stringsize_t integerTrigger(RexxObject *)
RexxTrigger(int, RexxObject *, size_t, RexxQueue *)
virtual void assign(RexxActivation *, RexxExpressionStack *, RexxObject *)
int type
Definition: cmdparse.cpp:1888
size_t stringsize_t
Definition: rexx.h:228