3. Grammar and Parser
In order for the IntelliJ Platform to parse a Simple Language file, tokens and elements must be defined based on IElementType
. The Simple Language grammar must also be defined to generate a parser.
Define a Token Type
Create SimpleTokenType
in the org.intellij.sdk.language.psi
package by subclassing IElementType
.
Define an Element Type
Create the SimpleElementType
in the org.intellij.sdk.language.psi
package by subclassing IElementType
.
Define the Grammar
Define a grammar for the Simple Language in the org/intellij/sdk/language/Simple.bnf file.
Please see Grammar-Kit documentation for more details on BNF syntax.
The grammar defines the flexibility of the support for a language. The above grammar specifies that a property may have or may not have a key and value. This flexibility allows the IntelliJ Platform to recognize incorrectly defined properties and provide corresponding code analysis and quick fixes.
Note that the SimpleTypes
class in the elementTypeHolderClass
attribute above specifies the name of a class that gets generated from the grammar in the scope of the Generate Parser Code action (see Generate a Parser); it doesn't exist at this point.
Generate a Parser
Now that the grammar is defined, generate a parser with PSI classes via Generate Parser Code from the context menu on the Simple.bnf file. This step generates a parser and PSI elements in the /src/main/gen folder of the project.
Add Generated Sources Root
To include the sources generated into /src/main/gen, the project's sourceSets
must be expanded by inserting the following line in the project's Gradle build script:
See build.gradle.kts for the reference.
Reload the Gradle project for changes to take effect and build the project.