Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2015 Santhosh Kumar Tekuri The JLibs authors license this file to you under the Apache License, version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package jlibs.nbp;
 
 
 import static java.lang.Character.*;

Author(s):
Santhosh Kumar T
 
 public abstract class NBParser{
     public static final boolean SHOW_STATS = false;
     public static int callRuleCount = 0;
     public static int chunkCount = 0;
     public void printStats(){
         ..println("callRuleCount = " + );
         ..println("chunkCount = " + );
         ..println("lineCount = " + getLineNumber());
         ..println("charCount = " + getCharacterOffset());
     }
 
     protected final Chars buffer = new Chars();
 
     private int startingRule;
     public NBParser(int maxLookAheadint startingRule){
          = new int[maxLookAhead];
         reset(startingRule);
     }
 
     public final void reset(int rule){
          = 0;
          =  = false;
          = ;
          =  =  = 0;
          =  = 0;
          = 1;
          = 'X';
         .clear();
 
          = 2;
         [0] =  = rule;
         [1] = 0;
     }
 
     public final void reset(){
         reset();
     }
 
     protected char input[];
     protected int start;
     protected int position;
     protected int limit;
     protected int marker;
 
     public static final int EOF = -1;
     public static final int EOC = -2;
     protected int increment;
     protected final int codePoint() throws IOException{
         if(==)
             return ;
 
         int ch0 = [];
         if(ch0>= && ch0<=){
             if(+1==)
                 return ;
 
             int ch1 = [+1];
             if(ch1>= && ch1<=){
                  = 2;
                 return ((ch0 - ) << 10) + (ch1 - ) + ;
             }else
                 throw ioError("bad surrogate pair");
         }else{
              = 1;
             return ch0;
         }
     }
 
     public boolean coalesceNewLines = false;
     protected static final int FROM_LA = -2;
     protected final void consume(int cp){
         if(cp==){
              += ;
            cp = [0];
        }else
             += ;
        assert cp!=;
        if(cp=='\r'){
            ++;
             = ;
            cp =  ? '\n' : '\r';
        }else if(cp=='\n'){
             = ;
            char lastChar = ==+1 ? this. : [-2];
            if(lastChar!='\r')
                ++;
            else if()
                    return;
        }
        if(.>0)
            .append(cp);
    }
    protected int la[];
    protected int laLen;
    private int laPosition;
    private int laIncrement;
    protected final void addToLookAhead(int cp){
        if(==0){
             = ;
             = ;
        }
        [++] = cp;
         += ;
    }
    protected final void resetLookAhead(){
        this. = ;
         = 0;
    }
    protected int offsetlinelinePosition;
    protected char lastChar;
    public final int getCharacterOffset(){
        return  + (-);
    }
    public final int getLineNumber(){
        return ;
    }
    public final int getColumnNumber(){
        return -;
    }
    public final void setLocation(NBParser parser){
        this. = parser.offset;
        this. = parser.line;
        this. = parser.linePosition;
        this. = parser.lastChar;
    }
    public boolean stoppop;
    public final int consume(char chars[], int positionint limitboolean eofthrows IOException{
        if(){
            ++;
            if(chars!=null)
                ..println("chunk["++"] = {"+new String(charspositionlimit-position)+'}');
        }
        try{
             = chars;
             = this. = position;
            this. = limit;
             = eof ?  : ;
             =  = false;
            do{
                if(==0){
                    if(this.<limit)
                        throw expected(codePoint(), "<EOF>");
                    break;
                }
                 -= 2;
            }while(callRule([], [+1]));
            if(>0)
                resetLookAhead();
            if(>0){
                if(+>.)
                     = Arrays.copyOf(, Math.max(+*2));
                do{
                     += 2;
                    [-2] = [-2];
                    [-1] = [-1];
                     -= 2;
                }while(!=0);
            }else if(==0 && this.==limit && eof)
                onSuccessful();
            if(this.!=position){
                 = [this.-1];
                 += this.-position;
                 -= this.;
                position = this.;
            }
             = this. = 0;
            return position;
        }catch(IOException ex){
            throw ex;
        }catch(Exception ex){
            if(ex.getCause() instanceof IOException)
                throw (IOException)ex.getCause();
            else
                throw new IOException(ex);
        }
    }
    protected abstract boolean callRule(int ruleint statethrows Exception;
    protected final Exception expected(int chString... matchers){
        String found;
        if(>0)
            found = [-1]== ? new String(, 0, -1).concat("<EOF>") : new String(, 0, );
        else{
            if(ch==)
                found = "<EOF>";
            else
                found = new String(toChars(ch));
        }
        StringBuilder buff = new StringBuilder();
        for(String matchermatchers){
            if(buff.length()>0)
                buff.append(" OR ");
            buff.append(matcher);
        }
        return fatalError("Found: '"+found+"' Expected: "+buff.toString());
    }
    protected abstract Exception fatalError(String message);
    protected abstract void onSuccessful() throws Exception;
    /*-------------------------------------------------[ Parsing Status ]---------------------------------------------------*/
    protected int free = 0;
    protected int stack[] = new int[100];
    public final IOException ioError(String messagethrows IOException{
        Exception ex = fatalError(message);
        return ex instanceof IOException ? (IOException)ex : new IOException(ex);
    }
    protected int exitStack[] = new int[100];
    protected int exitFree = 0;
    protected final void exiting(int ruleint state){
         += 2;
        if(>.)
             = Arrays.copyOf(*2);
        [-2] = rule;
        [-1] = state;
    }
    /*-------------------------------------------------[ Helpers ]---------------------------------------------------*/
    public static final int RULE_DYNAMIC_STRING_MATCH = .;
    public char[] dynamicStringToBeMatched;
    // NOTE: use only when bufferring is off and no new lines in expected
    protected final boolean matchString(int statechar expected[]) throws Exception{
        int length = expected.length;
        while(state<length && <){
            if([]!=expected[state])
                throw expected(codePoint(), new String(new int[]{ Character.codePointAt(expectedstate) }, 0, 1));
            state++;
            ++;
        }
        if(state==length)
            return true;
        else{
            if(==)
                throw expected(new String(expectedstatelength-state));
            exiting(state);
            return false;
        }
    }
    protected final boolean matchString(int ruleint stateint expected[]) throws Exception{
        int length = expected.length;
        for(int i=statei<lengthi++){
            int cp = codePoint();
            if(cp!=expected[i]){
                if(cp==){
                    exiting(rulei);
                    return false;
                }
                throw expected(cpnew String(expectedi, 1));
            }
            consume(cp);
        }
        return true;
    }
    protected final int finishAll(int chint expectedthrows IOException{
        while(ch==expected){
            consume(ch);
            ch = codePoint();
        }
        return ch;
    }
    protected final int finishAll_OtherThan(int chint expectedthrows IOException{
        while(ch>=0 && ch!=expected){
            consume(ch);
            ch = codePoint();
        }
        return ch;
    }
New to GrepCode? Check out our FAQ X