13447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/*
23447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein [The "BSD license"]
33447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Copyright (c) 2005-2009 Terence Parr
43447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein All rights reserved.
53447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
63447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Redistribution and use in source and binary forms, with or without
73447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein modification, are permitted provided that the following conditions
83447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein are met:
93447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1. Redistributions of source code must retain the above copyright
103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     notice, this list of conditions and the following disclaimer.
113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2. Redistributions in binary form must reproduce the above copyright
123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     notice, this list of conditions and the following disclaimer in the
133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     documentation and/or other materials provided with the distribution.
143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 3. The name of the author may not be used to endorse or promote products
153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     derived from this software without specific prior written permission.
163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */
283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpackage org.antlr.runtime;
293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/** A simple stream of integers used when all I care about is the char
313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  or token type sequence (such as interpretation).
323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */
333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic interface IntStream {
343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	void consume();
353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Get int at current input pointer + i ahead where i=1 is next int.
373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Negative indexes are allowed.  LA(-1) is previous token (token
383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  just matched).  LA(-i) where i is before first token should
393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  yield -1, invalid char / EOF.
403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	int LA(int i);
423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Tell the stream to start buffering if it hasn't already.  Return
443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     *  current input position, index(), or some other marker so that
453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  when passed to rewind() you get back to the same spot.
463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  rewind(mark()) should not affect the input cursor.  The Lexer
473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  track line/col info as well as input index so its markers are
483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  not pure input indexes.  Same for tree node streams.
493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     */
503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	int mark();
513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Return the current input symbol index 0..n where n indicates the
533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     *  last symbol has been read.  The index is the symbol about to be
543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  read not the most recently read symbol.
553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     */
563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	int index();
573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Reset the stream so that next call to index would return marker.
593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  The marker will usually be index() but it doesn't have to be.  It's
603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  just a marker to indicate what state the stream was in.  This is
613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  essentially calling release() and seek().  If there are markers
623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  created after this marker argument, this routine must unroll them
633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  like a stack.  Assume the state the stream was in when this marker
643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  was created.
653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	void rewind(int marker);
673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Rewind to the input position of the last marker.
693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Used currently only after a cyclic DFA and just
703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  before starting a sem/syn predicate to get the
713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  input position back to the start of the decision.
723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Do not "pop" the marker off the state.  mark(i)
733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  and rewind(i) should balance still. It is
743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  like invoking rewind(last marker) but it should not "pop"
753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  the marker off.  It's like seek(last marker's input position).
763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	void rewind();
783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** You may want to commit to a backtrack but don't want to force the
803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  stream to keep bookkeeping objects around for a marker that is
813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  no longer necessary.  This will have the same behavior as
823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  rewind() except it releases resources without the backward seek.
833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  This must throw away resources for all markers back to the marker
843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  argument.  So if you're nested 5 levels of mark(), and then release(2)
853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  you have to release resources for depths 2..5.
863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	void release(int marker);
883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Set the input cursor to the position indicated by index.  This is
903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  normally used to seek ahead in the input stream.  No buffering is
913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  required to do this unless you know your stream will use seek to
923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  move backwards such as when backtracking.
933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  This is different from rewind in its multi-directional
953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  requirement and in that its argument is strictly an input cursor (index).
963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  For char streams, seeking forward must update the stream state such
983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  as line number.  For seeking backwards, you will be presumably
993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  backtracking using the mark/rewind mechanism that restores state and
1003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  so this method does not need to update state when seeking backwards.
1013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Currently, this method is only used for efficient backtracking using
1033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  memoization, but in the future it may be used for incremental parsing.
1043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  The index is 0..n-1.  A seek to position i means that LA(1) will
1063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  return the ith symbol.  So, seeking to 0 means LA(1) will return the
1073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  first element in the stream.
1083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
1093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	void seek(int index);
1103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Only makes sense for streams that buffer everything up probably, but
1123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  might be useful to display the entire stream or for testing.  This
1133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  value includes a single EOF.
1143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
1153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	int size();
1163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Where are you getting symbols from?  Normally, implementations will
1183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  pass the buck all the way to the lexer who can ask its input stream
1193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  for the file name or whatever.
1203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
1213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public String getSourceName();
1223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein}
123