2 * Defines the basic structures used to manipulate character
3 * streams from any input source. The first implementation of
4 * this stream was ASCII 8 bit, but any character size and encoding
5 * can in theory be used, so long as they can return a 32 bit Integer
6 * representation of their characters amd efficiently mark and revert
7 * to specific offsets into their input streams.
9 #ifndef _ANTLR3_INPUT_H
10 #define _ANTLR3_INPUT_H
12 // [The "BSD licence"]
13 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
14 // http://www.temporal-wave.com
15 // http://www.linkedin.com/in/jimidle
17 // All rights reserved.
19 // Redistribution and use in source and binary forms, with or without
20 // modification, are permitted provided that the following conditions
22 // 1. Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 // 2. Redistributions in binary form must reproduce the above copyright
25 // notice, this list of conditions and the following disclaimer in the
26 // documentation and/or other materials provided with the distribution.
27 // 3. The name of the author may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <antlr3defs.h>
42 #include <antlr3string.h>
43 #include <antlr3commontoken.h>
44 #include <antlr3intstream.h>
50 /// Master context structure for an ANTLR3 C runtime based input stream.
51 /// \ingroup apistructures
53 typedef struct ANTLR3_INPUT_STREAM_struct
55 /** Interfaces that provide streams must all provide
56 * a generic ANTLR3_INT_STREAM interface and an ANTLR3_INPUT_STREAM
59 pANTLR3_INT_STREAM istream;
61 /** Whatever super structure is providing the INPUT stream needs a pointer to itself
62 * so that this can be passed back to it whenever the api functions
63 * are called back from this interface.
67 /// Indicates the size, in 8 bit units, of a single character. Note that
68 /// the C runtime does not deal with surrogates and UTF8 directly as this would be
69 /// slow and complicated. Variable character width inputs are expected to be converted
70 /// into fixed width formats, so that would be a UTF32 format for anything that cannot
71 /// work with a UCS2 encoding, such as UTF-8. Generally you are best
72 /// working internally with 32 bit characters.
74 ANTLR3_UINT8 charByteSize;
76 /** Pointer the start of the input string, characters may be
77 * taken as offsets from here and in original input format encoding.
81 /** Indicates if the data pointer was allocated by us, and so should be freed
82 * when the stream dies.
86 /** String factory for this input stream
88 pANTLR3_STRING_FACTORY strFactory;
91 /** Pointer to the next character to be consumed from the input data
92 * This is cast to point at the encoding of the original file that
93 * was read by the functions installed as pointer in this input stream
94 * context instance at file/string/whatever load time.
98 /** Number of characters that can be consumed at this point in time.
99 * Mostly this is just what is left in the pre-read buffer, but if the
100 * input source is a stream such as a socket or something then we may
101 * call special read code to wait for more input.
103 ANTLR3_UINT32 sizeBuf;
105 /** The line number we are traversing in the input file. This gets incremented
106 * by a newline() call in the lexer grammar actions.
110 /** Pointer into the input buffer where the current line
115 /** The offset within the current line of the current character
117 ANTLR3_INT32 charPositionInLine;
119 /** Tracks how deep mark() calls are nested
121 ANTLR3_UINT32 markDepth;
123 /** List of mark() points in the input stream
125 pANTLR3_VECTOR markers;
127 /** File name string, set to pointer to memory if
128 * you set it manually as it will be free()d
130 pANTLR3_STRING fileName;
132 /** File number, needs to be set manually to some file index of your devising.
134 ANTLR3_UINT32 fileNo;
136 /** Character that automatically causes an internal line count
139 ANTLR3_UCHAR newlineChar;
144 /** Pointer to function that closes the input stream
146 void (*close) (struct ANTLR3_INPUT_STREAM_struct * input);
147 void (*free) (struct ANTLR3_INPUT_STREAM_struct * input);
149 /** Pointer to function that resets the input stream
151 void (*reset) (struct ANTLR3_INPUT_STREAM_struct * input);
154 * Pinter to function that installs a version of LA that always
155 * returns upper case. Only valid for character streams and creates a case
156 * insensitive lexer if the lexer tokens are described in upper case. The
157 * tokens will preserve case in the token text.
159 void (*setUcaseLA) (pANTLR3_INPUT_STREAM input, ANTLR3_BOOLEAN flag);
161 /** Pointer to function to return input stream element at 1 based
162 * offset from nextChar. Same as _LA for char stream, but token
163 * streams etc. have one of these that does other stuff of course.
165 void * (*_LT) (struct ANTLR3_INPUT_STREAM_struct * input, ANTLR3_INT32 lt);
167 /** Pointer to function to return the total size of the input buffer. For streams
168 * this may be just the total we have available so far. This means of course that
169 * the input stream must be careful to accumulate enough input so that any backtracking
172 ANTLR3_UINT32 (*size) (struct ANTLR3_INPUT_STREAM_struct * input);
174 /** Pointer to function to return a substring of the input stream. String is returned in allocated
175 * memory and is in same encoding as the input stream itself, NOT internal ANTLR3_UCHAR form.
177 pANTLR3_STRING (*substr) (struct ANTLR3_INPUT_STREAM_struct * input, ANTLR3_MARKER start, ANTLR3_MARKER stop);
179 /** Pointer to function to return the current line number in the input stream
181 ANTLR3_UINT32 (*getLine) (struct ANTLR3_INPUT_STREAM_struct * input);
183 /** Pointer to function to return the current line buffer in the input stream
184 * The pointer returned is directly into the input stream so you must copy
185 * it if you wish to manipulate it without damaging the input stream. Encoding
186 * is obviously in the same form as the input stream.
188 * - Note taht this function wil lbe inaccurate if setLine is called as there
189 * is no way at the moment to position the input stream at a particular line
192 void * (*getLineBuf) (struct ANTLR3_INPUT_STREAM_struct * input);
194 /** Pointer to function to return the current offset in the current input stream line
196 ANTLR3_UINT32 (*getCharPositionInLine) (struct ANTLR3_INPUT_STREAM_struct * input);
198 /** Pointer to function to set the current line number in the input stream
200 void (*setLine) (struct ANTLR3_INPUT_STREAM_struct * input, ANTLR3_UINT32 line);
202 /** Pointer to function to set the current position in the current line.
204 void (*setCharPositionInLine) (struct ANTLR3_INPUT_STREAM_struct * input, ANTLR3_UINT32 position);
206 /** Pointer to function to override the default newline character that the input stream
207 * looks for to trigger the line and offset and line buffer recording information.
209 * - By default the chracter '\n' will be instaleldas tehe newline trigger character. When this
210 * character is seen by the consume() function then the current line number is incremented and the
211 * current line offset is reset to 0. The Pointer for the line of input we are consuming
212 * is updated to point to the next character after this one in the input stream (which means it
213 * may become invlaid if the last newline character in the file is seen (so watch out).
214 * - If for some reason you do not want teh counters and pointesr to be restee, yu can set the
215 * chracter to some impossible charater such as '\0' or whatever.
216 * - This is a single character only, so choose the last chracter in a sequence of two or more.
217 * - This is only a simple aid to error reporting - if you have a complicated binary inptu structure
218 * it may not be adequate, but you can always override every function in the input stream with your
219 * own of course, and can even write your own complete input stream set if you like.
220 * - It is your responsiblity to set a valid cahracter for the input stream type. Ther is no point
221 * setting this to 0xFFFFFFFF if the input stream is 8 bit ASCII as this will just be truncated and never
222 * trigger as the comparison will be (INT32)0xFF == (INT32)0xFFFFFFFF
224 void (*SetNewLineChar) (struct ANTLR3_INPUT_STREAM_struct * input, ANTLR3_UINT32 newlineChar);
231 /** \brief Structure for track lex input states as part of mark()
232 * and rewind() of lexer.
234 typedef struct ANTLR3_LEX_STATE_struct
236 /** Pointer to the next character to be consumed from the input data
237 * This is cast to point at the encoding of the original file that
238 * was read by the functions installed as pointer in this input stream
239 * context instance at file/string/whatever load time.
243 /** The line number we are traversing in the input file. This gets incremented
244 * by a newline() call in the lexer grammer actions.
248 /** Pointer into the input buffer where the current line
253 /** The offset within the current line of the current character
255 ANTLR3_INT32 charPositionInLine;
262 void antlr3AsciiSetupStream (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 type);
263 void antlr3UCS2SetupStream (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 type);
264 void antlr3GenericSetupStream (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 type);
270 #endif /* _ANTLR3_INPUT_H */