Appearance
In order for the Consulo 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.
Reference: Implementing a Parser and PSI
3.1. Define a Token Type
Create SimpleTokenType in the org.consulo.sdk.language.psi package (see the simple_language_plugin code sample) by subclassing IElementType.
java
package org.consulo.sdk.language.psi;
import consulo.language.ast.IElementType;
import org.consulo.sdk.language.SimpleLanguage;
import jakarta.annotation.Nonnull;
public class SimpleTokenType extends IElementType {
public SimpleTokenType(@Nonnull String debugName) {
super(debugName, SimpleLanguage.INSTANCE);
}
@Override
public String toString() {
return "SimpleTokenType." + super.toString();
}
}3.2. Define an Element Type
Create the SimpleElementType in the org.consulo.sdk.language.psi package by subclassing IElementType.
java
package org.consulo.sdk.language.psi;
import consulo.language.ast.IElementType;
import org.consulo.sdk.language.SimpleLanguage;
import jakarta.annotation.Nonnull;
public class SimpleElementType extends IElementType {
public SimpleElementType(@Nonnull String debugName) {
super(debugName, SimpleLanguage.INSTANCE);
}
}3.3. Define the Grammar
Define a grammar for the Simple Language in the org/consulo/sdk/language/Simple.bnf file.
properties
{
parserClass="org.consulo.sdk.language.parser.SimpleParser"
extends="consulo.language.impl.psi.ASTWrapperPsiElement"
psiClassPrefix="Simple"
psiImplClassSuffix="Impl"
psiPackage="org.consulo.sdk.language.psi"
psiImplPackage="org.consulo.sdk.language.psi.impl"
elementTypeHolderClass="org.consulo.sdk.language.psi.SimpleTypes"
elementTypeClass="org.consulo.sdk.language.psi.SimpleElementType"
tokenTypeClass="org.consulo.sdk.language.psi.SimpleTokenType"
}
simpleFile ::= item_*
private item_ ::= (property|COMMENT|CRLF)
property ::= (KEY? SEPARATOR VALUE?) | KEYAs shown, a properties file can contain properties, comments, and line breaks.
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 key and value. This flexibility allows the Consulo to recognize incorrectly defined properties and provide corresponding code analysis and quick-fixes.
Note that the SimpleTypes class in the elementTypeHolderClass field above specifies the name of a class that gets generated from the grammar; it doesn't exist at this point.
3.4. 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. Mark this folder as Generated Sources Root and make sure everything compiles without errors.
TIP
The maven-consulo-plugin can be used to automate parser generation as part of the Maven build.
