RuleDefinition.java :  » Parser » mesopotamia » org » mesopotamia » util » Java Open Source

Java Open Source » Parser » mesopotamia 
mesopotamia » org » mesopotamia » util » RuleDefinition.java
/*
 * mesopotamia @mesopotamia.version@
 * Multilingual parser and repository. 
 * Copyright (C) 2005  Hammurapi Group
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * URL: http://http://www.hammurapi.biz
 * e-Mail: support@hammurapi.biz
 */
package org.mesopotamia.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import antlr.collections.AST;
import biz.hammurapi.util.CollectionVisitable;
import biz.hammurapi.util.VisitableBase;
import biz.hammurapi.util.Visitor;


public class RuleDefinition extends VisitableBase {
  private BnfModel model;
  private String name;
  private Set matchPaths=new HashSet();
  private Collection attributes=new ArrayList();
  private boolean isInterface;
  private boolean isFactory;

  RuleDefinition(BnfModel model, AST node) {
    this.model=model;
    name=node.getText();
    for (AST child=node.getFirstChild(); child!=null; child=child.getNextSibling()) {
      switch (child.getType()) {
        case BnfTokenTypes.LITERAL_interface:
          isInterface=true;
          break;
        case BnfTokenTypes.LITERAL_factory:
          isFactory=true;
          break;
        case BnfTokenTypes.SUBTYPE:
          if (isInterface()) {
            model.addInterface(child.getText(), this);
          } else {
            RuleDefinition alreadyDefined = (RuleDefinition) model.superClasses.get(child.getText());
            if (alreadyDefined==null) {
              model.superClasses.put(child.getText(), this);
            } else {
              System.err.println("Superclass already defined: "+child.getText()+" extends "+alreadyDefined.getName()+", new superclass: "+node.getText());
            }
          }
          break;
        case BnfTokenTypes.CPATH:
          MatchPath path=new MatchPath(this, child);
          matchPaths.add(path);
          if (model.matchPath.put(path, this)!=null) {
            System.err.println(child.getLine()+":"+child.getColumn()+" Duplicate match path");
          }
          break;
        case BnfTokenTypes.ATTRIBUTE:
          attributes.add(new Attribute(this, child, model.getPackage()));
          break;
        default:
          throw new IllegalArgumentException("Unexpected node: "+BnfRecognizer._tokenNames[child.getType()]+" at "+child.getLine()+":"+child.getColumn());
        
      }
    }
    
    // Store paths in the model, validation - same match expression for more than one rule
    
  }
  
  public String getName() {
    return name;
  }
  
  public String toString() {
    return getName() + (isInterface() ? "" : " extends " + getSuperClassName()) + (getImplements().isEmpty() ? "" : " implements " + getImplements());
  }
  
  public boolean isInterface() {
    return isInterface;
  }
  
  public boolean isFactory() {
    return isFactory;
  }
  
  public String getSuperClassName() {
    RuleDefinition superRule = getSuperRule();
    return isInterface() ? "" : superRule==null ? "org.mesopotamia.LanguageElement" : superRule.getName();
  }

  /**
   * @return
   */
  public RuleDefinition getSuperRule() {
    RuleDefinition superRule=(RuleDefinition) model.superClasses.get(this.getName());
    return superRule;
  }
  
  public Collection getImplements() {
    Collection ret=new ArrayList();
    Collection interfaces=(Collection) model.interfaces.get(getName());
    if (interfaces!=null) {
      Iterator it=interfaces.iterator();
      while (it.hasNext()) {
        ret.add(((RuleDefinition) it.next()).getName());
      }
    }
    return ret;
  }

  protected Set getMatchPaths() {
    return matchPaths;
  }
  
  public BnfModel getModel() {
    return model;
  }
  
  protected void acceptChildren(Visitor visitor) {
    new CollectionVisitable(matchPaths, false).accept(visitor);
  }
  
  public Collection getAttributes() {
    return attributes;
  }
  
  public String getFullClassName() {
    return name.indexOf('.')==-1 ? model.getPackage()+"."+name : name;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.