Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] Applied[HEAD]

CORE
        Updated ScannerException to be more precise and include more 
information. 
        Updated Parser to be more careful of how it handles particular 
Scanner errors in COMPLETE_PARSE mode. 
 
TESTS
        Updated ScannerTestCase to keep up to date wrt ScannerException 
updates. 


JohnC

Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core.tests/ChangeLog,v
retrieving revision 1.85
diff -u -r1.85 ChangeLog
--- ChangeLog	9 Sep 2003 15:46:44 -0000	1.85
+++ ChangeLog	9 Sep 2003 18:01:51 -0000
@@ -1,3 +1,6 @@
+2003-09-09 John Camelon
+	Updated ScannerTestCase to keep up to date wrt ScannerException updates. 
+
 2003-09-09 Andrew Niefer
 	Modified resources/search/classDecl.cpp
 	 - to include more function declarations to test parameter matching
Index: parser/org/eclipse/cdt/core/parser/tests/ScannerTestCase.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ScannerTestCase.java,v
retrieving revision 1.6
diff -u -r1.6 ScannerTestCase.java
--- parser/org/eclipse/cdt/core/parser/tests/ScannerTestCase.java	28 Jul 2003 00:02:32 -0000	1.6
+++ parser/org/eclipse/cdt/core/parser/tests/ScannerTestCase.java	9 Sep 2003 18:01:54 -0000
@@ -1270,33 +1270,33 @@
 		try{
 			validateEOF();
 		} catch ( ScannerException e ){
-			assertTrue( e.getMessage().equals( "Unbounded string" ));
+			assertTrue( e.getErrorCode() == ScannerException.ErrorCode.UNBOUNDED_STRING); 
 		}
 	
 		initializeScanner( "#include <foo.h" );
 		try{
 			validateEOF();
 		} catch ( ScannerException e ){
-			assertTrue( e.getMessage().equals( "Ill-formed #include: reached end of line before >" ));
+			assertTrue( e.getErrorCode() == ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE);
 		}		
 		initializeScanner( "#define FOO(A" );
 		try{
 			validateEOF();
 		} catch( ScannerException e ){
-			assertTrue( e.getMessage().equals( "Unexpected newline in macro formal parameter list."));
+			assertTrue( e.getErrorCode() == ScannerException.ErrorCode.MALFORMED_MACRO_DEFN );
 		}
 		initializeScanner( "#define FOO(A \\ B" );
 		try{
 			validateEOF();
 		} catch( ScannerException e ){
-			assertTrue( e.getMessage().equals( "Unexpected '\\' in macro formal parameter list."));
+			assertTrue( e.getErrorCode() == ScannerException.ErrorCode.MALFORMED_MACRO_DEFN );
 		}
 		
 		initializeScanner( "#define FOO(A,\\\nB) 1\n FOO(foo" );
 		try{
 			validateInteger("1");
 		} catch( ScannerException e ){
-			assertTrue( e.getMessage().equals( "Improper use of macro FOO" ) );
+			assertTrue( e.getErrorCode() == ScannerException.ErrorCode.MACRO_USAGE_ERROR );
 		}
 	}
 	
Index: parser/ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/ChangeLog,v
retrieving revision 1.117
diff -u -r1.117 ChangeLog
--- parser/ChangeLog	9 Sep 2003 17:53:51 -0000	1.117
+++ parser/ChangeLog	9 Sep 2003 18:00:29 -0000
@@ -1,3 +1,7 @@
+2003-09-09 John Camelon
+	Updated ScannerException to be more precise and include more information.  
+	Updated Parser to be more careful of how it handles particular Scanner errors in COMPLETE_PARSE mode.  
+
 2003-09-08 Bogdan Gheorghe
 	Added ScannerExceptions in Preprocessor.java to PDE Error 
 	Log
Index: parser/org/eclipse/cdt/core/parser/Enum.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/Enum.java,v
retrieving revision 1.1
diff -u -r1.1 Enum.java
--- parser/org/eclipse/cdt/core/parser/Enum.java	27 Jun 2003 00:20:07 -0000	1.1
+++ parser/org/eclipse/cdt/core/parser/Enum.java	9 Sep 2003 18:00:29 -0000
@@ -21,5 +21,13 @@
 		this.enumValue = enumValue;
 	}
 	
-	private int enumValue;
+	private final int enumValue;
+    /**
+     * @return
+     */
+    public int getEnumValue()
+    {
+        return enumValue;
+    }
+
 }
