Clause.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 Clause.c */
40 /* */
41 /* Primitive Translator Clause Class */
42 /* */
43 /* */
44 /* */
45 /******************************************************************************/
46 #include "RexxCore.h"
47 #include "ArrayClass.hpp"
48 #include "Clause.hpp"
49 #include "ProtectedObject.hpp"
50 
51 #define INITIAL_SIZE 50 /* initial size of the token cache */
52 #define EXTEND_SIZE 25 /* size to extend when table fills */
53 
55 /******************************************************************************/
56 /* Function: Finish initialization of a REXX clause object */
57 /******************************************************************************/
58 {
59  /* an array for the tokens */
61  this->first = 1; /* first token is the start */
62  this->current = 1; /* no current token */
63  this->size = INITIAL_SIZE; /* set the token cache size */
64  this->free = 1; /* we have a free token */
65  this->cachedToken = OREF_NULL;
66 }
67 
68 void RexxClause::live(size_t liveMark)
69 /******************************************************************************/
70 /* Function: Normal garbage collection live marking */
71 /******************************************************************************/
72 {
73  memory_mark(this->tokens);
74 }
75 
76 void RexxClause::liveGeneral(int reason)
77 /******************************************************************************/
78 /* Function: Generalized object marking */
79 /******************************************************************************/
80 {
82 }
83 
85 /******************************************************************************/
86 /* Function: Flatten an object */
87 /******************************************************************************/
88 {
90 
91  flatten_reference(newThis->tokens, envelope);
92 
94 }
95 
97  size_t line, /* starting line number */
98  sizeB_t offset) /* starting line offset */
99 /******************************************************************************/
100 /* Function: Set a clause's starting position as a line/offset pair */
101 /******************************************************************************/
102 {
103  this->clauseLocation.setStart(line, offset);
104 }
105 
107  size_t line, /* ending line number */
108  sizeB_t offset) /* ending line offset */
109 /******************************************************************************/
110 /* Function: Set a clause's ending position as a line/offset pair */
111 /******************************************************************************/
112 {
113  clauseLocation.setEnd(line, offset);
114 }
115 
117 /******************************************************************************/
118 /* Function: Remove all tokens that precede the current token from the */
119 /* clause. Used to break a physical clause into mulitple logical */
120 /* clauses (such as a "label: procedure", which is two clauses. */
121 /******************************************************************************/
122 {
123  this->first = this->current; /* set first item to current */
124  /* get first token location */
125  SourceLocation l = ((RexxToken *)((this->tokens)->get(this->current)))->getLocation();
126  /* update the clause location info */
128 }
129 
131 /******************************************************************************/
132 /* Function : Reset a clause object for the "next" clause of the program. */
133 /* This involves resetting all of the caching information for */
134 /* token allocation. */
135 /******************************************************************************/
136 {
137  this->first = 1; /* first token is the start */
138  this->current = 1; /* no current token */
139  this->free = 1; /* we have a free token */
140  this->cachedToken = OREF_NULL;
141 }
142 
144  int classId, /* class of the token */
145  int subclass, /* subclass of the token */
146  RexxString *value, /* associated string value */
147  SourceLocation &l) /* location of the token */
148 /******************************************************************************/
149 /* Function : Return a new token object, with information appropriately */
150 /* filled in */
151 /******************************************************************************/
152 {
153  if (this->free > this->size)
154  { /* need to extend our cache? */
155  /* allocate a larger array */
156  /* first a bulk array of tokens */
157  RexxArray *newTokens = new_arrayOfObject(sizeof(RexxToken), EXTEND_SIZE, T_Token);
158  ProtectedObject p(newTokens);
159  RexxArray *newarray = (RexxArray *)this->tokens->join(newTokens);
160  this->size += EXTEND_SIZE; /* bump the cache size */
161  /* replace the old array */
162  OrefSet(this, this->tokens, newarray);
163  }
164  /* get the first free token */
165  RexxToken *token = (RexxToken *)this->tokens->get(this->free);
166  this->free++; /* step the free location */
167  /* fill in the token */
168  new ((void *)token) RexxToken(classId, subclass, value, l);
169 
170  // To avoid a pointer to the old array in case of reallocation
171  // and because the cached token is always the most recent token.
172  this->cachedToken = OREF_NULL;
173 
174  return token; /* send the token back */
175 }
176 
178 /******************************************************************************/
179 /* Function: Return next non-blank token in the clause list */
180 /******************************************************************************/
181 {
182  RexxToken *token = this->next(); /* get the next token */
183  while (token->classId == TOKEN_BLANK)/* now loop until get a non-blank */
184  {
185  token = this->next(); /* get the next token */
186  }
187  return token; /* return retrieved token */
188 }
189 
190 void *RexxClause::operator new(size_t size)
191 /******************************************************************************/
192 /* Function: Create a new translator object */
193 /******************************************************************************/
194 {
195  return new_object(size, T_Clause);
196 }
197 
@ T_Token
@ T_Clause
#define INITIAL_SIZE
Definition: Clause.cpp:51
#define EXTEND_SIZE
Definition: Clause.cpp:52
#define OREF_NULL
Definition: RexxCore.h:60
#define OrefSet(o, r, v)
Definition: RexxCore.h:94
RexxArray * new_arrayOfObject(size_t s, size_t c, size_t t)
Definition: RexxMemory.hpp:434
#define memory_mark(oref)
Definition: RexxMemory.hpp:445
RexxObject * new_object(size_t s)
Definition: RexxMemory.hpp:431
#define flatten_reference(oref, envel)
Definition: RexxMemory.hpp:493
#define memory_mark_general(oref)
Definition: RexxMemory.hpp:446
#define cleanUpFlatten
Definition: RexxMemory.hpp:479
#define setUpFlatten(type)
Definition: RexxMemory.hpp:473
#define TOKEN_BLANK
Definition: Token.hpp:77
RexxObject * join(RexxArray *)
RexxObject * get(size_t pos)
Definition: ArrayClass.hpp:203
RexxClause()
Definition: Clause.cpp:54
void newClause()
Definition: Clause.cpp:130
size_t current
Definition: Clause.hpp:80
void flatten(RexxEnvelope *)
Definition: Clause.cpp:84
void trim()
Definition: Clause.cpp:116
RexxToken * newToken(int, int, RexxString *, SourceLocation &)
Definition: Clause.cpp:143
void setEnd(size_t, sizeB_t)
Definition: Clause.cpp:106
void liveGeneral(int reason)
Definition: Clause.cpp:76
void setStart(size_t, sizeB_t)
Definition: Clause.cpp:96
size_t first
Definition: Clause.hpp:81
size_t free
Definition: Clause.hpp:84
RexxToken * nextRealToken()
Definition: Clause.cpp:177
SourceLocation clauseLocation
Definition: Clause.hpp:79
RexxArray * tokens
Definition: Clause.hpp:82
void live(size_t)
Definition: Clause.cpp:68
RexxToken * cachedToken
Definition: Clause.hpp:92
size_t size
Definition: Clause.hpp:83
RexxToken * next()
Definition: Clause.hpp:75
int classId
Definition: Token.hpp:448
void setEnd(SourceLocation &l)
void setStart(SourceLocation &l)
stringsizeB_t sizeB_t
Definition: rexx.h:248
char line[LINEBUFSIZE]