//  FILE. . . . . /home/hak/ilt/src/ilog/rif/Lexicon.java
//  EDIT BY . . . Hassan Ait-Kaci
//  ON MACHINE. . Latitude407.Ilog.Biz
//  STARTED ON. . Wed May  3 14:37:18 2006

package ilog.rif;

/**
 * This is a simple lexicon class implementing a very basic token
 * discriminator for the tokenizer in <a
 * href="Tokenizer.html"><tt>Tokenizer.java</tt></a>. It uses explicit
 * declararations for functional and relation symbols since there is no
 * <i>a priori</i> means to distinguish them - at least not from the
 * information in <a
 * href="http://lists.w3.org/Archives/Public/public-rif-wg/2006Apr/0068.html">
 * [RIF] Extensible Design</a>.
 *
 * @version     Last modified on Fri Oct 20 18:08:18 2006 by hak
 * @author      <a href="mailto:hak@ilog.com">Hassan A&iuml;t-Kaci</a>
 * @copyright   &copy; 2006 <a href="http://www.ilog.com/">ILOG, Inc.</a>
 */

import java.util.HashMap;
import java.util.HashSet;

class Lexicon
{
  /**
   * Returns <tt>true</tt> iff the specified string starts with a
   * question mark, and every other character is alphanumeric or
   * underscore.
   */
  public final static boolean isVariable (String s)
    {
      char c = s.charAt(0);

      if (c != '?')
        return false;

      for (int i=1; i<s.length(); i++)
          {
            c = s.charAt(i);
            if (!Character.isLetterOrDigit(c) && c != '_')
              return false;
          }

      return true;
    }       

  /**
   * The following is a store for RCL reserved words.
   */
  public static final HashSet reserved = new HashSet();

  /**
   * The following is a store for RCL function identifiers, associated
   * with a corresponding XML tag.
   */
  public static final HashMap functions = new HashMap();

  /**
   * The following is a store for RCL relation identifiers, associated
   * with a corresponding XML tag.
   */
  public static final HashMap relations = new HashMap();

  /**
   * Returns <tt>true</tt> iff the specified string is a function
   * identifier.
   */
  public final static boolean isFunction (String word)
    {
      return functions.containsKey(word);
    }

  /**
   * Returns <tt>true</tt> iff the specified string is a relation
   * identifier.
   */
  public final static boolean isRelation (String word)
    {
      return relations.containsKey(word);
    }

  /**
   * Returns <tt>true</tt> iff the specified string is a reserved
   * RCL word.
   */
  public final static boolean isReserved (String word)
    {
      return reserved.contains(word);
    }

  /**
   * Declares the specified string as a function identifier whose XML
   * tag is itself.
   */
  static final void function (String word)
    {
      functions.put(word,word);
    }

  /**
   * Declares the specified string as a function identifier, with
   * specified XML tag.
   */
  static final void function (String word, String tag)
    {
      functions.put(word,tag);
    }

  /**
   * Declares the specified string as a relation identifier whose XML
   * tag is itself.
   */
  static final void relation (String word)
    {
      relations.put(word,word);
    }

  /**
   * Declares the specified string as a relation identifier, with
   * specified XML tag.
   */
  static final void relation (String word, String tag)
    {
      relations.put(word,tag);
    }

  /**
   * Declares the specified string as a reserved word.
   */
  static final void reserved (String word)
    {
      reserved.add(word);
    }

}