Index: parser/org/eclipse/cdt/core/parser/ScannerException.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/ScannerException.java,v
retrieving revision 1.1
diff -u -r1.1 ScannerException.java
--- parser/org/eclipse/cdt/core/parser/ScannerException.java	13 Jun 2003 20:03:14 -0000	1.1
+++ parser/org/eclipse/cdt/core/parser/ScannerException.java	9 Sep 2003 18:00:30 -0000
@@ -10,21 +10,163 @@
  ******************************************************************************/
 package org.eclipse.cdt.core.parser;
 
+import java.util.HashMap;
+import java.util.Map;
+
 public class ScannerException extends Exception {
 
-	/**
-	 * Constructor for ScannerException.
-	 */
-	public ScannerException() {
-		super();
+	private final String info;
+    private final String fileName;
+    private final int offset;
+    private final static int OFFSET_NOT_PROVIDED = -1;
+    private final ErrorCode code;
+
+    public static class ErrorCode extends Enum  
+	{        
+        public static final ErrorCode POUND_ERROR = new ErrorCode( 0 );
+        public static final ErrorCode INCLUSION_NOT_FOUND = new ErrorCode( 1 );
+		public static final ErrorCode DEFINITION_NOT_FOUND = new ErrorCode( 2 );
+		public static final ErrorCode UNBALANCED_CONDITIONALS = new ErrorCode( 3 ); 
+		public static final ErrorCode MALFORMED_MACRO_DEFN = new ErrorCode( 4 );
+		public static final ErrorCode UNBOUNDED_STRING = new ErrorCode( 5 );
+		public static final ErrorCode BAD_FLOATING_POINT = new ErrorCode( 6 );
+		public static final ErrorCode BAD_HEXIDECIMAL_FORMAT = new ErrorCode( 7 );
+		public static final ErrorCode INVALID_PREPROCESSOR_DIRECTIVE = new ErrorCode( 8 );
+		public static final ErrorCode ATTEMPTED_REDEFINITION = new ErrorCode( 9 );
+		public static final ErrorCode UNDEF_DEFINITION_NOT_FOUND = new ErrorCode( 10 );
+		public static final ErrorCode INVALID_ESCAPE_CHARACTER_SEQUENCE = new ErrorCode( 11 );
+		public static final ErrorCode EXPRESSION_EVALUATION_ERROR = new ErrorCode( 12 );
+		public static final ErrorCode UNEXPECTED_EOF = new ErrorCode(13);
+		public static final ErrorCode MACRO_USAGE_ERROR = new ErrorCode( 14 );
+		public static final ErrorCode MACRO_PASTING_ERROR = new ErrorCode( 15 );
+		public static final ErrorCode CIRCULAR_INCLUSION = new ErrorCode( 16 );
+        /**
+         * @param enumValue
+         */
+        protected ErrorCode(int enumValue)
+        {
+            super(enumValue);
+        }
+        
+        public boolean hasInfo()
+        {
+        	if( this == ErrorCode.UNBALANCED_CONDITIONALS ||  
+				this == ErrorCode.UNBOUNDED_STRING  ||
+				this == ErrorCode.BAD_FLOATING_POINT ||
+				this == ErrorCode.BAD_HEXIDECIMAL_FORMAT ||
+				this == ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE ||
+				this == ErrorCode.UNEXPECTED_EOF || 
+				this == ErrorCode.MACRO_PASTING_ERROR )
+					return false; 
+			return true;
+        }
+        
+        public boolean hasOffsetInfo()
+        {
+        	if( this == INCLUSION_NOT_FOUND || this == POUND_ERROR  )
+        		return false;
+        	return true;
+        }
+
+        /**
+         * @param mode
+         * @return
+         */
+        public boolean isSeriousError(ParserMode mode)
+        {
+        	if( mode == ParserMode.COMPLETE_PARSE )
+				if( this == ErrorCode.POUND_ERROR ||
+					this == ErrorCode.DEFINITION_NOT_FOUND ||
+					this == ErrorCode.UNBALANCED_CONDITIONALS ||		  
+					this == ErrorCode.MALFORMED_MACRO_DEFN ||
+					this == ErrorCode.UNEXPECTED_EOF  ||
+					this == ErrorCode.MACRO_USAGE_ERROR  ||
+					this == ErrorCode.MACRO_PASTING_ERROR ||
+					this == ErrorCode.UNDEF_DEFINITION_NOT_FOUND || 
+					this == ErrorCode.EXPRESSION_EVALUATION_ERROR ||
+					this == ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE || 
+					this == ErrorCode.ATTEMPTED_REDEFINITION )
+						return true;
+			return false;
+        }
+	}
+
+	public ScannerException( ErrorCode code )
+	{
+		this( code, "", "UNKNOWN", OFFSET_NOT_PROVIDED );
+	}
+
+	public ScannerException( ErrorCode code, String info )
+	{
+		this( code, info, "UNKNOWN", OFFSET_NOT_PROVIDED );
 	}
 
-	/**
-	 * Constructor for ScannerException.
-	 * @param s
-	 */
-	public ScannerException(String s) {
-		super(s);
+	public ScannerException( ErrorCode code, String fileName, int offset )
+	{
+		this( code, "", fileName, offset );
+	}
+	
+	static Map errorMessages = new HashMap();
+	
+	static {
+		errorMessages.put( ErrorCode.POUND_ERROR, "#error " ); 
+		errorMessages.put( ErrorCode.INCLUSION_NOT_FOUND, "Inclusion not found: " ); 
+		errorMessages.put( ErrorCode.DEFINITION_NOT_FOUND, "Definition not found: " ); 
+		errorMessages.put( ErrorCode.MALFORMED_MACRO_DEFN, "Macro definition malformed: " ); 
+		errorMessages.put( ErrorCode.ATTEMPTED_REDEFINITION, "" );
+		errorMessages.put( ErrorCode.UNDEF_DEFINITION_NOT_FOUND, "" );
+		errorMessages.put( ErrorCode.INVALID_ESCAPE_CHARACTER_SEQUENCE, "" );
+		errorMessages.put( ErrorCode.EXPRESSION_EVALUATION_ERROR, "" );
+		errorMessages.put( ErrorCode.MACRO_USAGE_ERROR, "" );
+		errorMessages.put( ErrorCode.CIRCULAR_INCLUSION, "" );
+		
+		errorMessages.put( ErrorCode.UNBALANCED_CONDITIONALS , "Conditionals unbalanced " ); 
+		errorMessages.put( ErrorCode.UNBOUNDED_STRING, "Unbounded string " ); 
+		errorMessages.put( ErrorCode.BAD_FLOATING_POINT, "Invalid floating point format " );
+		errorMessages.put( ErrorCode.BAD_HEXIDECIMAL_FORMAT, "Invalid hexidecimal format " );
+		errorMessages.put( ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, "Invalid preprocessor directive format " );
+		errorMessages.put( ErrorCode.UNEXPECTED_EOF, "Unexpected End Of File " );		
+		errorMessages.put( ErrorCode.MACRO_PASTING_ERROR, "Invalid use of macro pasting " );		
 	}
+	
+	
+	public ScannerException( ErrorCode code, String info, String fileName, int offset )
+	{
+		this.code = code;
+		this.info = info; 
+		this.fileName = fileName; 
+		this.offset = offset;
+	}	
 
+    /**
+     * @return
+     */
+    public ErrorCode getErrorCode()
+    {
+        return code;
+    }
+    
+    public String getMessage()
+    {
+ 		StringBuffer buff = new StringBuffer(); 
+ 		String errorMessage = (String)errorMessages.get( getErrorCode() );
+ 		
+ 		if( errorMessage == null ) return "";
+ 		buff.append( errorMessage );
+ 		if( getErrorCode().hasInfo() )
+ 			buff.append( info ); 
+ 		if( getErrorCode().hasOffsetInfo() )
+ 		{
+ 			buff.append( "from file: ");
+ 			buff.append( fileName );
+ 			buff.append( " offset @ ");
+ 			buff.append( offset );
+ 		}
+ 		return buff.toString();	
+    }
+    
+    public boolean isSeriousError( ParserMode mode )
+    {
+    	return getErrorCode().isSeriousError( mode );
+    }
 }
Index: parser/org/eclipse/cdt/internal/core/parser/BranchTracker.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/BranchTracker.java,v
retrieving revision 1.3
diff -u -r1.3 BranchTracker.java
--- parser/org/eclipse/cdt/internal/core/parser/BranchTracker.java	13 Jun 2003 20:03:14 -0000	1.3
+++ parser/org/eclipse/cdt/internal/core/parser/BranchTracker.java	9 Sep 2003 18:00:31 -0000
@@ -85,7 +85,7 @@
 		}
 		catch( EmptyStackException ese )
 		{
-			throw new ScannerException( "#elif without a #if "); 
+			throw new ScannerException( ScannerException.ErrorCode.UNBALANCED_CONDITIONALS ); 
 		}
 		
 		if( ignore == IGNORE_SENTINEL )
@@ -135,7 +135,7 @@
 		}
 		catch( EmptyStackException ese )
 		{
-			throw new ScannerException( "#else without a #if "); 
+			throw new ScannerException( ScannerException.ErrorCode.UNBALANCED_CONDITIONALS );
 		}
 		
 		if( ignore == IGNORE_SENTINEL )
