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