+/// \file\r
+/// Provides implementations of string (or memory) streams as input\r
+/// for ANLTR3 lexers.\r
+///\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 <antlr3.h>\r
+\r
+/// \brief Create an in-place ASCII string stream as input to ANTLR 3.\r
+///\r
+/// An in-place string steam is the preferred method of supplying strings to ANTLR as input \r
+/// for lexing and compiling. This is because we make no copies of the input string but\r
+/// read from it right where it is.\r
+///\r
+/// \param[in] inString Pointer to the string to be used as the input stream\r
+/// \param[in] size Size (in 8 bit ASCII characters) of the input string\r
+/// \param[in] name NAme to attach the input stream (can be NULL pointer)\r
+///\r
+/// \return\r
+/// - Pointer to new input stream context upon success\r
+/// - One of the ANTLR3_ERR_ defines on error.\r
+///\r
+/// \remark\r
+/// - ANTLR does not alter the input string in any way.\r
+/// - String is slightly incorrect in that the passed in pointer can be to any\r
+/// memory in C version of ANTLR3 of course.\r
+////\r
+ANTLR3_API pANTLR3_INPUT_STREAM \r
+antlr3NewAsciiStringInPlaceStream (pANTLR3_UINT8 inString, ANTLR3_UINT32 size, pANTLR3_UINT8 name)\r
+{\r
+ // Pointer to the input stream we are going to create\r
+ //\r
+ pANTLR3_INPUT_STREAM input;\r
+\r
+ // Allocate memory for the input stream structure\r
+ //\r
+ input = (pANTLR3_INPUT_STREAM)\r
+ ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM));\r
+\r
+ if (input == NULL)\r
+ {\r
+ return NULL;\r
+ }\r
+\r
+ // Structure was allocated correctly, now we can install the pointer.\r
+ //\r
+ input->isAllocated = ANTLR3_FALSE;\r
+ input->data = inString;\r
+ input->sizeBuf = size;\r
+\r
+ // Call the common 8 bit ASCII input stream handler initializer.\r
+ //\r
+ antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);\r
+\r
+ // Now we can set up the file name\r
+ //\r
+ input->istream->streamName = input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)"-memory-" : name);\r
+ input->fileName = input->istream->streamName;\r
+\r
+ return input;\r
+}\r
+\r
+/// \brief Create an in-place UCS2 string stream as input to ANTLR 3.\r
+///\r
+/// An in-place string steam is the preferred method of supplying strings to ANTLR as input \r
+/// for lexing and compiling. This is because we make no copies of the input string but\r
+/// read from it right where it is.\r
+///\r
+/// \param[in] inString Pointer to the string to be used as the input stream\r
+/// \param[in] size Size (in 16 bit ASCII characters) of the input string\r
+/// \param[in] name Name to attach the input stream (can be NULL pointer)\r
+///\r
+/// \return\r
+/// - Pointer to new input stream context upon success\r
+/// - One of the ANTLR3_ERR_ defines on error.\r
+///\r
+/// \remark\r
+/// - ANTLR does not alter the input string in any way.\r
+/// - String is slightly incorrect in that the passed in pointer can be to any\r
+/// memory in C version of ANTLR3 of course.\r
+////\r
+ANTLR3_API pANTLR3_INPUT_STREAM \r
+antlr3NewUCS2StringInPlaceStream (pANTLR3_UINT16 inString, ANTLR3_UINT32 size, pANTLR3_UINT16 name)\r
+{\r
+ // Pointer to the input stream we are going to create\r
+ //\r
+ pANTLR3_INPUT_STREAM input;\r
+\r
+ // Layout default file name string in correct encoding\r
+ //\r
+ ANTLR3_UINT16 defaultName[] = { '-', 'm', 'e', 'm', 'o', 'r', 'y', '-', '\0' };\r
+\r
+ // Allocate memory for the input stream structure\r
+ //\r
+ input = (pANTLR3_INPUT_STREAM)\r
+ ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM));\r
+\r
+ if (input == NULL)\r
+ {\r
+ return NULL;\r
+ }\r
+\r
+ // Structure was allocated correctly, now we can install the pointer.\r
+ //\r
+ input->isAllocated = ANTLR3_FALSE;\r
+ input->data = inString;\r
+ input->sizeBuf = size;\r
+\r
+ // Call the common 16 bit input stream handler initializer.\r
+ //\r
+ antlr3UCS2SetupStream (input, ANTLR3_CHARSTREAM);\r
+\r
+ input->istream->streamName = input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)defaultName : (pANTLR3_UINT8)name);\r
+ input->fileName = input->istream->streamName;\r
+\r
+\r
+ return input;\r
+}\r
+\r
+/// \brief Create an ASCII string stream as input to ANTLR 3, copying the input string.\r
+///\r
+/// This string stream first makes a copy of the string at the supplied pointer\r
+///\r
+/// \param[in] inString Pointer to the string to be copied as the input stream\r
+/// \param[in] size Size (in 8 bit ASCII characters) of the input string\r
+/// \param[in] name NAme to attach the input stream (can be NULL pointer)\r
+///\r
+/// \return\r
+/// - Pointer to new input stream context upon success\r
+/// - One of the ANTLR3_ERR_ defines on error.\r
+///\r
+/// \remark\r
+/// - ANTLR does not alter the input string in any way.\r
+/// - String is slightly incorrect in that the passed in pointer can be to any\r
+/// memory in C version of ANTLR3 of course.\r
+////\r
+pANTLR3_INPUT_STREAM antlr3NewAsciiStringCopyStream (pANTLR3_UINT8 inString, ANTLR3_UINT32 size, pANTLR3_UINT8 name)\r
+{\r
+ // Pointer to the input stream we are going to create\r
+ //\r
+ pANTLR3_INPUT_STREAM input;\r
+\r
+ // Allocate memory for the input stream structure\r
+ //\r
+ input = (pANTLR3_INPUT_STREAM)\r
+ ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM));\r
+\r
+ if (input == NULL)\r
+ {\r
+ return NULL;\r
+ }\r
+\r
+ // Indicate that we allocated this input and allocate it\r
+ //\r
+ input->isAllocated = ANTLR3_TRUE;\r
+ input->data = ANTLR3_MALLOC((size_t)size);\r
+\r
+ if (input->data == NULL)\r
+ {\r
+ return NULL;\r
+ }\r
+\r
+ // Structure was allocated correctly, now we can install the pointer and set the size.\r
+ //\r
+ ANTLR3_MEMMOVE(input->data, (const void *)inString, size);\r
+ input->sizeBuf = size;\r
+\r
+ // Call the common 8 bit ASCII input stream handler\r
+ // initializer type thingy doobry function.\r
+ //\r
+ antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);\r
+\r
+\r
+ input->istream->streamName = input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)"-memory-" : name);\r
+ input->fileName = input->istream->streamName;\r
+\r
+ return input;\r
+}\r