// FILE. . . . . /home/hak/ilt/src/ilog/rif/Lexicon.java
// EDIT BY . . . Hassan Ait-Kaci
// ON MACHINE. . 4j4zn71
// STARTED ON. . Thu Apr 03 13:44:34 2008

package ilog.rif.bld;

/**
 * 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>.
 *
 * @version     Last modified on Wed Oct 29 11:36:10 2008 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.HashSet;

class Lexicon
{

  private final static boolean _isLetterOrUnderscore (char c)
    {
      return c == '_' || Character.isLetter(c);
    }

  private final static boolean _isWordChar (char c)
    {
      return (c >= '0' && c <= '9') || _isLetterOrUnderscore(c);
    }

  /**
   * Returns <tt>true</tt> iff all the characters in the string suffix
   * starting at position <tt>p</tt> in the specified string <tt>s</tt>
   * are word characters.
   */
  private final static boolean _isAllWordChars (String s, int p)
    {
      for (int i=p; i<s.length(); i++)
	if (!_isWordChar(s.charAt(i)))
	  return false;

      return true;
    }
    
  /**
   * Returns <tt>true</tt> iff the specified string starts with a letter
   * or undersscore, and every other character is a word character.
   */
  public final static boolean isIdentifier (String s)
    {
      return _isLetterOrUnderscore(s.charAt(0)) && _isAllWordChars(s,1);
    }       

  /**
   * Returns <tt>true</tt> iff the specified string starts with a
   * question mark, and every other character is a word character.
   */
  public final static boolean isVariable (String s)
    {
      if (s.charAt(0) != '?')
        return false;

      return _isAllWordChars(s,1);
    }       

  /**
   * Returns <tt>true</tt> iff the specified string starts with an
   * underscore character, and every other character is a word character.
   */
  public final static boolean isLocalName (String s)
    {
      if (s.charAt(0) != '_')
        return false;

      return _isAllWordChars(s,1);
    }       

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

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

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

}
