--- /dev/null
+/** \file\r
+ * \brief Defines the interface for a common token.\r
+ *\r
+ * All token streams should provide their tokens using an instance\r
+ * of this common token. A custom pointer is provided, wher you may attach\r
+ * a further structure to enhance the common token if you feel the need\r
+ * to do so. The C runtime will assume that a token provides implementations\r
+ * of the interface functions, but all of them may be rplaced by your own\r
+ * implementation if you require it.\r
+ */\r
+#ifndef _ANTLR3_COMMON_TOKEN_H\r
+#define _ANTLR3_COMMON_TOKEN_H\r
+\r
+// [The "BSD licence"]\r
+// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC\r
+// http://www.temporal-wave.com\r
+// http://www.linkedin.com/in/jimidle\r
+//\r
+// All rights reserved.\r
+//\r
+// Redistribution and use in source and binary forms, with or without\r
+// modification, are permitted provided that the following conditions\r
+// are met:\r
+// 1. Redistributions of source code must retain the above copyright\r
+// notice, this list of conditions and the following disclaimer.\r
+// 2. Redistributions in binary form must reproduce the above copyright\r
+// notice, this list of conditions and the following disclaimer in the\r
+// documentation and/or other materials provided with the distribution.\r
+// 3. The name of the author may not be used to endorse or promote products\r
+// derived from this software without specific prior written permission.\r
+//\r
+// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
+// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
+// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
+// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
+// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+#include <antlr3defs.h>\r
+\r
+/** How many tokens to allocate at once in the token factory\r
+ */\r
+#define ANTLR3_FACTORY_POOL_SIZE 1024\r
+\r
+/* Base token types, which all lexer/parser tokens come after in sequence.\r
+ */\r
+\r
+/** Indicator of an invalid token\r
+ */\r
+#define ANTLR3_TOKEN_INVALID 0\r
+\r
+#define ANTLR3_EOR_TOKEN_TYPE 1\r
+\r
+/** Imaginary token type to cause a traversal of child nodes in a tree parser\r
+ */\r
+#define ANTLR3_TOKEN_DOWN 2\r
+\r
+/** Imaginary token type to signal the end of a stream of child nodes.\r
+ */\r
+#define ANTLR3_TOKEN_UP 3\r
+\r
+/** First token that can be used by users/generated code\r
+ */\r
+\r
+#define ANTLR3_MIN_TOKEN_TYPE ANTLR3_TOKEN_UP + 1\r
+\r
+/** End of file token\r
+ */\r
+#define ANTLR3_TOKEN_EOF (ANTLR3_CHARSTREAM_EOF & 0xFFFFFFFF)\r
+\r
+/** Default channel for a token\r
+ */\r
+#define ANTLR3_TOKEN_DEFAULT_CHANNEL 0\r
+\r
+/** Reserved channel number for a HIDDEN token - a token that\r
+ * is hidden from the parser.\r
+ */\r
+#define HIDDEN 99\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+// Indicates whether this token is carrying:\r
+//\r
+// State | Meaning\r
+// ------+--------------------------------------\r
+// 0 | Nothing (neither rewrite text, nor setText)\r
+// 1 | char * to user supplied rewrite text\r
+// 2 | pANTLR3_STRING because of setText or similar action\r
+//\r
+#define ANTLR3_TEXT_NONE 0\r
+#define ANTLR3_TEXT_CHARP 1\r
+#define ANTLR3_TEXT_STRING 2\r
+\r
+/** The definition of an ANTLR3 common token structure, which all implementations\r
+ * of a token stream should provide, installing any further structures in the\r
+ * custom pointer element of this structure.\r
+ *\r
+ * \remark\r
+ * Token streams are in essence provided by lexers or other programs that serve\r
+ * as lexers.\r
+ */\r
+typedef struct ANTLR3_COMMON_TOKEN_struct\r
+{\r
+ /** The actual type of this token\r
+ */\r
+ ANTLR3_UINT32 type;\r
+\r
+ /** Indicates that a token was produced from the token factory and therefore\r
+ * the the freeToken() method should not do anything itself because\r
+ * token factory is responsible for deleting it.\r
+ */\r
+ ANTLR3_BOOLEAN factoryMade;\r
+\r
+ /// A string factory that we can use if we ever need the text of a token\r
+ /// and need to manufacture a pANTLR3_STRING\r
+ ///\r
+ pANTLR3_STRING_FACTORY strFactory;\r
+\r
+ /** The line number in the input stream where this token was derived from\r
+ */\r
+ ANTLR3_UINT32 line;\r
+\r
+ /** The offset into the input stream that the line in which this\r
+ * token resides starts.\r
+ */\r
+ void * lineStart;\r
+\r
+ /** The character position in the line that this token was derived from\r
+ */\r
+ ANTLR3_INT32 charPosition;\r
+\r
+ /** The virtual channel that this token exists in.\r
+ */\r
+ ANTLR3_UINT32 channel;\r
+\r
+ /** Pointer to the input stream that this token originated in.\r
+ */\r
+ pANTLR3_INPUT_STREAM input;\r
+\r
+ /** What the index of this token is, 0, 1, .., n-2, n-1 tokens\r
+ */\r
+ ANTLR3_MARKER index;\r
+\r
+ /** The character offset in the input stream where the text for this token\r
+ * starts.\r
+ */\r
+ ANTLR3_MARKER start;\r
+\r
+ /** The character offset in the input stream where the text for this token\r
+ * stops.\r
+ */\r
+ ANTLR3_MARKER stop;\r
+\r
+ /// Indicates whether this token is carrying:\r
+ ///\r
+ /// State | Meaning\r
+ /// ------+--------------------------------------\r
+ /// 0 | Nothing (neither rewrite text, nor setText)\r
+ /// 1 | char * to user supplied rewrite text\r
+ /// 2 | pANTLR3_STRING because of setText or similar action\r
+ ///\r
+ /// Affects the union structure tokText below\r
+ /// (uses 32 bit so alignment is always good)\r
+ ///\r
+ ANTLR3_UINT32 textState;\r
+\r
+ union\r
+ {\r
+ /// Pointer that is used when the token just has a pointer to\r
+ /// a char *, such as when a rewrite of an imaginary token supplies\r
+ /// a string in the grammar. No sense in constructing a pANTLR3_STRING just\r
+ /// for that, as mostly the text will not be accessed - if it is, then\r
+ /// we will build a pANTLR3_STRING for it a that point.\r
+ ///\r
+ pANTLR3_UCHAR chars;\r
+\r
+ /// Some token types actually do carry around their associated text, hence\r
+ /// (*getText)() will return this pointer if it is not NULL\r
+ ///\r
+ pANTLR3_STRING text;\r
+ }\r
+ tokText;\r
+\r
+ /** Because it is a bit more of a hassle to override an ANTLR3_COMMON_TOKEN\r
+ * as the standard structure for a token, a number of user programmable \r
+ * elements are allowed in a token. This is one of them.\r
+ */\r
+ ANTLR3_UINT32 user1;\r
+ \r
+ /** Because it is a bit more of a hassle to override an ANTLR3_COMMON_TOKEN\r
+ * as the standard structure for a token, a number of user programmable \r
+ * elements are allowed in a token. This is one of them.\r
+ */\r
+ ANTLR3_UINT32 user2;\r
+\r
+ /** Because it is a bit more of a hassle to override an ANTLR3_COMMON_TOKEN\r
+ * as the standard structure for a token, a number of user programmable \r
+ * elements are allowed in a token. This is one of them.\r
+ */\r
+ ANTLR3_UINT32 user3;\r
+\r
+ /** Pointer to a custom element that the ANTLR3 programmer may define and install\r
+ */\r
+ void * custom;\r
+\r
+ /** Pointer to a function that knows how to free the custom structure when the \r
+ * token is destroyed.\r
+ */\r
+ void (*freeCustom)(void * custom);\r
+\r
+ /* ==============================\r
+ * API \r
+ */\r
+\r
+ /** Pointer to function that returns the text pointer of a token, use\r
+ * toString() if you want a pANTLR3_STRING version of the token.\r
+ */\r
+ pANTLR3_STRING (*getText)(struct ANTLR3_COMMON_TOKEN_struct * token);\r
+\r
+ /** Pointer to a function that 'might' be able to set the text associated\r
+ * with a token. Imaginary tokens such as an ANTLR3_CLASSIC_TOKEN may actually\r
+ * do this, however many tokens such as ANTLR3_COMMON_TOKEN do not actaully have\r
+ * strings associated with them but just point into the current input stream. These\r
+ * tokens will implement this function with a function that errors out (probably\r
+ * drastically.\r
+ */\r
+ void (*setText)(struct ANTLR3_COMMON_TOKEN_struct * token, pANTLR3_STRING text);\r
+\r
+ /** Pointer to a function that 'might' be able to set the text associated\r
+ * with a token. Imaginary tokens such as an ANTLR3_CLASSIC_TOKEN may actually\r
+ * do this, however many tokens such as ANTLR3_COMMON_TOKEN do not actully have\r
+ * strings associated with them but just point into the current input stream. These\r
+ * tokens will implement this function with a function that errors out (probably\r
+ * drastically.\r
+ */\r
+ void (*setText8)(struct ANTLR3_COMMON_TOKEN_struct * token, pANTLR3_UINT8 text);\r
+\r
+ /** Pointer to a function that returns the token type of this token\r
+ */\r
+ ANTLR3_UINT32 (*getType)(struct ANTLR3_COMMON_TOKEN_struct * token);\r
+\r
+ /** Pointer to a function that sets the type of this token\r
+ */\r
+ void (*setType)(struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_UINT32 ttype);\r
+\r
+ /** Pointer to a function that gets the 'line' number where this token resides\r
+ */\r
+ ANTLR3_UINT32 (*getLine)(struct ANTLR3_COMMON_TOKEN_struct * token);\r
+\r
+ /** Pointer to a function that sets the 'line' number where this token reside\r
+ */\r
+ void (*setLine)(struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_UINT32 line);\r
+\r
+ /** Pointer to a function that gets the offset in the line where this token exists\r
+ */ \r
+ ANTLR3_INT32 (*getCharPositionInLine) (struct ANTLR3_COMMON_TOKEN_struct * token);\r
+\r
+ /** Pointer to a function that sets the offset in the line where this token exists\r
+ */\r
+ void (*setCharPositionInLine) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_INT32 pos);\r
+\r
+ /** Pointer to a function that gets the channel that this token was placed in (parsers\r
+ * can 'tune' to these channels.\r
+ */\r
+ ANTLR3_UINT32 (*getChannel) (struct ANTLR3_COMMON_TOKEN_struct * token);\r
+\r
+ /** Pointer to a function that sets the channel that this token should belong to\r
+ */\r
+ void (*setChannel) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_UINT32 channel);\r
+\r
+ /** Pointer to a function that returns an index 0...n-1 of the token in the token\r
+ * input stream.\r
+ */\r
+ ANTLR3_MARKER (*getTokenIndex) (struct ANTLR3_COMMON_TOKEN_struct * token);\r
+\r
+ /** Pointer to a function that can set the token index of this token in the token\r
+ * input stream.\r
+ */\r
+ void (*setTokenIndex) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_MARKER);\r
+\r
+ /** Pointer to a function that gets the start index in the input stream for this token.\r
+ */\r
+ ANTLR3_MARKER (*getStartIndex) (struct ANTLR3_COMMON_TOKEN_struct * token);\r
+\r
+ /** Pointer to a function that sets the start index in the input stream for this token.\r
+ */\r
+ void (*setStartIndex) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_MARKER index);\r
+ \r
+ /** Pointer to a function that gets the stop index in the input stream for this token.\r
+ */\r
+ ANTLR3_MARKER (*getStopIndex) (struct ANTLR3_COMMON_TOKEN_struct * token);\r
+\r
+ /** Pointer to a function that sets the stop index in the input stream for this token.\r
+ */\r
+ void (*setStopIndex) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_MARKER index);\r
+\r
+ /** Pointer to a function that returns this token as a text representation that can be \r
+ * printed with embedded control codes such as \n replaced with the printable sequence "\\n"\r
+ * This also yields a string structure that can be used more easily than the pointer to \r
+ * the input stream in certain situations.\r
+ */\r
+ pANTLR3_STRING (*toString) (struct ANTLR3_COMMON_TOKEN_struct * token);\r
+}\r
+ ANTLR3_COMMON_TOKEN;\r
+\r
+/** \brief ANTLR3 Token factory interface to create lots of tokens efficiently\r
+ * rather than creating and freeing lots of little bits of memory.\r
+ */\r
+typedef struct ANTLR3_TOKEN_FACTORY_struct\r
+{\r
+ /** Pointers to the array of tokens that this factory has produced so far\r
+ */\r
+ pANTLR3_COMMON_TOKEN *pools;\r
+\r
+ /** Current pool tokens we are allocating from\r
+ */\r
+ ANTLR3_INT32 thisPool;\r
+\r
+ /** The next token to throw out from the pool, will cause a new pool allocation\r
+ * if this exceeds the available tokenCount\r
+ */\r
+ ANTLR3_UINT32 nextToken;\r
+\r
+ /** Trick to initialize tokens and their API quickly, we set up this token when the\r
+ * factory is created, then just copy the memory it uses into the new token.\r
+ */\r
+ ANTLR3_COMMON_TOKEN unTruc;\r
+\r
+ /** Pointer to an input stream that is using this token factory (may be NULL)\r
+ * which will be assigned to the tokens automatically.\r
+ */\r
+ pANTLR3_INPUT_STREAM input;\r
+\r
+ /** Pointer to a function that returns a new token\r
+ */\r
+ pANTLR3_COMMON_TOKEN (*newToken) (struct ANTLR3_TOKEN_FACTORY_struct * factory);\r
+\r
+ /** Pointer to a function that changes teh curent inptu stream so that\r
+ * new tokens are created with reference to their originating text.\r
+ */\r
+ void (*setInputStream) (struct ANTLR3_TOKEN_FACTORY_struct * factory, pANTLR3_INPUT_STREAM input);\r
+ /** Pointer to a function the destroys the factory\r
+ */\r
+ void (*close) (struct ANTLR3_TOKEN_FACTORY_struct * factory);\r
+}\r
+ ANTLR3_TOKEN_FACTORY;\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+#endif\r