Index: parser/org/eclipse/cdt/internal/core/parser/ContextStack.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ContextStack.java,v
retrieving revision 1.13
diff -u -r1.13 ContextStack.java
--- parser/org/eclipse/cdt/internal/core/parser/ContextStack.java	26 Aug 2003 19:15:58 -0000	1.13
+++ parser/org/eclipse/cdt/internal/core/parser/ContextStack.java	9 Sep 2003 18:00:31 -0000
@@ -67,13 +67,13 @@
 		if( context.getKind() == IScannerContext.INCLUSION )
 		{
 			if( !inclusions.add( context.getFilename() ) )
-				throw new ScannerException( "Inclusion " + context.getFilename() + " already encountered." );
+				throw new ScannerException( ScannerException.ErrorCode.CIRCULAR_INCLUSION,  context.getFilename()  );
 			context.getExtension().enterScope( requestor );				
 
 		} else if( context.getKind() == IScannerContext.MACROEXPANSION )
 		{
 			if( !defines.add( context.getFilename() ) )
-				throw new ScannerException( "Define " + context.getFilename() + " already encountered." );
+				throw new ScannerException( ScannerException.ErrorCode.MALFORMED_MACRO_DEFN, context.getFilename() );
 		}
 		if( currentContext != null )
 			contextStack.push(currentContext);
