2 * \brief Defines the interface for a common token.
4 * All token streams should provide their tokens using an instance
5 * of this common token. A custom pointer is provided, wher you may attach
6 * a further structure to enhance the common token if you feel the need
7 * to do so. The C runtime will assume that a token provides implementations
8 * of the interface functions, but all of them may be rplaced by your own
9 * implementation if you require it.
11 #ifndef _ANTLR3_COMMON_TOKEN_H
12 #define _ANTLR3_COMMON_TOKEN_H
14 // [The "BSD licence"]
15 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
16 // http://www.temporal-wave.com
17 // http://www.linkedin.com/in/jimidle
19 // All rights reserved.
21 // Redistribution and use in source and binary forms, with or without
22 // modification, are permitted provided that the following conditions
24 // 1. Redistributions of source code must retain the above copyright
25 // notice, this list of conditions and the following disclaimer.
26 // 2. Redistributions in binary form must reproduce the above copyright
27 // notice, this list of conditions and the following disclaimer in the
28 // documentation and/or other materials provided with the distribution.
29 // 3. The name of the author may not be used to endorse or promote products
30 // derived from this software without specific prior written permission.
32 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
33 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
36 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 #include <antlr3defs.h>
45 /** How many tokens to allocate at once in the token factory
47 #define ANTLR3_FACTORY_POOL_SIZE 1024
49 /* Base token types, which all lexer/parser tokens come after in sequence.
52 /** Indicator of an invalid token
54 #define ANTLR3_TOKEN_INVALID 0
56 #define ANTLR3_EOR_TOKEN_TYPE 1
58 /** Imaginary token type to cause a traversal of child nodes in a tree parser
60 #define ANTLR3_TOKEN_DOWN 2
62 /** Imaginary token type to signal the end of a stream of child nodes.
64 #define ANTLR3_TOKEN_UP 3
66 /** First token that can be used by users/generated code
69 #define ANTLR3_MIN_TOKEN_TYPE ANTLR3_TOKEN_UP + 1
73 #define ANTLR3_TOKEN_EOF (ANTLR3_CHARSTREAM_EOF & 0xFFFFFFFF)
75 /** Default channel for a token
77 #define ANTLR3_TOKEN_DEFAULT_CHANNEL 0
79 /** Reserved channel number for a HIDDEN token - a token that
80 * is hidden from the parser.
88 // Indicates whether this token is carrying:
91 // ------+--------------------------------------
92 // 0 | Nothing (neither rewrite text, nor setText)
93 // 1 | char * to user supplied rewrite text
94 // 2 | pANTLR3_STRING because of setText or similar action
96 #define ANTLR3_TEXT_NONE 0
97 #define ANTLR3_TEXT_CHARP 1
98 #define ANTLR3_TEXT_STRING 2
100 /** The definition of an ANTLR3 common token structure, which all implementations
101 * of a token stream should provide, installing any further structures in the
102 * custom pointer element of this structure.
105 * Token streams are in essence provided by lexers or other programs that serve
108 typedef struct ANTLR3_COMMON_TOKEN_struct
110 /** The actual type of this token
114 /** Indicates that a token was produced from the token factory and therefore
115 * the the freeToken() method should not do anything itself because
116 * token factory is responsible for deleting it.
118 ANTLR3_BOOLEAN factoryMade;
120 /// A string factory that we can use if we ever need the text of a token
121 /// and need to manufacture a pANTLR3_STRING
123 pANTLR3_STRING_FACTORY strFactory;
125 /** The line number in the input stream where this token was derived from
129 /** The offset into the input stream that the line in which this
130 * token resides starts.
134 /** The character position in the line that this token was derived from
136 ANTLR3_INT32 charPosition;
138 /** The virtual channel that this token exists in.
140 ANTLR3_UINT32 channel;
142 /** Pointer to the input stream that this token originated in.
144 pANTLR3_INPUT_STREAM input;
146 /** What the index of this token is, 0, 1, .., n-2, n-1 tokens
150 /** The character offset in the input stream where the text for this token
155 /** The character offset in the input stream where the text for this token
160 /// Indicates whether this token is carrying:
163 /// ------+--------------------------------------
164 /// 0 | Nothing (neither rewrite text, nor setText)
165 /// 1 | char * to user supplied rewrite text
166 /// 2 | pANTLR3_STRING because of setText or similar action
168 /// Affects the union structure tokText below
169 /// (uses 32 bit so alignment is always good)
171 ANTLR3_UINT32 textState;
175 /// Pointer that is used when the token just has a pointer to
176 /// a char *, such as when a rewrite of an imaginary token supplies
177 /// a string in the grammar. No sense in constructing a pANTLR3_STRING just
178 /// for that, as mostly the text will not be accessed - if it is, then
179 /// we will build a pANTLR3_STRING for it a that point.
183 /// Some token types actually do carry around their associated text, hence
184 /// (*getText)() will return this pointer if it is not NULL
190 /** Because it is a bit more of a hassle to override an ANTLR3_COMMON_TOKEN
191 * as the standard structure for a token, a number of user programmable
192 * elements are allowed in a token. This is one of them.
196 /** Because it is a bit more of a hassle to override an ANTLR3_COMMON_TOKEN
197 * as the standard structure for a token, a number of user programmable
198 * elements are allowed in a token. This is one of them.
202 /** Because it is a bit more of a hassle to override an ANTLR3_COMMON_TOKEN
203 * as the standard structure for a token, a number of user programmable
204 * elements are allowed in a token. This is one of them.
208 /** Pointer to a custom element that the ANTLR3 programmer may define and install
212 /** Pointer to a function that knows how to free the custom structure when the
213 * token is destroyed.
215 void (*freeCustom)(void * custom);
217 /* ==============================
221 /** Pointer to function that returns the text pointer of a token, use
222 * toString() if you want a pANTLR3_STRING version of the token.
224 pANTLR3_STRING (*getText)(struct ANTLR3_COMMON_TOKEN_struct * token);
226 /** Pointer to a function that 'might' be able to set the text associated
227 * with a token. Imaginary tokens such as an ANTLR3_CLASSIC_TOKEN may actually
228 * do this, however many tokens such as ANTLR3_COMMON_TOKEN do not actaully have
229 * strings associated with them but just point into the current input stream. These
230 * tokens will implement this function with a function that errors out (probably
233 void (*setText)(struct ANTLR3_COMMON_TOKEN_struct * token, pANTLR3_STRING text);
235 /** Pointer to a function that 'might' be able to set the text associated
236 * with a token. Imaginary tokens such as an ANTLR3_CLASSIC_TOKEN may actually
237 * do this, however many tokens such as ANTLR3_COMMON_TOKEN do not actully have
238 * strings associated with them but just point into the current input stream. These
239 * tokens will implement this function with a function that errors out (probably
242 void (*setText8)(struct ANTLR3_COMMON_TOKEN_struct * token, pANTLR3_UINT8 text);
244 /** Pointer to a function that returns the token type of this token
246 ANTLR3_UINT32 (*getType)(struct ANTLR3_COMMON_TOKEN_struct * token);
248 /** Pointer to a function that sets the type of this token
250 void (*setType)(struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_UINT32 ttype);
252 /** Pointer to a function that gets the 'line' number where this token resides
254 ANTLR3_UINT32 (*getLine)(struct ANTLR3_COMMON_TOKEN_struct * token);
256 /** Pointer to a function that sets the 'line' number where this token reside
258 void (*setLine)(struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_UINT32 line);
260 /** Pointer to a function that gets the offset in the line where this token exists
262 ANTLR3_INT32 (*getCharPositionInLine) (struct ANTLR3_COMMON_TOKEN_struct * token);
264 /** Pointer to a function that sets the offset in the line where this token exists
266 void (*setCharPositionInLine) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_INT32 pos);
268 /** Pointer to a function that gets the channel that this token was placed in (parsers
269 * can 'tune' to these channels.
271 ANTLR3_UINT32 (*getChannel) (struct ANTLR3_COMMON_TOKEN_struct * token);
273 /** Pointer to a function that sets the channel that this token should belong to
275 void (*setChannel) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_UINT32 channel);
277 /** Pointer to a function that returns an index 0...n-1 of the token in the token
280 ANTLR3_MARKER (*getTokenIndex) (struct ANTLR3_COMMON_TOKEN_struct * token);
282 /** Pointer to a function that can set the token index of this token in the token
285 void (*setTokenIndex) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_MARKER);
287 /** Pointer to a function that gets the start index in the input stream for this token.
289 ANTLR3_MARKER (*getStartIndex) (struct ANTLR3_COMMON_TOKEN_struct * token);
291 /** Pointer to a function that sets the start index in the input stream for this token.
293 void (*setStartIndex) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_MARKER index);
295 /** Pointer to a function that gets the stop index in the input stream for this token.
297 ANTLR3_MARKER (*getStopIndex) (struct ANTLR3_COMMON_TOKEN_struct * token);
299 /** Pointer to a function that sets the stop index in the input stream for this token.
301 void (*setStopIndex) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_MARKER index);
303 /** Pointer to a function that returns this token as a text representation that can be
304 * printed with embedded control codes such as \n replaced with the printable sequence "\\n"
305 * This also yields a string structure that can be used more easily than the pointer to
306 * the input stream in certain situations.
308 pANTLR3_STRING (*toString) (struct ANTLR3_COMMON_TOKEN_struct * token);
312 /** \brief ANTLR3 Token factory interface to create lots of tokens efficiently
313 * rather than creating and freeing lots of little bits of memory.
315 typedef struct ANTLR3_TOKEN_FACTORY_struct
317 /** Pointers to the array of tokens that this factory has produced so far
319 pANTLR3_COMMON_TOKEN *pools;
321 /** Current pool tokens we are allocating from
323 ANTLR3_INT32 thisPool;
325 /** The next token to throw out from the pool, will cause a new pool allocation
326 * if this exceeds the available tokenCount
328 ANTLR3_UINT32 nextToken;
330 /** Trick to initialize tokens and their API quickly, we set up this token when the
331 * factory is created, then just copy the memory it uses into the new token.
333 ANTLR3_COMMON_TOKEN unTruc;
335 /** Pointer to an input stream that is using this token factory (may be NULL)
336 * which will be assigned to the tokens automatically.
338 pANTLR3_INPUT_STREAM input;
340 /** Pointer to a function that returns a new token
342 pANTLR3_COMMON_TOKEN (*newToken) (struct ANTLR3_TOKEN_FACTORY_struct * factory);
344 /** Pointer to a function that changes teh curent inptu stream so that
345 * new tokens are created with reference to their originating text.
347 void (*setInputStream) (struct ANTLR3_TOKEN_FACTORY_struct * factory, pANTLR3_INPUT_STREAM input);
348 /** Pointer to a function the destroys the factory
350 void (*close) (struct ANTLR3_TOKEN_FACTORY_struct * factory);
352 ANTLR3_TOKEN_FACTORY;