mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
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.
This commit is contained in:
parent
8f627892aa
commit
93b627eabc
9 changed files with 267 additions and 82 deletions
|
@ -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
|
||||
|
|
|
@ -1270,33 +1270,33 @@ public class ScannerTestCase extends BaseScannerTest
|
|||
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 );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -21,5 +21,13 @@ public class Enum
|
|||
this.enumValue = enumValue;
|
||||
}
|
||||
|
||||
private int enumValue;
|
||||
private final int enumValue;
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
public int getEnumValue()
|
||||
{
|
||||
return enumValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,21 +10,163 @@
|
|||
******************************************************************************/
|
||||
package org.eclipse.cdt.core.parser;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ScannerException extends Exception {
|
||||
|
||||
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 );
|
||||
/**
|
||||
* Constructor for ScannerException.
|
||||
* @param enumValue
|
||||
*/
|
||||
public ScannerException() {
|
||||
super();
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for ScannerException.
|
||||
* @param s
|
||||
* @param mode
|
||||
* @return
|
||||
*/
|
||||
public ScannerException(String s) {
|
||||
super(s);
|
||||
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 );
|
||||
}
|
||||
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ public class BranchTracker {
|
|||
}
|
||||
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 @@ public class BranchTracker {
|
|||
}
|
||||
catch( EmptyStackException ese )
|
||||
{
|
||||
throw new ScannerException( "#else without a #if ");
|
||||
throw new ScannerException( ScannerException.ErrorCode.UNBALANCED_CONDITIONALS );
|
||||
}
|
||||
|
||||
if( ignore == IGNORE_SENTINEL )
|
||||
|
|
|
@ -67,13 +67,13 @@ public class ContextStack {
|
|||
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);
|
||||
|
|
|
@ -4562,6 +4562,11 @@ public class Parser implements IParser
|
|||
catch (ScannerException e)
|
||||
{
|
||||
Util.debugLog( "ScannerException thrown : " + e.getMessage(), IDebugLogConstants.PARSER );
|
||||
if( e.isSeriousError(mode) )
|
||||
{
|
||||
failParse();
|
||||
throw endOfFile;
|
||||
}
|
||||
return fetchToken();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -318,7 +318,7 @@ public class Scanner implements IScanner {
|
|||
}
|
||||
|
||||
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 @@ public class Scanner implements IScanner {
|
|||
|
||||
} else {
|
||||
if (throwExceptionOnUnboundedString)
|
||||
throw new ScannerException(
|
||||
"Unbounded string" );
|
||||
throw new ScannerException( ScannerException.ErrorCode.UNBOUNDED_STRING, getCurrentFile(), getCurrentOffset() );
|
||||
}
|
||||
|
||||
} else if (
|
||||
|
@ -830,7 +829,7 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
}
|
||||
} 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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
continue;
|
||||
|
||||
case PreprocessorDirectives.ELSE :
|
||||
try
|
||||
{
|
||||
passOnToClient = branches.poundelse();
|
||||
}
|
||||
catch( ScannerException se )
|
||||
{
|
||||
repackageScannerExceptionAndThrow(se);
|
||||
}
|
||||
|
||||
skipOverTextUntilNewline();
|
||||
c = getChar();
|
||||
|
@ -1098,14 +1103,23 @@ public class Scanner implements IScanner {
|
|||
|
||||
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 )
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
passOnToClient = branches.poundelif( elsifResult );
|
||||
}
|
||||
catch( ScannerException se )
|
||||
{
|
||||
repackageScannerExceptionAndThrow( se );
|
||||
}
|
||||
c = getChar();
|
||||
continue;
|
||||
|
||||
|
@ -1124,8 +1138,9 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
}
|
||||
|
||||
|
||||
|
||||
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 @@ public class Scanner implements IScanner {
|
|||
|
||||
} else {
|
||||
if (throwExceptionOnUnboundedString)
|
||||
throw new ScannerException(
|
||||
"Unbounded string" );
|
||||
throw new ScannerException( ScannerException.ErrorCode.UNBOUNDED_STRING, getCurrentFile(), getCurrentOffset() );
|
||||
}
|
||||
|
||||
} else {
|
||||
|
@ -1565,12 +1600,12 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
|
||||
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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
|
||||
} 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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
}
|
||||
|
||||
} 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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
} 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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
// 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 @@ public class Scanner implements IScanner {
|
|||
|
||||
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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
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 @@ public class Scanner implements IScanner {
|
|||
|
||||
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 @@ public class Scanner implements IScanner {
|
|||
}
|
||||
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();
|
||||
|
|
Loading…
Add table
Reference in a new issue