Index: parser/org/eclipse/cdt/internal/core/parser/Parser.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/Parser.java,v
retrieving revision 1.104
diff -u -r1.104 Parser.java
--- parser/org/eclipse/cdt/internal/core/parser/Parser.java	8 Sep 2003 19:17:53 -0000	1.104
+++ parser/org/eclipse/cdt/internal/core/parser/Parser.java	9 Sep 2003 18:00:40 -0000
@@ -4562,6 +4562,11 @@
         catch (ScannerException e)
         {
             Util.debugLog( "ScannerException thrown : " + e.getMessage(), IDebugLogConstants.PARSER );
+            if( e.isSeriousError(mode) )
+            {
+            	failParse(); 
+            	throw endOfFile;
+            }
             return fetchToken();
         }
     }
Index: parser/org/eclipse/cdt/internal/core/parser/Scanner.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/Scanner.java,v
retrieving revision 1.51
diff -u -r1.51 Scanner.java
--- parser/org/eclipse/cdt/internal/core/parser/Scanner.java	8 Sep 2003 18:10:49 -0000	1.51
+++ parser/org/eclipse/cdt/internal/core/parser/Scanner.java	9 Sep 2003 18:00:44 -0000
@@ -318,7 +318,7 @@
 			}
 			
 			if (throwExceptionOnInclusionNotFound && inclusionReader == null )
