package v6.apps.zelvak;

import java.io.IOException;
import java.io.Reader;

public final class Parser {

	public static Application parse(Reader in) throws WTFException, IOException {
		State state = new InitialState();
		int c;
		int pos = 0;
		while( (c = in.read()) != -1){
			//System.out.print("("+(char)c+")");
			//System.out.println(state.getClass().getName());
			try{
				state = state.process((char)c);
			}catch (WTFException e) {
				e.printStackTrace();
				throw new WTFException(pos+ " " + state.getClass().getName()+": "+e.toString());
			}
			pos++;
		}
		return state.getApplication();
	}

	
	
}
