Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Grammar and Color high light of Nested Variable Names(Two type of variables, which we can use nested format )
Grammar and Color high light of Nested Variable Names [message #1798212] Wed, 14 November 2018 09:28 Go to next message
Santhiveeran S is currently offline Santhiveeran SFriend
Messages: 7
Registered: November 2018
Junior Member
Hi All,
First of Thanks to read my queries and trying to help me.

I am trying to write the Grammar for the new requirement

I have two type of variables, local and global variable.

local variable syntax: (* path1>path2) or (# path1>path2>)
terminal LOCALVARIABLESTRING: '(*' -> ')' | '(#' -> ')'
Global variable syntax: ($ path1>path2)
terminal GLOBALVARIABLESTRING: '($' -> ')'

I am able to write the grammar for the above and working fine for the color high light also.

But now the new requirement is nested structure variables

(* localpath1>localpath2>($ globalpath1>globalpath2)>localpath3)
($ glopath1>glopath2>(* localpath1>localpath2)>globalpath3)

It is like a path to get the values, so local variable can contain global variable same like global contains local variables.

I am struck in nested structure grammar and syntax color high light.
Re: Grammar and Color high light of Nested Variable Names [message #1798299 is a reply to message #1798212] Thu, 15 November 2018 06:05 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
are you sure path are terminals and not a datatype (parser rule) or a real parser rule something like:

Model:
	(paths +=Path ";")*;
	
Path:
	GlobalPath | LocalPath
;	

GlobalPath:
	"($" (PathSegment(">" PathSegment)*)? ")"
;

LocalPath:
	"(*" (PathSegment(">" PathSegment)*)? ")"	
;

PathSegment:
	ID | Path
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Grammar and Color high light of Nested Variable Names [message #1798364 is a reply to message #1798299] Fri, 16 November 2018 08:39 Go to previous messageGo to next message
Santhiveeran S is currently offline Santhiveeran SFriend
Messages: 7
Registered: November 2018
Junior Member
Thanks Christian.

I chose as Terminal to make simple for the color high light.

if it is terminal rule, then I need to use semantic high light approach. In that I am not able to make the color coding.

The Local variable color is Blue and Global variable color is PINK.

then I need to show this like below

(* localpath1>localpath2>($ globalpath1>globalpath2)>localpath3)

In Semantic logic, I am able to get the rootNode and loop the each node, but I am not able find the algorithm to make the color with the help of acceptor.addPosition().

Could you please guide me how to use the addPosition() method to make color partially in the node.
Re: Grammar and Color high light of Nested Variable Names [message #1798365 is a reply to message #1798364] Fri, 16 November 2018 08:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
can you please share why you have tried so far?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Grammar and Color high light of Nested Variable Names [message #1798366 is a reply to message #1798365] Fri, 16 November 2018 09:07 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
public class MyDslSemanticHighlightingCalculator extends DefaultSemanticHighlightingCalculator {
	
	@Inject 
	private MyDslGrammarAccess ga;
	
	@Inject
	private OperationCanceledManager operationCanceledManager;
	
	@Override
	protected boolean highlightElement(EObject object, IHighlightedPositionAcceptor acceptor,
			CancelIndicator cancelIndicator) {
		if (object instanceof Model) {
			ICompositeNode node = NodeModelUtils.findActualNodeFor(object);
			handlePathNode(node,acceptor,cancelIndicator);
		}
		return super.highlightElement(object, acceptor, cancelIndicator);
	}
	
	private void handlePathNode(INode n, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) {
                acceptor.addPosition(n.getOffset(), n.getLength()-1, HighlightingStyles.STRING_ID);
		BidiTreeIterable<INode> iterable = n.getAsTreeIterable();
		for (INode nx : iterable) {
			operationCanceledManager.checkCanceled(cancelIndicator);
			EObject grammarElement = nx.getGrammarElement();
			if (grammarElement instanceof RuleCall) {
				if (ga.getGlobalPathRule() == ((RuleCall) grammarElement).getRule()) {
					acceptor.addPosition(nx.getOffset(), nx.getLength(), HighlightingStyles.KEYWORD_ID);
				}
			}
		}
	}
	

}


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

[Updated on: Fri, 16 November 2018 09:09]

Report message to a moderator

Previous Topic:modifications to imported models
Next Topic:[Formatter2] format action does nothing
Goto Forum:
  


Current Time: Tue Apr 23 10:20:53 GMT 2024

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

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

Back to the top