Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses 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 org.apache.avro.io.parsing;
 
 
Parser is the class that maintains the stack for parsing. This class is used by encoders, which are not required to skip.
 
 public class Parser {
  
The parser knows how to handle the terminal and non-terminal symbols. But it needs help from outside to handle implicit and explicit actions. The clients implement this interface to provide this help.
 
   public interface ActionHandler {
    
Handle the action symbol top when the input is sought to be taken off the stack.

Parameters:
input The input symbol from the caller of advance
top The symbol at the top the stack.
Returns:
null if advance() is to continue processing the stack. If not null the return value will be returned by advance().
Throws:
java.io.IOException
 
     Symbol doAction(Symbol inputSymbol topthrows IOException;
   }
 
   protected final ActionHandler symbolHandler;
   protected Symbol[] stack;
   protected int pos;
 
   public Parser(Symbol rootActionHandler symbolHandler)
     throws IOException {
     this. = symbolHandler;
     this. = new Symbol[5]; // Start small to make sure expansion code works
     this.[0] = root;
     this. = 1;
   }

  
If there is no sufficient room in the stack, use this expand it.
 
   private void expandStack() {
      = Arrays.copyOf(.+Math.max(.,1024));
   }

  
Recursively replaces the symbol at the top of the stack with its production, until the top is a terminal. Then checks if the top symbol matches the terminal symbol suppled terminal.

Parameters:
input The symbol to match against the terminal at the top of the stack.
Returns:
The terminal symbol at the top of the stack unless an implicit action resulted in another symbol, in which case that symbol is returned.
 
   public final Symbol advance(Symbol inputthrows IOException {
     for (; ;) {
       Symbol top = [--];
       if (top == input) {
         return top// A common case
       }
 
       Symbol.Kind k = top.kind;
       if (k == ..) {
         Symbol result = .doAction(inputtop);
         if (result != null) {
           return result;
         }
       } else if (k == ..) {
         throw new AvroTypeException("Attempt to process a "
                 + input + " when a "
                 + top + " was expected.");
       } else if (k == ..
           && input == ((Symbol.Repeatertop).) {
         return input;
       } else {
        pushProduction(top);
      }
    }
  }
  
  
Performs any implicit actions at the top the stack, expanding any production (other than the root) that may be encountered. This method will fail if there are any repeaters on the stack.

  public final void processImplicitActions() throws IOException {
     while ( > 1) {
      Symbol top = [ - 1];
      if (top.kind == ..) {
        --;
        .doAction(nulltop);
      } else if (top.kind != ..) {
        --;
        pushProduction(top);
      } else {
        break;
      }
    }
  }

  
Performs any "trailing" implicit actions at the top the stack.
  public final void processTrailingImplicitActions() throws IOException {
    while ( >= 1) {
      Symbol top = [ - 1];
      if (top.kind == .. 
        && ((Symbol.ImplicitActiontop).) {
        --;
        .doAction(nulltop);
      } else {
        break;
      }
    }
  }

  
Pushes the production for the given symbol sym. If sym is a repeater and input is either Symbol.ARRAY_END or Symbol.MAP_END pushes nothing.

Parameters:
sym
  public final void pushProduction(Symbol sym) {
    Symbol[] p = sym.production;
    while ( + p.length > .) {
      expandStack();
    }
    System.arraycopy(p, 0, p.length);
     += p.length;
  }

  
Pops and returns the top symbol from the stack.
  public Symbol popSymbol() {
    return [--];
  }
  
  
Returns the top symbol from the stack.
  public Symbol topSymbol() {
    return [ - 1];
  }
  
  
Pushes sym on to the stack.
  public void pushSymbol(Symbol sym) {
    if ( == .) {
      expandStack();
    }
    [++] = sym;
  }
  
  
Returns the depth of the stack.
  public int depth() {
    return ;
  }
  public void reset() {
     = 1;
  }
New to GrepCode? Check out our FAQ X