-				throw new ScannerException("Cannot find inclusion " + fileName);
+				throw new ScannerException( ScannerException.ErrorCode.INCLUSION_NOT_FOUND, fileName );
 		}
 		else // local inclusion
 		{
@@ -721,8 +721,7 @@
 	
 				} else {
 					if (throwExceptionOnUnboundedString)
-						throw new ScannerException(
-							"Unbounded string" );
+						throw new ScannerException( ScannerException.ErrorCode.UNBOUNDED_STRING, getCurrentFile(), getCurrentOffset() );
 				}
 		
 			} else if (
@@ -830,7 +829,7 @@
 							if( getChar() == '.' )
 								return newToken( IToken.tELIPSE, "..." );
 							else
-								throw new ScannerException( "Invalid floating point @ offset " + contextStack.getCurrentContext().getOffset() ); 
+								throw new ScannerException( ScannerException.ErrorCode.BAD_FLOATING_POINT, getCurrentFile(), getCurrentOffset() ); 
 						} else {
 							ungetChar( c );
 							return newToken( IToken.tDOT, ".", contextStack.getCurrentContext() );
@@ -838,7 +837,7 @@
 					}
 				} else if (c == 'x') {
 					if( ! firstCharZero ) 
-						throw new ScannerException( "Invalid Hexidecimal @ offset " + contextStack.getCurrentContext().getOffset() );
+						throw new ScannerException( ScannerException.ErrorCode.BAD_HEXIDECIMAL_FORMAT, getCurrentFile(), getCurrentOffset() );
 					
 					hex = true;
 					c = getChar();
@@ -967,7 +966,7 @@
 				if( c == '#' )
 				{
 					if( skipped )
-						throw new ScannerException(BAD_PP + contextStack.getCurrentContext().getOffset()); 
+						throw new ScannerException(ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE,  getCurrentFile(), getCurrentOffset()); 
 					else 
 						return newToken( tPOUNDPOUND, "##" );
 				} else if( tokenizingMacroReplacementList ) {
@@ -987,8 +986,7 @@
 				Object directive = ppDirectives.get(token);
 				if (directive == null) {
 					if (throwExceptionOnBadPreprocessorSyntax)
-						throw new ScannerException(
-							BAD_PP + contextStack.getCurrentContext().getOffset());
+						throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE,  getCurrentFile(), getCurrentOffset());							
 				} else {
 					int type = ((Integer) directive).intValue();
 					switch (type) {
@@ -1026,7 +1024,7 @@
 							String toBeUndefined = getNextIdentifier();
 							
 							if( ( definitions.remove(toBeUndefined) == null ) && mode == ParserMode.COMPLETE_PARSE ) 
-								throw new ScannerException( "Attempt to #undef symbol " + toBeUndefined + " when it was never defined");
+								throw new ScannerException( ScannerException.ErrorCode.UNDEF_DEFINITION_NOT_FOUND, toBeUndefined, getCurrentFile(), getCurrentOffset() );
 							
 							skipOverTextUntilNewline();
 							c = getChar();
@@ -1064,7 +1062,7 @@
 						case PreprocessorDirectives.ENDIF :
 							String restOfLine = getRestOfPreprocessorLine().trim();
 							if( ! restOfLine.equals( "" ) && throwExceptionOnBadPreprocessorSyntax )
-								throw new ScannerException( BAD_PP + contextStack.getCurrentContext().getOffset() );
+								throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, getCurrentFile(), getCurrentOffset() );
 							passOnToClient = branches.poundendif(); 
 							c = getChar();
 							continue;
@@ -1086,7 +1084,14 @@
 							continue;
 
 						case PreprocessorDirectives.ELSE :
-							passOnToClient = branches.poundelse(); 
+							try
+							{
+								passOnToClient = branches.poundelse();
+							}
+							catch( ScannerException se )
+							{
+								repackageScannerExceptionAndThrow(se); 
+							}
 
 							skipOverTextUntilNewline();
 							c = getChar();
@@ -1098,14 +1103,23 @@
 
 							if (elsifExpression.equals(""))
 								if (throwExceptionOnBadPreprocessorSyntax)
-									throw new ScannerException("Malformed #elsif clause");
+									throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, getCurrentFile(), getCurrentOffset() );
 
 							boolean elsifResult = false;
 							try{
 								elsifResult = evaluateExpression(elsifExpression );
-							} catch( ScannerException e ){}
+							} catch( ScannerException e )
+							{
+							}
 
-							passOnToClient = branches.poundelif( elsifResult ); 
+							try
+							{
+								passOnToClient = branches.poundelif( elsifResult );
+							}
+							catch( ScannerException se )
+							{
+								repackageScannerExceptionAndThrow( se ); 
+							}
 							c = getChar();
 							continue;
 
@@ -1124,8 +1138,9 @@
 							String error = getRestOfPreprocessorLine();
 
 							if (mode == ParserMode.COMPLETE_PARSE) {
-								throw new ScannerException("#error " + error);
+								throw new ScannerException(ScannerException.ErrorCode.POUND_ERROR, error, getCurrentFile(), getCurrentOffset() );
 							}
+							
 							c = getChar();
 							continue;
 						case PreprocessorDirectives.PRAGMA :
@@ -1138,17 +1153,14 @@
 								getRestOfPreprocessorLine().trim();
 							if (!remainderOfLine.equals("")) {
 								if (throwExceptionOnBadPreprocessorSyntax)
-									throw new ScannerException(
-										BAD_PP + contextStack.getCurrentContext().getOffset());
+								throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, getCurrentFile(), getCurrentOffset() );
 							}
 
 							c = getChar();
 							continue;
 						default :
 							if (throwExceptionOnBadPreprocessorSyntax)
