Token.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 Kernel */
40 /* */
41 /* Primitive Translator Token Class */
42 /* */
43 /******************************************************************************/
44 #include <ctype.h>
45 #include <string.h>
46 #include "RexxCore.h"
47 #include "StringClass.hpp"
48 #include "Token.hpp"
49 #include "SourceFile.hpp"
50 
52  int _classId, /* class of token */
53  int _subclass, /* token subclass */
54  RexxString *_value, /* token value */
55  SourceLocation &_location) /* token location descriptor */
56 /******************************************************************************/
57 /* Function: Complete set up of a TOKEN object */
58 /******************************************************************************/
59 {
60  OrefSet(this, this->value, _value); /* use the provided string value */
61  this->classId = _classId; /* no assigned token class */
62  this->subclass = _subclass; /* no specialization yet */
63  this->tokenLocation = _location; /* copy it over */
64  this->numeric = 0;
65  this->precedence = 0;
66 }
67 
68 void RexxToken::live(size_t liveMark)
69 /******************************************************************************/
70 /* Function: Normal garbage collection live marking */
71 /******************************************************************************/
72 {
73  memory_mark(this->value);
74 }
75 
76 void RexxToken::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->value, envelope);
92 
94 }
95 
96 
97 /**
98  * Check and update this token for the special assignment forms
99  * (+=, -=, etc.).
100  *
101  * @param source The source for the original operator token.
102  */
104 {
105  // check if the next character is a special assignment shortcut
106  if (source->nextSpecial('=', tokenLocation))
107  {
108  // this is a special type, which uses the same subtype.
110  // this is the new string value of the token
111  value = newValue;
112  }
113 }
114 
115 
116 void *RexxToken::operator new(size_t size)
117 /******************************************************************************/
118 /* Function: Create a new token object */
119 /******************************************************************************/
120 {
121  /* Get new object */
122  return new_object(size, T_Token);
123 }
124 
125 
126 #define CODE_TEXT(code) case code : return #code;
127 
128 const char *RexxToken::codeText(int code)
129 /******************************************************************************/
130 /* Function: Returns the text of a code symbol */
131 /******************************************************************************/
132 {
133  static char buffer[10]; // Used only to return the code as number, when no text
134  switch(code)
135  {
136  /* token types */
157 
158  /* token extended types - symbols */
169 
170  /* token extended types - operators */
203 
204  /* token extended types - instruction keywords */
206  // For the text of each keyword see RexxToken::keywordText
207 
208  /* token extended types - instruction option keywords */
210 
211  /* token extended types - end of clause */
216 
217  /* directive types */
219 
220  /* directive sub-keywords */
222 
223  /* condition keywords */
225 
226  /* builtin function codes */
228 
229  default:
230  Utilities::snprintf(buffer, sizeof(buffer), "%i", code);
231  return buffer;
232  }
233 }
234 
235 
236 const char *RexxToken::keywordText(int code)
237 /******************************************************************************/
238 /* Function: Returns the text of a keyword */
239 /******************************************************************************/
240 {
241  static char buffer[10]; // Used only to return the code as number, when no text
242  switch(code)
243  {
244  /* token extended types - instruction keywords */
301 
302  default:
303  Utilities::snprintf(buffer, sizeof(buffer), "%i", code);
304  return buffer;
305  }
306 }
@ T_Token
#define OrefSet(o, r, v)
Definition: RexxCore.h:101
#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
#define CODE_TEXT(code)
Definition: Token.cpp:126
#define IS_SUBDIRECTIVE
Definition: Token.hpp:275
#define OPERATOR_DIVIDE
Definition: Token.hpp:113
#define KEYWORD_OTHERWISE
Definition: Token.hpp:180
#define KEYWORD_MESSAGE
Definition: Token.hpp:186
#define KEYWORD_PULL
Definition: Token.hpp:162
#define KEYWORD_SELECT
Definition: Token.hpp:168
#define TOKEN_LITERAL
Definition: Token.hpp:79
#define KEYWORD_UPPER
Definition: Token.hpp:201
#define KEYWORD_PARSE
Definition: Token.hpp:160
#define KEYWORD_BLOCK
Definition: Token.hpp:189
#define OPERATOR_SUBTRACT
Definition: Token.hpp:111
#define OPERATOR_INTDIV
Definition: Token.hpp:114
#define TOKEN_COLON
Definition: Token.hpp:87
#define KEYWORD_LIBRARY
Definition: Token.hpp:200
#define TOKEN_OPERATOR
Definition: Token.hpp:80
#define KEYWORD_NOP
Definition: Token.hpp:157
#define KEYWORD_ENDTHEN
Definition: Token.hpp:193
#define OPERATOR_BACKSLASH
Definition: Token.hpp:141
#define OPERATOR_EQUAL
Definition: Token.hpp:120
#define KEYWORD_REPLY
Definition: Token.hpp:165
#define KEYWORD_INSTRUCTION
Definition: Token.hpp:197
#define IS_DIRECTIVE
Definition: Token.hpp:264
#define KEYWORD_SIGNAL
Definition: Token.hpp:169
#define TOKEN_LEFT
Definition: Token.hpp:84
#define KEYWORD_RETURN
Definition: Token.hpp:166
#define TOKEN_POINT
Definition: Token.hpp:86
#define KEYWORD_LAST
Definition: Token.hpp:191
#define SYMBOL_CONSTANT
Definition: Token.hpp:98
#define OPERATOR_XOR
Definition: Token.hpp:140
#define KEYWORD_DO
Definition: Token.hpp:149
#define OPERATOR_STRICT_GREATERTHAN_EQUAL
Definition: Token.hpp:134
#define KEYWORD_REQUIRES
Definition: Token.hpp:195
#define KEYWORD_OPTIONS
Definition: Token.hpp:159
#define IS_BUILTIN
Definition: Token.hpp:321
#define SYMBOL_NAME
Definition: Token.hpp:100
#define OPERATOR_POWER
Definition: Token.hpp:116
#define KEYWORD_CALL
Definition: Token.hpp:148
#define KEYWORD_PUSH
Definition: Token.hpp:163
#define TOKEN_BLANK
Definition: Token.hpp:77
#define KEYWORD_ITERATE
Definition: Token.hpp:154
#define IS_SUBKEY
Definition: Token.hpp:204
#define KEYWORD_COMMAND
Definition: Token.hpp:185
#define OPERATOR_BACKSLASH_EQUAL
Definition: Token.hpp:121
#define KEYWORD_IFTHEN
Definition: Token.hpp:181
#define KEYWORD_WHENTHEN
Definition: Token.hpp:182
#define KEYWORD_PROCEDURE
Definition: Token.hpp:161
#define SYMBOL_DOTSYMBOL
Definition: Token.hpp:104
#define KEYWORD_LEAVE
Definition: Token.hpp:155
#define KEYWORD_ENDIF
Definition: Token.hpp:188
#define OPERATOR_PLUS
Definition: Token.hpp:110
#define TOKEN_DCOLON
Definition: Token.hpp:92
#define CLAUSEEND_SEMICOLON
Definition: Token.hpp:259
#define KEYWORD_ASSIGNMENT
Definition: Token.hpp:184
#define LITERAL_HEX
Definition: Token.hpp:106
#define OPERATOR_ABUTTAL
Definition: Token.hpp:117
#define KEYWORD_VAR
Definition: Token.hpp:171
#define OPERATOR_STRICT_EQUAL
Definition: Token.hpp:128
#define OPERATOR_LESSTHAN_EQUAL
Definition: Token.hpp:127
#define IS_KEYWORD
Definition: Token.hpp:145
#define KEYWORD_WHEN
Definition: Token.hpp:183
#define KEYWORD_NUMERIC
Definition: Token.hpp:158
#define TOKEN_COMMA
Definition: Token.hpp:82
#define KEYWORD_CLASS
Definition: Token.hpp:196
#define KEYWORD_ADDRESS
Definition: Token.hpp:146
#define OPERATOR_STRICT_BACKSLASH_EQUAL
Definition: Token.hpp:129
#define OPERATOR_GREATERTHAN_LESSTHAN
Definition: Token.hpp:137
#define SYMBOL_COMPOUND
Definition: Token.hpp:101
#define KEYWORD_QUEUE
Definition: Token.hpp:164
#define KEYWORD_ARG
Definition: Token.hpp:147
#define OPERATOR_GREATERTHAN_EQUAL
Definition: Token.hpp:126
#define KEYWORD_ELSE
Definition: Token.hpp:177
#define KEYWORD_IF
Definition: Token.hpp:152
#define KEYWORD_USE
Definition: Token.hpp:173
#define TOKEN_CONTINUE
Definition: Token.hpp:93
#define OPERATOR_BACKSLASH_LESSTHAN
Definition: Token.hpp:125
#define SYMBOL_STEM
Definition: Token.hpp:102
#define OPERATOR_CONCATENATE
Definition: Token.hpp:118
#define KEYWORD_LABEL
Definition: Token.hpp:187
#define KEYWORD_FORWARD
Definition: Token.hpp:198
#define OPERATOR_MULTIPLY
Definition: Token.hpp:112
#define OPERATOR_OR
Definition: Token.hpp:139
#define SYMBOL_VARIABLE
Definition: Token.hpp:99
#define OPERATOR_LESSTHAN_GREATERTHAN
Definition: Token.hpp:136
#define KEYWORD_INITPROC
Definition: Token.hpp:174
#define KEYWORD_GUARD
Definition: Token.hpp:172
#define OPERATOR_LESSTHAN
Definition: Token.hpp:124
#define TOKEN_SQRIGHT
Definition: Token.hpp:91
#define OPERATOR_STRICT_LESSTHAN_EQUAL
Definition: Token.hpp:135
#define OPERATOR_AND
Definition: Token.hpp:138
#define OPERATOR_GREATERTHAN
Definition: Token.hpp:122
#define CLAUSEEND_EOL
Definition: Token.hpp:260
#define CLAUSEEND_EOF
Definition: Token.hpp:258
#define KEYWORD_EXPOSE
Definition: Token.hpp:175
#define KEYWORD_THEN
Definition: Token.hpp:178
#define OPERATOR_STRICT_GREATERTHAN
Definition: Token.hpp:130
#define KEYWORD_INTERPRET
Definition: Token.hpp:153
#define OPERATOR_REMAINDER
Definition: Token.hpp:115
#define TOKEN_DTILDE
Definition: Token.hpp:89
#define KEYWORD_RAISE
Definition: Token.hpp:176
#define TOKEN_EOC
Definition: Token.hpp:81
#define KEYWORD_EXIT
Definition: Token.hpp:151
#define KEYWORD_ENDELSE
Definition: Token.hpp:192
#define OPERATOR_STRICT_LESSTHAN
Definition: Token.hpp:132
#define KEYWORD_DROP
Definition: Token.hpp:150
#define KEYWORD_FIRST
Definition: Token.hpp:190
#define IS_CONDITION
Definition: Token.hpp:304
#define TOKEN_RIGHT
Definition: Token.hpp:85
#define TOKEN_PREFIX
Definition: Token.hpp:83
#define TOKEN_SQLEFT
Definition: Token.hpp:90
#define KEYWORD_TRACE
Definition: Token.hpp:170
#define OPERATOR_BACKSLASH_GREATERTHAN
Definition: Token.hpp:123
#define TOKEN_ASSIGNMENT
Definition: Token.hpp:94
#define KEYWORD_METHOD
Definition: Token.hpp:156
#define KEYWORD_LOOP
Definition: Token.hpp:199
#define KEYWORD_SAY
Definition: Token.hpp:167
#define KEYWORD_ENDWHEN
Definition: Token.hpp:194
#define TOKEN_TILDE
Definition: Token.hpp:88
#define OPERATOR_STRICT_BACKSLASH_LESSTHAN
Definition: Token.hpp:133
#define INTEGER_CONSTANT
Definition: Token.hpp:105
#define TOKEN_SYMBOL
Definition: Token.hpp:78
#define OPERATOR_BLANK
Definition: Token.hpp:119
#define KEYWORD_END
Definition: Token.hpp:179
#define CLAUSEEND_NULL
Definition: Token.hpp:261
#define OPERATOR_STRICT_BACKSLASH_GREATERTHAN
Definition: Token.hpp:131
#define SYMBOL_DUMMY
Definition: Token.hpp:103
#define LITERAL_BIN
Definition: Token.hpp:107
#define TOKEN_SOURCE_LITERAL
Definition: Token.hpp:95
#define TOKEN_NULL
Definition: Token.hpp:76
bool nextSpecial(unsigned int, SourceLocation &, bool advance=true)
Definition: Scanner.cpp:221
int numeric
Definition: Token.hpp:451
RexxString * value
Definition: Token.hpp:448
SourceLocation tokenLocation
Definition: Token.hpp:447
void flatten(RexxEnvelope *)
Definition: Token.cpp:84
static const char * keywordText(int code)
Definition: Token.cpp:236
RexxToken(int, int, RexxString *, SourceLocation &)
Definition: Token.cpp:51
static const char * codeText(int code)
Definition: Token.cpp:128
void checkAssignment(RexxSource *source, RexxString *newValue)
Definition: Token.cpp:103
void liveGeneral(int reason)
Definition: Token.cpp:76
int precedence
Definition: Token.hpp:452
int classId
Definition: Token.hpp:449
int subclass
Definition: Token.hpp:450
void live(size_t)
Definition: Token.cpp:68
static int snprintf(char *buffer, size_t count, const char *format,...)