ANTLRIntStream.h revision 324c4644fee44b9898524c09511bd33c3f12e2df
1324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// [The "BSD licence"]
2324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// Copyright (c) 2006-2007 Kay Roepke 2010 Alan Condit
3324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// All rights reserved.
4324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//
5324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// Redistribution and use in source and binary forms, with or without
6324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// modification, are permitted provided that the following conditions
7324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// are met:
8324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// 1. Redistributions of source code must retain the above copyright
9324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//    notice, this list of conditions and the following disclaimer.
10324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// 2. Redistributions in binary form must reproduce the above copyright
11324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//    notice, this list of conditions and the following disclaimer in the
12324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//    documentation and/or other materials provided with the distribution.
13324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// 3. The name of the author may not be used to endorse or promote products
14324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//    derived from this software without specific prior written permission.
15324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//
16324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
27324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver@protocol ANTLRIntStream < NSObject, NSCopying >
28324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
29324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver- (void) consume;
30324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
31324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// Get unichar at current input pointer + i ahead where i=1 is next character as int for including ANTLRCharStreamEOF (-1) in the data range
32324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver- (NSInteger) LA:(NSInteger) i;
33324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
34324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// Tell the stream to start buffering if it hasn't already.  Return
35324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// current input position, index(), or some other marker so that
36324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// when passed to rewind() you get back to the same spot.
37324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// rewind(mark()) should not affect the input cursor.
38324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// TODO: problem in that lexer stream returns not index but some marker
39324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
40324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver- (NSInteger) mark;
41324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
42324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// Return the current input symbol index 0..n where n indicates the
43324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// last symbol has been read.
44324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
45324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver- (NSInteger) getIndex;
46324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
47324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// Reset the stream so that next call to index would return marker.
48324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// The marker will usually be -index but it doesn't have to be.  It's
49324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// just a marker to indicate what state the stream was in.  This is
50324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// essentially calling -release: and -seek:.  If there are markers
51324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// created after this marker argument, this routine must unroll them
52324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// like a stack.  Assume the state the stream was in when this marker
53324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// was created.
54324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
55324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver- (void) rewind;
56324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver- (void) rewind:(NSInteger) marker;
57324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
58324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// You may want to commit to a backtrack but don't want to force the
59324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// stream to keep bookkeeping objects around for a marker that is
60324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// no longer necessary.  This will have the same behavior as
61324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// rewind() except it releases resources without the backward seek.
62324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
63324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver- (void) release:(NSInteger) marker;
64324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
65324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// Set the input cursor to the position indicated by index.  This is
66324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// normally used to seek ahead in the input stream.  No buffering is
67324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// required to do this unless you know your stream will use seek to
68324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// move backwards such as when backtracking.
69324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// This is different from rewind in its multi-directional
70324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// requirement and in that its argument is strictly an input cursor (index).
71324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//
72324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// For char streams, seeking forward must update the stream state such
73324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// as line number.  For seeking backwards, you will be presumably
74324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// backtracking using the mark/rewind mechanism that restores state and
75324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// so this method does not need to update state when seeking backwards.
76324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver//
77324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// Currently, this method is only used for efficient backtracking, but
78324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver// in the future it may be used for incremental parsing.
79324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
80324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver- (void) seek:(NSInteger) index;
81324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
82324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver/** Only makes sense for streams that buffer everything up probably, but
83324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *  might be useful to display the entire stream or for testing.  This
84324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *  value includes a single EOF.
85324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver */
86324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver- (NSUInteger) size;
87324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver/** Where are you getting symbols from?  Normally, implementations will
88324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *  pass the buck all the way to the lexer who can ask its input stream
89324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *  for the file name or whatever.
90324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver */
91324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver- (NSString *)getSourceName;
92324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
93324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver@end