How to make syntax hightlight with different style?

已回答

I want to highlight my IDENTIFIER token type in different ways. With my .bnf file, I defined something like below:

...
tokens = [
  IDENTIFIER='regexp:([:letter:] | \_)(\w | \.)*'
]

NamespaceDefinition ::= 'namespace' IDENTIFIER
IncludeDefinition ::= 'include' IDENTIFIER

Now I'm trying to highlight IDENTIFIER in different ways for example:

I tried to use flex state to distinguish them, like:

This will highlight as expected. But also will cause syntax error, because my .bnf requires IDENTIFIER, but the flex gives CUSTOM types:

I considered defining duplicate IDENTIFIER in my bnf file, but that's not looks like the best way :

...
tokens = [
  IDENTIFIERA='regexp:([:letter:] | \_)(\w | \.)*'
  IDENTIFIERB='regexp:([:letter:] | \_)(\w | \.)*'
]

NamespaceDefinition ::= 'namespace' IDENTIFIERA
IncludeDefinition ::= 'include' IDENTIFIERB

Please teach my how to resolve this, I have been agonizing over this question for several days, Please help o(╥﹏╥)o……

0

If the only purpose is different highlighting, you should not use Lexer states. You may want to use different tokens though, but that's really depending on the language and the features/semantics.

You can use additional highlighting via Annotator as described here https://plugins.jetbrains.com/docs/intellij/syntax-highlighting-and-error-highlighting.html#syntax

0

If I use different tokens, is that means I shoud define two different token with same regexp like below? Cause this looks really weird…

...
tokens = [
  IDENTIFIER_A='regexp:([:letter:] | \_)(\w | \.)*'
  IDENTIFIER_B='regexp:([:letter:] | \_)(\w | \.)*'
]

NamespaceDefinition ::= 'namespace' IDENTIFIER_A
IncludeDefinition ::= 'include' IDENTIFIER_B

 

0

If you want to, then yes. Nothing wrong with that per se.

0

请先登录再写评论。