-								throw new ScannerException(
-									BAD_PP + contextStack.getCurrentContext().getOffset());
-
+						throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, getCurrentFile(), getCurrentOffset() );
 					}
 				}
 			} else {
@@ -1163,12 +1175,12 @@
 							if( next == '\'' )
 								return newToken( type, '\\' + new Character( (char)c ).toString(), contextStack.getCurrentContext() );
 							else if( throwExceptionOnBadCharacterRead )
-								throw new ScannerException( "Invalid character '" + (char)c + "' read @ offset " + contextStack.getCurrentContext().getOffset() + " of file " + contextStack.getCurrentContext().getFilename() );
+								throw new ScannerException( ScannerException.ErrorCode.INVALID_ESCAPE_CHARACTER_SEQUENCE, getCurrentFile(), getCurrentOffset() );
 						} else if( next == '\'' )
 							return newToken( type, new Character( (char)c ).toString(), contextStack.getCurrentContext() ); 
 						else
 							if( throwExceptionOnBadCharacterRead )
-								throw new ScannerException( "Invalid character '" + (char)c + "' read @ offset " + contextStack.getCurrentContext().getOffset() + " of file " + contextStack.getCurrentContext().getFilename() );
+								throw new ScannerException( ScannerException.ErrorCode.INVALID_ESCAPE_CHARACTER_SEQUENCE, getCurrentFile(), getCurrentOffset() );
 					case ':' :
 						c = getChar();
 						switch (c) {
@@ -1482,7 +1494,7 @@
 					default :
 						// Bad character
 						if( throwExceptionOnBadCharacterRead )
-							throw new ScannerException( "Invalid character '" + (char)c + "' read @ offset " + contextStack.getCurrentContext().getOffset() + " of file " + contextStack.getCurrentContext().getFilename() );
+							throw new ScannerException( ScannerException.ErrorCode.INVALID_ESCAPE_CHARACTER_SEQUENCE, getCurrentFile(), getCurrentOffset() );
 						else
 						{
 							c = ' ';
@@ -1497,7 +1509,7 @@
 		if (throwExceptionOnEOFWithoutBalancedEndifs && ( getDepth() != 0) && !atEOF )
 		{
 			atEOF = true;
-			throw new ScannerException("End of file encountered without terminating #endif");
+			throw new ScannerException(ScannerException.ErrorCode.UNBALANCED_CONDITIONALS, getCurrentFile(), getCurrentOffset() );
 		}
 
 		// we're done
@@ -1505,6 +1517,30 @@
 	}
 
 
+
+    protected void repackageScannerExceptionAndThrow(ScannerException se)
+        throws ScannerException
+    {
+        throw new ScannerException( se.getErrorCode(), getCurrentFile(), getCurrentOffset() );
+    }
+
+
+	protected String getCurrentFile()
+	{
+		if( contextStack.getCurrentContext() != null )
+			return contextStack.getCurrentContext().getFilename();
+		return "";
+	}
+
+
+    protected int getCurrentOffset()
+    {
+		if( contextStack.getCurrentContext() != null )
+        	return contextStack.getCurrentContext().getOffset();
+        return -1;
+    }
+
+
     protected static class endOfMacroTokenException extends Exception {};
     // the static instance we always use
     protected static endOfMacroTokenException endOfMacroToken = new endOfMacroTokenException();
@@ -1549,8 +1585,7 @@
     
                 } else {
                     if (throwExceptionOnUnboundedString)
-                        throw new ScannerException(
-                            "Unbounded string" );
+                        throw new ScannerException( ScannerException.ErrorCode.UNBOUNDED_STRING, getCurrentFile(), getCurrentOffset() ); 
                 }
         
             } else {
@@ -1565,12 +1600,12 @@
                             if( next == '\'' )
                                 return newToken( IToken.tCHAR, '\\' + new Character( (char)c ).toString(), contextStack.getCurrentContext() );
                             else if( throwExceptionOnBadCharacterRead )
-                                throw new ScannerException( "Invalid character '" + (char)c + "' read @ offset " + contextStack.getCurrentContext().getOffset() + " of file " + contextStack.getCurrentContext().getFilename() );
+                                throw new ScannerException( ScannerException.ErrorCode.INVALID_ESCAPE_CHARACTER_SEQUENCE, getCurrentFile(), getCurrentOffset());
                         } else if( next == '\'' )
                             return newToken( IToken.tCHAR, new Character( (char)c ).toString(), contextStack.getCurrentContext() ); 
                         else
                             if( throwExceptionOnBadCharacterRead )
