1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 17:56:01 +02:00

74180 - [Scanner] Scanner doesn't distinguish C & C++ keywords

This commit is contained in:
Andrew Niefer 2004-09-17 17:56:16 +00:00
parent b68b6a74dd
commit cdfae9c75b
9 changed files with 155 additions and 106 deletions

View file

@ -2081,4 +2081,11 @@ public class CompleteParseASTTest extends CompleteParseBaseTest
parse( writer.toString(), false ); parse( writer.toString(), false );
} }
} }
public void testBug74180() throws Exception
{
parse( "enum DHCPFOBoolean { false, true } additionalHB, more_payload; \n", true, ParserLanguage.C );
assertTrue( callback.problems.isEmpty() );
} }
}

View file

@ -58,6 +58,13 @@ public class BaseScanner2Test extends TestCase {
scanner = createScanner( new CodeReader(input.toCharArray()), new ScannerInfo(), mode, ParserLanguage.CPP, requestor, null, null ); //$NON-NLS-1$ scanner = createScanner( new CodeReader(input.toCharArray()), new ScannerInfo(), mode, ParserLanguage.CPP, requestor, null, null ); //$NON-NLS-1$
} }
protected void initializeScanner( String input, ParserLanguage language ) throws ParserFactoryError
{
scanner = createScanner( new CodeReader(input.toCharArray()),
new ScannerInfo(), ParserMode.COMPLETE_PARSE, language,
new NullSourceElementRequestor( ParserMode.COMPLETE_PARSE ), null, null );
}
protected void initializeScanner(String input) throws ParserFactoryError protected void initializeScanner(String input) throws ParserFactoryError
{ {
initializeScanner( input, ParserMode.COMPLETE_PARSE ); initializeScanner( input, ParserMode.COMPLETE_PARSE );

View file

@ -22,6 +22,7 @@ import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.IToken; import org.eclipse.cdt.core.parser.IToken;
import org.eclipse.cdt.core.parser.NullSourceElementRequestor; import org.eclipse.cdt.core.parser.NullSourceElementRequestor;
import org.eclipse.cdt.core.parser.ParserFactoryError; import org.eclipse.cdt.core.parser.ParserFactoryError;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ParserMode; import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ast.IASTInclusion; import org.eclipse.cdt.core.parser.ast.IASTInclusion;
import org.eclipse.cdt.internal.core.parser.QuickParseCallback; import org.eclipse.cdt.internal.core.parser.QuickParseCallback;
@ -1818,4 +1819,13 @@ public class Scanner2Test extends BaseScanner2Test
validateEOF(); validateEOF();
} }
public void testBug74180() throws Exception{
initializeScanner( "true false", ParserLanguage.C ); //$NON-NLS-1$
validateIdentifier( "true" ); //$NON-NLS-1$
validateIdentifier( "false" ); //$NON-NLS-1$
initializeScanner( "true false", ParserLanguage.CPP ); //$NON-NLS-1$
validateToken( IToken.t_true );
validateToken( IToken.t_false);
}
} }

View file

