Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [newbie] how to make a block scope
[newbie] how to make a block scope [message #743251] Fri, 21 October 2011 09:13
gary s thompson is currently offline gary s thompsonFriend
Messages: 92
Registered: July 2009
Member
I am trying create a block scope with xbase with some named variables

here is an example of the use of my dsl (which should look a bit like awk for emf)

----
open windle from "tnder"
//open wendle from "tinder"

path test : windle/nmrProjects/experiments[dataSources/peakLists/peaks/@figOfMerit = -1.]
path test2 : windle/nmrProjects/experiments[dataSources/peakLists/peaks/peakDims]

test as target {
return target
}
---

here the xpath expression test selects elements from a containment hierarchy

and the loop will in the end iterate over each member of the selection assigning it to target.

here is my definition of the loop grammar

Loop:
pathSelection=MainPathSelection '{' expression=XExpression '}'
;

so my question is: I know how to find the type for target but how do I create the scope for Loop?

do I needs to override createLocalVarScope as shown below?
	@SuppressWarnings("restriction")
	@Override
	protected IScope  createLocalVarScope(IScope parent, LocalVariableScopeContext scopeContext) {
		
		IScope result = null;
		EObject context = scopeContext.getContext();
		EReference reference = scopeContext.getReference();
		System.err.println(context);
		System.err.println(reference);
		
		List<IEObjectDescription> eObjectDescriptions = makeEObjectDescriptionsForLoopScope(context);
		
		if (eObjectDescriptions.size() > 0) {
			result = new SimpleScope(parent, eObjectDescriptions);
		}
		
		System.err.println(result);
		if (result == null) {
			result = super.createLocalVarScope(parent, scopeContext);
		}

		return result;
	}


	private List<IEObjectDescription> makeEObjectDescriptionsForLoopScope(
			EObject context) {
		final CorkPackage CORK_EPACKAGE = CorkPackage.eINSTANCE;
		final EClass LOOP = CORK_EPACKAGE.getLoop();
		List<IEObjectDescription> eObjectDescriptions = Lists.newArrayList();
		if(sameClass(context, LOOP)) {
			Loop loop = (Loop) context;
			if(loop.getPathSelection() != null) {
				String selectionName = loop.getPathSelection().getName();
				RelativeLocationPathTail path = loop.getPathSelection().getPathDefinition().getHead().getTail();
				System.err.println("start");
				EStructuralFeature type = getTailType(path);
				IEObjectDescription eObjectDescription =  EObjectDescription.create(selectionName, type.getEType());
				eObjectDescriptions.add(eObjectDescription);
				System.err.println(type);
				System.err.println("here");
			}
		}
		return eObjectDescriptions;
	}


or do I need to add a test to getScope
@Override
	public IScope getScope(EObject context, EReference reference) {
		
		final CorkPackage CORK_EPACKAGE = CorkPackage.eINSTANCE;
		final EClass STEP_ECLASS = CORK_EPACKAGE.getStep();
		final EClass PATH_HEAD_ECLASS = CORK_EPACKAGE.getPathHead();
		final EClass RELATIVE_LOCATION_PATH_HEAD_ECLASS = CORK_EPACKAGE.getRelativeLocationPathHead();
		final EClass RELATIVE_LOCATION_PATH_TAIL_ECLASS = CORK_EPACKAGE.getRelativeLocationPathTail();
		
		final EClass LOOP = CORK_EPACKAGE.getLoop();
		final EClass PATH_DEFINITION = CORK_EPACKAGE.getPathDefinition();
		
		final TypesPackage TYPES_PACKAGE =TypesPackage.eINSTANCE;
		final EClass JVM_DECLARED_TYPE =  TYPES_PACKAGE.getJvmDeclaredType();
		
		
		final XbasePackage  X_BASE_PACKAGE  = XbasePackage.eINSTANCE;
		final EClass X_FEATURE_CALL  =  X_BASE_PACKAGE.getXFeatureCall();

		reportScopeContext(context, reference);

		IScope result = null;
		
		EClass referenceContext = (EClass) reference.eContainer();

		if (referenceContext.equals(STEP_ECLASS)) {
			EObject container = findScopeContainer(context);
			StepAxis stepAxis = getStepAxis(context);
			
			if (sameClass(container, PATH_HEAD_ECLASS)) {
				result = getScopeForPathHead(stepAxis);
		    } else if (sameClass(container, RELATIVE_LOCATION_PATH_TAIL_ECLASS)) {
				RelativeLocationPathTail pathTail = (RelativeLocationPathTail) container;
				result=getScopeForRelativeLocationPathTail(pathTail, stepAxis);
			} else if (sameClass(container, RELATIVE_LOCATION_PATH_HEAD_ECLASS)) {
				RelativeLocationPathHead relativeLocationPathHead  = (RelativeLocationPathHead) container;
				result =  getScopeForRelativeLocationPathHead((relativeLocationPathHead), stepAxis);
			}
		}
		if (referenceContext.equals(X_FEATURE_CALL)) {
			System.err.println("feature");
			List<IEObjectDescription>  eObjectDescriptions = makeEObjectDescriptionsForLoopScope(context);
			if (eObjectDescriptions.size() > 0) {
				result = new SimpleScope(eObjectDescriptions);
			}
		}
		
//		System.err.println(context);
//		if (sameClass(context, LOOP) && reference.getEReferenceType().equals(JVM_DECLARED_TYPE)) {
//			Loop loop = (Loop) context;
//			
//			System.err.println(references(PATH_DEFINITION));
//			result = getGlobalScopeForReference(PATH_DEFINITION);
//			System.err.println("here " + reference.getEReferenceType());
//			System.err.println(loop.getPathSelection());
//		}


		if (result == null) {
			result = super.getScope(context, reference);
		}

		return result;

	}
 


sorry I am fairly confused and am having problems finding the right documentation, there are lots of docs on scopes and I understand the general ideas but when I get down to the nitty gritty of which classes to use where I seem to run out of steam


regards
gary

Previous Topic:[newbie] how to make a block scope
Next Topic:migration to xtend2
Goto Forum:
  


Current Time: Tue Apr 16 14:48:36 GMT 2024

Powered by FUDForum. Page generated in 0.99446 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top