tightened up some of the constraints on SymbolTable

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1103 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-10-10 13:33:31 +00:00
parent 74afbf6612
commit d538ec57ce
3 changed files with 24 additions and 16 deletions

View File

@ -8,21 +8,27 @@ package test.net.sourceforge.pmd.symboltable;
import junit.framework.TestCase;
import net.sourceforge.pmd.symboltable.ScopeCreator;
import net.sourceforge.pmd.symboltable.LocalScope;
import net.sourceforge.pmd.symboltable.GlobalScope;
import net.sourceforge.pmd.ast.ASTTryStatement;
import net.sourceforge.pmd.ast.ASTIfStatement;
import net.sourceforge.pmd.ast.ASTCompilationUnit;
public class ScopeCreatorTest extends TestCase {
public void testScopesAreCreated() {
ScopeCreator sc = new ScopeCreator();
ASTTryStatement tryNode = new ASTTryStatement(1);
tryNode.setScope(new LocalScope());
ASTCompilationUnit acu = new ASTCompilationUnit(1);
acu.setScope(new GlobalScope());
ASTIfStatement ifNode = new ASTIfStatement(2);
ASTTryStatement tryNode = new ASTTryStatement(2);
tryNode.setScope(new LocalScope());
tryNode.jjtSetParent(acu);
ASTIfStatement ifNode = new ASTIfStatement(3);
ifNode.jjtSetParent(tryNode);
sc.visit(ifNode, null);
sc.visit(acu, null);
assertTrue(tryNode.getScope() instanceof LocalScope);
assertTrue(ifNode.getScope() instanceof LocalScope);
}
}

View File

@ -13,27 +13,27 @@ public class SymbolTableTest extends TestCase {
public void testPush() {
SymbolTable s = new SymbolTable();
s.push(new LocalScope());
assertEquals(2,s.depth());
s.push(new GlobalScope());
assertEquals(1,s.depth());
}
public void testPop() {
SymbolTable s = new SymbolTable();
s.push(new LocalScope());
s.push(new GlobalScope());
s.pop();
assertEquals(1,s.depth());
assertEquals(0,s.depth());
}
public void testPeek() {
SymbolTable s = new SymbolTable();
Scope scope = new LocalScope();
Scope scope = new GlobalScope();
s.push(scope);
assertEquals(scope, s.peek());
}
public void testParentLinkage() {
SymbolTable s = new SymbolTable();
Scope scope = new LocalScope();
Scope scope = new GlobalScope();
s.push(scope);
Scope scope2 = new LocalScope();
s.push(scope2);

View File

@ -14,10 +14,6 @@ public class SymbolTable {
// and the individual scopes provide the other part
private Stack scopes = new Stack();
public SymbolTable() {
scopes.add(new LocalScope());
}
public Scope peek() {
return (Scope)scopes.peek();
}
@ -27,7 +23,13 @@ public class SymbolTable {
}
public void push(Scope scope) {
scope.setParent(peek());
if (scopes.empty()) {
if (!(scope instanceof GlobalScope)) {
throw new RuntimeException("First scope should be a GlobalScope");
}
} else {
scope.setParent(peek());
}
scopes.add(scope);
}