1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-09 18:56:02 +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(); String name = currName.toString();
Structure elem = ((Structure)container.getElement()); Structure elem = ((Structure)container.getElement());
elem.setElementName( name ); elem.setElementName( name );
elem.setIdPos(currName.getEndOffset(), name.length()); elem.setIdPos(currName.getStartOffset(), name.length());
elem.setPos(currName.getEndOffset(), 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 // hook up the offsets
declaration.setIdPos( declaration.setIdPos( currentDeclarator.getName().getStartOffset(), currentDeclarator.getName().toString().length());
currentDeclarator.getName().getEndOffset(),currentDeclarator.getName().toString().length()); declaration.setPos( currentDeclarator.getName().getStartOffset(), currentDeclarator.getName().toString().length() );
declaration.setPos( currentDeclarator.getName().getEndOffset(), currentDeclarator.getName().toString().length() );
// add to parent // add to parent
parentElement.addChild( declaration ); parentElement.addChild( declaration );

View file

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

View file

@ -800,6 +800,10 @@ public class ScannerTestCase extends TestCase
validateEOF(); validateEOF();
validateBalance(); validateBalance();
} }
catch (Exception e) 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());
}
}
} }