package ca.uwaterloo.joos;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ca.uwaterloo.joos.scanner.Token;
import ca.uwaterloo.joos.scanner.Scanner.ScanException;

public class Preprocessor {

	private final Set<String> undesiredTokenKinds = new HashSet<String>();

	public Preprocessor() {
		this.undesiredTokenKinds.add("COMMENT");
		this.undesiredTokenKinds.add("BLOCKCOMMENT");
		this.undesiredTokenKinds.add("JAVADOC");
	}

	public List<Token> processTokens(List<Token> rawTokens) throws ScanException {
		List<Token> tokens = new ArrayList<Token>();

		for(Token token: rawTokens) {
			if(!this.undesiredTokenKinds.contains(token.getKind())) tokens.add(token);
			if(token.getKind().equals("INTLIT"))
			{
				checkIntRange(token.getLexeme());
			}
		}

		tokens.add(0, new Token("BOF","BOF"));
		tokens.add(new Token("EOF","EOF"));
		return tokens;
	}
	
	private static void checkIntRange(String intString)throws ScanException{
		String intergerThreshold = "2147483647";
		
		if(intString.length() == intergerThreshold.length())
		{
		   for(int i = 0;i<intergerThreshold.length();i++)
		   {
			   if((int)intString.charAt(i)>(int)intergerThreshold.charAt(i)){
				  
					throw new ScanException("Interger out of Range");
			   }
		   }
		}
		if(intString.length() > intergerThreshold.length())
		{
			throw new ScanException("Interger out of Range");	
		}
	}
}

@@ -614,15 +614,52 @@
 ();%0A
-%0A
 %09%09for(
+int i = 0;i%3CrawTokens.size();i++)%7B%0A%09%09%09
 Toke
@@ -669,9 +669,10 @@
 oken
-:
+ =
  raw
@@ -677,19 +677,28 @@
 awTokens
-) %7B
+.get(i);%0A%09%09%09
 %0A%09%09%09if(!
@@ -816,46 +816,506 @@
 %09%09%09%7B
+ 
 %0A%09%09%09%09
-checkIntRange(token.getLexeme());
+String intPositiveThreshold = %222147483647%22;%0A%09%09%09%09String intNegThreshold = %222147483648%22;%0A%09%09%09%09Set%3CString%3E expressionTokenKinds = new HashSet%3CString%3E();%0A%09%09%09%09expressionTokenKinds.add(%22INTLIT%22);%0A%09%09%09%09expressionTokenKinds.add(%22RPAREN%22);%0A%09%09%09%09if(rawTokens.get(i-1).getKind().equals(%22MINUS%22)&&!expressionTokenKinds.contains(rawTokens.get(i-2).getKind()))%0A%09%09%09%09%7B%0A%09%09%09%09%09checkIntRange(token.getLexeme(),intNegThreshold);%0A%09%09%09%09%7D%0A%09%09%09%09else%0A%09%09%09   %7B%0A%09%09%09%09checkIntRange(token.getLexeme(),intPositiveThreshold);%0A%09%09%09%09%7D
 %0A%09%09%09
@@ -1473,16 +1473,41 @@
 ntString
+,String intergerThreshold
 )throws 
@@ -1527,48 +1527,8 @@
 %7B%0A%09%09
-String intergerThreshold = %222147483647%22;
 %0A%09%09%0A
@@ -1853,16 +1853,18 @@
 h())%0A%09%09%7B
+  
 %0A%09%09%09thro

modify integer range check