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

Patch for John Camelon:

- fixed qualified name offsets in outline view
- added loop-detection code to Parser
- added Scanner testcase to validate Sebastien's comments
- first pass at basic declarator initializers
This commit is contained in:
Doug Schaefer 2003-03-06 19:29:36 +00:00
parent 5c44e281ca
commit 40b44d0371
4 changed files with 108 additions and 20 deletions

View file

@ -72,8 +72,8 @@ public class NewModelBuilder implements IParserCallback {
String name = currName.toString();
Structure elem = ((Structure)container.getElement());
elem.setElementName( name );
elem.setIdPos(currName.getEndOffset(), name.length());
elem.setPos(currName.getEndOffset(), name.length());
elem.setIdPos(currName.getStartOffset(), name.length());
elem.setPos(currName.getStartOffset(), name.length());
}
/**

View file

@ -115,9 +115,8 @@ public class SimpleDeclarationWrapper extends DeclSpecifier implements Declarati
}
// hook up the offsets
declaration.setIdPos(
currentDeclarator.getName().getEndOffset(),currentDeclarator.getName().toString().length());
declaration.setPos( currentDeclarator.getName().getEndOffset(), currentDeclarator.getName().toString().length() );
declaration.setIdPos( currentDeclarator.getName().getStartOffset(), currentDeclarator.getName().toString().length());
declaration.setPos( currentDeclarator.getName().getStartOffset(), currentDeclarator.getName().toString().length() );
// add to parent
parentElement.addChild( declaration );

View file

@ -78,9 +78,13 @@ c, quick);
public void translationUnit() throws Exception {
Object translationUnit = callback.translationUnitBegin();
Token lastBacktrack = null;
Token lastToken = null;
while (LT(1) != Token.tEOF) {
try {
lastToken = currToken;
declaration( translationUnit );
if( currToken == lastToken )
skipToNextSemi();
} catch (Backtrack b) {
// Mark as failure and try to reach a recovery point
parsePassed = false;
@ -88,12 +92,7 @@ c, quick);
if (lastBacktrack != null && lastBacktrack == LA(1)) {
// we haven't progressed from the last backtrack
// try and find tne next definition
for (int t = LT(1); t != Token.tEOF; t = LT(1)) {
consume();
// TO DO: we should really check for matching braces too
if (t == Token.tSEMI)
break;
}
skipToNextSemi();
} else {
// start again from here
lastBacktrack = LA(1);
@ -102,6 +101,15 @@ c, quick);
}
callback.translationUnitEnd(translationUnit);
}
public void skipToNextSemi() {
for (int t = LT(1); t != Token.tEOF; t = LT(1)) {
consume();
// TO DO: we should really check for matching braces too
if (t == Token.tSEMI)
break;
}
}
/**
* declaration
@ -406,10 +414,21 @@ c, quick);
* - handle initializers
*/
public void initDeclarator( Object owner ) throws Exception {
declarator( owner );
Object declarator = declarator( owner );
// handle = initializerClause
if (LT(1) == Token.tASSIGN) {
consume();
consume();
// assignmentExpression || { initializerList , } || { }
try
{
assignmentExpression();
}
catch( Backtrack b )
{
// doNothing
}
if (LT(1) == Token.tLBRACE) {
// for now, just consume to matching brace
@ -430,6 +449,8 @@ c, quick);
}
}
}
callback.declaratorEnd( declarator );
}
/**
@ -446,12 +467,10 @@ c, quick);
* declaratorId
* : name
*/
public void declarator( Object container ) throws Exception {
public Object declarator( Object container ) throws Exception {
boolean aborted;
do
{
aborted = false;
Object declarator = callback.declaratorBegin( container );
for (;;) {
@ -466,7 +485,7 @@ c, quick);
consume();
declarator(declarator);
consume(Token.tRPAREN);
return;
return declarator;
}
name();
@ -509,11 +528,10 @@ c, quick);
{
callback.declaratorAbort( container, declarator );
declarator = null;
aborted = true;
}
else
callback.declaratorEnd(declarator);
} while( aborted );
return declarator;
} while( true );
}
/**

View file

@ -799,6 +799,10 @@ public class ScannerTestCase extends TestCase
initializeScanner("#if defined( NOTHING ) \nint x = NOTHING;\n#endif");
validateEOF();
validateBalance();
}
catch (Exception e)
@ -987,4 +991,71 @@ public class ScannerTestCase extends TestCase
}
}
public void testConditionalWithBraces()
{
try
{
for( int i = 0; i < 4; ++i )
{
initializeScanner( "int foobar(int a) { if(a == 0) {\n#ifdef THIS\n} else {}\n#elif THAT\n} else {}\n#endif\nreturn 0;}" );
switch( i )
{
case 0:
scanner.addDefinition( "THIS", "1");
scanner.addDefinition( "THAT", "1" );
break;
case 1:
scanner.addDefinition( "THIS", "1");
scanner.addDefinition( "THAT", "0" );
break;
case 2:
scanner.addDefinition( "THAT", "1" );
break;
case 3:
scanner.addDefinition( "THAT", "0" );
break;
}
validateToken( Token.t_int );
validateIdentifier( "foobar");
validateToken( Token.tLPAREN );
validateToken( Token.t_int );
validateIdentifier( "a" );
validateToken( Token.tRPAREN );
validateToken( Token.tLBRACE );
validateToken( Token.t_if );
validateToken( Token.tLPAREN );
validateIdentifier( "a" );
validateToken( Token.tEQUAL );
validateInteger( "0" );
validateToken( Token.tRPAREN );
validateToken( Token.tLBRACE );
if( i <= 1 )
{
validateToken( Token.tRBRACE );
validateToken( Token.t_else );
validateToken( Token.tLBRACE );
validateToken( Token.tRBRACE );
}
if( i == 2 )
{
validateToken( Token.tRBRACE );
validateToken( Token.t_else );
validateToken( Token.tLBRACE );
validateToken( Token.tRBRACE );
}
validateToken( Token.t_return );
validateInteger( "0");
validateToken( Token.tSEMI );
validateToken( Token.tRBRACE );
validateToken( Token.tEOF );
}
} catch( ScannerException se )
{
fail(EXCEPTION_THROWN + se.toString());
}
}
}