@ -36,6 +36,13 @@ public class CharArrayIntMap extends CharTable {
for( int i = 0; i < capacity(); i++ ) for( int i = 0; i < capacity(); i++ )
valueTable[i] = undefined; valueTable[i] = undefined;
} }
public Object clone(){
CharArrayIntMap newMap = (CharArrayIntMap) super.clone();
newMap.valueTable = new int[ capacity() ];
System.arraycopy(valueTable, 0, newMap.valueTable, 0, valueTable.length);
return newMap;
}
public int put(char[] key, int start, int length, int value) { public int put(char[] key, int start, int length, int value) {
int i = addIndex(key, start, length); int i = addIndex(key, start, length);
int oldvalue = valueTable[i]; int oldvalue = valueTable[i];

View file

@ -78,18 +78,10 @@ public class CharArrayObjectMap extends CharTable {
} }
public Object clone(){ public Object clone(){
int size = capacity(); CharArrayObjectMap newTable = (CharArrayObjectMap) super.clone();
newTable.valueTable = new Object[ capacity() ];
CharArrayObjectMap newTable = new CharArrayObjectMap( size );
System.arraycopy(valueTable, 0, newTable.valueTable, 0, valueTable.length); System.arraycopy(valueTable, 0, newTable.valueTable, 0, valueTable.length);
System.arraycopy(keyTable, 0, newTable.keyTable, 0, keyTable.length);
if( hashTable != null )
System.arraycopy(hashTable, 0, newTable.hashTable, 0, hashTable.length);
if( nextTable != null )
System.arraycopy(nextTable, 0, newTable.nextTable, 0, nextTable.length);
newTable.currEntry = currEntry;
return newTable; return newTable;
} }

View file

@ -38,6 +38,15 @@ public class CharTable extends HashTable {
for( int i = 0; i < capacity(); i++ ) for( int i = 0; i < capacity(); i++ )
keyTable[i] = null; keyTable[i] = null;
} }
public Object clone(){
CharTable newTable = (CharTable) super.clone();
int size = capacity();
newTable.keyTable = new char[size][];
System.arraycopy(keyTable, 0, newTable.keyTable, 0, keyTable.length);
return newTable;
}
protected final int hash(char[] source, int start, int length) { protected final int hash(char[] source, int start, int length) {
return CharArrayUtils.hash(source, start, length) & ((keyTable.length * 2) - 1); return CharArrayUtils.hash(source, start, length) & ((keyTable.length * 2) - 1);
} }

View file

@ -16,7 +16,7 @@ import java.util.Comparator;
/** /**
* @author ddaoust * @author ddaoust
*/ */
public class HashTable { public class HashTable implements Cloneable{
protected static final int minHashSize = 2; protected static final int minHashSize = 2;
protected int currEntry = -1; protected int currEntry = -1;
@ -47,6 +47,27 @@ public class HashTable {
} }
} }
public Object clone(){
HashTable newTable = null;
try {
newTable = (HashTable) super.clone();
} catch ( CloneNotSupportedException e ) {
//shouldn't happen because object supports clone.
return null;
}
int size = capacity();
if( hashTable != null ){
newTable.hashTable = new int[ size*2 ];
newTable.nextTable = new int[ size ];
System.arraycopy(hashTable, 0, newTable.hashTable, 0, hashTable.length);
System.arraycopy(nextTable, 0, newTable.nextTable, 0, nextTable.length);
}
newTable.currEntry = currEntry;
return newTable;
}
protected void resize() { protected void resize() {
resize(capacity() << 1); resize(capacity() << 1);
} }

View file

@ -31,25 +31,12 @@ public abstract class ObjectTable extends HashTable implements Cloneable{
} }
public Object clone(){ public Object clone(){
ObjectTable newTable = null; ObjectTable newTable = (ObjectTable) super.clone();
try {
newTable = (ObjectTable) super.clone();
} catch ( CloneNotSupportedException e ) {
//shouldn't happen because object supports clone.
return null;
}
int size = capacity(); int size = capacity();
newTable.keyTable = new Object[ size ]; newTable.keyTable = new Object[ size ];
System.arraycopy(keyTable, 0, newTable.keyTable, 0, keyTable.length); System.arraycopy(keyTable, 0, newTable.keyTable, 0, keyTable.length);
if( hashTable != null ){
newTable.hashTable = new int[ size*2 ];
newTable.nextTable = new int[ size ];
System.arraycopy(hashTable, 0, newTable.hashTable, 0, hashTable.length);
System.arraycopy(nextTable, 0, newTable.nextTable, 0, nextTable.length);
}
newTable.currEntry = currEntry;
return newTable; return newTable;
} }

View file

