/*
 * Transition.java  
 *
 * Copyright 2001
 * College of Computer Science
 * Northeastern University
 * Boston, MA  02115
 *
 * This software may be used for educational purposes as long as
 * this copyright notice is retained intact at the top of all files.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either in whole
 * or in part without explicit permission.
 *
 * Contact information:
 *   Richard Rasala    rasala@ccs.neu.edu
 *   Viera Proulx      vkp@ccs.neu.edu
 *   Jeff Raab         jmr@ccs.neu.edu
 *   Jennifer McDonald jenimac@ccs.neu.edu
 * 
 * Telephone:          617-373-2462
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

import edu.neu.ccs.*;
import edu.neu.ccs.codec.*;
import java.text.ParseException;

//Defines a State

public class Transition implements Stringable{

	// Character to move on
	private Symbol token;
	
	// Character needed to pop
	private Symbol search;
	
	// Character to replace the popped one with
	private Symbol replace;
		
	// constructors
	public Transition() {
		token = new Symbol();
		search = new Symbol();
		replace = new Symbol();
	}
	
	public Transition(String data) 
					 throws ParseException {
		fromStringData(data);
	}	
	
	public void fromStringData(String data) 
		throws ParseException{
	
		token = new Symbol(data.charAt(0));
		search = new Symbol(data.charAt(2));
		replace = new Symbol(data.charAt(4));
	}

	public String toStringData() {
		
        return token.value() + ":" +
        	   search.value() + "/" +
        	   replace.value();
   	}
	
	public boolean matches(Symbol t, Symbol s) {
	
		if ((token.is(t) || 
			 token.is(Symbol.epsilon())) && 
			 (search.is(s) || search.is(Symbol.epsilon())))
			 
			return true;
		
		return false;
	}
	
	public Symbol getTokenSymbol() {
		return token;
	}
	
	public Symbol getSearchSymbol() {
		return search;
	}
	
	public Symbol getReplaceSymbol() {
		return replace;
	}
	
	public StackClass.Action getStackAction() {
	
		// transition can either be a pop
		// a push
		// or a nop
	
		if (search.is(Symbol.epsilon())) {
			if (replace.is(Symbol.epsilon()))
				return StackClass.nopAction();
			else
				return StackClass.pushAction(replace);
		}
		else
			return StackClass.popAction();
			
	}
	
	public String getActionString() {
		return getStackAction().getActionString();
	}
}

