FileDocCategorySizeDatePackage
InputBuffer.javaAPI DocGlassfish v2 API3862Wed Aug 30 15:34:08 BST 2006persistence.antlr

InputBuffer

public abstract class InputBuffer extends Object
A Stream of characters fed to the lexer from a InputStream that can be rewound via mark()/rewind() methods.

A dynamic array is used to buffer up all the input characters. Normally, "k" characters are stored in the buffer. More characters may be stored during guess mode (testing syntactic predicate), or when LT(i>k) is referenced. Consumption of characters is deferred. In other words, reading the next character is not done by conume(), but deferred until needed by LA or LT.

see
persistence.antlr.CharQueue

Fields Summary
protected int
nMarkers
protected int
markerOffset
protected int
numToConsume
protected CharQueue
queue
Constructors Summary
public InputBuffer()
Create an input buffer


         
      
        queue = new CharQueue(1);
    
Methods Summary
public charLA(int i)
Get a lookahead character

        fill(i);
        return queue.elementAt(markerOffset + i - 1);
    
public voidcommit()
This method updates the state of the input buffer so that the text matched since the most recent mark() is no longer held by the buffer. So, you either do a mark/rewind for failed predicate or mark/commit to keep on parsing without rewinding the input.

        nMarkers--;
    
public voidconsume()
Mark another character for deferred consumption

        numToConsume++;
    
public abstract voidfill(int amount)
Ensure that the input buffer is sufficiently full

public java.lang.StringgetLAChars()

        StringBuffer la = new StringBuffer();
        for (int i = markerOffset; i < queue.nbrEntries; i++)
            la.append(queue.elementAt(i));
        return la.toString();
    
public java.lang.StringgetMarkedChars()

        StringBuffer marked = new StringBuffer();
        for (int i = 0; i < markerOffset; i++)
            marked.append(queue.elementAt(i));
        return marked.toString();
    
public booleanisMarked()

        return (nMarkers != 0);
    
public intmark()
Return an integer marker that can be used to rewind the buffer to its current state.

        syncConsume();
        nMarkers++;
        return markerOffset;
    
public voidreset()
Reset the input buffer

        nMarkers = 0;
        markerOffset = 0;
        numToConsume = 0;
        queue.reset();
    
public voidrewind(int mark)
Rewind the character buffer to a marker.

param
mark Marker returned previously from mark()

        syncConsume();
        markerOffset = mark;
        nMarkers--;
    
protected voidsyncConsume()
Sync up deferred consumption

        while (numToConsume > 0) {
            if (nMarkers > 0) {
                // guess mode -- leave leading characters and bump offset.
                markerOffset++;
            }
            else {
                // normal mode -- remove first character
                queue.removeFirst();
            }
            numToConsume--;
        }