//
import java.util.*; // defines StringTokenizer
public class Script
{
public final static int CAMERA = -1;
public final static int EVERYONE = -2;
public final static int NEAREST = -3;
public final static int NOTHING = -4;
public final static int NARRATOR = -5;
static int nActions;
static Action actions[];
static StringTokenizer st;
static Action action() { return actions[nActions-1]; }
public static Action[] parse(String script) {
script = removeComments(script);
actions = new Action[1000];
nActions = 0;
st = new StringTokenizer(script);
while (st.hasMoreTokens()) {
String t = st.nextToken();
if (t.equals("actor")) { add(Action.ACTOR) ; vector(); }
else if (t.equals("begin")) { add(Action.BEGIN) ; no_arg(); }
else if (t.equals("cameraTo")) { add(Action.CAMERA_TO) ; vector(); }
else if (t.equals("end")) { add(Action.END) ; no_arg(); }
else if (t.equals("everyone")) { add(Action.ACTOR) ; vector(EVERYONE); }
else if (t.equals("faces")) { add(Action.FACE) ; vector(); }
else if (t.equals("leftArm")) { add(Action.LEFT_ARM) ; vector(); }
else if (t.equals("looksAt")) { add(Action.LOOK_AT) ; vector(); }
else if (t.equals("movesBy")) { add(Action.MOVE_BY) ; vector(); }
else if (t.equals("movesTo")) { add(Action.MOVE_TO) ; vector(); }
else if (t.equals("narrator")) { add(Action.ACTOR) ; vector(NARRATOR); }
else if (t.equals("pauses")) { add(Action.PAUSE) ; vector(); }
else if (t.equals("rightArm")) { add(Action.RIGHT_ARM) ; vector(); }
else if (t.equals("says")) { add(Action.SAY) ; string(); }
else if (t.equals("then")) { add(Action.SYNC) ; no_arg(); }
else if (t.equals("thinks")) { add(Action.THINK) ; string(); }
}
add(Action.SYNC);
return actions;
}
static String removeComments(String str) {
String s = "";
for (int i = 0 ; i < str.length() ; i++)
if (! pattern2(str, i, '/', '*'))
s += (char)str.charAt(i);
else
for (i++ ; i < str.length() ; i++)
if (pattern2(str, i-1, '*', '/'))
break;
return s;
}
static boolean pattern2(String str, int i, char a, char b) {
return i < str.length()-1 && str.charAt(i) == a && str.charAt(i+1) == b;
}
static Action add(int type) {
return actions[nActions++] = new Action(type);
}
static void no_arg() {
action().argType = Action.HAS_NO_ARG;
}
static void vector(double a) {
action().argType = Action.VECTOR_ARG;
action().s = "" + a;
}
static void vector() {
action().argType = Action.VECTOR_ARG;
action().s = st.nextToken();
}
public static double number(String t) {
if (t.equals("camera" )) return CAMERA;
if (t.equals("nothing")) return NOTHING;
if (t.equals("nearest")) return NEAREST;
return (new Double(t)).doubleValue();
}
static void string() {
action().argType = Action.STRING_ARG;
String str = "";
for (int i = 0 ; st.hasMoreTokens() ; i++) {
String t = st.nextToken();
if (i==0)
if (t.equals("nothing"))
break;
else if (! t.substring(0,2).equals("``"))
System.err.println("string not starting with '``' at " + t);
str += i==0 ? t.substring(2,t.length()) : " " + t;
int len = str.length();
if (len >= 2 && str.substring(len-2,len).equals("''")) {
str = str.substring(0, str.length()-2);
break;
}
}
action().s = str;
}
}