@ -146,6 +146,11 @@ public class Scanner2 implements IScanner, IScannerData {
this.log = log; this.log = log;
this.workingCopies = workingCopies; this.workingCopies = workingCopies;
if( language == ParserLanguage.C )
keywords = ckeywords;
else
keywords = cppkeywords;
if (reader.filename != null) if (reader.filename != null)
fileCache.put(reader.filename, reader); fileCache.put(reader.filename, reader);
@ -2896,7 +2901,9 @@ public class Scanner2 implements IScanner, IScannerData {
return 0; return 0;
} }
private static CharArrayIntMap keywords; private final CharArrayIntMap keywords;
private static CharArrayIntMap ckeywords;
private static CharArrayIntMap cppkeywords;
private static CharArrayIntMap ppKeywords; private static CharArrayIntMap ppKeywords;
private static final int ppIf = 0; private static final int ppIf = 0;
private static final int ppIfdef = 1; private static final int ppIfdef = 1;
@ -2914,93 +2921,95 @@ public class Scanner2 implements IScanner, IScannerData {
private static final char[] SPACE = { ' ' }; private static final char[] SPACE = { ' ' };
static { static {
keywords = new CharArrayIntMap(IToken.tLAST, -1); CharArrayIntMap words = new CharArrayIntMap(IToken.tLAST, -1);
// Common keywords // Common keywords
keywords.put("auto".toCharArray(), IToken.t_auto); //$NON-NLS-1$ words.put("auto".toCharArray(), IToken.t_auto); //$NON-NLS-1$
keywords.put("break".toCharArray(), IToken.t_break); //$NON-NLS-1$ words.put("break".toCharArray(), IToken.t_break); //$NON-NLS-1$
keywords.put("case".toCharArray(), IToken.t_case); //$NON-NLS-1$ words.put("case".toCharArray(), IToken.t_case); //$NON-NLS-1$
keywords.put("char".toCharArray(), IToken.t_char); //$NON-NLS-1$ words.put("char".toCharArray(), IToken.t_char); //$NON-NLS-1$
keywords.put("const".toCharArray(), IToken.t_const); //$NON-NLS-1$ words.put("const".toCharArray(), IToken.t_const); //$NON-NLS-1$
keywords.put("continue".toCharArray(), IToken.t_continue); //$NON-NLS-1$ words.put("continue".toCharArray(), IToken.t_continue); //$NON-NLS-1$
keywords.put("default".toCharArray(), IToken.t_default); //$NON-NLS-1$ words.put("default".toCharArray(), IToken.t_default); //$NON-NLS-1$
keywords.put("do".toCharArray(), IToken.t_do); //$NON-NLS-1$ words.put("do".toCharArray(), IToken.t_do); //$NON-NLS-1$
keywords.put("double".toCharArray(), IToken.t_double); //$NON-NLS-1$ words.put("double".toCharArray(), IToken.t_double); //$NON-NLS-1$
keywords.put("else".toCharArray(), IToken.t_else); //$NON-NLS-1$ words.put("else".toCharArray(), IToken.t_else); //$NON-NLS-1$
keywords.put("enum".toCharArray(), IToken.t_enum); //$NON-NLS-1$ words.put("enum".toCharArray(), IToken.t_enum); //$NON-NLS-1$
keywords.put("extern".toCharArray(), IToken.t_extern); //$NON-NLS-1$ words.put("extern".toCharArray(), IToken.t_extern); //$NON-NLS-1$
keywords.put("float".toCharArray(), IToken.t_float); //$NON-NLS-1$ words.put("float".toCharArray(), IToken.t_float); //$NON-NLS-1$
keywords.put("for".toCharArray(), IToken.t_for); //$NON-NLS-1$ words.put("for".toCharArray(), IToken.t_for); //$NON-NLS-1$
keywords.put("goto".toCharArray(), IToken.t_goto); //$NON-NLS-1$ words.put("goto".toCharArray(), IToken.t_goto); //$NON-NLS-1$
keywords.put("if".toCharArray(), IToken.t_if); //$NON-NLS-1$ words.put("if".toCharArray(), IToken.t_if); //$NON-NLS-1$
keywords.put("inline".toCharArray(), IToken.t_inline); //$NON-NLS-1$ words.put("inline".toCharArray(), IToken.t_inline); //$NON-NLS-1$
keywords.put("int".toCharArray(), IToken.t_int); //$NON-NLS-1$ words.put("int".toCharArray(), IToken.t_int); //$NON-NLS-1$
keywords.put("long".toCharArray(), IToken.t_long); //$NON-NLS-1$ words.put("long".toCharArray(), IToken.t_long); //$NON-NLS-1$
keywords.put("register".toCharArray(), IToken.t_register); //$NON-NLS-1$ words.put("register".toCharArray(), IToken.t_register); //$NON-NLS-1$
keywords.put("return".toCharArray(), IToken.t_return); //$NON-NLS-1$ words.put("return".toCharArray(), IToken.t_return); //$NON-NLS-1$
keywords.put("short".toCharArray(), IToken.t_short); //$NON-NLS-1$ words.put("short".toCharArray(), IToken.t_short); //$NON-NLS-1$
keywords.put("signed".toCharArray(), IToken.t_signed); //$NON-NLS-1$ words.put("signed".toCharArray(), IToken.t_signed); //$NON-NLS-1$
keywords.put("sizeof".toCharArray(), IToken.t_sizeof); //$NON-NLS-1$ words.put("sizeof".toCharArray(), IToken.t_sizeof); //$NON-NLS-1$
keywords.put("static".toCharArray(), IToken.t_static); //$NON-NLS-1$ words.put("static".toCharArray(), IToken.t_static); //$NON-NLS-1$
keywords.put("struct".toCharArray(), IToken.t_struct); //$NON-NLS-1$ words.put("struct".toCharArray(), IToken.t_struct); //$NON-NLS-1$
keywords.put("switch".toCharArray(), IToken.t_switch); //$NON-NLS-1$ words.put("switch".toCharArray(), IToken.t_switch); //$NON-NLS-1$
keywords.put("typedef".toCharArray(), IToken.t_typedef); //$NON-NLS-1$ words.put("typedef".toCharArray(), IToken.t_typedef); //$NON-NLS-1$
keywords.put("union".toCharArray(), IToken.t_union); //$NON-NLS-1$ words.put("union".toCharArray(), IToken.t_union); //$NON-NLS-1$
keywords.put("unsigned".toCharArray(), IToken.t_unsigned); //$NON-NLS-1$ words.put("unsigned".toCharArray(), IToken.t_unsigned); //$NON-NLS-1$
keywords.put("void".toCharArray(), IToken.t_void); //$NON-NLS-1$ words.put("void".toCharArray(), IToken.t_void); //$NON-NLS-1$
keywords.put("volatile".toCharArray(), IToken.t_volatile); //$NON-NLS-1$ words.put("volatile".toCharArray(), IToken.t_volatile); //$NON-NLS-1$
keywords.put("while".toCharArray(), IToken.t_while); //$NON-NLS-1$ words.put("while".toCharArray(), IToken.t_while); //$NON-NLS-1$
// ANSI C keywords // ANSI C keywords
keywords.put("restrict".toCharArray(), IToken.t_restrict); //$NON-NLS-1$ ckeywords = (CharArrayIntMap) words.clone();
keywords.put("_Bool".toCharArray(), IToken.t__Bool); //$NON-NLS-1$ ckeywords.put("restrict".toCharArray(), IToken.t_restrict); //$NON-NLS-1$
keywords.put("_Complex".toCharArray(), IToken.t__Complex); //$NON-NLS-1$ ckeywords.put("_Bool".toCharArray(), IToken.t__Bool); //$NON-NLS-1$
keywords.put("_Imaginary".toCharArray(), IToken.t__Imaginary); //$NON-NLS-1$ ckeywords.put("_Complex".toCharArray(), IToken.t__Complex); //$NON-NLS-1$
ckeywords.put("_Imaginary".toCharArray(), IToken.t__Imaginary); //$NON-NLS-1$
// C++ Keywords // C++ Keywords
keywords.put("asm".toCharArray(), IToken.t_asm); //$NON-NLS-1$ cppkeywords = words;
keywords.put("bool".toCharArray(), IToken.t_bool); //$NON-NLS-1$ cppkeywords.put("asm".toCharArray(), IToken.t_asm); //$NON-NLS-1$
keywords.put("catch".toCharArray(), IToken.t_catch); //$NON-NLS-1$ cppkeywords.put("bool".toCharArray(), IToken.t_bool); //$NON-NLS-1$
keywords.put("class".toCharArray(), IToken.t_class); //$NON-NLS-1$ cppkeywords.put("catch".toCharArray(), IToken.t_catch); //$NON-NLS-1$
keywords.put("const_cast".toCharArray(), IToken.t_const_cast); //$NON-NLS-1$ cppkeywords.put("class".toCharArray(), IToken.t_class); //$NON-NLS-1$
keywords.put("delete".toCharArray(), IToken.t_delete); //$NON-NLS-1$ cppkeywords.put("const_cast".toCharArray(), IToken.t_const_cast); //$NON-NLS-1$
keywords.put("dynamic_cast".toCharArray(), IToken.t_dynamic_cast); //$NON-NLS-1$ cppkeywords.put("delete".toCharArray(), IToken.t_delete); //$NON-NLS-1$
keywords.put("explicit".toCharArray(), IToken.t_explicit); //$NON-NLS-1$ cppkeywords.put("dynamic_cast".toCharArray(), IToken.t_dynamic_cast); //$NON-NLS-1$
keywords.put("export".toCharArray(), IToken.t_export); //$NON-NLS-1$ cppkeywords.put("explicit".toCharArray(), IToken.t_explicit); //$NON-NLS-1$
keywords.put("false".toCharArray(), IToken.t_false); //$NON-NLS-1$ cppkeywords.put("export".toCharArray(), IToken.t_export); //$NON-NLS-1$
keywords.put("friend".toCharArray(), IToken.t_friend); //$NON-NLS-1$ cppkeywords.put("false".toCharArray(), IToken.t_false); //$NON-NLS-1$
keywords.put("mutable".toCharArray(), IToken.t_mutable); //$NON-NLS-1$ cppkeywords.put("friend".toCharArray(), IToken.t_friend); //$NON-NLS-1$
keywords.put("namespace".toCharArray(), IToken.t_namespace); //$NON-NLS-1$ cppkeywords.put("mutable".toCharArray(), IToken.t_mutable); //$NON-NLS-1$
keywords.put("new".toCharArray(), IToken.t_new); //$NON-NLS-1$ cppkeywords.put("namespace".toCharArray(), IToken.t_namespace); //$NON-NLS-1$
keywords.put("operator".toCharArray(), IToken.t_operator); //$NON-NLS-1$ cppkeywords.put("new".toCharArray(), IToken.t_new); //$NON-NLS-1$
keywords.put("private".toCharArray(), IToken.t_private); //$NON-NLS-1$ cppkeywords.put("operator".toCharArray(), IToken.t_operator); //$NON-NLS-1$
keywords.put("protected".toCharArray(), IToken.t_protected); //$NON-NLS-1$ cppkeywords.put("private".toCharArray(), IToken.t_private); //$NON-NLS-1$
keywords.put("public".toCharArray(), IToken.t_public); //$NON-NLS-1$ cppkeywords.put("protected".toCharArray(), IToken.t_protected); //$NON-NLS-1$
keywords.put("reinterpret_cast".toCharArray(), IToken.t_reinterpret_cast); //$NON-NLS-1$ cppkeywords.put("public".toCharArray(), IToken.t_public); //$NON-NLS-1$
keywords.put("static_cast".toCharArray(), IToken.t_static_cast); //$NON-NLS-1$ cppkeywords.put("reinterpret_cast".toCharArray(), IToken.t_reinterpret_cast); //$NON-NLS-1$
keywords.put("template".toCharArray(), IToken.t_template); //$NON-NLS-1$ cppkeywords.put("static_cast".toCharArray(), IToken.t_static_cast); //$NON-NLS-1$
keywords.put("this".toCharArray(), IToken.t_this); //$NON-NLS-1$ cppkeywords.put("template".toCharArray(), IToken.t_template); //$NON-NLS-1$
keywords.put("throw".toCharArray(), IToken.t_throw); //$NON-NLS-1$ cppkeywords.put("this".toCharArray(), IToken.t_this); //$NON-NLS-1$
keywords.put("true".toCharArray(), IToken.t_true); //$NON-NLS-1$ cppkeywords.put("throw".toCharArray(), IToken.t_throw); //$NON-NLS-1$
keywords.put("try".toCharArray(), IToken.t_try); //$NON-NLS-1$ cppkeywords.put("true".toCharArray(), IToken.t_true); //$NON-NLS-1$
keywords.put("typeid".toCharArray(), IToken.t_typeid); //$NON-NLS-1$ cppkeywords.put("try".toCharArray(), IToken.t_try); //$NON-NLS-1$
keywords.put("typename".toCharArray(), IToken.t_typename); //$NON-NLS-1$ cppkeywords.put("typeid".toCharArray(), IToken.t_typeid); //$NON-NLS-1$
keywords.put("using".toCharArray(), IToken.t_using); //$NON-NLS-1$ cppkeywords.put("typename".toCharArray(), IToken.t_typename); //$NON-NLS-1$
keywords.put("virtual".toCharArray(), IToken.t_virtual); //$NON-NLS-1$ cppkeywords.put("using".toCharArray(), IToken.t_using); //$NON-NLS-1$
keywords.put("wchar_t".toCharArray(), IToken.t_wchar_t); //$NON-NLS-1$ cppkeywords.put("virtual".toCharArray(), IToken.t_virtual); //$NON-NLS-1$
cppkeywords.put("wchar_t".toCharArray(), IToken.t_wchar_t); //$NON-NLS-1$
// C++ operator alternative // C++ operator alternative
keywords.put("and".toCharArray(), IToken.t_and); //$NON-NLS-1$ cppkeywords.put("and".toCharArray(), IToken.t_and); //$NON-NLS-1$
keywords.put("and_eq".toCharArray(), IToken.t_and_eq); //$NON-NLS-1$ cppkeywords.put("and_eq".toCharArray(), IToken.t_and_eq); //$NON-NLS-1$
keywords.put("bitand".toCharArray(), IToken.t_bitand); //$NON-NLS-1$ cppkeywords.put("bitand".toCharArray(), IToken.t_bitand); //$NON-NLS-1$
keywords.put("bitor".toCharArray(), IToken.t_bitor); //$NON-NLS-1$ cppkeywords.put("bitor".toCharArray(), IToken.t_bitor); //$NON-NLS-1$
keywords.put("compl".toCharArray(), IToken.t_compl); //$NON-NLS-1$ cppkeywords.put("compl".toCharArray(), IToken.t_compl); //$NON-NLS-1$
keywords.put("not".toCharArray(), IToken.t_not); //$NON-NLS-1$ cppkeywords.put("not".toCharArray(), IToken.t_not); //$NON-NLS-1$
keywords.put("not_eq".toCharArray(), IToken.t_not_eq); //$NON-NLS-1$ cppkeywords.put("not_eq".toCharArray(), IToken.t_not_eq); //$NON-NLS-1$
keywords.put("or".toCharArray(), IToken.t_or); //$NON-NLS-1$ cppkeywords.put("or".toCharArray(), IToken.t_or); //$NON-NLS-1$
keywords.put("or_eq".toCharArray(), IToken.t_or_eq); //$NON-NLS-1$ cppkeywords.put("or_eq".toCharArray(), IToken.t_or_eq); //$NON-NLS-1$
keywords.put("xor".toCharArray(), IToken.t_xor); //$NON-NLS-1$ cppkeywords.put("xor".toCharArray(), IToken.t_xor); //$NON-NLS-1$
keywords.put("xor_eq".toCharArray(), IToken.t_xor_eq); //$NON-NLS-1$ cppkeywords.put("xor_eq".toCharArray(), IToken.t_xor_eq); //$NON-NLS-1$
// Preprocessor keywords // Preprocessor keywords
ppKeywords = new CharArrayIntMap(16, -1); ppKeywords = new CharArrayIntMap(16, -1);