IIntStream.cs revision 324c4644fee44b9898524c09511bd33c3f12e2df
1/*
2[The "BSD licence"]
3Copyright (c) 2007-2008 Johannes Luber
4Copyright (c) 2005-2007 Kunle Odutola
5All rights reserved.
6
7Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions
9are met:
101. Redistributions of source code MUST RETAIN the above copyright
11   notice, this list of conditions and the following disclaimer.
122. Redistributions in binary form MUST REPRODUCE the above copyright
13   notice, this list of conditions and the following disclaimer in
14   the documentation and/or other materials provided with the
15   distribution.
163. The name of the author may not be used to endorse or promote products
17   derived from this software without specific prior WRITTEN permission.
184. Unless explicitly state otherwise, any contribution intentionally
19   submitted for inclusion in this work to the copyright owner or licensor
20   shall be under the terms and conditions of this license, without any
21   additional terms or conditions.
22
23THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*/
34
35
36namespace Antlr.Runtime
37{
38    using System;
39
40	/// <summary>
41    /// A simple stream of integers. This is useful when all we care about is the char
42	/// or token type sequence (such as for interpretation).
43	/// </summary>
44	public interface IIntStream
45	{
46		void Consume();
47
48		/// <summary>
49        /// Get int at current input pointer + i ahead (where i=1 is next int)
50		/// Negative indexes are allowed.  LA(-1) is previous token (token just matched).
51		/// LA(-i) where i is before first token should yield -1, invalid char or EOF.
52        /// </summary>
53		int LA(int i);
54
55		/// <summary>Tell the stream to start buffering if it hasn't already.</summary>
56        /// <remarks>
57        /// Executing Rewind(Mark()) on a stream should not affect the input position.
58		/// The Lexer tracks line/col info as well as input index so its markers are
59		/// not pure input indexes.  Same for tree node streams.													*/
60        /// </remarks>
61        /// <returns>Return a marker that can be passed to
62        /// <see cref="IIntStream.Rewind(int)"/> to return to the current position.
63        /// This could be the current input position, a value return from
64        /// <see cref="IIntStream.Index"/>, or some other marker.</returns>
65		int Mark();
66
67		/// <summary>
68        /// Return the current input symbol index 0..n where n indicates the
69		/// last symbol has been read. The index is the symbol about to be
70		/// read not the most recently read symbol.
71		/// </summary>
72		int Index { get; }
73
74        /// <summary>
75		/// Resets the stream so that the next call to
76        /// <see cref="IIntStream.Index"/> would  return marker.
77        /// </summary>
78        /// <remarks>
79        /// The marker will usually be <see cref="IIntStream.Index"/> but
80        /// it doesn't have to be.  It's just a marker to indicate what
81        /// state the stream was in.  This is essentially calling
82        /// <see cref="IIntStream.Release"/> and <see cref="IIntStream.Seek"/>.
83        /// If there are other markers created after the specified marker,
84        /// this routine must unroll them like a stack.  Assumes the state the
85        /// stream was in when this marker was created.
86        /// </remarks>
87		void Rewind(int marker);
88
89		/// <summary>
90		/// Rewind to the input position of the last marker.
91		/// </summary>
92		/// <remarks>
93		/// Used currently only after a cyclic DFA and just before starting
94		/// a sem/syn predicate to get the input position back to the start
95		/// of the decision. Do not "pop" the marker off the state.  Mark(i)
96		/// and Rewind(i) should balance still. It is like invoking
97		/// Rewind(last marker) but it should not "pop" the marker off.
98		/// It's like Seek(last marker's input position).
99		/// </remarks>
100		void Rewind();
101
102		/// <summary>
103        /// You may want to commit to a backtrack but don't want to force the
104		/// stream to keep bookkeeping objects around for a marker that is
105		/// no longer necessary.  This will have the same behavior as
106        /// <see cref="IIntStream.Rewind(int)"/> except it releases resources without
107        /// the backward seek.
108		/// </summary>
109		/// <remarks>
110		/// This must throw away resources for all markers back to the marker
111		/// argument. So if you're nested 5 levels of Mark(), and then Release(2)
112		/// you have to release resources for depths 2..5.
113		/// </remarks>
114		void Release(int marker);
115
116		/// <summary>
117        /// Set the input cursor to the position indicated by index.  This is
118		/// normally used to seek ahead in the input stream.
119        /// </summary>
120        /// <remarks>
121        /// No buffering is required to do this unless you know your stream
122        /// will use seek to move backwards such as when backtracking.
123		///
124		/// This is different from rewind in its multi-directional requirement
125        /// and in that its argument is strictly an input cursor (index).
126		///
127		/// For char streams, seeking forward must update the stream state such
128		/// as line number.  For seeking backwards, you will be presumably
129        /// backtracking using the
130        /// <see cref="IIntStream.Mark"/>/<see cref="IIntStream.Rewind(int)"/>
131        /// mechanism that restores state and so this method does not need to
132        /// update state when seeking backwards.
133		///
134		/// Currently, this method is only used for efficient backtracking using
135		/// memoization, but in the future it may be used for incremental parsing.
136		///
137		/// The index is 0..n-1. A seek to position i means that LA(1) will return
138		/// the ith symbol.  So, seeking to 0 means LA(1) will return the first
139		/// element in the stream.
140        /// </remarks>
141		void Seek(int index);
142
143		/// <summary>Returns the size of the entire stream.</summary>
144        /// <remarks>
145        /// Only makes sense for streams that buffer everything up probably,
146        /// but might be useful to display the entire stream or for testing.
147		/// This value includes a single EOF.
148        /// </remarks>
149        int Count { get; }
150
151		/// <summary>
152		/// Where are you getting symbols from?  Normally, implementations will
153		/// pass the buck all the way to the lexer who can ask its input stream
154		/// for the file name or whatever.
155		/// </summary>
156		string SourceName {
157			get;
158		}
159	}
160}