Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Control Variable Assignments
Control Variable Assignments [message #1708165] Mon, 14 September 2015 10:25 Go to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
Hi everyone,

I have a problem to control the values of variables. In my DSL I have a Declarationpart ("VAR"), where a variable can be declared and also assigned. In the StatementBlock of the "BEGIN" part the value of a variable can be changed or assigned. How can I recognize or control the values of the variables in such a way that the last assignment should be the current/desired one?
My first idea was to store them in a hashmap. But I was not sure how to handle it if a statement gets deleted.

Thank you in advance!

PROGRAM test;
VAR
    int : a := 3;
    int : b ;
    ARRAY [1..4] OF int = tempArray;

BEGIN
    a := 4;
    b := 0;
    tempArray[a] := 1;

ENDPROGRAM

Re: Control Variable Assignments [message #1708175 is a reply to message #1708165] Mon, 14 September 2015 11:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,

i dont understand this question. can you please explain?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1708178 is a reply to message #1708175] Mon, 14 September 2015 11:48 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
Hi,

I am sorry for the bad explanation.
My problem in general is: How can I detect the current value of a variable?
If I have an assignment in my StatementBlock ("BEGIN") .. the Value should be adapted.



Re: Control Variable Assignments [message #1708179 is a reply to message #1708178] Mon, 14 September 2015 11:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
what do you mean by detect? ast and generation/interpretation are 2 different things. so what you you actually try to do?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1708182 is a reply to message #1708179] Mon, 14 September 2015 12:12 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
it is more an interpretation problem. I want to interpret the value of a variable. If there is a new assignment the value should be updated.

[Updated on: Thu, 01 October 2015 08:52]

Report message to a moderator

Re: Control Variable Assignments [message #1708183 is a reply to message #1708182] Mon, 14 September 2015 12:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
this should work fine with a map

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1708184 is a reply to message #1708183] Mon, 14 September 2015 12:16 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
Ok thanks. I'll try it out
Re: Control Variable Assignments [message #1708186 is a reply to message #1708184] Mon, 14 September 2015 12:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
(p.s. depending on your lang the values of the map might be stacks)
or you have maps covering each other.
you may have a look at the EvaluationContext of xbase as well


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Mon, 14 September 2015 12:30]

Report message to a moderator

Re: Control Variable Assignments [message #1708882 is a reply to message #1708186] Tue, 22 September 2015 08:29 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
Hi Christian,

do you know how I can get the current line number of a hover object?

Re: Control Variable Assignments [message #1708899 is a reply to message #1708882] Tue, 22 September 2015 10:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
org.eclipse.xtext.nodemodel.INode.getStartLine() ??? (NodeModelUtil should give you the node)

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1708901 is a reply to message #1708899] Tue, 22 September 2015 11:27 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
I want to interpret the value of a variable. For that I override getHoverInfoAsHtml(EObject o). When I have an assignment I try to get the CURRENT position of that variable...But I get the position where the variable was declared:
VAR 
   int : a;

BEGIN
    a := 4;


Thats why I wanted to know how to get the current position of the cursor.

[Updated on: Tue, 22 September 2015 11:28]

Report message to a moderator

Re: Control Variable Assignments [message #1708903 is a reply to message #1708901] Tue, 22 September 2015 11:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
go for it

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1708905 is a reply to message #1708903] Tue, 22 September 2015 11:32 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
The question is HOW to get the current position of the cursor Smile
Re: Control Variable Assignments [message #1708909 is a reply to message #1708905] Tue, 22 September 2015 12:10 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
as i said: use nodemodelutils to get the node of the object. and the node for its position

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1708911 is a reply to message #1708909] Tue, 22 September 2015 12:18 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
I tried that. It doesn't work when I try to hover. As I said: instead of the current variable position I get the position where the variable was declared, which I don't want.

That is why I would like to have the current cursor position; NOT the object position. Is that possible?
Re: Control Variable Assignments [message #1708913 is a reply to message #1708911] Tue, 22 September 2015 12:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
The you have to take the thing that holds the variable (VariableRef) and not the variable
it is a feature that you get the origin of the object by default


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Tue, 22 September 2015 12:33]

Report message to a moderator

Re: Control Variable Assignments [message #1708914 is a reply to message #1708913] Tue, 22 September 2015 12:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
(you might hook deeper in the the hover org.eclipse.xtext.ui.editor.hover.AbstractEObjectHover.getXtextElementAt(XtextResource, int)

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1709043 is a reply to message #1708914] Wed, 23 September 2015 14:17 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
I cant give the VariableRef to the Hover. It is not working. It is not jumping into my switch query.
And unfortunately I was not able to find an example of getXtextElementAt where I can figure out the current cursor position.
Could you please provide me with a simple example.
Thank you very much!
Re: Control Variable Assignments [message #1709057 is a reply to message #1709043] Wed, 23 September 2015 15:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi i do not get that

Model:
	greetings+=Greeting*;
	
Greeting:
	'Hello' name=ID ('from' from=[Greeting])? '!';


public class DispatchingEObjectTextHover2 extends DispatchingEObjectTextHover {
	
	@Inject
	private MyDslGrammarAccess ga;
	
	@Override
	protected Pair<EObject, IRegion> getXtextElementAt(XtextResource resource, int offset) {
		if (resource.getParseResult() != null) {
			ICompositeNode rootNode = resource.getParseResult().getRootNode();
			if (rootNode != null) {
				ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset);
				EObject sem = NodeModelUtils.findActualSemanticObjectFor(leafNode);
				if (leafNode.getGrammarElement() instanceof CrossReference) {
					CrossReference cr = (CrossReference) leafNode.getGrammarElement();
					if (ga.getGreetingAccess().getFromGreetingCrossReference_2_1_0() == cr) {
						ITextRegion leafRegion = leafNode.getTextRegion();
						return Tuples.create(sem, (IRegion) new Region(leafRegion.getOffset(), leafRegion.getLength()));
					}
				}
			}
		}
		
		
		return super.getXtextElementAt(resource, offset);
	}

}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1709117 is a reply to message #1709057] Thu, 24 September 2015 07:53 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
Thank you very much. This helped a lot !!!
Re: Control Variable Assignments [message #1709460 is a reply to message #1709117] Mon, 28 September 2015 14:56 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
Hi,

is it possible to (Hyper-)Link the DSL with a generated (C++) file?
After pressing (e.g.) shift+ctrl+ "a DSL object", I want to jump to specific part of the generated file. Is that feasible?

Thank you in advance
Re: Control Variable Assignments [message #1709465 is a reply to message #1709460] Mon, 28 September 2015 16:00 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
yes

have a look at Hyperlink helper


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1709549 is a reply to message #1709465] Tue, 29 September 2015 08:46 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
I have this code snippet from a former question. With this the Hyperlinking is appearing but it is not jumping to the corresponding place in the generated file. How can I specify this?
class IRLLinkHelper extends HyperlinkHelper {
	override createHyperlinksByOffset (XtextResource resource, int offset, IHyperlinkAcceptor acceptor) {
		var EObjectAtOffsetHelper eObjectAtOffsetHelper = new EObjectAtOffsetHelper();
		var EObject eObject = eObjectAtOffsetHelper.resolveElementAt(resource, offset);

		if (eObject instanceof VariableDeclaration) {

			val variable  = eObject as VariableDeclaration;
			var CompositeNode adapter = NodeModelUtils.getNode(variable) as CompositeNode;

			val Region region = new Region(adapter.getOffset(), adapter.getLength());
			
			val URIConverter uriConverter = resource.getResourceSet().getURIConverter();
			val String hyperlinkText = "Variable";
			
			val URI uri = EcoreUtil.getURI(variable);
			val URI normalized = if (uri.platformResource != false)  uri uriConverter.normalize(uri);
			println("URI " + uri)
			println("normalized " + normalized)
			val XtextHyperlink result = getHyperlinkProvider().get()
			result.setHyperlinkRegion(region);
			result.setURI(normalized);
			result.setHyperlinkText(hyperlinkText);
			acceptor.accept(result);
		}	
	}
}
Re: Control Variable Assignments [message #1709555 is a reply to message #1709549] Tue, 29 September 2015 09:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
sry that is a question for the tool that provides you the editor for. which tool do you use for c++


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1709566 is a reply to message #1709555] Tue, 29 September 2015 10:25 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
I do everything in eclipse. I provide you the xtext project and the corresponding c++-project to have a look.
Thank you very much!!

[Updated on: Tue, 29 September 2015 10:28]

Report message to a moderator

Re: Control Variable Assignments [message #1709567 is a reply to message #1709566] Tue, 29 September 2015 10:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
sry i cannot do this for you.
my question was: which tool for c++ do you use?
you have to look how that Tools uris/Hyperlinks look like


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1709569 is a reply to message #1709567] Tue, 29 September 2015 10:28 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
ok. I use eclipse. So how can I do this with eclipse

[Updated on: Tue, 29 September 2015 11:08]

Report message to a moderator

Re: Control Variable Assignments [message #1709581 is a reply to message #1709569] Tue, 29 September 2015 11:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
as is said: this is a matter of the Editor implementation you use. (e.g. Eclipse CDT)

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1709613 is a reply to message #1709581] Tue, 29 September 2015 14:47 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
ok thank you!
Re: Control Variable Assignments [message #1709807 is a reply to message #1709613] Thu, 01 October 2015 09:02 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
Hi,
for hyperlinking I have the URI of another file. Jumping to that file is working with:
>> result.setURI(normalized); (see code above)

But how can I specify also the exact position of that referenced (here) variable with e.g. the offset of that variable in the linked file?
What I have is the IASTFileLocation (= path,offset,length)
Thanks in advance
Re: Control Variable Assignments [message #1709814 is a reply to message #1709807] Thu, 01 October 2015 10:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i do not get that question. please explain exaclty (based on sample models and generated code) what you are talking about


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1709832 is a reply to message #1709814] Thu, 01 October 2015 12:14 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
So what I try to do is to tell the XtextHyperlink to consider not only the path of that file as URI (here: curi) but also the position (offset, length) of a sepcific variable (here as IASTFIleLocation location).

The sample project looks like this:

override createHyperlinksByOffset (XtextResource resource, int offset, IHyperlinkAcceptor acceptor) {
var EObjectAtOffsetHelper eObjectAtOffsetHelper = new EObjectAtOffsetHelper();
var EObject eObject = eObjectAtOffsetHelper.resolveElementAt(resource, offset);

if (eObject instanceof VariableDeclaration) {

	val variable  = eObject as VariableDeclaration;
	var CompositeNode adapter = NodeModelUtils.getNode(variable) as CompositeNode;

	val Region region = new Region(adapter.getOffset(), adapter.getLength());
	
	val String hyperlinkText = "Variable";
	
	val ICProject[] cppPrjs = CoreModel.^default.CModel.CProjects;

	var URI curi = null;
	val IIndex index = CCorePlugin.indexManager.getIndex(cppPrjs.head)
	var IIndexFile file = null
	for (f : index.allFiles) {
		if (f.location.toString.contains("IRL_variables")){
			println("file path " + f.location.URI)
			curi = URI.createURI(f.location.URI.toString)
			file = f  
		}
	}
	var IASTFileLocation location= null
    val ICProject cproject = CoreModel.getDefault().getCModel().getCProject("IRLSampleProject");
	
	try {
        	
		var IPath _path = new Path("IRLSampleProject/src-gen/IRL_variables/testfile.cpp")
		var IFile _ifile = ResourcesPlugin.workspace.root.getFile(_path)
		var ITranslationUnit unit = CoreModelUtil.findTranslationUnit(_ifile);
		if (unit == null) {
		    unit = CoreModel.getDefault().createTranslationUnitFrom(cproject, _ifile.getLocation() );
		}
		var IASTTranslationUnit ast = null;
		var IIndex idx = null;
		try {
		    idx = CCorePlugin.getIndexManager().getIndex(cproject);
		} catch (CoreException e) {
		    
		}
		
	    ast = unit.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
	    for (a : ast.children){
	    	println("ast ---------------------------->   " + a)
	    	if (a instanceof CPPASTFunctionDefinition){	
	    		// Path of that declaration, with offset and length		    		
	    		location = ((a as CPPASTFunctionDefinition).body.children.head as CPPASTDeclarationStatement).declaration.children.get(1).fileLocation
	    	}
	    }
	    
	    if (ast == null) {
	        throw new IllegalArgumentException(_ifile.getLocation().toPortableString() + ": not a valid C/C++ code file");
	    }
	
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
    
	val XtextHyperlink result = getHyperlinkProvider().get()
	result.setHyperlinkRegion(region);
	//*************************************************************************
	// here I want set the URI with the position of variable that should be linked to
	result.setURI(curi);
	result.setHyperlinkText(hyperlinkText);
	acceptor.accept(result);
}	


}
Re: Control Variable Assignments [message #1709837 is a reply to message #1709832] Thu, 01 October 2015 12:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

still the question. where does the hyplink point to? xtext cannot know in which line in a non xtext file an object is or not.
so do you use cdt? or what is the stuff you use? the stuff you use can tell you how the links for the stuff you use look like?!?
thus you should not create an xtext hyperlink but an IHyperlink for the tool you use.
thus this is a question on e.g. cdt and not xtext


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1709841 is a reply to message #1709837] Thu, 01 October 2015 13:07 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
P.S:

i asumes the nodes in cdt provide offset and length. is so you can have your own ihyperlink method
that calls a job that opens the editor and then calls selectandreveal(offset,length) - the same as
LangaugeSpecific/GlobalUriEditorOpener do


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1709842 is a reply to message #1709837] Thu, 01 October 2015 13:08 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
Hi,

oh ok. yes you are right... I am using CDT. Then I'll try it with IHyperlink.
Thanks again.
Re: Control Variable Assignments [message #1709959 is a reply to message #1709842] Fri, 02 October 2015 09:12 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
I have solved the problem now in a different way. Instead of fighting with IHyperlink...I got the solution in the context of my first approach. What I did is to link still to the cpp-file. When I jump to that file this file gets my active window. With that I focused a variable/object simply with its offset and length:
val XtextHyperlink result = getHyperlinkProvider().get()
result.setHyperlinkRegion(region);
result.setURI(curi);
result.setHyperlinkText(hyperlinkText);
acceptor.accept(result);
			
if (u != null) { 
	val IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
	val ITextEditor editor = IDE.openEditor(page, _ifile) as ITextEditor;
	editor.selectAndReveal(u.nodeOffset, u.nodeLength);
}


The problem I have right now is ... that linking/jumping to that generated cpp-file occurs as soon as I click "ctrl" and hover the variable/object. But I want to link when clicking at an object, not during hovering. How can I do this?

Thanks
Re: Control Variable Assignments [message #1709960 is a reply to message #1709959] Fri, 02 October 2015 09:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
As i said, you should move that to the open() method of the IHyperlink().

val IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
val ITextEditor editor = IDE.openEditor(page, _ifile) as ITextEditor;
editor.selectAndReveal(u.nodeOffset, u.nodeLength);



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1709962 is a reply to message #1709960] Fri, 02 October 2015 09:37 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
That is not the problem. It is working fine. But how can I check if an object is clicked? How can I check a "Clicking" event?
Re: Control Variable Assignments [message #1709963 is a reply to message #1709962] Fri, 02 October 2015 09:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
no you dont
you simply create the hyperlink
the framework will then call open on it if you click/f3


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1709966 is a reply to message #1709963] Fri, 02 October 2015 09:43 Go to previous messageGo to next message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
click/f3 is working fine. But is there no event handler for clicking on the editor?
Re: Control Variable Assignments [message #1709973 is a reply to message #1709966] Fri, 02 October 2015 10:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi i do not understand this question. can you please elaborate.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Control Variable Assignments [message #1711007 is a reply to message #1709973] Mon, 12 October 2015 10:06 Go to previous message
tam ay is currently offline tam ayFriend
Messages: 44
Registered: May 2015
Member
Thank you, I solved it.

But I have another important problem. Do you have an idea? :
http://www.eclipse.org/forums/index.php?t=msg&th=1071102&goto=1710948&

Previous Topic:Building current Xtext master branch locally fails
Next Topic:[Xtext] Backslash at end of line
Goto Forum:
  


Current Time: Thu Apr 25 07:00:41 GMT 2024

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

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

Back to the top