package ca.uwaterloo.joos.ast.expr;

import java.util.ArrayList;
import java.util.List;

import ca.uwaterloo.joos.ast.ASTNode;
import ca.uwaterloo.joos.parser.ParseTree.LeafNode;
import ca.uwaterloo.joos.parser.ParseTree.Node;
import ca.uwaterloo.joos.parser.ParseTree.TreeNode;
import ca.uwaterloo.joos.parser.ParseTreeTraverse;
import ca.uwaterloo.joos.parser.ParseTreeTraverse.Traverser;

public abstract class Primary extends ASTNode {

	public Primary(Node node, ASTNode parent) throws Exception {
		super(node, parent);
	}
	
	private static class PrimaryTraverser implements Traverser {
		public ASTNode node;
		private ASTNode parent;
		
		public PrimaryTraverser(ASTNode parent) {
			this.parent = parent;
		}

		@Override
		public List<Node> processTreeNode(TreeNode treeNode) throws Exception {
			if (treeNode.productionRule.getLefthand().equals("literal")) {
				node = new LiteralPrimary(treeNode, parent);
			} else if (treeNode.productionRule.getLefthand().equals("expr")) {
				node = new Expression(treeNode, parent);
			} else {
				List<Node> offers = new ArrayList<Node>();
				offers.addAll(treeNode.children);
				return offers;
			}
			return null;
		}

		@Override
		public void processLeafNode(LeafNode leafNode) throws Exception {
			if (leafNode.token.getKind().equals("THIS")) {
				node = new ThisPrimary(leafNode, parent);
			} 
		}
	}
	
	public static ASTNode newPrimary(Node node, ASTNode parent) throws Exception {
		
		PrimaryTraverser traverser = new PrimaryTraverser(parent);
		ParseTreeTraverse traverse = new ParseTreeTraverse(traverser);
		traverse.traverse(node);
		
		return traverser.node;
	}

}

@@ -82,16 +82,72 @@
 .List;%0A%0A
+import ca.uwaterloo.joos.ast.AST.ASTConstructException;%0A
 import c
@@ -172,24 +172,24 @@
 st.ASTNode;%0A
-
 import ca.uw
@@ -1076,32 +1076,795 @@
 eNode, parent);%0A
+%09%09%09%7D else if (treeNode.productionRule.getLefthand().equals(%22arraycreate%22)) %7B%0A//%09%09%09%09node = new ArrayCreate(treeNode, parent);%0A%09%09%09%7D else if (treeNode.productionRule.getLefthand().equals(%22arrayaccess%22)) %7B%0A//%09%09%09%09node = new ArrayAccess(treeNode, parent);%0A%09%09%09%09// TODO part of lefthand%0A%09%09%09%7D else if (treeNode.productionRule.getLefthand().equals(%22classcreateexpr%22)) %7B%0A//%09%09%09%09node = new ClassCreateExpr(treeNode, parent);%0A%09%09%09%09// TODO part of statement expr%0A%09%09%09%7D else if (treeNode.productionRule.getLefthand().equals(%22fieldaccess%22)) %7B%0A//%09%09%09%09node = new FieldAccess(treeNode, parent);%0A%09%09%09%09// TODO part of lefthand%0A%09%09%09%7D else if (treeNode.productionRule.getLefthand().equals(%22methodinvoke%22)) %7B%0A//%09%09%09%09node = new MethodInvoke(treeNode, parent);%0A%09%09%09%09// TODO part of statement expr%0A
 %09%09%09%7D else %7B%0A%09%09%09%09
@@ -2389,16 +2389,16 @@
 erser);%0A
-
 %09%09traver
@@ -2419,16 +2419,138 @@
 de);%0A%09%09%0A
+%09%09if (traverser.node == null) %7B%0A%09%09%09throw new ASTConstructException(node + %22 is not / does not contain a primary%22);%0A%09%09%7D%0A%09%09%0A
 %09%09return

Add place holder in Primary