antlraux package
v0.2.1

antlraux.context
Class Scope

java.lang.Object
  |
  +--antlraux.context.Scope

public class Scope
extends java.lang.Object

This is the class tha modelizes scopes.

Scopes admit data in the form of instances of Declaration.

Usually scopes are represented as instances of Hashtable containing instances of Declaration, and organized in a pile.

This implementation is slightly different.

Firstly, the scope's hashtables' elements are linked lists instead of simple Declarations, so several elements with the same name are allowed on each tabe's position. This is more flexible than having just one declaration per position, allowing for example inserting several method declarations with the same name in a class scope (thus making polymorphism easier). The counterpoint is that now the search methods return a linked list instead of a single element - several helping methods ( searchFirst(String), searchFirstLocally(String), searchFirst(int) and searchFirstLocally(int)) have been included to ease this issue.

Other difference between this implementation of scopes is scope organization. Instead of having an explicit pile of scopes, the pile is implicit in this case. This is achieved simply making every Scope know its "father scope". In other words, each "method scope" will have to know the scope of the class that holds the method, etc. A root ("global") scope will have null as father.

Finally, the declarations are organized in two different ways:

A tag is an integuer which codes the "type" of a declaration. We don't mean here if the declaration is "integuer" or "float"; there's the Declaration.type field for that. The tag says wether a declaration is a "class", a "parameter", a "method", etc.

This two types allow more powerful search techniques. For example, you can ask for "all the elements named 'foo' in a class scope", but you could also ask for "all the attributes of the class".

There are two ways of working with Scope. The first one is using the Context class. This class has facilities for authomatically checking scope closing.

The other option is not using Context, and write subclasses of Scope (like GlobalScope, ClassScope, MethodScope, etc). Override insert(Declaration) to perform insertion checks and check the opening on every constructor (see if the father scope allows "this" as a child scope)

Author:
Enrique José García Cota
See Also:
Context, Declaration

Field Summary
protected  Scope father
          The scope's father.
protected  java.lang.String name
          The name of a scope is just a "caption", that will be used for most printing procedures.
protected  java.util.Hashtable namesTable
          The scope's set of declarations, hashed by name.
protected  int tag
          This integer needed on every Scope constructor represents a type of scope.
protected  java.util.Hashtable tagsTable
          The scope's set of declarations, hashed by tag.
 
Constructor Summary
Scope(int tag, java.lang.String name)
          Constructor that sets tag and name
Scope(int tag, java.lang.String name, Scope father)
          Constructor that sets tag, name and father
 
Method Summary
 void addDeclarations(java.util.LinkedList list)
          It adds the declarations contained in "list" to this scope.
 Scope getFather()
          Accessor for father
 java.lang.String getName()
          Accessor for name
 int getTag()
          Accessor for tag
 void insert(Declaration d)
          Inserts a new Declaration
 java.util.LinkedList searchAll(int tag)
          It calls searchAllLocally(int) with tag as param and, if nothing found, it continues with the father, the father of the father, etc.
 java.util.LinkedList searchAll(int tag, java.lang.String name)
          It calls searchAllLocally(int, String) with tag as param and, if nothing found, it continues with the scope's father, the father of the father, etc.
 java.util.LinkedList searchAll(java.lang.String name)
          It calls searchAllLocally(String) with "name" as param and, if nothing found, it continues with the father, the father of the father, etc.
 java.util.LinkedList searchAllLocally(int tag)
          Searches in current scope every Declaration tagged like "tag"
 java.util.LinkedList searchAllLocally(int tag, java.lang.String name)
          Searches in current scope every Declaration tagged like "tag" and named like "name"
 java.util.LinkedList searchAllLocally(java.lang.String name)
          Searches in current scope every Declaration named "name"
 Declaration searchFirst(int tag)
          It calls searchFirstLocally(int) with "tag" as param and, if nothing found, it continues with the father, the father of the father, etc.
 Declaration searchFirst(int tag, java.lang.String name)
          It calls searchFirstLocally(int,String) with "tag" and "name" as params and, if nothing found, it continues with the scope's father, the father of the father, etc.
 Declaration searchFirst(java.lang.String name)
          It calls searchFirstLocally(String) with "name" as param and, if nothing found, it continues with the father, the father of the father, etc.
 Declaration searchFirstLocally(int tag)
          Searches in current scope the first (last inserted) Declaration with a tag==tag
 Declaration searchFirstLocally(int tag, java.lang.String name)
          Searches in current scope the first (last inserted) Declaration with a tag==tag AND name==name
 Declaration searchFirstLocally(java.lang.String name)
          Searches in current scope the first (last inserted) Declaration named "name"
 void setFather(Scope s)
          Accessor for father
 void setName(java.lang.String s)
          Accessor for name
 void setTag(int i)
          Accessor for tag
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

father

protected Scope father
The scope's father.


name

protected java.lang.String name
The name of a scope is just a "caption", that will be used for most printing procedures. It is desirable guiving an appropiate name to each Scope. For example, the global scope should be called "global", or "root", while class Foo's scope name should look like "class Foo".


tag

