/* **********************************************
 * Duale Hochschule Baden-Württemberg Karlsruhe
 * Prof. Dr. Jörn Eisenbiegler
 * 
 * Vorlesung Übersetzerbau
 * Praxis X Abstiegsparser
 * - Abstiegsparser
 * 
 * **********************************************
 */

package de.dhbw.compiler.xparser;

public class XParser {
	private final TokenReader in;

	public XParser(TokenReader in) {
		this.in = in;
	}

	public Tree parseProgram() {
		int oldPosition = in.getPosition();
		Tree program, id, semicolon, block, dot, eof;

		if ((program = parseToken(Token.PROGRAM)) != null
			&& ((id = parseToken(Token.ID)) != null)
			&& ((semicolon = parseToken(Token.SEMICOLON)) != null)
			&& ((block = parseBlock()) != null)
			&& ((dot = parseToken(Token.DOT)) != null)
			&& ((eof = parseToken(Token.EOF)) != null)) {
			Tree tree = new Tree(new Token(Token.APROGRAM));
			tree.addLastChild(program);
			tree.addLastChild(id);
			tree.addLastChild(semicolon);
			tree.addLastChild(block);
			tree.addLastChild(dot);
			tree.addLastChild(eof);

			return tree;
		}

		System.out.println("> Skipping Tree");
		in.setPosition(oldPosition);
		return null;
	}

	private Tree parseBlock() {
		int oldPosition = in.getPosition();

		Tree begin, statList, end;
		if ((begin = parseToken(Token.BEGIN)) != null
			&& (statList = parseStatList()) != null
			&& (end = parseToken(Token.END)) != null) {
			Tree tree = new Tree(new Token(Token.BLOCK));
			tree.addFirstChild(begin);
			tree.addLastChild(statList);
			tree.addLastChild(end);

			return tree;
		}

		System.out.println("parseBlock> Skipping Tree");
		in.setPosition(oldPosition);
		return null;
	}

	private Tree parseStatList() {
		// int oldPosition = in.getPosition();
		Tree tree = new Tree(new Token(Token.STATLIST));
		Tree stat;

		while ((stat = parseStatWithSemicolon()) != null) {
			tree.addLastChild(stat);
		}

//		if ((block = parseBlock()) != null) {
//			tree.addFirstChild(new Tree(new Token(Token.STATWITHSEMI)));
//			tree.addFirstChild(block);
//			tree.addLastChild(new Tree(new Token(Token.SEMICOLON)));
//
//			return tree;
//		}

		// in.setPosition(oldPosition);
		return tree;
	}

	private Tree parseStatWithSemicolon() {
		int oldPosition = in.getPosition();
		Tree tree = new Tree(new Token(Token.STATWITHSEMI));
		Tree state, semicolon;

		if (((state = parseStat()) != null)
			&& ((semicolon = parseToken(Token.SEMICOLON)) != null)) {
			tree.addLastChild(state);
			tree.addLastChild(semicolon);
			return tree;
		}

		in.setPosition(oldPosition);
		return null;
	}

	private Tree parseStat() {
		int oldPosition = in.getPosition();
		Tree tree = new Tree(new Token(Token.STAT));
		Tree numAssign, condStat, block;

		if ((numAssign = parseNumAssign()) != null) {
			tree.addLastChild(numAssign);
			return tree;
		}

		in.setPosition(oldPosition);
		if ((condStat = parseCondStat()) != null) {
			tree.addLastChild(condStat);
			return tree;
		}

		in.setPosition(oldPosition);
		if ((block = parseBlock()) != null) {
			tree.addLastChild(block);
			return tree;
		}

		in.setPosition(oldPosition);
		return null;
	}

	private Tree parseCondStat() {
		return null;
	}

	private Tree parseNumAssign() {
		int oldPosition = in.getPosition();
		Tree tree = new Tree(new Token(Token.ASSIGNSTAT));
		Tree id, assign, numExpr;

		if ((id = parseToken(Token.ID)) != null
			&& (assign = parseToken(Token.ASSIGN)) != null
			&& (numExpr = parseNumExpr0()) != null) {
			tree.addLastChild(id);
			tree.addLastChild(assign);
			tree.addLastChild(numExpr);
			return tree;
		}

		in.setPosition(oldPosition);
		return null;
	}

	private Tree parseNumExpr0() {
		int oldPosition = in.getPosition();
		Tree tree = new Tree(new Token(Token.EXPR));
		Tree intConst, a, operator, b;

		if ((intConst = parseNumExpr2()) != null) {
			tree.addLastChild(intConst);
			return tree;
		}
		in.setPosition(oldPosition);

		if (((a = parseNumExpr2()) != null)
				&& ((operator = parseToken(Token.MULT)) != null
				|| ((operator = parseToken(Token.DIV)) != null
				|| ((operator = parseToken(Token.PLUS)) != null
				|| ((operator = parseToken(Token.MINUS)) != null))))
			&& ((b = parseNumExpr()) != null)) {
			tree.addLastChild(a);
			tree.addLastChild(operator);
			tree.addLastChild(b);
			return tree;
		}

		in.setPosition(oldPosition);
		return null;
	}

	private Tree parseNumExpr() {
		int oldPosition = in.getPosition();
		Tree tree = new Tree(new Token(Token.EXPR));
		Tree id, intConst;

		if ((intConst = parseNumExpr2()) != null) {
			tree.addLastChild(intConst);
			return tree;
		}

		if ((id = parseToken(Token.ID)) != null) {
			tree.addLastChild(id);
			return tree;
		}

		in.setPosition(oldPosition);
		return null;
	}

	private Tree parseNumExpr2() {
		int oldPosition = in.getPosition();
		Tree tree = new Tree(new Token(Token.EXPR2));
		Tree id, intConst;

		if ((intConst = parseNumExpr3()) != null) {
			tree.addLastChild(intConst);
			return tree;
		}
		in.setPosition(oldPosition);

		if ((id = parseToken(Token.ID)) != null) {
			tree.addLastChild(id);
			return tree;
		}
		in.setPosition(oldPosition);
		return null;
	}

	private Tree parseNumExpr3() {
		int oldPosition = in.getPosition();
		Tree tree = new Tree(new Token(Token.EXPR3));
		Tree id, intConst;

		if ((intConst = parseToken(Token.INTCONST)) != null) {
			tree.addLastChild(intConst);
			return tree;
		}
		in.setPosition(oldPosition);

		if ((id = parseToken(Token.ID)) != null) {
			tree.addLastChild(id);
			return tree;
		}

		in.setPosition(oldPosition);
		return null;
	}

	private Tree parseToken(int tokenType) {
		int oldPosition = in.getPosition();
		Token nextToken = in.nextToken();
		System.out.println("parseToken> " + nextToken + " = " + Token.getTypeName(tokenType));
		if (nextToken.getType() == tokenType) {
			return new Tree(nextToken);
		} else {
			// Reset to last position as it needs to be re-read again
			in.setPosition(oldPosition);
			return null;
		}
	}

}