-                                throw new ScannerException( "Invalid character '" + (char)c + "' read @ offset " + contextStack.getCurrentContext().getOffset() + " of file " + contextStack.getCurrentContext().getFilename() );
+                                throw new ScannerException( ScannerException.ErrorCode.INVALID_ESCAPE_CHARACTER_SEQUENCE, getCurrentFile(), getCurrentOffset());
                     case ',' :
                         if (tokenImage.length() > 0) throw endOfMacroToken;
                         return newToken(IToken.tCOMMA, ",", contextStack.getCurrentContext());
@@ -1806,15 +1841,11 @@
 				throwExpressionEvaluationError(expression);
 			}
 			return true; 
-	
 		}
 	}
 
 	protected void throwExpressionEvaluationError(String expression) throws ScannerException {
-		throw new ScannerException(
-				"Expression "
-					+ expression
-					+ " evaluates to an undefined value");			 					
+		throw new ScannerException( ScannerException.ErrorCode.EXPRESSION_EVALUATION_ERROR, expression, getCurrentFile(), getCurrentOffset());
 	}
 	
 	protected void skipOverSinglelineComment() throws ScannerException {
@@ -1875,7 +1906,7 @@
 
 		if (c == NOCHAR) {
 			if (throwExceptionOnEOFWithinMultilineComment)
-				throw new ScannerException("Encountered EOF while in multiline comment");
+				throw new ScannerException( ScannerException.ErrorCode.UNEXPECTED_EOF, getCurrentFile(), getCurrentOffset());
 		}
 
 		ungetChar(c);
@@ -1911,7 +1942,7 @@
 				t = helperScanner.nextToken(false);
 			} catch (EndOfFile eof) {
 				if (throwExceptionOnBadPreprocessorSyntax) {
-						 throw new ScannerException( "Encountered ill-formed #include" );
+					throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, getCurrentFile(), getCurrentOffset());
 				}
 				
 				return;
@@ -1928,7 +1959,7 @@
 					t = helperScanner.nextToken(false);
 
 					if (throwExceptionOnBadPreprocessorSyntax) {
-						throw new ScannerException( "Encountered ill-formed #include" );
+						throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, getCurrentFile(), getCurrentOffset());
 					}
 					
 					return;
@@ -1950,7 +1981,7 @@
 						
 					} catch (EndOfFile eof) {
 						if (throwExceptionOnBadPreprocessorSyntax) {
-								 throw new ScannerException( "Ill-formed #include: reached end of line before >" );
+							throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, getCurrentFile(), getCurrentOffset());
 						}
 				
 						return;
@@ -1960,13 +1991,13 @@
 					 t = helperScanner.nextToken(false);
 
 					 if (throwExceptionOnBadPreprocessorSyntax) {
-						 throw new ScannerException( "Encountered ill-formed #include" );
+						throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, getCurrentFile(), getCurrentOffset());
 					 }
 	
 					 return;
 					
 				} else if (throwExceptionOnBadPreprocessorSyntax) {
-					throw new ScannerException( "Encountered ill-formed #include" );
+					throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, getCurrentFile(), getCurrentOffset());
 				}
 			}
 			catch( EndOfFile eof )
@@ -1975,7 +2006,7 @@
 			}
 			
 		} else if (throwExceptionOnBadPreprocessorSyntax) {
-			throw new ScannerException( "Encountered ill-formed #include" );
+			throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, getCurrentFile(), getCurrentOffset());
 		}
 
 		String f = fileName.toString();
@@ -2003,12 +2034,7 @@
 		if (mode == ParserMode.COMPLETE_PARSE) {
 			String checkForRedefinition = (String) definitions.get(key);
 			if (checkForRedefinition != null) {
-				throw new ScannerException(
-					"Preprocessor symbol "
-						+ key
-						+ " has already been defined to "
-						+ checkForRedefinition
-						+ " cannot redefined.");
+				throw new ScannerException( ScannerException.ErrorCode.ATTEMPTED_REDEFINITION, key, getCurrentFile(), getCurrentOffset());
 			}
 		}
 
