Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Lexical syntax highlighting
Lexical syntax highlighting [message #479001] Fri, 07 August 2009 22:21 Go to next message
Eclipse UserFriend
Originally posted by: c.krause.cwi.nl

Hi,
sorry I know this is covered in the docu, but I do not fully understand
how I can hook up my own custom lexical highlighter. Is there any
example that shows how exactly I should hook up the
ILexicalHighlightingConfiguration and the AbstractTokenScanner +
AbstractAntlrTokenToAttributeIdMapper?

The token scanner I should hook up via bindITokenScanner() in my
XXXUIModule, right? And then I override getTokenIdMapper() in there?

How about the ILexicalHighlightingConfiguration?

Cheers,
Christian
Re: Lexical syntax highlighting [message #479020 is a reply to message #479001] Sat, 08 August 2009 08:21 Go to previous messageGo to next message
Dakshinamurthy Karra is currently offline Dakshinamurthy KarraFriend
Messages: 45
Registered: July 2009
Member
Christian Krause wrote:

> sorry I know this is covered in the docu, but I do not fully understand
> how I can hook up my own custom lexical highlighter. Is there any
> example that shows how exactly I should hook up the
> ILexicalHighlightingConfiguration and the AbstractTokenScanner +
> AbstractAntlrTokenToAttributeIdMapper?

The documentation is not really helpful for beginners. But once you get
the hang of guice injection and how it is used within XText, everything
should look simple.

> The token scanner I should hook up via bindITokenScanner() in my
> XXXUIModule, right? And then I override getTokenIdMapper() in there?

If you bind a ITokenScanner you need to implement all the stuff. In XText,
you just need to bind a AbstractAntlrTokenToAttributeIdMapper.

In detail:

1. Create a LexicalHighlightingConfiguration class that extends either
DefaultLexicalHighlightingConfiguration or implements
ILexicalHighlightingConfiguration. You need to override
configure(IHighlightingConfigurationAcceptor acceptor) method and call
acceptor.acceptDefaultHighlighting to register textstyles.
2. Create a AntlrTokenToAttributeIdMapper class that extends either
DefaultAntlrTokenToAttributeIdMapper or
AbstractAntlrTokenToAttributeIdMapper. You need to override the
calculateId(String tokenName, int tokenType) method and return the ID
defined in the LexicalHighlightingConfiguration for the tokens.
3. Update your <grammar>UIModule.java and add these two functions:
public Class<? extends ILexicalHighlightingConfiguration>
bindILexicalHighlightingConfiguration() {
return LexicalHighlightingConfiguration.class;
}

public Class<? extends AbstractAntlrTokenToAttributeIdMapper>
bindAbstractAntlrTokenToAttributeIdMapper() {
return AntlrTokenToAttributeIdMapper.class ;
}

XText uses reflection to find all bind* methods and injects the
appropriate objects.

Done.

HTH.

-- KD
Re: Lexical syntax highlighting [message #479067 is a reply to message #479020] Sat, 08 August 2009 18:55 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: christian.krause.cwi.nl

Thanks a lot. That worked like a charm.

Christian

Am 08.08.2009 10:21, schrieb KD:
> Christian Krause wrote:
>
>> sorry I know this is covered in the docu, but I do not fully
>> understand how I can hook up my own custom lexical highlighter. Is
>> there any example that shows how exactly I should hook up the
>> ILexicalHighlightingConfiguration and the AbstractTokenScanner +
>> AbstractAntlrTokenToAttributeIdMapper?
>
> The documentation is not really helpful for beginners. But once you get
> the hang of guice injection and how it is used within XText, everything
> should look simple.
>
>> The token scanner I should hook up via bindITokenScanner() in my
>> XXXUIModule, right? And then I override getTokenIdMapper() in there?
>
> If you bind a ITokenScanner you need to implement all the stuff. In
> XText, you just need to bind a AbstractAntlrTokenToAttributeIdMapper.
>
> In detail:
>
> 1. Create a LexicalHighlightingConfiguration class that extends either
> DefaultLexicalHighlightingConfiguration or implements
> ILexicalHighlightingConfiguration. You need to override
> configure(IHighlightingConfigurationAcceptor acceptor) method and call
> acceptor.acceptDefaultHighlighting to register textstyles.
> 2. Create a AntlrTokenToAttributeIdMapper class that extends either
> DefaultAntlrTokenToAttributeIdMapper or
> AbstractAntlrTokenToAttributeIdMapper. You need to override the
> calculateId(String tokenName, int tokenType) method and return the ID
> defined in the LexicalHighlightingConfiguration for the tokens.
> 3. Update your <grammar>UIModule.java and add these two functions:
> public Class<? extends ILexicalHighlightingConfiguration>
> bindILexicalHighlightingConfiguration() {
> return LexicalHighlightingConfiguration.class;
> }
>
> public Class<? extends AbstractAntlrTokenToAttributeIdMapper>
> bindAbstractAntlrTokenToAttributeIdMapper() {
> return AntlrTokenToAttributeIdMapper.class ;
> }
>
> XText uses reflection to find all bind* methods and injects the
> appropriate objects.
>
> Done.
>
> HTH.
>
> -- KD
>
>
Re: Lexical syntax highlighting [message #552723 is a reply to message #479001] Fri, 13 August 2010 09:29 Go to previous messageGo to next message
Jos Warmer is currently offline Jos WarmerFriend
Messages: 66
Registered: July 2009
Member
Hi, this thread is helpful, but I cannot see how to achieve the following.
I want a subset of the keywords in my language to have a different color than the default keyword color. I don't understand how to adapt the lexical highlighting to achieve this.

-- Jos
Re: Lexical syntax highlighting [message #552731 is a reply to message #552723] Fri, 13 August 2010 09:41 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

in the highlighting configuration you only state which styles are
available. Which style is in fact used (for lexical highlighting) is
determined in the DefaultAntlrTokenToAttributeIdMapper. You will have to
bind your own implementation and determine the style based on the token
name/type.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Lexical syntax highlighting [message #552736 is a reply to message #552731] Fri, 13 August 2010 10:31 Go to previous messageGo to next message
Jos Warmer is currently offline Jos WarmerFriend
Messages: 66
Registered: July 2009
Member
Alex, this is what I am trying to do. IOn the Lexical highlighting I define a new style:
public class MyHighLightingConfiguration extends DefaultHighlightingConfiguration{
	public static final String LAYOUT_ID = "layout";
	
	public void configure(IHighlightingConfigurationAcceptor acceptor) {
		super.configure(acceptor);
		acceptor.acceptDefaultHighlighting(LAYOUT_ID, "Layout",	layoutTextStyle());

	}
	
	public TextStyle layoutTextStyle() {
		TextStyle textStyle = new TextStyle();
		textStyle.setColor(new RGB(0, 0, 255));
		textStyle.setStyle(SWT.ITALIC);
		return textStyle;
	}

}

And in the token id scanner I try to use it:
public class MyAntlrTokenToAttributeIdMapper extends DefaultAntlrTokenToAttributeIdMapper {

	@Override
	protected String calculateId(String tokenName, int tokenType) {
		if( "container".equals(tokenName)) {
			return MyHighLightingConfiguration.LAYOUT_ID;
		}
		return super.calculateId(tokenName, tokenType);
	}
}

And in the module I bind them:
	public Class<? extends IHighlightingConfiguration> bindILexicalHighlightingConfiguration() {
		return MyHighLightingConfiguration.class;
	}

	public Class<? extends AbstractAntlrTokenToAttributeIdMapper> bindAbstractAntlrTokenToAttributeIdMapper() {
		return MyAntlrTokenToAttributeIdMapper.class ;
	}

Nothing happens, I still get the default keyword color for the "container" keyword.
If I take out the call to 'super' in MyHighlightingConfiguration all keyword syntax coloring dissappears, so the binding works ok.

-- Jos
Re: Lexical syntax highlighting [message #552740 is a reply to message #552736] Fri, 13 August 2010 10:42 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

have you checked whether the token name is indeed "container"? Have you
tried returing your LAYOUT_ID for all elements, to see whether then all
keywords get your new style?

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Lexical syntax highlighting [message #552742 is a reply to message #552740] Fri, 13 August 2010 11:00 Go to previous messageGo to next message
Jos Warmer is currently offline Jos WarmerFriend
Messages: 66
Registered: July 2009
Member
Alex,

Good point! I print the tokenName and it prints as 'container'. Looks ok, but after rereading it several times I realized that the single quotes are part of the tokenName. I now check for equality with 'container' including the quotes (read carefully) it works ok:
	@Override
	protected String calculateId(String tokenName, int tokenType)kenName + "] type [" + tokenType + "]");
		if( "'container'".equals(tokenName)) {
			return MyHighLightingConfiguration.LAYOUT_ID;
		}
		return super.calculateId(tokenName, tokenType);
	}

Surprising behavior at least because tehre are no quotes in the input anywhere, maybe even a bug.

-- Thanks, Jos
Re: Lexical syntax highlighting [message #552744 is a reply to message #552740] Fri, 13 August 2010 10:56 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
ps:
> have you checked whether the token name is indeed "container"?
I am almost prepared to bet that it is "'container'".

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Lexical syntax highlighting [message #552746 is a reply to message #552742] Fri, 13 August 2010 11:09 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi

> Surprising behavior at least because tehre are no quotes in the input
> anywhere, maybe even a bug.

I don't think it is a bug. I believe it is intended in order to
distinguish keywords from rule names. After all, nobody prevents you
from having a grammar containing

SomeRule: 'ID' id=ID;

in which case the token names could be identical if keywords did not get
the single quotes (don't hit me if the name for the ID rule is ID_RULE,
in this case there could be a keyword ID_RULE as well).

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Lexical syntax highlighting [message #552761 is a reply to message #552746] Fri, 13 August 2010 12:20 Go to previous messageGo to next message
Jos Warmer is currently offline Jos WarmerFriend
Messages: 66
Registered: July 2009
Member
Alex,

Ok, then it is just me who didn't expect this.

-- Jos
Re: Lexical syntax highlighting [message #727588 is a reply to message #552761] Wed, 21 September 2011 15:20 Go to previous messageGo to next message
Andrey Mising name is currently offline Andrey Mising nameFriend
Messages: 26
Registered: September 2011
Junior Member
Hi,

Is it possible to highlight also hidden parts?
In my DSL everything that start with % is hidden, but i want to be able to change the color of this tokens. Is it possible?

Thank you
Regards,
Andrey
Re: Lexical syntax highlighting [message #727630 is a reply to message #727588] Wed, 21 September 2011 17:27 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Yes,
That is how comments get highlighted. This is all in the documentation.
- henrik

On 9/21/11 5:20 PM, Andrey wrote:
> Hi,
>
> Is it possible to highlight also hidden parts?
> In my DSL everything that start with % is hidden, but i want to be able
> to change the color of this tokens. Is it possible?
>
> Thank you
> Regards,
> Andrey
Re: Lexical syntax highlighting [message #1782138 is a reply to message #727630] Mon, 19 February 2018 11:47 Go to previous message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 200
Registered: August 2013
Senior Member
Hi

I can't find neither bindILexicalHighlightingConfiguration(), nor bindAbstractAntlrTokenToAttributeIdMapper() in my <grammar>UIModule.java.

Also I can't find any information on how to register custom highlighters here: https://www.eclipse.org/Xtext/documentation/310_eclipse_support.html#highlighting

Could you suggest how to register them?

Thanks
Previous Topic:Creating hyperlinks in Xtext editor
Next Topic:Problem with ID Terminal Rule
Goto Forum:
  


Current Time: Sun Sep 23 10:34:31 GMT 2018

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

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

Back to the top