Package net.sourceforge.chaperon.model.extended

Source Code of net.sourceforge.chaperon.model.extended.Definition

/*
*  Copyright (C) Chaperon. All rights reserved.
*  -------------------------------------------------------------------------
*  This software is published under the terms of the Apache Software License
*  version 1.1, a copy of which has been included  with this distribution in
*  the LICENSE file.
*/

package net.sourceforge.chaperon.model.extended;

import net.sourceforge.chaperon.model.Violations;

/**
* This class presents a definition of a grammar
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
* @version CVS $Id: Definition.java,v 1.2 2003/12/14 09:44:21 benedikta Exp $
*/
public class Definition extends PatternList
{
  private String symbol = null;
  private String location = null;

  /**
   * Create an empty definition.
   */
  public Definition() {}

  /**
   * Create a definition.
   *
   * @param ntsymbol The symbol of this definition.
   */
  public Definition(String symbol)
  {
    setSymbol(symbol);
  }

  /**
   * Set the symbol for this definition
   *
   * @param ntsymbol Non terminal symbol
   */
  public void setSymbol(String symbol)
  {
    if (symbol==null)
      throw new NullPointerException();

    this.symbol = symbol;
  }

  /**
   * Return the symbol from this definition
   *
   * @return Nonterminal symbol
   */
  public String getSymbol()
  {
    return symbol;
  }

  /**
   * Set the location from the input source.
   *
   * @param location Location in the input source.
   */
  public void setLocation(String location)
  {
    this.location = location;
  }

  /**
   * Returns the location from the input source.
   *
   * @return Location in the input source.
   */
  public String getLocation()
  {
    return location;
  }

  /**
   * Validates the definition.
   *
   * @return Return a list of violations, if this object isn't valid.
   */
  public Violations validate()
  {
    Violations violations = new Violations();

    if (symbol==null)
      violations.addViolation("No symbol is for the left side defined", location);

    if (getPatternCount()==0)
      violations.addViolation("No pattern are for the right side defined", location);

    for (int i = 0; i<getPatternCount(); i++)
      violations.addViolations(getPattern(i).validate());

    return violations;
  }

  /**
   * Compares the definition with another definition.
   *
   * @param o Other object.
   *
   * @return True, if the definition are equal.
   */
  public boolean equals(Object o)
  {
    if (o==this)
      return true;

    if (o instanceof Definition)
    {
      Definition definition = (Definition)o;

      return (symbol.equals(definition.symbol)) && (super.equals(o));
    }

    return false;
  }

  /**
   * Return a string representation of the definition.
   *
   * @return String representation of the definition.
   */
  public String toString(PatternSet previous, PatternSet next)
  {
    StringBuffer buffer = new StringBuffer();

    buffer.append(symbol);
    buffer.append(" := ");
    buffer.append(super.toString(previous, next));

    return buffer.toString();
  }

  /**
   * @return
   *
   * @throws CloneNotSupportedException
   */
  public Object clone() throws CloneNotSupportedException
  {
    Definition clone = new Definition();

    clone.symbol = symbol;

    for (int i = 0; i<getPatternCount(); i++)
      clone.addPattern((Pattern)getPattern(i).clone());

    clone.location = location;

    return clone;
  }
}
TOP

Related Classes of net.sourceforge.chaperon.model.extended.Definition

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.