Top Description Fields Constructors Methods
org.antlr.runtime

public Class ANTLRStringStream

extends Object
implements CharStream
Class Inheritance
All Implemented Interfaces
org.antlr.runtime.CharStream, org.antlr.runtime.IntStream
Known Direct Subclasses
org.antlr.runtime.ANTLRFileStream, org.antlr.runtime.ANTLRReaderStream
Imports
java.util.ArrayList, .List

A pretty quick CharStream that pulls all data from an array directly. Every method call counts in the lexer. Java's strings aren't very good so I'm avoiding.

Field Summary

Modifier and TypeField and Description
protected int
charPositionInLine

The index of the character relative to the beginning of the line 0..n-1

protected char[]
data

The data being scanned

protected int
lastMarker

Track the last mark() call result value for use in rewind().

protected int
line

line number 1..n within the input

protected int
markDepth

tracks how deep mark() calls are nested

protected List<CharStreamState>
markers

A list of CharStreamState objects that tracks the stream state values line, charPositionInLine, and p that can change as you move through the input stream.

protected int
n

How many characters are actually in the buffer

public String
name

What is name or source of this char stream?

protected int
p

0..n-1 index into string of next char

Constructor Summary

AccessConstructor and Description
public
public
ANTLRStringStream(String input)

Copy data in string to a local char array

public
ANTLRStringStream(char[] data, int numberOfActualCharsInArray)

This is the preferred constructor as no data is copied

Method Summary

Modifier and TypeMethod and Description
public void
public int
getCharPositionInLine()

Implements org.antlr.runtime.CharStream.getCharPositionInLine.

The index of the character relative to the beginning of the line 0..n-1
public int
getLine()

Implements org.antlr.runtime.CharStream.getLine.

ANTLR tracks the line information automatically
public String
getSourceName()

Implements org.antlr.runtime.IntStream.getSourceName.

Where are you getting symbols from?
public int
index()

Implements org.antlr.runtime.IntStream.index.

Return the current input symbol index 0..n where n indicates the last symbol has been read.
public int
LA(int i)

Implements org.antlr.runtime.IntStream.LA.

Get int at current input pointer + i ahead where i=1 is next int.
public int
LT(int i)

Implements org.antlr.runtime.CharStream.LT.

Get the ith character of lookahead.
public int
mark()

Implements org.antlr.runtime.IntStream.mark.

Tell the stream to start buffering if it hasn't already.
public void
release(int marker)

Implements org.antlr.runtime.IntStream.release.

You may want to commit to a backtrack but don't want to force the stream to keep bookkeeping objects around for a marker that is no longer necessary.
public void
reset()

Reset the stream so that it's in the same state it was when the object was created *except* the data array is not touched.

public void
rewind(int m)

Implements org.antlr.runtime.IntStream.rewind.

Reset the stream so that next call to index would return marker.
public void
rewind()

Implements org.antlr.runtime.IntStream.rewind.

Rewind to the input position of the last marker.
public void
seek(int index)

Implements org.antlr.runtime.IntStream.seek.

consume() ahead until p==index; can't just set p=index as we must update line and charPositionInLine.
public void
public void
setLine(int line)

Implements org.antlr.runtime.CharStream.setLine.

Because this stream can rewind, we need to be able to reset the line
public int
size()

Implements org.antlr.runtime.IntStream.size.

Only makes sense for streams that buffer everything up probably, but might be useful to display the entire stream or for testing.
public String
substring(int start, int stop)

Implements org.antlr.runtime.CharStream.substring.

For infinite streams, you don't need this; primarily I'm providing a useful interface for action code.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

charPositionInLineback to summary
protected int charPositionInLine

The index of the character relative to the beginning of the line 0..n-1

databack to summary
protected char[] data

The data being scanned

lastMarkerback to summary
protected int lastMarker

Track the last mark() call result value for use in rewind().

lineback to summary
protected int line

line number 1..n within the input

markDepthback to summary
protected int markDepth

tracks how deep mark() calls are nested

markersback to summary
protected List<CharStreamState> markers

A list of CharStreamState objects that tracks the stream state values line, charPositionInLine, and p that can change as you move through the input stream. Indexed from 1..markDepth. A null is kept @ index 0. Create upon first call to mark().

nback to summary
protected int n

How many characters are actually in the buffer

nameback to summary
public String name

What is name or source of this char stream?

pback to summary
protected int p

0..n-1 index into string of next char

Constructor Detail

ANTLRStringStreamback to summary
public ANTLRStringStream()
ANTLRStringStreamback to summary
public ANTLRStringStream(String input)

Copy data in string to a local char array

ANTLRStringStreamback to summary
public ANTLRStringStream(char[] data, int numberOfActualCharsInArray)

This is the preferred constructor as no data is copied

Method Detail

consumeback to summary
public void consume()

Implements org.antlr.runtime.IntStream.consume.

Annotations
@Override
getCharPositionInLineback to summary
public int getCharPositionInLine()

