package chameleon.core.context;


import chameleon.core.MetamodelException;
import chameleon.core.declaration.Declaration;
import chameleon.core.declaration.DeclarationSelector;
import chameleon.core.element.Element;

/**
 * @author Marko van Dooren
 */

public class LexicalContext extends Context {

	//public abstract Context getParentContext() throws MetamodelException;

  public LexicalContext(Context local, Element element) {
  	setLocalContext(local);
  	setElement(element);
  }
  
  public void setElement(Element element) {
  	_element=element;
  }
  
  public Element element() {
  	return _element;
  }
  
  private Element _element;
  
  public void setLocalContext(Context local) {
  	_localContext = local;
  }
  
  public Context localContext() {
  	return _localContext;
  }
  
  private Context _localContext;
  
  
  
	/**
	 * Return the parent context of this context.
	 * @throws MetamodelException 
	 */
	public Context parentContext() throws MetamodelException {
	  return element().parent().lexicalContext(element());
	}

	public <T extends Declaration> T lookUp(DeclarationSelector<T> selector) throws MetamodelException {
	  T tmp = localContext().lookUp(selector);
	  if(tmp != null) {
	    return tmp;
	  } else {
	    return parentContext().lookUp(selector);
	  }
	}

}

@@ -987,24 +987,33 @@
 tion %7B%0A%09
-  return
+%09Element parent =
  element
@@ -1027,35 +1027,220 @@
 nt()
-.lexicalContext(element());
+;%0A%09%09if(parent != null) %7B%0A%09    return parent.lexicalContext(element());%0A%09%09%7D else %7B%0A%09%09%09throw new MetamodelException(%22Lookup wants to go to the parent element of a %22+element().getClass() +%22 but it is null.%22);%0A%09%09%7D%0A%09%09
 %0A%09%7D%0A

throw metamodel exception when parent is null