Language Plugin: Use specific rule as elementType

Hi there,

working on my first IntelliJ plugin but struggling a bit:

My grammar in BNF looks somewhat like this:

{
     tokens = [
          id = "regexp:\p{Alpha}\w*"
     ]
}

// [...]

var_def ::= 'var' var_name
var_name ::= id


Now I try to apply syntax highlighting by comparing IElementTypes but naturally the variable name has the elementType 'id'.

I feel like I'm missing something really basic, but reading the plugin docs over and over again didn't enlighten me so I'm hoping someone here can.

Cheers,
Gregor

0

Hello Gregor,

I may misunderstand how bnf works but here is my view of it:

Complex elements are usually named lowercase

Complex elements are defined in terms of 'leaves' which are named uppercase.

Leaves do not need to be defined via other elements

Complex elements are made up from a mix of complex elements & leaves

A simple example:

symbol ::= WORD DOT WORD


here symbol is complex, WORD and DOT are leaves.

lexer returns leaves, parser makes up complex elements.


try ID instead of id

0
Avatar
Permanently deleted user

Hello Imants and ty for the quick reply.

I changed the casing as your argument does make sense to me. Sadly the elementType is still 'ID' ?:|

0
{
     tokens = [
          id = "regexp:\p{Alpha}\w*"
     ]
}

// [...]

var_def ::= 'var' var_name
var_name ::= id


could you post a link to the tutorial, please?

0

You seem to follow this one, right:

https://github.com/JetBrains/Grammar-Kit/blob/master/testData/livePreview/Json.bnf
?

this is Grammar kit test data, no? Not sure what its purpose is. It is possible that this is not an example to follow.


Could you try to follow this example instead:

https://github.com/JetBrains/intellij-sdk-docs/blob/master/code_samples/simple_language_plugin/src/com/simpleplugin/Simple.bnf

?

0
Avatar
Permanently deleted user

exactly, I'm using GrammarKit. I understood it's purpose to be that writing a grammar in BNF is pretty straight-forward. And with it you can just compile your BNF grammar to lexer & parser files.

When I try the second example (removing the ID token), I end up with a lot of bad characters.

0

from
https://github.com/JetBrains/Grammar-Kit

Adds BNF Grammars and JFlex files editing support including parser/PSI code generator.




Grammar kit is a helper tool. The simple example shows how to design bnf with the help of Grammar kit plugin.

Basically, we use Grammar kit to design bnf as per simple plugin example. Grammar kit wraps some detail and makes the task easier.


> I end up with a lot of bad characters.

it's a start. Need to work on bnf structure to make the bad characters interpreted correctly. ;)

0
Avatar
Permanently deleted user

Yup, that's what I expected from GrammarKit as well :)

Without adding ID to the token list the PSI Viewer already shows me my basic grammar structure but the basic elements are resolved one step too far (e.g. shown as PsiElements of type ID instead of type var_name). So the basic PSI Tree looks good, but I guess it's not as shallow as I expect it to be. I also didn't find a way to write a syntax highlighter that checks for the context of an element...

0

why not

var_def ::= VAR ID


?

or is
     var_name
          ID

necessary?

0
Avatar
Permanently deleted user

I would need to have the VarNameElement to style it differently, changing the rules that way leads to the var_name still being of type 'ID' but it removes the 'var_name' layer in between.

Basically it changes from this tree:

var_def(var_name(ID))

To this tree:

var_def(ID)

ID is something I user in other places for non-var-names as well, so I can't just style every ID tag a certain way.

0

is it possible to use e.g. ID1 and ID2?

they could be named more appropriately. The point is: use different leaves for different elements.

if your lexer can tell them apart, this should work. You would not need that extra var_name

0

请先登录再写评论。