@Override
	public ASTNode parse(Token token) throws Exception {
	    Token startingToken = token;
		token = nextToken();  // consume the SWITCH

		Expr conditionNode = null;
		Stmt elseStmt = null;
		List<Pair<Expr, Stmt>> whenStmts = new ArrayList<Pair<Expr,Stmt>>();

		// if { or WHEN, put in a TRUE
		LeolaTokenType type = token.getType();
		if ( SWITCH_SET.contains(type) ) {
			conditionNode = new BooleanExpr(true);
		}
		else {
	        // Parse the expression.
	        // The CASE node adopts the expression subtree as its first child.
	        conditionNode = parseExpr(token);
		}

        boolean hasOpeningBrace = false;

        // Synchronize at the { or WHEN.
        token = expectedTokens(SWITCH_SET);
        type = token.getType();

        if ( type.equals(WHEN) ) {
            token = nextToken(); // eat the token ({ or WHEN)

            Pair<Expr, Stmt> whenExpr = parseWhen(token);
            whenStmts.add(whenExpr);

            eatOptionalStmtEnd(currentToken());

            token = currentToken();

            if ( token.getType().equals(ELSE) ) {
                token = nextToken(); // eat ELSE
                elseStmt = parseStmt(token);
            }

        }
        else if ( type.equals(LEFT_BRACE) ) {
            hasOpeningBrace = true;

            token = nextToken(); // eat the token {
            type = token.getType();

            while( type.equals(WHEN)) {
                token = nextToken(); // eat the token WHEN

                Pair<Expr, Stmt> whenExpr = parseWhen(token);
                whenStmts.add(whenExpr);

                eatOptionalStmtEnd(currentToken());
                type = currentToken().getType();

            }

            token = currentToken();

            if ( token.getType().equals(ELSE) ) {
                token = nextToken(); // eat ELSE
                elseStmt = parseStmt(token);
            }

        }
        else {
            throwParseError(token, LeolaErrorCode.MISSING_LEFT_BRACE);
        }

        token = currentToken();
        if ( token.getType().equals(RIGHT_BRACE) && hasOpeningBrace) {
            nextToken();
        }
        else if ( hasOpeningBrace ) {
            throwParseError(token, LeolaErrorCode.MISSING_RIGHT_BRACE);
        }

        SwitchStmt switchStmt = new SwitchStmt(conditionNode, whenStmts, elseStmt);
        setLineNumber(switchStmt, startingToken);

        return switchStmt;
	}