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;
 
 import java.util.List;
 import java.util.Map;
 
The class that generates validating grammar.
 
 public class ValidatingGrammarGenerator {
  
Returns the non-terminal that is the start symbol for the grammar for the given schema sc.
 
   public Symbol generate(Schema schema) {
     return Symbol.root(generate(schemanew HashMap<LitSSymbol>()));
   }

  
Returns the non-terminal that is the start symbol for the grammar for the given schema sc. If there is already an entry for the given schema in the given map seen then that entry is returned. Otherwise a new symbol is generated and an entry is inserted into the map.

Parameters:
sc The schema for which the start symbol is required
seen A map of schema to symbol mapping done so far.
Returns:
The start symbol for the schema
 
   public Symbol generate(Schema scMap<LitSSymbolseen) {
     switch (sc.getType()) {
     case :
       return .;
     case :
       return .;
     case :
       return .;
     case :
       return .;
     case :
       return .;
     case :
       return .;
     case :
       return .;
     case :
       return .;
     case :
       return Symbol.seq(new Symbol.IntCheckAction(sc.getFixedSize()),
           .);
     case :
       return Symbol.seq(new Symbol.IntCheckAction(sc.getEnumSymbols().size()),
           .);
     case :
       return Symbol.seq(Symbol.repeat(.generate(sc.getElementType(), seen)),
           .);
     case :
       return Symbol.seq(Symbol.repeat(.,
               generate(sc.getValueType(), seen), .),
           .);
     case : {
       LitS wsc = new LitS(sc);
       Symbol rresult = seen.get(wsc);
       if (rresult == null) {
         Symbol[] production = new Symbol[sc.getFields().size()];

        
We construct a symbol without filling the array. Please see Symbol.production for the reason.
 
         rresult = Symbol.seq(production);
         seen.put(wscrresult);
 
         int i = production.length;
         for (Field f : sc.getFields()) {
           production[--i] = generate(f.schema(), seen);
         }
       }
       return rresult;
     }
    case :
      List<Schemasubs = sc.getTypes();
      Symbol[] symbols = new Symbol[subs.size()];
      String[] labels = new String[subs.size()];
      
      int i = 0;
      for (Schema b : sc.getTypes()) {
        symbols[i] = generate(bseen);
        labels[i] = b.getName();
        i++;
      }
      return Symbol.seq(Symbol.alt(symbolslabels), .);
    default:
      throw new RuntimeException("Unexpected schema type");
    }
  }

  
A wrapper around Schema that does "==" equality.
  static class LitS {
    public final Schema actual;
    public LitS(Schema actual) { this. = actual; }
    
    
Two LitS are equal if and only if their underlying schema is the same (not merely equal).
    public boolean equals(Object o) {
      if (! (o instanceof LitS)) return false;
      return  == ((LitS)o).;
    }
    
    public int hashCode() {
      return .hashCode();
    }
  }
New to GrepCode? Check out our FAQ X