Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Terminal rule keywords highlighting(Terminal rule keywords highlighting)
Terminal rule keywords highlighting [message #1733138] Tue, 24 May 2016 10:19 Go to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
Hi,

Can we highlight selected keywords in terminal rule?

Terminal Notes: 'Start' -> NL+;

terminal NL : ('\r'? '\n')+;

If anything comes after can be any text. So if any keywords comes in the text I need to highlight them. Is it possible?


I can highlight every word in the rule but I don't want that.
Re: Terminal rule keywords highlighting [message #1733213 is a reply to message #1733138] Wed, 25 May 2016 12:58 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
Have a look at semantic highlighting: http://www.eclipse.org/Xtext/documentation/310_eclipse_support.html#highlighting

---
Get professional support from the Xtext committers at www.typefox.io
Re: Terminal rule keywords highlighting [message #1733222 is a reply to message #1733213] Wed, 25 May 2016 13:44 Go to previous messageGo to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
Will it works for terminal rules? How?
Re: Terminal rule keywords highlighting [message #1733229 is a reply to message #1733222] Wed, 25 May 2016 14:55 Go to previous messageGo to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
Hi,

Why my custom terminal rule is not showing up as instanceof tTerminalRuleImpl?

I have written below code:

class JPLSemanticHighlightingCalculator implements ISemanticHighlightingCalculator {
override provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor,CancelIndicator cancelIndicator) {
val ICompositeNode rootNode = resource.getParseResult().getRootNode();
val BidiTreeIterator<INode> it = rootNode.getAsTreeIterable().iterator();
while(it.hasNext){
val INode node = it.next();
if( node.getGrammarElement() instanceof TerminalRuleImpl)
{
val s = node.text
println("Node ######################### "+s)
acceptor.addPosition( node.getOffset(), node.getLength(), s );
}
}
}
}

and my terminal rules are:
terminal DBMS: 'DBMS' -> ('\n' | '\r')+;

terminal ID : ('a'..'z'|'A'..'Z'|'_'|'$'|'.'|'@')('a'..'z'|'A'..'Z'|'_'|'-'|'+'|'$'|'.'|'0'..'9'|'@')*;

terminal STRING :
'"' ( '\\'('"'|('\r'? '\n')) | !('\n'|'"') )* '"' |
"'" ( '\\'("'"|('\r'? '\n')) | !('\n'|"'") )* "'";

terminal SL_COMMENT: '//' !('\n' | '\r')*;

terminal HASH_COMMENT : ('#'('\r'? '\n')?) | ('#' !('#'|'\n'|'\r')+ ('\r'? '\n')?) | (('#''#''#') !('\n'|'\r')* ('\r'? '\n')?);

terminal WS : (' '|'\t'| ('\\'(' '* '\r'? '\n')))+;

terminal NL : ('\r'? '\n')+;

terminal NUMBER: (OCTAL|FLOAT|INT|BINARY|HEX);

terminal OCTAL returns ecore::EInt: ('0')('0'..'7')+;

terminal FLOAT returns ecore::EFloat: ('0'..'9' ('0'..'9')*) '.' ('0'..'9')*;

terminal INT returns ecore::EInt: '0'..'9' ('0'..'9')*;

terminal RANGE: '0'..'9' '..' ('0'..'9')*;

terminal BINARY returns ecore::EInt: ('0b'|'0B') ('0'|'1')+;

terminal HEX returns ecore::EInt:('0x'|'0X')('0'..'9'|'a'..'f'|'A'..'F')+;
Re: Terminal rule keywords highlighting [message #1733252 is a reply to message #1733229] Wed, 25 May 2016 17:32 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13214
Registered: July 2009
Senior Member
Looks like you have 2 missunderstandings at one

- how the node model grammar elements look like
- how the api of the accept method looks like

class JPLSemanticHighlightingCalculator implements 
ISemanticHighlightingCalculator {
	override provideHighlightingFor(XtextResource resource, 
IHighlightedPositionAcceptor acceptor,
		CancelIndicator cancelIndicator) {
		val ICompositeNode rootNode = resource.getParseResult().getRootNode();
		val BidiTreeIterator<INode> it = rootNode.getAsTreeIterable().iterator();
		while (it.hasNext) {
			val INode node = it.next();
			if (node.getGrammarElement() instanceof RuleCall) {
				val rc = node.getGrammarElement() as RuleCall
				val rule = rc.getRule()
				if (rule instanceof TerminalRule) {
					val s = node.text
					println("Node ######################### " + s)
					acceptor.addPosition(node.getOffset(), node.getLength(), 
DefaultHighlightingConfiguration.KEYWORD_ID);
				}
			}
		}
	}
}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Antlr Error with grammar
Next Topic:Web editing of DSLs
Goto Forum:
  


Current Time: Thu Nov 21 09:06:18 GMT 2019

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

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

Back to the top