mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 22:52:11 +02:00
Fixed NumberFormatException in TokenFactory for large int's.
Fixed numerous SelectionParse problems found through my own experimentation.
This commit is contained in:
parent
66ef2bac23
commit
e856459e93
8 changed files with 105 additions and 12 deletions
|
@ -16,9 +16,9 @@ import org.eclipse.cdt.core.parser.NullLogService;
|
|||
import org.eclipse.cdt.core.parser.ParserFactory;
|
||||
import org.eclipse.cdt.core.parser.ParserLanguage;
|
||||
import org.eclipse.cdt.core.parser.ParserMode;
|
||||
import org.eclipse.cdt.core.parser.ParserUtil;
|
||||
import org.eclipse.cdt.core.parser.ScannerInfo;
|
||||
import org.eclipse.cdt.core.parser.ast.IASTNode;
|
||||
import org.eclipse.cdt.internal.core.parser.InternalParserUtil;
|
||||
|
||||
/**
|
||||
* @author johnc
|
||||
|
@ -54,7 +54,7 @@ public class SelectionParseBaseTest extends CompleteParseBaseTest {
|
|||
callback,
|
||||
ParserMode.SELECTION_PARSE,
|
||||
ParserLanguage.CPP,
|
||||
ParserUtil.getParserLogService());
|
||||
InternalParserUtil.createDefaultLogService());
|
||||
|
||||
IParser.ISelectionParseResult result =parser.parse( offset1, offset2 );
|
||||
if( expectedToPass )
|
||||
|
|
|
@ -14,7 +14,6 @@ import java.io.StringWriter;
|
|||
import java.io.Writer;
|
||||
|
||||
import org.eclipse.cdt.core.parser.ast.IASTClassSpecifier;
|
||||
import org.eclipse.cdt.core.parser.ast.IASTField;
|
||||
import org.eclipse.cdt.core.parser.ast.IASTFunction;
|
||||
import org.eclipse.cdt.core.parser.ast.IASTMethod;
|
||||
import org.eclipse.cdt.core.parser.ast.IASTNamespaceDefinition;
|
||||
|
@ -237,4 +236,30 @@ public class SelectionParseTest extends SelectionParseBaseTest {
|
|||
}
|
||||
}
|
||||
|
||||
public void testMethodReference() throws Exception
|
||||
{
|
||||
Writer writer = new StringWriter();
|
||||
writer.write( "class Sample { public:\n"); //$NON-NLS-1$
|
||||
writer.write( " int getAnswer() const;\n"); //$NON-NLS-1$
|
||||
writer.write( "};\n"); //$NON-NLS-1$
|
||||
writer.write( "int main(int argc, char **argv) {\n" ); //$NON-NLS-1$
|
||||
writer.write( " Sample * s = new Sample();\n" ); //$NON-NLS-1$
|
||||
writer.write( " return s->getAnswer();\n" ); //$NON-NLS-1$
|
||||
writer.write( "}\n" ); //$NON-NLS-1$
|
||||
String code = writer.toString();
|
||||
int startIndex = code.indexOf( "->getAnswer") + 2; //$NON-NLS-1$
|
||||
IASTNode node = parse( code, startIndex, startIndex+9);
|
||||
assertTrue( node instanceof IASTMethod );
|
||||
assertEquals( ((IASTMethod)node).getName(), "getAnswer" ); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
public void testConstructorDefinition() throws Exception
|
||||
{
|
||||
String code = "class ABC { public: ABC(); }; ABC::ABC(){}"; //$NON-NLS-1$
|
||||
int startIndex = code.indexOf( "::ABC") + 2; //$NON-NLS-1$
|
||||
IASTNode node = parse( code, startIndex, startIndex + 3 );
|
||||
assertTrue( node instanceof IASTMethod );
|
||||
IASTMethod constructor = (IASTMethod) node;
|
||||
assertTrue( constructor.isConstructor() );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,4 +58,8 @@ public interface ITokenDuple {
|
|||
* @return
|
||||
*/
|
||||
public boolean contains(ITokenDuple duple);
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
public abstract String [] toQualifiedName();
|
||||
}
|
|
@ -183,7 +183,7 @@ public abstract class Parser extends ExpressionParser implements IParser
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.traceLog( "Parser::translationUnit: Unexpected exception occurred : " + e.getMessage() ); //$NON-NLS-1$
|
||||
logException( "translationUnit", e ); //$NON-NLS-1$
|
||||
failParse();
|
||||
}
|
||||
}
|
||||
|
@ -1087,6 +1087,11 @@ public abstract class Parser extends ExpressionParser implements IParser
|
|||
{
|
||||
throw backtrack;
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
logException( "simpleDecl", e ); //$NON-NLS-1$
|
||||
throw backtrack;
|
||||
}
|
||||
Iterator i = l.iterator();
|
||||
if (hasFunctionBody && l.size() != 1)
|
||||
{
|
||||
|
@ -2813,6 +2818,7 @@ public abstract class Parser extends ExpressionParser implements IParser
|
|||
consume(IToken.t_case);
|
||||
IASTExpression constant_expression = constantExpression(scope, CompletionKind.SINGLE_NAME_REFERENCE, Key.EXPRESSION );
|
||||
constant_expression.acceptElement(requestor);
|
||||
endExpression(constant_expression);
|
||||
consume(IToken.tCOLON);
|
||||
statement(scope);
|
||||
cleanupLastToken();
|
||||
|
@ -2888,6 +2894,7 @@ public abstract class Parser extends ExpressionParser implements IParser
|
|||
{
|
||||
IASTExpression finalExpression = expression(scope, CompletionKind.SINGLE_NAME_REFERENCE, Key.DECLARATION);
|
||||
finalExpression.acceptElement(requestor);
|
||||
endExpression(finalExpression);
|
||||
}
|
||||
consume(IToken.tRPAREN);
|
||||
statement(scope);
|
||||
|
@ -2909,6 +2916,7 @@ public abstract class Parser extends ExpressionParser implements IParser
|
|||
{
|
||||
IASTExpression retVal = expression(scope, CompletionKind.SINGLE_NAME_REFERENCE, Key.EXPRESSION);
|
||||
retVal.acceptElement(requestor);
|
||||
endExpression(retVal);
|
||||
}
|
||||
consume(IToken.tSEMI);
|
||||
cleanupLastToken();
|
||||
|
@ -2950,7 +2958,7 @@ public abstract class Parser extends ExpressionParser implements IParser
|
|||
IASTExpression expressionStatement = expression(scope, CompletionKind.SINGLE_NAME_REFERENCE, Key.STATEMENT);
|
||||
consume(IToken.tSEMI);
|
||||
expressionStatement.acceptElement( requestor );
|
||||
endExpressionStatement(expressionStatement);
|
||||
endExpression(expressionStatement);
|
||||
return;
|
||||
}
|
||||
catch (BacktrackException b)
|
||||
|
@ -3014,7 +3022,7 @@ public abstract class Parser extends ExpressionParser implements IParser
|
|||
{
|
||||
IASTExpression someExpression = expression( scope, CompletionKind.SINGLE_NAME_REFERENCE, Key.EXPRESSION );
|
||||
someExpression.acceptElement(requestor);
|
||||
//TODO type-specifier-seq declarator = assignment expression
|
||||
endExpression(someExpression);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3188,7 +3196,7 @@ public abstract class Parser extends ExpressionParser implements IParser
|
|||
}
|
||||
|
||||
|
||||
protected void endExpressionStatement( IASTExpression expression ) throws EndOfFileException
|
||||
protected void endExpression( IASTExpression expression ) throws EndOfFileException
|
||||
{
|
||||
cleanupLastToken();
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
***********************************************************************/
|
||||
package org.eclipse.cdt.internal.core.parser;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
@ -33,6 +34,7 @@ import org.eclipse.cdt.core.parser.ast.IASTInitializerClause;
|
|||
import org.eclipse.cdt.core.parser.ast.IASTNode;
|
||||
import org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement;
|
||||
import org.eclipse.cdt.core.parser.ast.IASTParameterDeclaration;
|
||||
import org.eclipse.cdt.core.parser.ast.IASTQualifiedNameElement;
|
||||
import org.eclipse.cdt.core.parser.ast.IASTScope;
|
||||
import org.eclipse.cdt.core.parser.ast.IASTVariable;
|
||||
import org.eclipse.cdt.core.parser.extension.IParserExtension;
|
||||
|
@ -198,6 +200,12 @@ public class SelectionParser extends ContextualParser {
|
|||
if( ((IASTOffsetableNamedElement)contextNode).getName().equals( finalDuple.toString() ) )
|
||||
return contextNode;
|
||||
}
|
||||
if( contextNode instanceof IASTQualifiedNameElement )
|
||||
{
|
||||
String [] elementQualifiedName = ((IASTQualifiedNameElement)contextNode).getFullyQualifiedName();
|
||||
if( Arrays.equals( elementQualifiedName, finalDuple.toQualifiedName() ) )
|
||||
return contextNode;
|
||||
}
|
||||
try {
|
||||
if( ourKind == IASTCompletionNode.CompletionKind.NEW_TYPE_REFERENCE )
|
||||
{
|
||||
|
@ -293,10 +301,10 @@ public class SelectionParser extends ContextualParser {
|
|||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.internal.core.parser.Parser#endExpressionStatement(org.eclipse.cdt.core.parser.ast.IASTExpression)
|
||||
*/
|
||||
protected void endExpressionStatement(IASTExpression expression)
|
||||
protected void endExpression(IASTExpression expression)
|
||||
throws EndOfFileException {
|
||||
if( ! tokenDupleCompleted() )
|
||||
super.endExpressionStatement(expression);
|
||||
super.endExpression(expression);
|
||||
else
|
||||
{
|
||||
contextNode = expression;
|
||||
|
|
|
@ -71,9 +71,15 @@ public abstract class AbstractToken implements IToken {
|
|||
* @see org.eclipse.cdt.core.parser.IToken#isKeyword()
|
||||
*/
|
||||
public boolean canBeAPrefix() {
|
||||
if( getType() == tIDENTIFIER ) return true;
|
||||
if( getType() >= t_and && getType() <= t_xor_eq ) return true;
|
||||
if( getType() >= t__Bool && getType() <= t_restrict ) return true;
|
||||
switch( getType() )
|
||||
{
|
||||
case tIDENTIFIER:
|
||||
case tCOMPL:
|
||||
return true;
|
||||
default:
|
||||
if( getType() >= t_and && getType() <= t_xor_eq ) return true;
|
||||
if( getType() >= t__Bool && getType() <= t_restrict ) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -191,6 +191,7 @@ public class TokenDuple implements ITokenDuple {
|
|||
private static final Integer LT = new Integer( IToken.tLT );
|
||||
private static final Integer LBRACKET = new Integer( IToken.tLBRACKET );
|
||||
private static final Integer LPAREN = new Integer( IToken.tLPAREN );
|
||||
private String [] qualifiedName = null;
|
||||
|
||||
public IToken consumeTemplateIdArguments( IToken name, Iterator iter ){
|
||||
IToken token = name;
|
||||
|
@ -494,4 +495,43 @@ public class TokenDuple implements ITokenDuple {
|
|||
|
||||
return ( foundFirst && foundLast );
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.core.parser.ITokenDuple#toQualifiedName()
|
||||
*/
|
||||
public String[] toQualifiedName() {
|
||||
if( qualifiedName == null )
|
||||
generateQualifiedName();
|
||||
return qualifiedName;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private void generateQualifiedName() {
|
||||
List qn = new ArrayList();
|
||||
Iterator i = iterator();
|
||||
while( i.hasNext() )
|
||||
{
|
||||
IToken t = (IToken) i.next();
|
||||
boolean compl = false;
|
||||
if( t.getType() == IToken.tCOLONCOLON ) continue;
|
||||
if( t.getType() == IToken.tCOMPL )
|
||||
{
|
||||
compl = true;
|
||||
if( !i.hasNext() ) break;
|
||||
t = (IToken) i.next();
|
||||
}
|
||||
if( t.getType() == IToken.tIDENTIFIER )
|
||||
{
|
||||
if( compl )
|
||||
qn.add( "~" + t.getImage() ); //$NON-NLS-1$
|
||||
else
|
||||
qn.add( t.getImage() );
|
||||
}
|
||||
}
|
||||
qualifiedName = new String[ qn.size() ];
|
||||
qualifiedName = (String[]) qn.toArray( qualifiedName );
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,8 @@ public class TokenFactory {
|
|||
|
||||
public static IToken createIntegerToken( String value, IScannerData scannerData )
|
||||
{
|
||||
if( value.length() > 15 )
|
||||
return createUniquelyImagedToken( IToken.tINTEGER, value, scannerData );
|
||||
if( scannerData.getContextStack().getCurrentContext().getMacroOffset() >= 0 )
|
||||
return new IntegerExpansionToken( IToken.tINTEGER, Integer.parseInt(value ), scannerData.getContextStack() );
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue