import java.util.*;
import java.io.*;

public class Cfunction {
	boolean valid;
	boolean needSpace;
	String type;
	String name;
	Vector args = new Vector();
	Cfunction(Lex lex) {
		String[] upfront = new String[10];
		int index = 0;
		valid = true;
		while(lex.isSymbol() || (lex.getTokenType() == Lex.ASTERISK)) {
			if(lex.getToken().equals("static"))
				valid = false;
			upfront[index++] = lex.getToken();
			lex.gtkn();
		}
		if(index == 0) 
			valid = false;
		if(lex.getTokenType() != Lex.LEFTPAREN)
			valid = false;
		if(!valid)
			return;

		lex.gtkn();
		name = upfront[--index];
		needSpace = false;
		type = "";
		for(int i=0; i<index; i++) {
			if(needSpace)
				type += " " + upfront[i];
			else
				type += upfront[i];
			if(upfront[i].equals("*"))
				needSpace = false;
			else
				needSpace = true;
		}
		String arg = "";
		needSpace = false;
		while((lex.getTokenType() != Lex.RIGHTPAREN) && 
				(lex.getTokenType() != Lex.EOF)) {
			if(needSpace)
				arg += " " + lex.getToken();
			else
				arg += lex.getToken();
			needSpace = true;
			if(lex.getTokenType() == Lex.ASTERISK)
				needSpace = false;
			if(lex.gtkn() == Lex.COMMA) {
				needSpace = false;
				args.addElement(arg);
				arg = "";
				lex.gtkn();
			}
		}
		if(arg.length() > 1)
			args.addElement(arg);
		if(valid) {
			if(lex.getTokenType() == Lex.RIGHTPAREN) {
				if(lex.gtkn() == Lex.LEFTBRACE) {
					return;
				}
			}
		}
		valid = false;
	}
	public void dumpNoParmsNoType(PrintWriter out) {
		dump(out,true,true);
	}
	public void dumpNoParms(PrintWriter out) {
		dump(out,false,true);
	}
	public void dumpNoType(PrintWriter out) {
		dump(out,true,false);
	}
	public void dump(PrintWriter out) {
		dump(out,false,false);
	}
	public void dump(PrintWriter out,boolean noType,boolean noParms) {
		String str;
		if(noType)
			str = name + "(";
		else if(type.endsWith("*"))
			str = type + name + "(";
		else
			str = type + " " + name + "(";
		if(noParms) {
			out.println(str + ")");
			return;
		}
		for(int i=0; i<args.size(); i++) {
			String arg = (String)args.elementAt(i);
			if((str.length() + arg.length()) > 60) {
				if(i > 0)
					out.println(str + ",");
				else
					out.println(str);
				str = "    " + arg;
			} else {
				if(i > 0)
					str += ", " + arg;
				else
					str += arg;
			}
		}
		out.println(str + ")");
	}
	public boolean isValid() {
		return(valid);
	}
	public int compare(Cfunction that) {
		return(this.name.compareTo(that.name));
	}
	String[] getKeyList() {
		StringTokenizer st = new StringTokenizer(name,"_ \t\r\n");
		String[] list = new String[st.countTokens()];
		for(int i=0; st.countTokens() > 0; i++)
			list[i] = st.nextToken();
		return(list);
	}
	String getReturnType() {
		return(type);
	}
	public String getName() {
		return(name);
	}
}
