[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[cdt-patch] Applied [HEAD] - Beginning of SymbolTable integration work for Parser
|
A Niefer/Camelon collaborative effort.
Core
Added Symboltable infrastructure into main
parser. Hooked up namespaces properly.
Tests
Added CrossReferenceTests to ParserTestSuite
to test symbol-table/DOM interworking.
JohnC
Index: dom/org/eclipse/cdt/internal/core/dom/DOMBuilder.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/dom/org/eclipse/cdt/internal/core/dom/DOMBuilder.java,v
retrieving revision 1.29
diff -u -r1.29 DOMBuilder.java
--- dom/org/eclipse/cdt/internal/core/dom/DOMBuilder.java 28 Apr 2003 16:00:14 -0000 1.29
+++ dom/org/eclipse/cdt/internal/core/dom/DOMBuilder.java 5 May 2003 20:27:28 -0000
@@ -935,5 +935,15 @@
BitField b = (BitField)bitfield;
b.getOwnerDeclarator().setBitField( b );
}
+
+ /* (non-Javadoc)
+ * @see org.eclipse.cdt.internal.core.parser.IParserCallback#simpleDeclSpecifierType(java.lang.Object, java.lang.Object)
+ */
+ public void simpleDeclSpecifierType(Object declaration, Object type) {
+ if( type instanceof TypeSpecifier )
+ {
+ System.out.println( "Told you so!");
+ }
+ }
}
Index: dom/org/eclipse/cdt/internal/core/dom/ElaboratedTypeSpecifier.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/dom/org/eclipse/cdt/internal/core/dom/ElaboratedTypeSpecifier.java,v
retrieving revision 1.4
diff -u -r1.4 ElaboratedTypeSpecifier.java
--- dom/org/eclipse/cdt/internal/core/dom/ElaboratedTypeSpecifier.java 9 Apr 2003 21:11:59 -0000 1.4
+++ dom/org/eclipse/cdt/internal/core/dom/ElaboratedTypeSpecifier.java 5 May 2003 20:27:28 -0000
@@ -29,5 +29,20 @@
public void setName(Name n) { name = n; }
public Name getName() { return name; }
+ private ClassSpecifier classSpec = null;
+
+ /**
+ * @return
+ */
+ public ClassSpecifier getClassSpec() {
+ return classSpec;
+ }
+
+ /**
+ * @param specifier
+ */
+ public void setClassSpec(ClassSpecifier specifier) {
+ classSpec = specifier;
+ }
}
Index: parser/ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/ChangeLog,v
retrieving revision 1.47
diff -u -r1.47 ChangeLog
--- parser/ChangeLog 5 May 2003 17:01:00 -0000 1.47
+++ parser/ChangeLog 5 May 2003 20:27:28 -0000
@@ -1,3 +1,6 @@
+2003-05-05 John Camelon/Andrew Niefer
+ Added Symboltable infrastructure into main parser.
+
2003-05-05 Andrew Niefer
Structural changes to ParserSymbolTable:
- moved TypeInfo & Declaration inside ParserSymbolTable
Index: parser/org/eclipse/cdt/internal/core/parser/ExpressionEvaluator.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ExpressionEvaluator.java,v
retrieving revision 1.23
diff -u -r1.23 ExpressionEvaluator.java
--- parser/org/eclipse/cdt/internal/core/parser/ExpressionEvaluator.java 25 Apr 2003 16:13:17 -0000 1.23
+++ parser/org/eclipse/cdt/internal/core/parser/ExpressionEvaluator.java 5 May 2003 20:27:29 -0000
@@ -730,5 +730,11 @@
* @see org.eclipse.cdt.internal.core.parser.IParserCallback#endBitfield(java.lang.Object)
*/
public void endBitfield(Object bitfield) {
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.cdt.internal.core.parser.IParserCallback#simpleDeclSpecifierType(java.lang.Object, java.lang.Object)
+ */
+ public void simpleDeclSpecifierType(Object declaration, Object type) {
}
}
Index: parser/org/eclipse/cdt/internal/core/parser/IParserCallback.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/IParserCallback.java,v
retrieving revision 1.22
diff -u -r1.22 IParserCallback.java
--- parser/org/eclipse/cdt/internal/core/parser/IParserCallback.java 25 Apr 2003 16:13:17 -0000 1.22
+++ parser/org/eclipse/cdt/internal/core/parser/IParserCallback.java 5 May 2003 20:27:29 -0000
@@ -24,6 +24,7 @@
public Object simpleDeclarationBegin(Object Container, Token firstToken);
public void simpleDeclSpecifier(Object Container, Token specifier);
public void simpleDeclSpecifierName( Object declaration );
+ public void simpleDeclSpecifierType( Object declaration, Object type );
public void simpleDeclarationEnd(Object declaration, Token lastToken);
public Object parameterDeclarationBegin( Object Container );
Index: parser/org/eclipse/cdt/internal/core/parser/ISymbol.java
===================================================================
RCS file: parser/org/eclipse/cdt/internal/core/parser/ISymbol.java
diff -N parser/org/eclipse/cdt/internal/core/parser/ISymbol.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ parser/org/eclipse/cdt/internal/core/parser/ISymbol.java 5 May 2003 20:27:29 -0000
@@ -0,0 +1,21 @@
+/**********************************************************************
+ * Copyright (c) 2002,2003 Rational Software 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 Rational Software - Initial API and implementation
+***********************************************************************/
+package org.eclipse.cdt.internal.core.parser;
+
+/**
+ * @author jcamelon
+ *
+ */
+public interface ISymbol {
+
+ public Object getObject();
+
+}
Index: parser/org/eclipse/cdt/internal/core/parser/NullParserCallback.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/NullParserCallback.java,v
retrieving revision 1.22
diff -u -r1.22 NullParserCallback.java
--- parser/org/eclipse/cdt/internal/core/parser/NullParserCallback.java 25 Apr 2003 16:13:17 -0000 1.22
+++ parser/org/eclipse/cdt/internal/core/parser/NullParserCallback.java 5 May 2003 20:27:29 -0000
@@ -635,4 +635,10 @@
public void endBitfield(Object bitfield) {
}
+ /* (non-Javadoc)
+ * @see org.eclipse.cdt.internal.core.parser.IParserCallback#simpleDeclSpecifierType(java.lang.Object, java.lang.Object)
+ */
+ public void simpleDeclSpecifierType(Object declaration, Object type) {
+ }
+
}
Index: parser/org/eclipse/cdt/internal/core/parser/Parser.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/Parser.java,v
retrieving revision 1.44
diff -u -r1.44 Parser.java
--- parser/org/eclipse/cdt/internal/core/parser/Parser.java 30 Apr 2003 22:14:55 -0000 1.44
+++ parser/org/eclipse/cdt/internal/core/parser/Parser.java 5 May 2003 20:27:30 -0000
@@ -16,6 +16,8 @@
import java.io.StringReader;
import org.eclipse.cdt.internal.core.model.Util;
+import org.eclipse.cdt.internal.core.parser.ParserSymbolTable.Declaration;
+import org.eclipse.cdt.internal.core.parser.ParserSymbolTable.TypeInfo;
/**
* This is our first implementation of the IParser interface, serving as a parser for
@@ -33,6 +35,7 @@
private boolean quickParse = false; // are we doing the high-level parse, or an in depth parse?
private boolean parsePassed = true; // did the parse pass?
private boolean cppNature = true; // true for C++, false for C
+ private ParserSymbolTable pst = new ParserSymbolTable(); // names
/**
* This is the single entry point for setting parsePassed to
@@ -160,12 +163,13 @@
try { callback.setParser( this ); } catch( Exception e) {}
Object translationUnit = null;
try{ translationUnit = callback.translationUnitBegin();} catch( Exception e ) {}
+ pst.getCompilationUnit().setObject(translationUnit);
Token lastBacktrack = null;
Token checkToken;
while (true) {
try {
checkToken = LA(1);
- declaration( translationUnit );
+ declaration( translationUnit, pst.getCompilationUnit() );
if( LA(1) == checkToken )
errorHandling();
} catch (EndOfFile e) {
@@ -341,7 +345,7 @@
default:
try
{
- declaration(linkageSpec);
+ declaration(linkageSpec, null);
}
catch( Backtrack bt )
{
@@ -359,7 +363,7 @@
}
else // single declaration
{
- declaration( linkageSpec );
+ declaration( linkageSpec, null );
try{ callback.linkageSpecificationEnd( linkageSpec );} catch( Exception e ) {}
}
}
@@ -393,7 +397,7 @@
// explicit-instantiation
Object instantiation = null;
try { instantiation = callback.explicitInstantiationBegin( container ); } catch( Exception e ) { }
- declaration( instantiation );
+ declaration( instantiation, null );
try { callback.explicitInstantiationEnd( instantiation ); } catch( Exception e ) { }
return;
}
@@ -406,7 +410,7 @@
// explicit-specialization
Object specialization = null;
try{ specialization = callback.explicitSpecializationBegin( container ); } catch( Exception e ) { }
- declaration( specialization );
+ declaration( specialization, null );
try{ callback.explicitSpecializationEnd( specialization ); } catch( Exception e ) { }
return;
}
@@ -418,7 +422,7 @@
try{ templateDeclaration = callback.templateDeclarationBegin( container, firstToken ); } catch ( Exception e ) {}
templateParameterList( templateDeclaration );
consume( Token.tGT );
- declaration( templateDeclaration );
+ declaration( templateDeclaration, null );
try{ callback.templateDeclarationEnd( templateDeclaration, lastToken ); } catch( Exception e ) {}
} catch( Backtrack bt )
@@ -548,7 +552,7 @@
* @param container IParserCallback object which serves as the owner scope for this declaration.
* @throws Backtrack request a backtrack
*/
- protected void declaration( Object container ) throws Backtrack {
+ protected void declaration( Object container, Declaration scope ) throws Backtrack {
switch (LT(1)) {
case Token.t_asm:
consume( Token.t_asm );
@@ -561,7 +565,7 @@
try{ callback.asmDefinition( container, assembly );} catch( Exception e ) {}
return;
case Token.t_namespace:
- namespaceDefinition( container );
+ namespaceDefinition( container, scope );
return;
case Token.t_using:
usingClause( container );
@@ -580,13 +584,13 @@
Token mark = mark();
try
{
- simpleDeclaration( container, true ); // try it first with the original strategy
+ simpleDeclaration( container, true, scope ); // try it first with the original strategy
}
catch( Backtrack bt)
{
// did not work
backup( mark );
- simpleDeclaration( container, false ); // try it again with the second strategy
+ simpleDeclaration( container, false, scope ); // try it again with the second strategy
}
}
}
@@ -602,21 +606,61 @@
* @throws Backtrack request a backtrack
*/
- protected void namespaceDefinition( Object container ) throws Backtrack
+ protected void namespaceDefinition( Object container, Declaration scope ) throws Backtrack
{
Object namespace = null;
- try{ namespace = callback.namespaceDefinitionBegin( container, consume( Token.t_namespace) );} catch( Exception e ) {}
+ boolean redeclared = false;
+ Token firstToken = consume( Token.t_namespace );
- // optional name
+ // optional name
+ String identifier = "";
if( LT(1) == Token.tIDENTIFIER )
{
- name();
- try{ callback.namespaceDefinitionId( namespace );} catch( Exception e ) {}
+ identifier = LA(1).getImage();
+ identifier();
}
if( LT(1) == Token.tLBRACE )
{
- consume();
+ consume( Token.tLBRACE);
+
+ Declaration namespaceSymbol = null;
+ try {
+ namespaceSymbol = scope.Lookup( identifier );
+ } catch (ParserSymbolTableException e1) {
+ // should not get ambiguity here
+ }
+
+ if( namespaceSymbol == null )
+ {
+ namespaceSymbol = pst.new Declaration( identifier );
+ try
+ {
+ namespaceSymbol.setType( TypeInfo.t_namespace );
+ }
+ catch( ParserSymbolTableException pste )
+ {
+ // should never happen
+ }
+ try{ namespace = callback.namespaceDefinitionBegin( container, firstToken );} catch( Exception e ) {}
+ namespaceSymbol.setObject( namespace );
+ try {
+ scope.addDeclaration( namespaceSymbol );
+ } catch (ParserSymbolTableException e2) {
+ // TODO ambiguity?
+ }
+ if( !identifier.equals( "" ))
+ try{ callback.namespaceDefinitionId( namespace );} catch( Exception e ) {}
+ }
+ else
+ {
+ if( namespaceSymbol.getType() != TypeInfo.t_namespace )
+ throw backtrack;
+ namespace = namespaceSymbol.getObject();
+ redeclared = true;
+ }
+
+
namepsaceDeclarationLoop:
while (LT(1) != Token.tRBRACE) {
Token checkToken = LA(1);
@@ -627,7 +671,7 @@
default:
try
{
- declaration(namespace);
+ declaration(namespace, namespaceSymbol);
}
catch( Backtrack bt )
{
@@ -641,7 +685,9 @@
}
// consume the }
- try{ callback.namespaceDefinitionEnd( namespace, consume( Token.tRBRACE ));} catch( Exception e ) {}
+ Token lastToken =consume( Token.tRBRACE );
+ if( ! redeclared )
+ try{ callback.namespaceDefinitionEnd( namespace, lastToken );} catch( Exception e ) {}
}
else
{
@@ -669,10 +715,10 @@
* @param tryConstructor true == take strategy1 (constructor ) : false == take strategy 2 ( pointer to function)
* @throws Backtrack request a backtrack
*/
- protected void simpleDeclaration( Object container, boolean tryConstructor ) throws Backtrack {
+ protected void simpleDeclaration( Object container, boolean tryConstructor, Declaration scope ) throws Backtrack {
Object simpleDecl = null;
try{ simpleDecl = callback.simpleDeclarationBegin( container, LA(1));} catch( Exception e ) {}
- declSpecifierSeq(simpleDecl, false, tryConstructor);
+ declSpecifierSeq(simpleDecl, false, tryConstructor, scope);
Object declarator = null;
if (LT(1) != Token.tSEMI)
@@ -800,7 +846,7 @@
Token current = LA(1);
Object parameterDecl = null;
try{ parameterDecl = callback.parameterDeclarationBegin( containerObject );} catch( Exception e ) {}
- declSpecifierSeq( parameterDecl, true, false );
+ declSpecifierSeq( parameterDecl, true, false, null );
if (LT(1) != Token.tSEMI)
try {
@@ -956,7 +1002,7 @@
* @param tryConstructor true for constructor, false for pointer to function strategy
* @throws Backtrack request a backtrack
*/
- protected void declSpecifierSeq( Object decl, boolean parm, boolean tryConstructor ) throws Backtrack {
+ protected void declSpecifierSeq( Object decl, boolean parm, boolean tryConstructor, Declaration scope ) throws Backtrack {
Flags flags = new Flags( parm, tryConstructor );
declSpecifiers:
for (;;) {
@@ -1024,6 +1070,7 @@
return;
if ( lookAheadForDeclarator( flags ) )
return;
+
try{ callback.simpleDeclSpecifier(decl,LA(1));} catch( Exception e ) {}
name();
try{ callback.simpleDeclSpecifierName( decl );} catch( Exception e ) {}
@@ -1038,7 +1085,7 @@
{
try
{
- classSpecifier(decl);
+ classSpecifier(decl, scope);
return;
}
catch( Backtrack bt )
@@ -1804,7 +1851,7 @@
* @param owner IParserCallback object that represents the declaration that owns this classSpecifier
* @throws Backtrack request a backtrack
*/
- protected void classSpecifier( Object owner ) throws Backtrack {
+ protected void classSpecifier( Object owner, Declaration scope ) throws Backtrack {
Token classKey = null;
Token mark = mark();
@@ -1862,7 +1909,7 @@
default:
try
{
- declaration(classSpec);
+ declaration(classSpec, scope);
}
catch( Backtrack bt )
{
@@ -2046,7 +2093,7 @@
while (LT(1) == Token.t_catch) {
consume();
consume(Token.tLPAREN);
- declaration(null); // was exceptionDeclaration
+ declaration(null, null); // was exceptionDeclaration
consume(Token.tRPAREN);
compoundStatement();
}
@@ -2078,7 +2125,7 @@
}
// declarationStatement
- declaration(null);
+ declaration(null, null);
}
}
Index: parser/org/eclipse/cdt/internal/core/parser/ParserSymbolTable.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ParserSymbolTable.java,v
retrieving revision 1.12
diff -u -r1.12 ParserSymbolTable.java
--- parser/org/eclipse/cdt/internal/core/parser/ParserSymbolTable.java 5 May 2003 17:00:59 -0000 1.12
+++ parser/org/eclipse/cdt/internal/core/parser/ParserSymbolTable.java 5 May 2003 20:27:31 -0000
@@ -1323,7 +1323,7 @@
}
}
- public class Declaration implements Cloneable {
+ public class Declaration implements Cloneable, ISymbol {
/**
* Constructor for Declaration.
Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui.tests/ChangeLog,v
retrieving revision 1.50
diff -u -r1.50 ChangeLog
--- ChangeLog 5 May 2003 17:01:04 -0000 1.50
+++ ChangeLog 5 May 2003 20:27:54 -0000
@@ -1,3 +1,6 @@
+2003-05-05 John Camelon/Andrew Niefer
+ Added CrossReferenceTests to ParserTestSuite to test symbol-table/DOM interworking.
+
2003-05-05 Andrew Niefer
Rewrote ParserSymbolTableTest to reflect structural changes to the symbol table.
Index: parser/org/eclipse/cdt/core/parser/tests/CrossReferenceTests.java
===================================================================
RCS file: parser/org/eclipse/cdt/core/parser/tests/CrossReferenceTests.java
diff -N parser/org/eclipse/cdt/core/parser/tests/CrossReferenceTests.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ parser/org/eclipse/cdt/core/parser/tests/CrossReferenceTests.java 5 May 2003 20:27:54 -0000
@@ -0,0 +1,52 @@
+/**********************************************************************
+ * Copyright (c) 2002,2003 Rational Software 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 Rational Software - Initial API and implementation
+***********************************************************************/
+package org.eclipse.cdt.core.parser.tests;
+
+import java.io.StringWriter;
+import java.io.Writer;
+
+import org.eclipse.cdt.internal.core.dom.NamespaceDefinition;
+import org.eclipse.cdt.internal.core.dom.SimpleDeclaration;
+import org.eclipse.cdt.internal.core.dom.TranslationUnit;
+
+/**
+ * @author jcamelon
+ *
+ */
+public class CrossReferenceTests extends BaseDOMTest {
+
+ public CrossReferenceTests( String arg )
+ {
+ super( arg );
+ }
+
+
+ public void testMultipleNamespaceDefinition() throws Exception
+ {
+ Writer code = new StringWriter();
+ code.write( "namespace A { int a; }\n" );
+ code.write( "namespace A { int k; }\n" );
+ TranslationUnit tu = parse( code.toString() );
+ assertEquals( tu.getDeclarations().size(), 1 );
+ assertEquals( ((NamespaceDefinition)tu.getDeclarations().get(0)).getDeclarations().size(), 2 );
+ }
+
+ public void testElaboratedTypeReference() throws Exception
+ {
+ Writer code = new StringWriter();
+ code.write( "class A { int x; }; \n");
+ code.write( "class A myA;");
+ TranslationUnit tu = parse( code.toString() );
+ assertEquals( tu.getDeclarations().size(), 2 );
+ SimpleDeclaration first = (SimpleDeclaration)tu.getDeclarations().get(0);
+ SimpleDeclaration second = (SimpleDeclaration)tu.getDeclarations().get(1);
+ }
+}
Index: parser/org/eclipse/cdt/core/parser/tests/ParserTestSuite.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/tests/ParserTestSuite.java,v
retrieving revision 1.3
diff -u -r1.3 ParserTestSuite.java
--- parser/org/eclipse/cdt/core/parser/tests/ParserTestSuite.java 24 Apr 2003 21:01:24 -0000 1.3
+++ parser/org/eclipse/cdt/core/parser/tests/ParserTestSuite.java 5 May 2003 20:27:55 -0000
@@ -32,6 +32,7 @@
suite.addTestSuite(ParserSymbolTableTest.class);
suite.addTestSuite(LineNumberTest.class);
suite.addTestSuite(CModelElementsTests.class);
+ suite.addTestSuite(CrossReferenceTests.class);
return suite;
}