#!/usr/bin/perl

local $class = $ARGV[0];	# The name of the class.
local $printed = $ARGV[1];	# The printed name of tokens.
local $java = "$class.java";
local $parameterized;
local $superclass;
if ($#ARGV == 2) {
  $parameterized = 1;
  $superclass = "ParameterizedToken";
}
else {
  $parameterized = 0;
  $superclass = "Token";
}

# Wrong number of parameters: Crash and burn
if (($#ARGV < 1) || ($#ARGV > 2)) {
  print stderr "Usage: maketoken Class Printable (parameterized)\n";
  exit 1;
}

if (-f $java) {
  print stderr "$java already exists.\n";
  exit 2;
}


print "Class: $class\n";
print "Printable: $printed\n";
print "Number of arguments: $#ARGV\n";

open(JAVA, ">$java");

# Print the import statement(s)
print JAVA "import Token;\n";
if ($parameterized) {
  print JAVA "import ParameterizedToken;\n";
}

# Print the common introductory stuff.
print JAVA<<"COMMON_ONE";
/**
 * Tokens intended to represent $printed.
 * Make sure to create these tokens with
 *   $class.build(String)
 * rather than
 *   new $class(...)
 *
 * This file was generated automatically by maketoken.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of February 2001.
 */
public class $class
  extends superclass
{
COMMON_ONE

# Non-parameterized tokens have an extra field.

# We only have constructors for parameterized tokens.
if ($parameterized) {
  print JAVA<<"PARAM_CONSTRUCTOR";
  // +--------------+--------------------------------------------
  // | Constructors |
  // +--------------+

  /** 
   * Create a new parameterized token with a particular value.
   */ 
  public $class(String val) {
    super(val);
  } // $class(String)
PARAM_CONSTRUCTOR
}

  // +----------------+------------------------------------------
  // | Static Methods |
  // +----------------+

  /**
   * Build a new token.
   */
  public static Token build(String val) {
    return new ParameterizedToken(val);
  } // build(String)

  // +---------+-------------------------------------------------
  // | Methods |
  // +---------+

  /** 
   * Convert to a string for easy printing.
   */
  public String toString() {
    return "PARAMETERIZED(" + this.value + ")";
  } // toString()
  
  /**
   * Determine if this token equals another object.  That
   * only happens if both are ParameterizedTokens and they
   * have the same value.
   *
   * Warning!  Subclasses should probably override this method.
   * Otherwise, two different tokens with the same parameter
   * will be treated as equal.
   */
  public boolean equals(Object other) {
    return ( (other instanceof ParameterizedToken) 
             && (this.equals((ParameterizedToken) other)) );
  } // equals(Object)
 
  /**
   * Determine if this token equals another parameterized token.
   * That only happens if they have the same associated value.
   *
   * While subclasses may not need to override this method, they
   * may want to provide a more restricted version.
   */
  public boolean equals(ParameterizedToken other) {
    return this.value.equals(other.value);
  } // equals(ParameterizedToken)

} // class ParameterizedToken
close(JAVA);

