2 /// Provides implementations of string (or memory) streams as input
\r
3 /// for ANLTR3 lexers.
\r
6 // [The "BSD licence"]
\r
7 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
\r
8 // http://www.temporal-wave.com
\r
9 // http://www.linkedin.com/in/jimidle
\r
11 // All rights reserved.
\r
13 // Redistribution and use in source and binary forms, with or without
\r
14 // modification, are permitted provided that the following conditions
\r
16 // 1. Redistributions of source code must retain the above copyright
\r
17 // notice, this list of conditions and the following disclaimer.
\r
18 // 2. Redistributions in binary form must reproduce the above copyright
\r
19 // notice, this list of conditions and the following disclaimer in the
\r
20 // documentation and/or other materials provided with the distribution.
\r
21 // 3. The name of the author may not be used to endorse or promote products
\r
22 // derived from this software without specific prior written permission.
\r
24 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
\r
25 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
26 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
\r
27 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
28 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
29 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
37 /// \brief Create an in-place ASCII string stream as input to ANTLR 3.
\r
39 /// An in-place string steam is the preferred method of supplying strings to ANTLR as input
\r
40 /// for lexing and compiling. This is because we make no copies of the input string but
\r
41 /// read from it right where it is.
\r
43 /// \param[in] inString Pointer to the string to be used as the input stream
\r
44 /// \param[in] size Size (in 8 bit ASCII characters) of the input string
\r
45 /// \param[in] name NAme to attach the input stream (can be NULL pointer)
\r
48 /// - Pointer to new input stream context upon success
\r
49 /// - One of the ANTLR3_ERR_ defines on error.
\r
52 /// - ANTLR does not alter the input string in any way.
\r
53 /// - String is slightly incorrect in that the passed in pointer can be to any
\r
54 /// memory in C version of ANTLR3 of course.
\r
56 ANTLR3_API pANTLR3_INPUT_STREAM
\r
57 antlr3NewAsciiStringInPlaceStream (pANTLR3_UINT8 inString, ANTLR3_UINT32 size, pANTLR3_UINT8 name)
\r
59 // Pointer to the input stream we are going to create
\r
61 pANTLR3_INPUT_STREAM input;
\r
63 // Allocate memory for the input stream structure
\r
65 input = (pANTLR3_INPUT_STREAM)
\r
66 ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM));
\r
73 // Structure was allocated correctly, now we can install the pointer.
\r
75 input->isAllocated = ANTLR3_FALSE;
\r
76 input->data = inString;
\r
77 input->sizeBuf = size;
\r
79 // Call the common 8 bit ASCII input stream handler initializer.
\r
81 antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);
\r
83 // Now we can set up the file name
\r
85 input->istream->streamName = input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)"-memory-" : name);
\r
86 input->fileName = input->istream->streamName;
\r
91 /// \brief Create an in-place UCS2 string stream as input to ANTLR 3.
\r
93 /// An in-place string steam is the preferred method of supplying strings to ANTLR as input
\r
94 /// for lexing and compiling. This is because we make no copies of the input string but
\r
95 /// read from it right where it is.
\r
97 /// \param[in] inString Pointer to the string to be used as the input stream
\r
98 /// \param[in] size Size (in 16 bit ASCII characters) of the input string
\r
99 /// \param[in] name Name to attach the input stream (can be NULL pointer)
\r
102 /// - Pointer to new input stream context upon success
\r
103 /// - One of the ANTLR3_ERR_ defines on error.
\r
106 /// - ANTLR does not alter the input string in any way.
\r
107 /// - String is slightly incorrect in that the passed in pointer can be to any
\r
108 /// memory in C version of ANTLR3 of course.
\r
110 ANTLR3_API pANTLR3_INPUT_STREAM
\r
111 antlr3NewUCS2StringInPlaceStream (pANTLR3_UINT16 inString, ANTLR3_UINT32 size, pANTLR3_UINT16 name)
\r
113 // Pointer to the input stream we are going to create
\r
115 pANTLR3_INPUT_STREAM input;
\r
117 // Layout default file name string in correct encoding
\r
119 ANTLR3_UINT16 defaultName[] = { '-', 'm', 'e', 'm', 'o', 'r', 'y', '-', '\0' };
\r
121 // Allocate memory for the input stream structure
\r
123 input = (pANTLR3_INPUT_STREAM)
\r
124 ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM));
\r
131 // Structure was allocated correctly, now we can install the pointer.
\r
133 input->isAllocated = ANTLR3_FALSE;
\r
134 input->data = inString;
\r
135 input->sizeBuf = size;
\r
137 // Call the common 16 bit input stream handler initializer.
\r
139 antlr3UCS2SetupStream (input, ANTLR3_CHARSTREAM);
\r
141 input->istream->streamName = input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)defaultName : (pANTLR3_UINT8)name);
\r
142 input->fileName = input->istream->streamName;
\r
148 /// \brief Create an ASCII string stream as input to ANTLR 3, copying the input string.
\r
150 /// This string stream first makes a copy of the string at the supplied pointer
\r
152 /// \param[in] inString Pointer to the string to be copied as the input stream
\r
153 /// \param[in] size Size (in 8 bit ASCII characters) of the input string
\r
154 /// \param[in] name NAme to attach the input stream (can be NULL pointer)
\r
157 /// - Pointer to new input stream context upon success
\r
158 /// - One of the ANTLR3_ERR_ defines on error.
\r
161 /// - ANTLR does not alter the input string in any way.
\r
162 /// - String is slightly incorrect in that the passed in pointer can be to any
\r
163 /// memory in C version of ANTLR3 of course.
\r
165 pANTLR3_INPUT_STREAM antlr3NewAsciiStringCopyStream (pANTLR3_UINT8 inString, ANTLR3_UINT32 size, pANTLR3_UINT8 name)
\r
167 // Pointer to the input stream we are going to create
\r
169 pANTLR3_INPUT_STREAM input;
\r
171 // Allocate memory for the input stream structure
\r
173 input = (pANTLR3_INPUT_STREAM)
\r
174 ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM));
\r
181 // Indicate that we allocated this input and allocate it
\r
183 input->isAllocated = ANTLR3_TRUE;
\r
184 input->data = ANTLR3_MALLOC((size_t)size);
\r
186 if (input->data == NULL)
\r
191 // Structure was allocated correctly, now we can install the pointer and set the size.
\r
193 ANTLR3_MEMMOVE(input->data, (const void *)inString, size);
\r
194 input->sizeBuf = size;
\r
196 // Call the common 8 bit ASCII input stream handler
\r
197 // initializer type thingy doobry function.
\r
199 antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);
\r
202 input->istream->streamName = input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)"-memory-" : name);
\r
203 input->fileName = input->istream->streamName;
\r