Package net.sourceforge.chaperon.model.lexicon

Source Code of net.sourceforge.chaperon.model.lexicon.Lexeme

/*
*  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.lexicon;

import net.sourceforge.chaperon.model.Violations;
import net.sourceforge.chaperon.model.pattern.Pattern;
import net.sourceforge.chaperon.model.symbol.Symbol;

/**
* This class represents a lexeme within a lexicon. The lexeme associates a terminal symbol to a
* pattern.
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels </a>
* @version CVS $Id: Lexeme.java,v 1.4 2003/12/09 19:55:52 benedikta Exp $
*/
public class Lexeme
{
  private Symbol symbol = null;
  private Pattern definition = null;
  private String location = null;

  /**
   * Create a lexeme.
   */
  public Lexeme() {}

  /**
   * Create a lexeme.
   *
   * @param symbol Terminal symbol for this lexeme.
   */
  public Lexeme(Symbol symbol)
  {
    setSymbol(symbol);
  }

  /**
   * Set the terminal symbol for this lexeme.
   *
   * @param symbol Terminal symbol.
   */
  public void setSymbol(Symbol symbol)
  {
    this.symbol = symbol;
  }

  /**
   * Return the symbol for this lexeme.
   *
   * @return Terminal symbol.
   */
  public Symbol getSymbol()
  {
    return symbol;
  }

  /**
   * Set a pattern as definition for this lexeme.
   *
   * @param definition Definition of the lexeme.
   */
  public void setDefinition(Pattern definition)
  {
    this.definition = definition;
  }

  /**
   * Return the definition.
   *
   * @return Definition of the lexeme.
   */
  public Pattern getDefinition()
  {
    return definition;
  }

  /**
   * 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;
  }

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

    if (definition==null)
    {
      if (symbol!=null)
        violations.addViolation("Lexeme "+symbol+" contains no definition", location);
      else
        violations.addViolation("Lexeme contains no definition", location);
    }

    if ((symbol!=null) && (symbol.getName().equals("error")))
      violations.addViolation("Symbol with name \"error\" is not allowed", location);

    return violations;
  }

  /**
   * Return a string representation of the lexeme.
   *
   * @return String representation.
   */
  public String toString()
  {
    if (symbol!=null)
      return symbol.toString()+" = \""+definition+"\"";

    return "/* noname */ = \""+definition+"\"";
  }

  /**
   * Creates a clone of this lexeme.
   *
   * @return Clone of this lexeme.
   *
   * @throws CloneNotSupportedException If an exception occurs during the cloning.
   */
  public Object clone() throws CloneNotSupportedException
  {
    Lexeme clone = new Lexeme();

    clone.symbol = symbol;
    if (definition!=null)
      clone.definition = (Pattern)definition.clone();

    clone.location = location;

    return clone;
  }
}
TOP

Related Classes of net.sourceforge.chaperon.model.lexicon.Lexeme

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.