@@ -2032,16 +2058,16 @@
 					} else {
 						ungetChar( c );
 						if( throwExceptionOnBadPreprocessorSyntax )
-							throw new ScannerException( "Unexpected '\\' in macro formal parameter list." );
+							throw new ScannerException( ScannerException.ErrorCode.MALFORMED_MACRO_DEFN, getCurrentFile(), getCurrentOffset());
 						else return;
 					}
 				} else if( c == '\r' || c == '\n' ){
 					if( throwExceptionOnBadPreprocessorSyntax )
-						throw new ScannerException( "Unexpected newline in macro formal parameter list." );
+						throw new ScannerException( ScannerException.ErrorCode.MALFORMED_MACRO_DEFN, getCurrentFile(), getCurrentOffset());
 					else return;
 				} else if( c == NOCHAR ){
 					if( throwExceptionOnBadPreprocessorSyntax )
-						throw new ScannerException( "Unexpected EOF in macro formal parameter list." );
+						throw new ScannerException( ScannerException.ErrorCode.UNEXPECTED_EOF, getCurrentFile(), getCurrentOffset());
 					else return;
 				}
 				
@@ -2081,8 +2107,8 @@
 							if (index == -1 ) {
 								//not found
 								if (throwExceptionOnBadPreprocessorSyntax)
-									throw new ScannerException(
-										BAD_PP + contextStack.getCurrentContext().getOffset());
+									throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, getCurrentFile(), getCurrentOffset() ); 
+									
 								return;
 							}
 						}
@@ -2139,13 +2165,12 @@
 				// this is not a comment 
 				// it is a bad statement
 				if (throwExceptionOnBadPreprocessorSyntax)
-					throw new ScannerException(
-						BAD_PP + contextStack.getCurrentContext().getOffset());
+				throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, getCurrentFile(), getCurrentOffset() );
 			}
 		} else {
 			Util.debugLog("Scanner : Encountered unexpected character " + ((char) c), IDebugLogConstants.PARSER);
 			if (throwExceptionOnBadPreprocessorSyntax)
-				throw new ScannerException(BAD_PP + contextStack.getCurrentContext().getOffset());
+			throw new ScannerException( ScannerException.ErrorCode.INVALID_PREPROCESSOR_DIRECTIVE, getCurrentFile(), getCurrentOffset() );
 		}
 		
 		astFactory.createMacro( key, beginning, contextStack.getCurrentContext().getOffset(), offset ).acceptElement( requestor ); 
@@ -2245,8 +2270,7 @@
 
 				if (parameterNames.size() != parameterValues.size()) {
 					if (throwExceptionOnBadMacroExpansion)
-						throw new ScannerException(
-							"Improper use of macro " + symbol);
+						throw new ScannerException( ScannerException.ErrorCode.MACRO_USAGE_ERROR, symbol, getCurrentFile(), getCurrentOffset() ); 
 				}
 
 				int numberOfTokens = tokens.size();
@@ -2274,7 +2298,7 @@
 						int index = parameterNames.indexOf(t.image);
 						if( index == -1 ){
 							if (throwExceptionOnBadMacroExpansion)
-								throw new ScannerException(	"Improper use of the # preprocessing token." );	
+								throw new ScannerException(	ScannerException.ErrorCode.MACRO_PASTING_ERROR, getCurrentFile(), getCurrentOffset() );	
 						} else {
 							buffer.append('\"');
 							String value = (String)parameterValuesForStringizing.elementAt(index);
@@ -2331,8 +2355,7 @@
 					POUND_DEFINE + macro.getSignature(), ScannerContext.MACROEXPANSION, null, requestor, symbolOffset, endMacroOffset - symbolOffset + 1 );
 			} else 
 				if (throwExceptionOnBadMacroExpansion)
-					throw new ScannerException(
-						"Improper use of macro " + symbol);
+					throw new ScannerException( ScannerException.ErrorCode.MACRO_USAGE_ERROR, symbol, getCurrentFile(), getCurrentOffset() );
 
 		} else {
 			Util.debugLog(
@@ -2349,7 +2372,7 @@
 
 		if (c != '(') {
 			if (throwExceptionOnBadMacroExpansion)
-				throw new ScannerException("Improper use of macro defined()");
+				throw new ScannerException( ScannerException.ErrorCode.MACRO_USAGE_ERROR, "defined()", getCurrentFile(), getCurrentOffset() );
 		}
 
 		StringBuffer buffer = new StringBuffer();
@@ -2360,7 +2383,7 @@
 		}
 		if (c == NOCHAR) {
 			if (throwExceptionOnBadMacroExpansion)
-				throw new ScannerException("Improper use of macro defined()");
+			throw new ScannerException( ScannerException.ErrorCode.MACRO_USAGE_ERROR, "defined()", getCurrentFile(), getCurrentOffset() );
 		}
 
 		String definitionIdentifier = buffer.toString().trim();

Back to the top