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
请先登录再写评论。
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:
here symbol is complex, WORD and DOT are leaves.
lexer returns leaves, parser makes up complex elements.
try ID instead of id
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' ?:|
{ tokens = [ id = "regexp:\p{Alpha}\w*" ] } // [...] var_def ::= 'var' var_name var_name ::= idcould you post a link to the tutorial, please?
http://www.jetbrains.org/intellij/sdk/docs/reference_guide/custom_language_support/implementing_parser_and_psi.html
http://www.jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support/grammar_and_parser.html
And a few examples I was looking at:
https://github.com/JetBrains/intellij-sdk-docs/blob/master/code_samples/simple_language_plugin/src/com/simpleplugin/Simple.bnf
https://github.com/JetBrains/Grammar-Kit/blob/master/testData/livePreview/Json.bnf
Thanks for diving into it!
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
?
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.
from
https://github.com/JetBrains/Grammar-Kit
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. ;)
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...
why not
?
or is
var_name
ID
necessary?
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.
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
Annotator was the key!
https://github.com/Gregoor/graphql-intellij-plugin/issues/1