Implements org.antlr.runtime.CharStream.getCharPositionInLine.

Doc from org.antlr.runtime.CharStream.getCharPositionInLine.

The index of the character relative to the beginning of the line 0..n-1

Annotations
@Override
getLineback to summary
public int getLine()

Implements org.antlr.runtime.CharStream.getLine.

Doc from org.antlr.runtime.CharStream.getLine.

ANTLR tracks the line information automatically

Annotations
@Override
getSourceNameback to summary
public String getSourceName()

Implements org.antlr.runtime.IntStream.getSourceName.

Doc from org.antlr.runtime.IntStream.getSourceName.

Where are you getting symbols from? Normally, implementations will pass the buck all the way to the lexer who can ask its input stream for the file name or whatever.

Annotations
@Override
indexback to summary
public int index()

Implements org.antlr.runtime.IntStream.index.

Return the current input symbol index 0..n where n indicates the last symbol has been read. The index is the index of char to be returned from LA(1).

Annotations
@Override
LAback to summary
public int LA(int i)

Implements org.antlr.runtime.IntStream.LA.

Doc from org.antlr.runtime.IntStream.LA.

Get int at current input pointer + i ahead where i=1 is next int. Negative indexes are allowed. LA(-1) is previous token (token just matched). LA(-i) where i is before first token should yield -1, invalid char / EOF.

Annotations
@Override
LTback to summary
public int LT(int i)

Implements org.antlr.runtime.CharStream.LT.

Doc from org.antlr.runtime.CharStream.LT.

Get the ith character of lookahead. This is the same usually as LA(i). This will be used for labels in the generated lexer code. I'd prefer to return a char here type-wise, but it's probably better to be 32-bit clean and be consistent with LA.

Annotations
@Override
markback to summary
public int mark()

Implements org.antlr.runtime.IntStream.mark.

Doc from org.antlr.runtime.IntStream.mark.

Tell the stream to start buffering if it hasn't already. Return current input position, index(), or some other marker so that when passed to rewind() you get back to the same spot. rewind(mark()) should not affect the input cursor. The Lexer track line/col info as well as input index so its markers are not pure input indexes. Same for tree node streams.

Annotations
@Override
releaseback to summary
public void release(int marker)

Implements org.antlr.runtime.IntStream.release.

Doc from org.antlr.runtime.IntStream.release.

You may want to commit to a backtrack but don't want to force the stream to keep bookkeeping objects around for a marker that is no longer necessary. This will have the same behavior as rewind() except it releases resources without the backward seek. This must throw away resources for all markers back to the marker argument. So if you're nested 5 levels of mark(), and then release(2) you have to release resources for depths 2..5.

Annotations
@Override
resetback to summary
public void reset()

Reset the stream so that it's in the same state it was when the object was created *except* the data array is not touched.

rewindback to summary
public void rewind(int m)

Implements org.antlr.runtime.IntStream.rewind.

Doc from org.antlr.runtime.IntStream.rewind.

Reset the stream so that next call to index would return marker. The marker will usually be index() but it doesn't have to be. It's just a marker to indicate what state the stream was in. This is essentially calling release() and seek(). If there are markers created after this marker argument, this routine must unroll them like a stack. Assume the state the stream was in when this marker was created.

Annotations
@Override
rewindback to summary
public void rewind()

Implements org.antlr.runtime.IntStream.rewind.

Doc from org.antlr.runtime.IntStream.rewind.

Rewind to the input position of the last marker. Used currently only after a cyclic DFA and just before starting a sem/syn predicate to get the input position back to the start of the decision. Do not "pop" the marker off the state. mark(i) and rewind(i) should balance still. It is like invoking rewind(last marker) but it should not "pop" the marker off. It's like seek(last marker's input position).

Annotations
@Override
seekback to summary
public void seek(int index)

Implements org.antlr.runtime.IntStream.seek.

consume() ahead until p==index; can't just set p=index as we must update line and charPositionInLine.

Annotations
@Override
setCharPositionInLineback to summary
public void setCharPositionInLine(int pos)

Implements org.antlr.runtime.CharStream.setCharPositionInLine.

Annotations
@Override
setLineback to summary
public void setLine(int line)

Implements org.antlr.runtime.CharStream.setLine.

Doc from org.antlr.runtime.CharStream.setLine.

Because this stream can rewind, we need to be able to reset the line

Annotations
@Override
sizeback to summary
public int size()

Implements org.antlr.runtime.IntStream.size.

Doc from org.antlr.runtime.IntStream.size.

Only makes sense for streams that buffer everything up probably, but might be useful to display the entire stream or for testing. This value includes a single EOF.

Annotations
@Override
substringback to summary
public String substring(int start, int stop)

Implements org.antlr.runtime.CharStream.substring.

Doc from org.antlr.runtime.CharStream.substring.

For infinite streams, you don't need this; primarily I'm providing a useful interface for action code. Just make sure actions don't use this on streams that don't support it.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override