mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Symbol table/Parser interworking.
First blood has been drawn.
This commit is contained in:
parent
4a88230536
commit
23a5c599d4
9 changed files with 137 additions and 28 deletions
|
@ -936,4 +936,14 @@ public class DOMBuilder implements IParserCallback
|
||||||
b.getOwnerDeclarator().setBitField( b );
|
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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -29,5 +29,20 @@ public class ElaboratedTypeSpecifier extends TypeSpecifier {
|
||||||
public void setName(Name n) { name = n; }
|
public void setName(Name n) { name = n; }
|
||||||
public Name getName() { return name; }
|
public Name getName() { return name; }
|
||||||
|
|
||||||
|
private ClassSpecifier classSpec = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public ClassSpecifier getClassSpec() {
|
||||||
|
return classSpec;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param specifier
|
||||||
|
*/
|
||||||
|
public void setClassSpec(ClassSpecifier specifier) {
|
||||||
|
classSpec = specifier;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
2003-05-05 John Camelon/Andrew Niefer
|
||||||
|
Added Symboltable infrastructure into main parser.
|
||||||
|
|
||||||
2003-05-05 Andrew Niefer
|
2003-05-05 Andrew Niefer
|
||||||
Structural changes to ParserSymbolTable:
|
Structural changes to ParserSymbolTable:
|
||||||
- moved TypeInfo & Declaration inside ParserSymbolTable
|
- moved TypeInfo & Declaration inside ParserSymbolTable
|
||||||
|
|
|
@ -731,4 +731,10 @@ public class ExpressionEvaluator implements IParserCallback {
|
||||||
*/
|
*/
|
||||||
public void endBitfield(Object bitfield) {
|
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) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ public interface IParserCallback {
|
||||||
public Object simpleDeclarationBegin(Object Container, Token firstToken);
|
public Object simpleDeclarationBegin(Object Container, Token firstToken);
|
||||||
public void simpleDeclSpecifier(Object Container, Token specifier);
|
public void simpleDeclSpecifier(Object Container, Token specifier);
|
||||||
public void simpleDeclSpecifierName( Object declaration );
|
public void simpleDeclSpecifierName( Object declaration );
|
||||||
|
public void simpleDeclSpecifierType( Object declaration, Object type );
|
||||||
public void simpleDeclarationEnd(Object declaration, Token lastToken);
|
public void simpleDeclarationEnd(Object declaration, Token lastToken);
|
||||||
|
|
||||||
public Object parameterDeclarationBegin( Object Container );
|
public Object parameterDeclarationBegin( Object Container );
|
||||||
|
|
|
@ -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();
|
||||||
|
|
||||||
|
}
|
|
@ -635,4 +635,10 @@ public class NullParserCallback implements IParserCallback {
|
||||||
public void endBitfield(Object bitfield) {
|
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) {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,8 @@ import java.io.InputStreamReader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.core.model.Util;
|
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
|
* This is our first implementation of the IParser interface, serving as a parser for
|
||||||
|
@ -33,6 +35,7 @@ public class Parser implements IParser {
|
||||||
private boolean quickParse = false; // are we doing the high-level parse, or an in depth parse?
|
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 parsePassed = true; // did the parse pass?
|
||||||
private boolean cppNature = true; // true for C++, false for C
|
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
|
* This is the single entry point for setting parsePassed to
|
||||||
|
@ -160,12 +163,13 @@ c, quickParse);
|
||||||
try { callback.setParser( this ); } catch( Exception e) {}
|
try { callback.setParser( this ); } catch( Exception e) {}
|
||||||
Object translationUnit = null;
|
Object translationUnit = null;
|
||||||
try{ translationUnit = callback.translationUnitBegin();} catch( Exception e ) {}
|
try{ translationUnit = callback.translationUnitBegin();} catch( Exception e ) {}
|
||||||
|
pst.getCompilationUnit().setObject(translationUnit);
|
||||||
Token lastBacktrack = null;
|
Token lastBacktrack = null;
|
||||||
Token checkToken;
|
Token checkToken;
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
checkToken = LA(1);
|
checkToken = LA(1);
|
||||||
declaration( translationUnit );
|
declaration( translationUnit, pst.getCompilationUnit() );
|
||||||
if( LA(1) == checkToken )
|
if( LA(1) == checkToken )
|
||||||
errorHandling();
|
errorHandling();
|
||||||
} catch (EndOfFile e) {
|
} catch (EndOfFile e) {
|
||||||
|
@ -341,7 +345,7 @@ c, quickParse);
|
||||||
default:
|
default:
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
declaration(linkageSpec);
|
declaration(linkageSpec, null);
|
||||||
}
|
}
|
||||||
catch( Backtrack bt )
|
catch( Backtrack bt )
|
||||||
{
|
{
|
||||||
|
@ -359,7 +363,7 @@ c, quickParse);
|
||||||
}
|
}
|
||||||
else // single declaration
|
else // single declaration
|
||||||
{
|
{
|
||||||
declaration( linkageSpec );
|
declaration( linkageSpec, null );
|
||||||
try{ callback.linkageSpecificationEnd( linkageSpec );} catch( Exception e ) {}
|
try{ callback.linkageSpecificationEnd( linkageSpec );} catch( Exception e ) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -393,7 +397,7 @@ c, quickParse);
|
||||||
// explicit-instantiation
|
// explicit-instantiation
|
||||||
Object instantiation = null;
|
Object instantiation = null;
|
||||||
try { instantiation = callback.explicitInstantiationBegin( container ); } catch( Exception e ) { }
|
try { instantiation = callback.explicitInstantiationBegin( container ); } catch( Exception e ) { }
|
||||||
declaration( instantiation );
|
declaration( instantiation, null );
|
||||||
try { callback.explicitInstantiationEnd( instantiation ); } catch( Exception e ) { }
|
try { callback.explicitInstantiationEnd( instantiation ); } catch( Exception e ) { }
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -406,7 +410,7 @@ c, quickParse);
|
||||||
// explicit-specialization
|
// explicit-specialization
|
||||||
Object specialization = null;
|
Object specialization = null;
|
||||||
try{ specialization = callback.explicitSpecializationBegin( container ); } catch( Exception e ) { }
|
try{ specialization = callback.explicitSpecializationBegin( container ); } catch( Exception e ) { }
|
||||||
declaration( specialization );
|
declaration( specialization, null );
|
||||||
try{ callback.explicitSpecializationEnd( specialization ); } catch( Exception e ) { }
|
try{ callback.explicitSpecializationEnd( specialization ); } catch( Exception e ) { }
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -418,7 +422,7 @@ c, quickParse);
|
||||||
try{ templateDeclaration = callback.templateDeclarationBegin( container, firstToken ); } catch ( Exception e ) {}
|
try{ templateDeclaration = callback.templateDeclarationBegin( container, firstToken ); } catch ( Exception e ) {}
|
||||||
templateParameterList( templateDeclaration );
|
templateParameterList( templateDeclaration );
|
||||||
consume( Token.tGT );
|
consume( Token.tGT );
|
||||||
declaration( templateDeclaration );
|
declaration( templateDeclaration, null );
|
||||||
try{ callback.templateDeclarationEnd( templateDeclaration, lastToken ); } catch( Exception e ) {}
|
try{ callback.templateDeclarationEnd( templateDeclaration, lastToken ); } catch( Exception e ) {}
|
||||||
|
|
||||||
} catch( Backtrack bt )
|
} catch( Backtrack bt )
|
||||||
|
@ -548,7 +552,7 @@ c, quickParse);
|
||||||
* @param container IParserCallback object which serves as the owner scope for this declaration.
|
* @param container IParserCallback object which serves as the owner scope for this declaration.
|
||||||
* @throws Backtrack request a backtrack
|
* @throws Backtrack request a backtrack
|
||||||
*/
|
*/
|
||||||
protected void declaration( Object container ) throws Backtrack {
|
protected void declaration( Object container, Declaration scope ) throws Backtrack {
|
||||||
switch (LT(1)) {
|
switch (LT(1)) {
|
||||||
case Token.t_asm:
|
case Token.t_asm:
|
||||||
consume( Token.t_asm );
|
consume( Token.t_asm );
|
||||||
|
@ -561,7 +565,7 @@ c, quickParse);
|
||||||
try{ callback.asmDefinition( container, assembly );} catch( Exception e ) {}
|
try{ callback.asmDefinition( container, assembly );} catch( Exception e ) {}
|
||||||
return;
|
return;
|
||||||
case Token.t_namespace:
|
case Token.t_namespace:
|
||||||
namespaceDefinition( container );
|
namespaceDefinition( container, scope );
|
||||||
return;
|
return;
|
||||||
case Token.t_using:
|
case Token.t_using:
|
||||||
usingClause( container );
|
usingClause( container );
|
||||||
|
@ -580,13 +584,13 @@ c, quickParse);
|
||||||
Token mark = mark();
|
Token mark = mark();
|
||||||
try
|
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)
|
catch( Backtrack bt)
|
||||||
{
|
{
|
||||||
// did not work
|
// did not work
|
||||||
backup( mark );
|
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 @@ c, quickParse);
|
||||||
* @throws Backtrack request a backtrack
|
* @throws Backtrack request a backtrack
|
||||||
|
|
||||||
*/
|
*/
|
||||||
protected void namespaceDefinition( Object container ) throws Backtrack
|
protected void namespaceDefinition( Object container, Declaration scope ) throws Backtrack
|
||||||
{
|
{
|
||||||
Object namespace = null;
|
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 )
|
if( LT(1) == Token.tIDENTIFIER )
|
||||||
{
|
{
|
||||||
name();
|
identifier = LA(1).getImage();
|
||||||
try{ callback.namespaceDefinitionId( namespace );} catch( Exception e ) {}
|
identifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
if( LT(1) == Token.tLBRACE )
|
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:
|
namepsaceDeclarationLoop:
|
||||||
while (LT(1) != Token.tRBRACE) {
|
while (LT(1) != Token.tRBRACE) {
|
||||||
Token checkToken = LA(1);
|
Token checkToken = LA(1);
|
||||||
|
@ -627,7 +671,7 @@ c, quickParse);
|
||||||
default:
|
default:
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
declaration(namespace);
|
declaration(namespace, namespaceSymbol);
|
||||||
}
|
}
|
||||||
catch( Backtrack bt )
|
catch( Backtrack bt )
|
||||||
{
|
{
|
||||||
|
@ -641,7 +685,9 @@ c, quickParse);
|
||||||
}
|
}
|
||||||
// consume the }
|
// 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
|
else
|
||||||
{
|
{
|
||||||
|
@ -669,10 +715,10 @@ c, quickParse);
|
||||||
* @param tryConstructor true == take strategy1 (constructor ) : false == take strategy 2 ( pointer to function)
|
* @param tryConstructor true == take strategy1 (constructor ) : false == take strategy 2 ( pointer to function)
|
||||||
* @throws Backtrack request a backtrack
|
* @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;
|
Object simpleDecl = null;
|
||||||
try{ simpleDecl = callback.simpleDeclarationBegin( container, LA(1));} catch( Exception e ) {}
|
try{ simpleDecl = callback.simpleDeclarationBegin( container, LA(1));} catch( Exception e ) {}
|
||||||
declSpecifierSeq(simpleDecl, false, tryConstructor);
|
declSpecifierSeq(simpleDecl, false, tryConstructor, scope);
|
||||||
Object declarator = null;
|
Object declarator = null;
|
||||||
|
|
||||||
if (LT(1) != Token.tSEMI)
|
if (LT(1) != Token.tSEMI)
|
||||||
|
@ -800,7 +846,7 @@ c, quickParse);
|
||||||
Token current = LA(1);
|
Token current = LA(1);
|
||||||
Object parameterDecl = null;
|
Object parameterDecl = null;
|
||||||
try{ parameterDecl = callback.parameterDeclarationBegin( containerObject );} catch( Exception e ) {}
|
try{ parameterDecl = callback.parameterDeclarationBegin( containerObject );} catch( Exception e ) {}
|
||||||
declSpecifierSeq( parameterDecl, true, false );
|
declSpecifierSeq( parameterDecl, true, false, null );
|
||||||
|
|
||||||
if (LT(1) != Token.tSEMI)
|
if (LT(1) != Token.tSEMI)
|
||||||
try {
|
try {
|
||||||
|
@ -956,7 +1002,7 @@ c, quickParse);
|
||||||
* @param tryConstructor true for constructor, false for pointer to function strategy
|
* @param tryConstructor true for constructor, false for pointer to function strategy
|
||||||
* @throws Backtrack request a backtrack
|
* @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 );
|
Flags flags = new Flags( parm, tryConstructor );
|
||||||
declSpecifiers:
|
declSpecifiers:
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
@ -1024,6 +1070,7 @@ c, quickParse);
|
||||||
return;
|
return;
|
||||||
if ( lookAheadForDeclarator( flags ) )
|
if ( lookAheadForDeclarator( flags ) )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
try{ callback.simpleDeclSpecifier(decl,LA(1));} catch( Exception e ) {}
|
try{ callback.simpleDeclSpecifier(decl,LA(1));} catch( Exception e ) {}
|
||||||
name();
|
name();
|
||||||
try{ callback.simpleDeclSpecifierName( decl );} catch( Exception e ) {}
|
try{ callback.simpleDeclSpecifierName( decl );} catch( Exception e ) {}
|
||||||
|
@ -1038,7 +1085,7 @@ c, quickParse);
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
classSpecifier(decl);
|
classSpecifier(decl, scope);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
catch( Backtrack bt )
|
catch( Backtrack bt )
|
||||||
|
@ -1804,7 +1851,7 @@ c, quickParse);
|
||||||
* @param owner IParserCallback object that represents the declaration that owns this classSpecifier
|
* @param owner IParserCallback object that represents the declaration that owns this classSpecifier
|
||||||
* @throws Backtrack request a backtrack
|
* @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 classKey = null;
|
||||||
|
|
||||||
Token mark = mark();
|
Token mark = mark();
|
||||||
|
@ -1862,7 +1909,7 @@ c, quickParse);
|
||||||
default:
|
default:
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
declaration(classSpec);
|
declaration(classSpec, scope);
|
||||||
}
|
}
|
||||||
catch( Backtrack bt )
|
catch( Backtrack bt )
|
||||||
{
|
{
|
||||||
|
@ -2046,7 +2093,7 @@ c, quickParse);
|
||||||
while (LT(1) == Token.t_catch) {
|
while (LT(1) == Token.t_catch) {
|
||||||
consume();
|
consume();
|
||||||
consume(Token.tLPAREN);
|
consume(Token.tLPAREN);
|
||||||
declaration(null); // was exceptionDeclaration
|
declaration(null, null); // was exceptionDeclaration
|
||||||
consume(Token.tRPAREN);
|
consume(Token.tRPAREN);
|
||||||
compoundStatement();
|
compoundStatement();
|
||||||
}
|
}
|
||||||
|
@ -2078,7 +2125,7 @@ c, quickParse);
|
||||||
}
|
}
|
||||||
|
|
||||||
// declarationStatement
|
// declarationStatement
|
||||||
declaration(null);
|
declaration(null, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1323,7 +1323,7 @@ public class ParserSymbolTable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Declaration implements Cloneable {
|
public class Declaration implements Cloneable, ISymbol {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for Declaration.
|
* Constructor for Declaration.
|
||||||
|
|
Loading…
Add table
Reference in a new issue