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