protected int tag
This integer needed on every Scope constructor represents a type of scope. This is, it codes if current scope is global scope, class scope, method scope, etc. It is very usefull for performing scope closing checks.


namesTable

protected java.util.Hashtable namesTable
The scope's set of declarations, hashed by name.


tagsTable

protected java.util.Hashtable tagsTable
The scope's set of declarations, hashed by tag.

Constructor Detail

Scope

public Scope(int tag,
             java.lang.String name)
Constructor that sets tag and name


Scope

public Scope(int tag,
             java.lang.String name,
             Scope father)
Constructor that sets tag, name and father

Method Detail

getName

public java.lang.String getName()
Accessor for name


getTag

public int getTag()
Accessor for tag


getFather

public Scope getFather()
Accessor for father


setName

public void setName(java.lang.String s)
Accessor for name


setTag

public void setTag(int i)
Accessor for tag


setFather

public void setFather(Scope s)
Accessor for father


insert

public void insert(Declaration d)
            throws ContextException
Inserts a new Declaration

Parameters:
d - the Declaration that is going to be inserted in the scope.
Throws:
ContextException - if could not insert d. (Actually default implementation of insert will never throw it, but subclasses may do)

searchFirstLocally

public Declaration searchFirstLocally(int tag)
Searches in current scope the first (last inserted) Declaration with a tag==tag

Parameters:
tag - the tag of the Declaration being searched
Returns:
declaration whose tag matches "tag" or null if none found

searchFirstLocally

public Declaration searchFirstLocally(java.lang.String name)
Searches in current scope the first (last inserted) Declaration named "name"

Parameters:
name - the name of the Declaration being searched
Returns:
a declaration whose name matches ("name") or null if none found

searchFirstLocally

public Declaration searchFirstLocally(int tag,
                                      java.lang.String name)
Searches in current scope the first (last inserted) Declaration with a tag==tag AND name==name

Parameters:
tag - the tag of the Declaration being searched
name - the name of the Declaration being searched
Returns:
a declaration wose tag matches "tag" and name matches "name". if there are several matching tags in the scope, it is recommended to use searchAllLocally(int, String)

searchFirst

public Declaration searchFirst(int tag)
It calls searchFirstLocally(int) with "tag" as param and, if nothing found, it continues with the father, the father of the father, etc.

Parameters:
tag - the searched Declaration's tag
Returns:
a declaration whose tag matches "tag" or null if none found

searchFirst

public Declaration searchFirst(java.lang.String name)
It calls searchFirstLocally(String) with "name" as param and, if nothing found, it continues with the father, the father of the father, etc.

Parameters:
name - the searched Declaration's name
Returns:
a declaration whose name matches "name" or null if none found

searchFirst

public Declaration searchFirst(int tag,
                               java.lang.String name)
It calls searchFirstLocally(int,String) with "tag" and "name" as params and, if nothing found, it continues with the scope's father, the father of the father, etc.

Parameters:
name - the searched Declaration's name
tag - the searched Declaration's tag
Returns:
a declaration whose name matches "name", and whose tag matches "tag", or null if none found

searchAllLocally

public java.util.LinkedList searchAllLocally(java.lang.String name)
Searches in current scope every Declaration named "name"

Parameters:
name - the name of the Declarations being searched
Returns:
a list of declarations whose name matches ("name") or null if none found

searchAllLocally

public java.util.LinkedList searchAllLocally(int tag)
Searches in current scope every Declaration tagged like "tag"

Parameters:
tag - the tag of the Declarations being searched
Returns:
a list of declarations whose tag matches "tag" or null if none found

searchAllLocally

public java.util.LinkedList searchAllLocally(int tag,
                                             java.lang.String name)
Searches in current scope every Declaration tagged like "tag" and named like "name"

Parameters:
tag - the tag of the Declarations being searched
name - the name of the Declarations being searched
Returns:
a list of declarations whose tag matches "tag" or null if none found

searchAll

public java.util.LinkedList searchAll(java.lang.String name)
It calls searchAllLocally(String) with "name" as param and, if nothing found, it continues with the father, the father of the father, etc. This is the most powerfull (and slowest) search method.

Parameters:
name - the searched Declaration's name
Returns:
a list of declarations whose name matches "name" or null if none found

searchAll

public java.util.LinkedList searchAll(int tag)
It calls searchAllLocally(int) with tag as param and, if nothing found, it continues with the father, the father of the father, etc.

Parameters:
tag - the searched Declarations' tag
Returns:
a list of declarations tagged like "tag" or null if none found

searchAll

public java.util.LinkedList searchAll(int tag,
                                      java.lang.String name)
It calls searchAllLocally(int, String) with tag as param and, if nothing found, it continues with the scope's father, the father of the father, etc.

Parameters:
tag - the searched Declarations' tag
name - the searched Declarations' name
Returns:
a list of declarations tagged like "tag" and named like "name" or null if none found

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

addDeclarations

public void addDeclarations(java.util.LinkedList list)
                     throws ContextException
It adds the declarations contained in "list" to this scope.

Throws:
ContextException - if an error ocurrs (never by default, but subclasses may want to)

antlraux package
v0.2.1

Created by Enrique José García Cota