IntelliJ Platform Plugin SDK
 
IntelliJ Platform Explorer

Implementing Parser and PSI

Edit pageLast modified: 18 November 2024

Parsing files in IntelliJ Platform is a two-step process.

First, an abstract syntax tree (AST) is built, defining the structure of the program. AST nodes are created internally by the IDE and are represented by instances of the ASTNode class. Each AST node has an associated element type IElementType instance, and the element types are defined by the language plugin. The AST tree's top-level node for a file needs to have a special element type, which extends the IFileElementType class.

The AST nodes have a direct mapping to text ranges in the underlying document. The bottom-most nodes of the AST match individual tokens returned by the lexer, and higher-level nodes match multiple-token fragments. Operations performed on nodes of the AST tree, such as inserting, removing, reordering nodes, and so on, are immediately reflected as changes to the underlying document's text.

Second, a Program Structure Interface (PSI) tree is built on top of the AST, adding semantics and methods for manipulating specific language constructs. Nodes of the PSI tree are represented by classes implementing the PsiElement interface and are created by the language plugin in the ParserDefinition.createElement() method. The top-level node of the PSI tree for a file needs to implement the PsiFile interface and is created in the ParserDefinition.createFile() method.

Example: ParserDefinition for Properties language plugin

The PSI's lifecycle is described in more detail in Fundamentals.

The base classes for the PSI implementation, including PsiFileBase, the base implementation of PsiFile, and ASTWrapperPsiElement, the base implementation of PsiElement, are provided by IntelliJ Platform.