Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  
  package org.jruby.lexer.yacc;
  
  import java.util.List;
  
 
 public class InputStreamLexerSource extends LexerSource {
     private static final int INITIAL_PUSHBACK_SIZE = 100;
     
     public static final int DATA_READ_BUFFER_SIZE = 65536;
     
     // Where we get our newest char's
     private final InputStream in;
     
     // Our readback/pushback buffer.
     private char buf[] = new char[];
     
     // index of last character in pushback buffer
     private int bufLength = -1;
     
     // Character read before previous read
     private int oneAgo = '\n';
     private int twoAgo = 0;
    
    
Create our food-source for the lexer

Parameters:
sourceName is the file we are reading
reader is what represents the contents of file sourceName
line starting line number for source (used by eval)
extraPositionInformation will gives us extra information that an IDE may want
 
     public InputStreamLexerSource(String sourceNameInputStream inList<Stringlistint line
             boolean extraPositionInformation) {
         super(sourceNamelistlineextraPositionInformation);
         
         this. = in;
     }
    
    
Read next character from this source

Returns:
next character to viewed by the source
 
     @Override
     public int read() throws IOException {
         int c;
         
         if ( >= 0) {
             c = [--];
         } else {
             c = wrappedRead();
             
             if (c == -1) return .;
         }
         
         advance(c);
         
         if (c == '\n'++;
             
         return c
     }

    
Pushes char back onto this source. Note, this also allows us to push whatever is passes back into the source.

Parameters:
to be put back onto the source
 
     @Override
     public void unread(int c) {
         if (c == .return;
         
         retreat();
             
         if (c == '\n'--;
 
         [++] = (charc;
         
         growBuf();
     }
    
    
Is the next character equal to 'to'

Parameters:
to character to compare against
Returns:
true if the same
Throws:
java.io.IOException
 
     @Override
     public boolean peek(int tothrows IOException {
         // keep value of twoAgo around so we can restore after we unread
         int captureTwoAgo = ;
         int c = read();
         unread(c);
         = captureTwoAgo;
        return c == to;
    }
    private void advance(int c) {
         = ;
         = c;
        ++;
    }
    private int carriageReturn(int cthrows IOException {
        if ((c = .read()) != '\n') {
            unread((charc);
        } else {
            // Position within source must reflect the actual offset and column.  Since
            // we ate an extra character here (this accounting is normally done in read
            // ), we should update position info.
            ++;
        }
        return c;
    }
    private void growBuf() {
        // If we outgrow our pushback stack then grow it (this should only happen in pretty 
        // pathological cases).
        if ( + 1 == .) {
            char[] newBuf = new char[. + ];
            System.arraycopy(, 0, newBuf, 0, .);
             = newBuf;
        }
    }
    private void retreat() {
        --;
         = ;
         = 0;
    }
    
    
Convenience method to hide exception. If we do hit an exception we will pretend we EOF'd.

Returns:
the current char or EOF (at EOF or on error)
    private int wrappedRead() throws IOException {
        int c = .read();
        
        // If \r\n then just pass along \n (windows). 
        if (c == '\r') { 
            c = carriageReturn(c);
        }
        captureFeature(c);
        return c;
    }
    @Override
    public ByteList readLineBytes() throws IOException {
        ByteList bytelist = new ByteList(80);
        for (int c = read(); c != '\n' && c != .c = read()) {
            bytelist.append(c);
        }
        
        return bytelist;
    }
    
    @Override
    public int skipUntil(int markerthrows IOException {
        int c;
        for (c = read(); c != marker && c != .c = read()) {}
        return c;
    }
    @Override
    public void unreadMany(CharSequence buffer) {
        int length = buffer.length();
        for (int i = length - 1; i >= 0; i--) {
            unread(buffer.charAt(i));
        }
    }
    @Override
    public boolean matchMarker(ByteList matchboolean indentboolean checkNewlinethrows IOException {
        int length = match.length();
        ByteList buffer = new ByteList(length + 1);
        
        if (indent) {
            indentLoop(buffer);
        }
        
        if (!matches(matchbufferlength)) return false;
        
        return finishMarker(checkNewlinebuffer); 
    }
    private void indentLoop(ByteList bufferthrows IOException {
        int c;
        while ((c = read()) != .) {
            if (!Character.isWhitespace(c) || c == '\n') {
                unread(c);
                break;
            }
            buffer.append(c);
        }
    }
    
    private boolean matches(ByteList matchByteList bufferint lengththrows IOException {
        int c;
        for (int i = 0; i < lengthi++) {
            c = read();
            buffer.append(c);
            if (match.charAt(i) != c) {
                unreadMany(buffer);
                return false;
            }
        }
        return true;
    }
    private boolean finishMarker(boolean checkNewlineByteList bufferthrows IOException {
        if (!checkNewline) {
            return true;
        }
        int c = read();
        if (c == . || c == '\n') {
            return true;
        }
        buffer.append(c);
        unreadMany(buffer);
        return false;
    }
    
    
Was the last character read from the stream the first character on a line

Returns:
true if so
    @Override
    public boolean wasBeginOfLine() {
        return  == '\n';
    }
    @Override
    public boolean lastWasBeginOfLine() {
        return  == '\n';
    }
    
    static final ByteList EOF_LABEL = new ByteList(new byte[] {'{''e''o''f''}'});
    @Override
    public String toString() {
        try {
            ByteList buffer = new ByteList(20);
            ByteList unreadBuffer = new ByteList(20);
            if ( != -1 &&  != 0) buffer.append();
            if ( != -1 &&  != 0) buffer.append();
            buffer.append('<');
            int i = 0;
            int c = read();
            unreadBuffer.append(c);
            
            if (c == -1) {
                unread(unreadBuffer.charAt(0));
                buffer.append();
                buffer.append('>');
                
                return buffer.toString();
            } else {
                buffer.append(c).append('>');
            }
            i = 1;
            
            for (; i < 20; i++) {
                c = read();
                unreadBuffer.append(c);
                if (c == -1) {
                    buffer.append();
                    i--;
                    break;
                }
                buffer.append(c);
            }
            for (; i >= 0; i--) {
                unread(unreadBuffer.charAt(i));
            }
            buffer.append(new byte[] {' ''.''.''.'});
            return buffer.toString();
        } catch(Exception e) {
            return null;
        }
    }
    @Override
    public ByteList readUntil(char markerthrows IOException {
        ByteList list = new ByteList(20);
        int c;
        
        for (c = read(); c != marker && c != .c = read()) {
            list.append(c);
        }
        
        if (c == .return null;
        
        unread(c);
        
        return list;
    }
    @Override
    public InputStream getRemainingAsStream() throws IOException {
        return ;
    }
New to GrepCode? Check out our FAQ X