mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-04 14:55:41 +02:00
Checking in Scanner2 and friends. This should hopefully be a faster scanner
and set us up for using char[]'s up the stack.
This commit is contained in:
parent
008ca93b73
commit
b560898413
15 changed files with 3872 additions and 125 deletions
|
@ -0,0 +1,247 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2001 IBM Corporation and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Common Public License v0.5
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/cpl-v05.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* IBM Corp. - Rational Software - initial implementation
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
package org.eclipse.cdt.core.parser.tests;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.parser.CodeReader;
|
||||||
|
import org.eclipse.cdt.core.parser.EndOfFileException;
|
||||||
|
import org.eclipse.cdt.core.parser.IParserLogService;
|
||||||
|
import org.eclipse.cdt.core.parser.IScannerInfo;
|
||||||
|
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
|
||||||
|
import org.eclipse.cdt.core.parser.IToken;
|
||||||
|
import org.eclipse.cdt.core.parser.NullSourceElementRequestor;
|
||||||
|
import org.eclipse.cdt.core.parser.ParserFactory;
|
||||||
|
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.ScannerException;
|
||||||
|
import org.eclipse.cdt.core.parser.ScannerInfo;
|
||||||
|
import org.eclipse.cdt.core.parser.extension.ExtensionDialect;
|
||||||
|
import org.eclipse.cdt.internal.core.parser.ParserExtensionFactory;
|
||||||
|
import org.eclipse.cdt.internal.core.parser.scanner2.CharArrayUtils;
|
||||||
|
import org.eclipse.cdt.internal.core.parser.scanner2.ObjectStyleMacro;
|
||||||
|
import org.eclipse.cdt.internal.core.parser.scanner2.Scanner2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author jcamelon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class BaseScanner2Test extends TestCase {
|
||||||
|
|
||||||
|
protected Scanner2 scanner;
|
||||||
|
|
||||||
|
public BaseScanner2Test( String x )
|
||||||
|
{
|
||||||
|
super(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void initializeScanner( String input, ParserMode mode ) throws ParserFactoryError
|
||||||
|
{
|
||||||
|
initializeScanner( input, mode, new NullSourceElementRequestor( mode ));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void initializeScanner( String input, ParserMode mode, ISourceElementRequestor requestor ) throws ParserFactoryError
|
||||||
|
{
|
||||||
|
scanner = createScanner( new CodeReader(input.toCharArray()), new ScannerInfo(), mode, ParserLanguage.CPP, requestor, null, null ); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void initializeScanner(String input) throws ParserFactoryError
|
||||||
|
{
|
||||||
|
initializeScanner( input, ParserMode.COMPLETE_PARSE );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Scanner2 createScanner( CodeReader code, IScannerInfo config, ParserMode mode, ParserLanguage language, ISourceElementRequestor requestor, IParserLogService log, List workingCopies ) throws ParserFactoryError
|
||||||
|
{
|
||||||
|
if( config == null ) throw new ParserFactoryError( ParserFactoryError.Kind.NULL_CONFIG );
|
||||||
|
if( language == null ) throw new ParserFactoryError( ParserFactoryError.Kind.NULL_LANGUAGE );
|
||||||
|
IParserLogService logService = ( log == null ) ? ParserFactory.createDefaultLogService() : log;
|
||||||
|
ParserMode ourMode = ( (mode == null )? ParserMode.COMPLETE_PARSE : mode );
|
||||||
|
ISourceElementRequestor ourRequestor = (( requestor == null) ? new NullSourceElementRequestor() : requestor );
|
||||||
|
return new Scanner2( code, config, ourRequestor, ourMode, language, logService, new ParserExtensionFactory( ExtensionDialect.GCC ).createScannerExtension(), workingCopies );
|
||||||
|
}
|
||||||
|
|
||||||
|
public int fullyTokenize() throws Exception
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
IToken t= scanner.nextToken();
|
||||||
|
while (t != null)
|
||||||
|
{
|
||||||
|
if (verbose)
|
||||||
|
System.out.println("Token t = " + t); //$NON-NLS-1$
|
||||||
|
|
||||||
|
if ((t.getType()> IToken.tLAST))
|
||||||
|
System.out.println("Unknown type for token " + t); //$NON-NLS-1$
|
||||||
|
t= scanner.nextToken();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch ( EndOfFileException e)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
catch (ScannerException se)
|
||||||
|
{
|
||||||
|
throw se;
|
||||||
|
}
|
||||||
|
return scanner.getCount();
|
||||||
|
}
|
||||||
|
public void validateIdentifier(String expectedImage) throws ScannerException
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
IToken t= scanner.nextToken();
|
||||||
|
assertEquals( t.getType(), IToken.tIDENTIFIER );
|
||||||
|
assertEquals(t.getImage(), expectedImage );
|
||||||
|
} catch (EndOfFileException e) {
|
||||||
|
assertTrue(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateInteger(String expectedImage) throws ScannerException
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
IToken t= scanner.nextToken();
|
||||||
|
assertTrue(t.getType() == IToken.tINTEGER);
|
||||||
|
assertTrue(t.getImage().equals(expectedImage));
|
||||||
|
} catch (EndOfFileException e) {
|
||||||
|
assertTrue(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateFloatingPointLiteral(String expectedImage) throws ScannerException
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
IToken t= scanner.nextToken();
|
||||||
|
assertTrue(t.getType() == IToken.tFLOATINGPT);
|
||||||
|
assertTrue(t.getImage().equals(expectedImage));
|
||||||
|
} catch (EndOfFileException e) {
|
||||||
|
assertTrue(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateChar( char expected )throws ScannerException
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
IToken t= scanner.nextToken();
|
||||||
|
assertTrue(t.getType() == IToken.tCHAR );
|
||||||
|
Character c = new Character( expected );
|
||||||
|
assertEquals( t.getImage(), c.toString() );
|
||||||
|
} catch (EndOfFileException e) {
|
||||||
|
assertTrue(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateChar( String expected ) throws ScannerException
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
IToken t= scanner.nextToken();
|
||||||
|
assertTrue(t.getType() == IToken.tCHAR );
|
||||||
|
assertEquals( t.getImage(), expected );
|
||||||
|
} catch (EndOfFileException e) {
|
||||||
|
assertTrue(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateString( String expectedImage ) throws ScannerException
|
||||||
|
{
|
||||||
|
validateString( expectedImage, false );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateString(String expectedImage, boolean lString ) throws ScannerException
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
IToken t= scanner.nextToken();
|
||||||
|
if( lString )
|
||||||
|
assertTrue(t.getType() == IToken.tLSTRING);
|
||||||
|
else
|
||||||
|
assertTrue(t.getType() == IToken.tSTRING);
|
||||||
|
assertTrue(t.getImage().equals(expectedImage));
|
||||||
|
} catch (EndOfFileException e) {
|
||||||
|
assertTrue(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateToken(int tokenType) throws ScannerException
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
IToken t= scanner.nextToken();
|
||||||
|
assertTrue(t.getType() == tokenType);
|
||||||
|
} catch (EndOfFileException e) {
|
||||||
|
assertTrue(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateBalance(int expected)
|
||||||
|
{
|
||||||
|
assertTrue(scanner.getDepth() == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateBalance()
|
||||||
|
{
|
||||||
|
assertTrue(scanner.getDepth() == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateEOF() throws ScannerException
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
assertNull(scanner.nextToken());
|
||||||
|
} catch (EndOfFileException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateDefinition(String name, String value)
|
||||||
|
{
|
||||||
|
Object expObject = scanner.getRealDefinitions().get(name.toCharArray());
|
||||||
|
assertNotNull(expObject);
|
||||||
|
assertTrue(expObject instanceof ObjectStyleMacro);
|
||||||
|
assertTrue(CharArrayUtils.equals(value.toCharArray(), ((ObjectStyleMacro)expObject).expansion));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateDefinition(String name, int value)
|
||||||
|
{
|
||||||
|
String definition= null;
|
||||||
|
definition= scanner.getDefinition(name).getExpansionSignature();
|
||||||
|
assertNotNull(definition);
|
||||||
|
int intValue= (Integer.valueOf(definition)).intValue();
|
||||||
|
assertEquals(value, intValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateAsUndefined(String name)
|
||||||
|
{
|
||||||
|
assertNull(scanner.getDefinition(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String EXCEPTION_THROWN = "Exception thrown "; //$NON-NLS-1$
|
||||||
|
|
||||||
|
public static final String EXPECTED_FAILURE = "This statement should not be reached " //$NON-NLS-1$
|
||||||
|
+ "as we sent in bad preprocessor input to the scanner"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
public static final boolean verbose = false;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string
|
||||||
|
*/
|
||||||
|
protected void validateWideChar(String string) throws Exception
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
IToken t= scanner.nextToken();
|
||||||
|
assertTrue(t.getType() == IToken.tLCHAR );
|
||||||
|
assertEquals( t.getImage(), string );
|
||||||
|
} catch (EndOfFileException e) {
|
||||||
|
assertTrue(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -1,22 +1,23 @@
|
||||||
/*
|
/*******************************************************************************
|
||||||
* Created on May 28, 2004
|
* Copyright (c) 2004 IBM Corporation and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Common Public License v0.5
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/cpl-v05.html
|
||||||
*
|
*
|
||||||
* TODO To change the template for this generated file go to
|
* Contributors:
|
||||||
* Window - Preferences - Java - Code Style - Code Templates
|
* IBM Corporation - initial implementation
|
||||||
*/
|
******************************************************************************/
|
||||||
package org.eclipse.cdt.core.parser.tests;
|
package org.eclipse.cdt.core.parser.tests;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.parser.CharArrayMap;
|
import org.eclipse.cdt.internal.core.parser.scanner2.CharArrayObjectMap;
|
||||||
import org.eclipse.cdt.core.parser.CharArrayPool;
|
import org.eclipse.cdt.internal.core.parser.scanner2.CharArrayPool;
|
||||||
import org.eclipse.cdt.core.parser.CharArrayUtils;
|
import org.eclipse.cdt.internal.core.parser.scanner2.CharArrayUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author dschaefe
|
* @author Doug Schaefer
|
||||||
*
|
|
||||||
* TODO To change the template for this generated type comment go to
|
|
||||||
* Window - Preferences - Java - Code Style - Code Templates
|
|
||||||
*/
|
*/
|
||||||
public class CharArrayUtilsTest extends TestCase {
|
public class CharArrayUtilsTest extends TestCase {
|
||||||
|
|
||||||
|
@ -75,7 +76,7 @@ public class CharArrayUtilsTest extends TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testMapAdd() {
|
public void testMapAdd() {
|
||||||
CharArrayMap map = new CharArrayMap(4);
|
CharArrayObjectMap map = new CharArrayObjectMap(4);
|
||||||
char[] key1 = "key1".toCharArray();
|
char[] key1 = "key1".toCharArray();
|
||||||
Object value1 = new Integer(43);
|
Object value1 = new Integer(43);
|
||||||
map.put(key1, value1);
|
map.put(key1, value1);
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,101 +0,0 @@
|
||||||
/*
|
|
||||||
* Created on May 28, 2004
|
|
||||||
*
|
|
||||||
* TODO To change the template for this generated file go to
|
|
||||||
* Window - Preferences - Java - Code Style - Code Templates
|
|
||||||
*/
|
|
||||||
package org.eclipse.cdt.core.parser;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author dschaefe
|
|
||||||
*
|
|
||||||
* TODO To change the template for this generated type comment go to
|
|
||||||
* Window - Preferences - Java - Code Style - Code Templates
|
|
||||||
*/
|
|
||||||
public class CharArrayMap {
|
|
||||||
|
|
||||||
private char[][] keyTable;
|
|
||||||
private Object[] valueTable;
|
|
||||||
private int[] hashTable;
|
|
||||||
private int[] nextTable;
|
|
||||||
|
|
||||||
private int currEntry;
|
|
||||||
|
|
||||||
public CharArrayMap(int initialSize) {
|
|
||||||
// Make sure size is a power of two
|
|
||||||
int size = 1;
|
|
||||||
while (size < initialSize)
|
|
||||||
size <<= 1;
|
|
||||||
createTables(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
private final void createTables(int size) {
|
|
||||||
keyTable = new char[size][];
|
|
||||||
valueTable = new Object[size];
|
|
||||||
nextTable = new int[size];
|
|
||||||
hashTable = new int[size << 1];
|
|
||||||
currEntry = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private final int hash(char[] key) {
|
|
||||||
return CharArrayUtils.hash(key) & (hashTable.length - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private final int add(char[] key, Object value) {
|
|
||||||
keyTable[currEntry] = key;
|
|
||||||
valueTable[currEntry] = value;
|
|
||||||
return ++currEntry;
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns the overwritten object if there was one
|
|
||||||
public Object put(char[] key, Object value) {
|
|
||||||
try {
|
|
||||||
int hash = hash(key);
|
|
||||||
int i = hashTable[hash] - 1;
|
|
||||||
if (i < 0) {
|
|
||||||
// Nobody here
|
|
||||||
hashTable[hash] = add(key, value);
|
|
||||||
} else {
|
|
||||||
// See if the key is already defined
|
|
||||||
int last = i;
|
|
||||||
while (i >= 0) {
|
|
||||||
if (CharArrayUtils.equals(key, keyTable[i])) {
|
|
||||||
Object oldvalue = valueTable[i];
|
|
||||||
valueTable[i] = value;
|
|
||||||
// Nothing left to do, escape...
|
|
||||||
return oldvalue;
|
|
||||||
}
|
|
||||||
last = i;
|
|
||||||
i = nextTable[i] - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not there, time to add
|
|
||||||
nextTable[last] = add(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
} catch (IndexOutOfBoundsException e) {
|
|
||||||
// Oops, too many, resize and try again
|
|
||||||
char[][] oldKeyTable = keyTable;
|
|
||||||
Object[] oldValueTable = valueTable;
|
|
||||||
|
|
||||||
int newSize = hashTable.length << 1;
|
|
||||||
createTables(newSize);
|
|
||||||
for (int i = 0; i < oldKeyTable.length; ++i)
|
|
||||||
put(oldKeyTable[i], oldValueTable[i]);
|
|
||||||
|
|
||||||
return put(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object get(char[] key) {
|
|
||||||
int hash = hash(key);
|
|
||||||
int i = hashTable[hash] - 1;
|
|
||||||
while (i >= 0) {
|
|
||||||
if (CharArrayUtils.equals(key, keyTable[i]))
|
|
||||||
return valueTable[i];
|
|
||||||
i = nextTable[i] - 1;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -309,6 +309,9 @@ public interface IToken {
|
||||||
|
|
||||||
static public final int t_restrict = 137;
|
static public final int t_restrict = 137;
|
||||||
|
|
||||||
static public final int tLAST = t_restrict;
|
static public final int tMACROEXP = 138;
|
||||||
|
|
||||||
|
static public final int tPOUNDPOUND = 139;
|
||||||
|
|
||||||
|
static public final int tLAST = 139;
|
||||||
}
|
}
|
|
@ -21,7 +21,7 @@ import org.eclipse.cdt.core.parser.ISourceElementRequestor;
|
||||||
*/
|
*/
|
||||||
public class ScannerUtility {
|
public class ScannerUtility {
|
||||||
|
|
||||||
static String reconcilePath(String originalPath ) {
|
public static String reconcilePath(String originalPath ) {
|
||||||
if( originalPath == null ) return null;
|
if( originalPath == null ) return null;
|
||||||
originalPath = removeQuotes( originalPath );
|
originalPath = removeQuotes( originalPath );
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ public class ScannerUtility {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static CodeReader createReaderDuple( String path, ISourceElementRequestor requestor, Iterator workingCopies )
|
public static CodeReader createReaderDuple( String path, ISourceElementRequestor requestor, Iterator workingCopies )
|
||||||
{
|
{
|
||||||
return requestor.createReader( path, workingCopies );
|
return requestor.createReader( path, workingCopies );
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ public class ScannerUtility {
|
||||||
return reconcilePath( newPathBuffer.toString() );
|
return reconcilePath( newPathBuffer.toString() );
|
||||||
}
|
}
|
||||||
|
|
||||||
static class InclusionDirective
|
public static class InclusionDirective
|
||||||
{
|
{
|
||||||
public InclusionDirective( String fileName, boolean useIncludePaths, int startOffset, int endOffset )
|
public InclusionDirective( String fileName, boolean useIncludePaths, int startOffset, int endOffset )
|
||||||
{
|
{
|
||||||
|
@ -121,7 +121,7 @@ public class ScannerUtility {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class InclusionParseException extends Exception
|
public static class InclusionParseException extends Exception
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
/**********************************************************************
|
||||||
|
* Copyright (c) 2004 IBM and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Common Public License v0.5
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/cpl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* IBM - Initial implementation
|
||||||
|
***********************************************************************/
|
||||||
|
package org.eclipse.cdt.internal.core.parser.scanner2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Doug Schaefer
|
||||||
|
*/
|
||||||
|
public class CharArrayIntMap extends CharArrayMap {
|
||||||
|
|
||||||
|
private int[] valueTable;
|
||||||
|
|
||||||
|
public CharArrayIntMap(int initialSize) {
|
||||||
|
super(initialSize);
|
||||||
|
valueTable = new int[capacity()];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void resize(int size) {
|
||||||
|
int[] oldValueTable = valueTable;
|
||||||
|
valueTable = new int[size];
|
||||||
|
System.arraycopy(oldValueTable, 0, valueTable, 0, oldValueTable.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int put(char[] key, int start, int length, int value) {
|
||||||
|
int i = add(key, start, length);
|
||||||
|
int oldvalue = valueTable[i];
|
||||||
|
valueTable[i] = value;
|
||||||
|
return oldvalue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int put(char[] key, int value) {
|
||||||
|
return put(key, 0, key.length, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int get(char[] key, int start, int length) {
|
||||||
|
int i = lookup(key, start, length);
|
||||||
|
if (i >= 0)
|
||||||
|
return valueTable[i];
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,137 @@
|
||||||
|
/**********************************************************************
|
||||||
|
* Copyright (c) 2004 IBM and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Common Public License v0.5
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/cpl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* IBM - Initial implementation
|
||||||
|
***********************************************************************/
|
||||||
|
package org.eclipse.cdt.internal.core.parser.scanner2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Doug Schaefer
|
||||||
|
*/
|
||||||
|
public abstract class CharArrayMap {
|
||||||
|
|
||||||
|
private char[][] keyTable;
|
||||||
|
private int[] hashTable;
|
||||||
|
private int[] nextTable;
|
||||||
|
protected int currEntry = -1;
|
||||||
|
|
||||||
|
protected CharArrayMap(int initialSize) {
|
||||||
|
int size = 1;
|
||||||
|
while (size < initialSize)
|
||||||
|
size <<= 1;
|
||||||
|
|
||||||
|
keyTable = new char[size][];
|
||||||
|
hashTable = new int[size * 2];
|
||||||
|
nextTable = new int[size];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int capacity() {
|
||||||
|
return keyTable.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int hash(char[] buffer, int start, int len) {
|
||||||
|
return CharArrayUtils.hash(buffer, start, len) & (keyTable.length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insert(int i) {
|
||||||
|
insert(i, hash(keyTable[i], 0, keyTable[i].length));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insert(int i, int hash) {
|
||||||
|
|
||||||
|
if (hashTable[hash] == 0) {
|
||||||
|
hashTable[hash] = i + 1;
|
||||||
|
} else {
|
||||||
|
// need to link
|
||||||
|
int j = hashTable[hash] - 1;
|
||||||
|
while (nextTable[j] != 0)
|
||||||
|
j = nextTable[j] - 1;
|
||||||
|
nextTable[j] = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void resize(int size) {
|
||||||
|
char[][] oldKeyTable = keyTable;
|
||||||
|
keyTable = new char[size][];
|
||||||
|
System.arraycopy(oldKeyTable, 0, keyTable, 0, oldKeyTable.length);
|
||||||
|
|
||||||
|
// Need to rehash everything
|
||||||
|
hashTable = new int[size * 2];
|
||||||
|
nextTable = new int[size];
|
||||||
|
for (int i = 0; i < oldKeyTable.length; ++i) {
|
||||||
|
insert(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resize() {
|
||||||
|
resize(keyTable.length << 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final int add(char[] buffer, int start, int len) {
|
||||||
|
int hash = hash(buffer, start, len);
|
||||||
|
|
||||||
|
if (hashTable[hash] == 0) {
|
||||||
|
keyTable[++currEntry] = CharArrayUtils.extract(buffer, start, len);
|
||||||
|
insert(currEntry, hash);
|
||||||
|
return currEntry;
|
||||||
|
} else {
|
||||||
|
// is the key already registered?
|
||||||
|
int i = hashTable[hash] - 1;
|
||||||
|
if (CharArrayUtils.equals(buffer, start, len, keyTable[i]))
|
||||||
|
// yup
|
||||||
|
return i;
|
||||||
|
|
||||||
|
// follow the next chain
|
||||||
|
int last = i;
|
||||||
|
for (i = nextTable[i] - 1; i >= 0; i = nextTable[i] - 1) {
|
||||||
|
if (CharArrayUtils.equals(buffer, start, len, keyTable[i]))
|
||||||
|
// yup this time
|
||||||
|
return i;
|
||||||
|
last = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// nope, add it in
|
||||||
|
keyTable[++currEntry] = CharArrayUtils.extract(buffer, start, len);
|
||||||
|
nextTable[last] = currEntry + 1;
|
||||||
|
return currEntry;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final int lookup(char[] buffer, int start, int len) {
|
||||||
|
int hash = hash(buffer, start, len);
|
||||||
|
|
||||||
|
if (hashTable[hash] == 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
int i = hashTable[hash] - 1;
|
||||||
|
if (CharArrayUtils.equals(buffer, start, len, keyTable[i]))
|
||||||
|
return i;
|
||||||
|
|
||||||
|
// Follow the next chain
|
||||||
|
for (i = nextTable[i] - 1; i >= 0; i = nextTable[i] - 1)
|
||||||
|
if (CharArrayUtils.equals(buffer, start, len, keyTable[i]))
|
||||||
|
return i;
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dumpNexts() {
|
||||||
|
for (int i = 0; i < nextTable.length; ++i) {
|
||||||
|
if (nextTable[i] == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
System.out.print(i);
|
||||||
|
|
||||||
|
for (int j = nextTable[i] - 1; j >= 0; j = nextTable[j] - 1)
|
||||||
|
System.out.print(" -> " + j);
|
||||||
|
|
||||||
|
System.out.println("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
/**********************************************************************
|
||||||
|
* Copyright (c) 2004 IBM and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Common Public License v0.5
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/cpl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* IBM - Initial implementation
|
||||||
|
***********************************************************************/
|
||||||
|
package org.eclipse.cdt.internal.core.parser.scanner2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Doug Schaefer
|
||||||
|
*/
|
||||||
|
public class CharArrayObjectMap extends CharArrayMap {
|
||||||
|
|
||||||
|
private Object[] valueTable;
|
||||||
|
|
||||||
|
public CharArrayObjectMap(int initialSize) {
|
||||||
|
super(initialSize);
|
||||||
|
valueTable = new Object[capacity()];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void resize(int size) {
|
||||||
|
Object[] oldValueTable = valueTable;
|
||||||
|
valueTable = new Object[size];
|
||||||
|
System.arraycopy(oldValueTable, 0, valueTable, 0, oldValueTable.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object put(char[] key, int start, int length, Object value) {
|
||||||
|
int i = add(key, start, length);
|
||||||
|
Object oldvalue = valueTable[i];
|
||||||
|
valueTable[i] = value;
|
||||||
|
return oldvalue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object put(char[] key, Object value) {
|
||||||
|
return put(key, 0, key.length, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(char[] key, int start, int length) {
|
||||||
|
int i = lookup(key, start, length);
|
||||||
|
if (i >= 0)
|
||||||
|
return valueTable[i];
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(char[] key) {
|
||||||
|
return get(key, 0, key.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
* TODO To change the template for this generated file go to
|
* TODO To change the template for this generated file go to
|
||||||
* Window - Preferences - Java - Code Style - Code Templates
|
* Window - Preferences - Java - Code Style - Code Templates
|
||||||
*/
|
*/
|
||||||
package org.eclipse.cdt.core.parser;
|
package org.eclipse.cdt.internal.core.parser.scanner2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author dschaefe
|
* @author dschaefe
|
|
@ -4,7 +4,7 @@
|
||||||
* TODO To change the template for this generated file go to
|
* TODO To change the template for this generated file go to
|
||||||
* Window - Preferences - Java - Code Style - Code Templates
|
* Window - Preferences - Java - Code Style - Code Templates
|
||||||
*/
|
*/
|
||||||
package org.eclipse.cdt.core.parser;
|
package org.eclipse.cdt.internal.core.parser.scanner2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author dschaefe
|
* @author dschaefe
|
||||||
|
@ -32,9 +32,6 @@ public class CharArrayUtils {
|
||||||
if (str1 == str2)
|
if (str1 == str2)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (str1 == null || str2 == null)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (str1.length != str2.length)
|
if (str1.length != str2.length)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -44,4 +41,24 @@ public class CharArrayUtils {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final boolean equals(char[] str1, int start1, int length1, char[] str2) {
|
||||||
|
if (length1 != str2.length)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (int i = 0; i < length1; ++i)
|
||||||
|
if (str1[start1++] != str2[i])
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final char[] extract(char[] str, int start, int length) {
|
||||||
|
if (start == 0 && length == str.length)
|
||||||
|
return str;
|
||||||
|
|
||||||
|
char[] copy = new char[length];
|
||||||
|
System.arraycopy(str, start, copy, 0, length);
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2004 IBM Corporation and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Common Public License v0.5
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/cpl-v05.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* IBM Corporation - initial implementation
|
||||||
|
******************************************************************************/
|
||||||
|
package org.eclipse.cdt.internal.core.parser.scanner2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Doug Schaefer
|
||||||
|
*/
|
||||||
|
public class FunctionStyleMacro extends ObjectStyleMacro {
|
||||||
|
|
||||||
|
public char[][] arglist;
|
||||||
|
|
||||||
|
public FunctionStyleMacro(char[] name, char[] expansion, char[][] arglist) {
|
||||||
|
super(name, expansion);
|
||||||
|
this.arglist = arglist;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Expansion {
|
||||||
|
|
||||||
|
public final CharArrayObjectMap definitions
|
||||||
|
= new CharArrayObjectMap(FunctionStyleMacro.this.arglist.length);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,134 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2004 IBM Corporation and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Common Public License v0.5
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/cpl-v05.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* IBM Corporation - initial implementation
|
||||||
|
******************************************************************************/
|
||||||
|
package org.eclipse.cdt.internal.core.parser.scanner2;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.parser.IToken;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Doug Schaefer
|
||||||
|
*/
|
||||||
|
public class MacroExpansionToken implements IToken {
|
||||||
|
|
||||||
|
public MacroExpansionToken() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#getType()
|
||||||
|
*/
|
||||||
|
public int getType() {
|
||||||
|
return IToken.tMACROEXP;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#getImage()
|
||||||
|
*/
|
||||||
|
public String getImage() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#getOffset()
|
||||||
|
*/
|
||||||
|
public int getOffset() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#getLength()
|
||||||
|
*/
|
||||||
|
public int getLength() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#getEndOffset()
|
||||||
|
*/
|
||||||
|
public int getEndOffset() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#getLineNumber()
|
||||||
|
*/
|
||||||
|
public int getLineNumber() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#getNext()
|
||||||
|
*/
|
||||||
|
public IToken getNext() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#setImage(java.lang.String)
|
||||||
|
*/
|
||||||
|
public void setImage(String i) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#setNext(org.eclipse.cdt.core.parser.IToken)
|
||||||
|
*/
|
||||||
|
public void setNext(IToken t) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#setType(int)
|
||||||
|
*/
|
||||||
|
public void setType(int i) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#looksLikeExpression()
|
||||||
|
*/
|
||||||
|
public boolean looksLikeExpression() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#isPointer()
|
||||||
|
*/
|
||||||
|
public boolean isPointer() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#isOperator()
|
||||||
|
*/
|
||||||
|
public boolean isOperator() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.cdt.core.parser.IToken#canBeAPrefix()
|
||||||
|
*/
|
||||||
|
public boolean canBeAPrefix() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2004 IBM Corporation and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Common Public License v0.5
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/cpl-v05.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* IBM Corporation - initial implementation
|
||||||
|
******************************************************************************/
|
||||||
|
package org.eclipse.cdt.internal.core.parser.scanner2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Doug Schaefer
|
||||||
|
*/
|
||||||
|
public class ObjectStyleMacro {
|
||||||
|
|
||||||
|
public char[] name;
|
||||||
|
public char[] expansion;
|
||||||
|
|
||||||
|
public ObjectStyleMacro(char[] name, char[] expansion) {
|
||||||
|
this.name = name;
|
||||||
|
this.expansion = expansion;
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue