1/// \file
2/// Provides implementations of string (or memory) streams as input
3/// for ANLTR3 lexers.
4///
5
6// [The "BSD licence"]
7// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
8// http://www.temporal-wave.com
9// http://www.linkedin.com/in/jimidle
10//
11// All rights reserved.
12//
13// Redistribution and use in source and binary forms, with or without
14// modification, are permitted provided that the following conditions
15// are met:
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.
23//
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.
34
35#include    <antlr3.h>
36
37/// \brief Create an in-place ASCII string stream as input to ANTLR 3.
38///
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.
42///
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)
46///
47/// \return
48///	- Pointer to new input stream context upon success
49///	- One of the ANTLR3_ERR_ defines on error.
50///
51/// \remark
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.
55////
56ANTLR3_API pANTLR3_INPUT_STREAM
57antlr3NewAsciiStringInPlaceStream   (pANTLR3_UINT8 inString, ANTLR3_UINT32 size, pANTLR3_UINT8 name)
58{
59	// Pointer to the input stream we are going to create
60	//
61	pANTLR3_INPUT_STREAM    input;
62
63	// Allocate memory for the input stream structure
64	//
65	input   = (pANTLR3_INPUT_STREAM)
66					ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM));
67
68	if	(input == NULL)
69	{
70		return	NULL;
71	}
72
73	// Structure was allocated correctly, now we can install the pointer.
74	//
75	input->isAllocated	= ANTLR3_FALSE;
76	input->data			= inString;
77	input->sizeBuf		= size;
78
79	// Call the common 8 bit ASCII input stream handler initializer.
80	//
81	antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);
82
83	// Now we can set up the file name
84	//
85	input->istream->streamName	= input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)"-memory-" : name);
86	input->fileName				= input->istream->streamName;
87
88	return  input;
89}
90
91/// \brief Create an in-place UCS2 string stream as input to ANTLR 3.
92///
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.
96///
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)
100///
101/// \return
102///	- Pointer to new input stream context upon success
103///	- One of the ANTLR3_ERR_ defines on error.
104///
105/// \remark
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.
109////
110ANTLR3_API pANTLR3_INPUT_STREAM
111antlr3NewUCS2StringInPlaceStream   (pANTLR3_UINT16 inString, ANTLR3_UINT32 size, pANTLR3_UINT16 name)
112{
113	// Pointer to the input stream we are going to create
114	//
115	pANTLR3_INPUT_STREAM    input;
116
117	// Layout default file name string in correct encoding
118	//
119	ANTLR3_UINT16   defaultName[] = { '-', 'm', 'e', 'm', 'o', 'r', 'y', '-', '\0' };
120
121	// Allocate memory for the input stream structure
122	//
123	input   = (pANTLR3_INPUT_STREAM)
124					ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM));
125
126	if	(input == NULL)
127	{
128		return	NULL;
129	}
130
131	// Structure was allocated correctly, now we can install the pointer.
132	//
133	input->isAllocated	= ANTLR3_FALSE;
134	input->data			= inString;
135	input->sizeBuf		= size;
136
137	// Call the common 16 bit input stream handler initializer.
138	//
139	antlr3UCS2SetupStream   (input, ANTLR3_CHARSTREAM);
140
141	input->istream->streamName	= input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)defaultName : (pANTLR3_UINT8)name);
142	input->fileName				= input->istream->streamName;
143
144
145	return  input;
146}
147
148/// \brief Create an ASCII string stream as input to ANTLR 3, copying the input string.
149///
150/// This string stream first makes a copy of the string at the supplied pointer
151///
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)
155///
156/// \return
157///	- Pointer to new input stream context upon success
158///	- One of the ANTLR3_ERR_ defines on error.
159///
160/// \remark
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.
164////
165pANTLR3_INPUT_STREAM	antlr3NewAsciiStringCopyStream	    (pANTLR3_UINT8 inString, ANTLR3_UINT32 size, pANTLR3_UINT8 name)
166{
167	// Pointer to the input stream we are going to create
168	//
169	pANTLR3_INPUT_STREAM    input;
170
171	// Allocate memory for the input stream structure
172	//
173	input   = (pANTLR3_INPUT_STREAM)
174		ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM));
175
176	if	(input == NULL)
177	{
178		return	NULL;
179	}
180
181	// Indicate that we allocated this input and allocate it
182	//
183	input->isAllocated	    = ANTLR3_TRUE;
184	input->data		    = ANTLR3_MALLOC((size_t)size);
185
186	if	(input->data == NULL)
187	{
188		return		NULL;
189	}
190
191	// Structure was allocated correctly, now we can install the pointer and set the size.
192	//
193	ANTLR3_MEMMOVE(input->data, (const void *)inString, size);
194	input->sizeBuf  = size;
195
196	// Call the common 8 bit ASCII input stream handler
197	// initializer type thingy doobry function.
198	//
199	antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);
200
201
202	input->istream->streamName	= input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)"-memory-" : name);
203	input->fileName				= input->istream->streamName;
204
205	return  input;
206}
207