Skip to content

Commit

Permalink
fix: handle cases when our lexer is instantiated before the ParserDef…
Browse files Browse the repository at this point in the history
…inition (fixes #681)
  • Loading branch information
bjansen committed Jan 9, 2024
1 parent 4e41c9b commit a67e6db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,16 @@
import com.intellij.psi.PsiFile;
import com.intellij.psi.tree.IFileElementType;
import com.intellij.psi.tree.TokenSet;
import org.antlr.intellij.adaptor.lexer.PSIElementTypeFactory;
import org.antlr.intellij.plugin.adaptors.ANTLRv4GrammarParser;
import org.antlr.intellij.plugin.adaptors.ANTLRv4LexerAdaptor;
import org.antlr.intellij.plugin.parser.ANTLRv4Lexer;
import org.antlr.intellij.plugin.parser.ANTLRv4Parser;
import org.jetbrains.annotations.NotNull;

/** The general interface between IDEA and ANTLR. */
public class ANTLRv4ParserDefinition implements ParserDefinition {
public static final IFileElementType FILE =
new IFileElementType(ANTLRv4Language.INSTANCE);

public ANTLRv4ParserDefinition() {
PSIElementTypeFactory.defineLanguageIElementTypes(
ANTLRv4Language.INSTANCE,
ANTLRv4Lexer.tokenNames,
ANTLRv4Parser.ruleNames
);
}

@NotNull
@Override
public Lexer createLexer(Project project) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
package org.antlr.intellij.plugin.adaptors;

import org.antlr.intellij.adaptor.lexer.ANTLRLexerAdaptor;
import org.antlr.intellij.adaptor.lexer.PSIElementTypeFactory;
import org.antlr.intellij.plugin.ANTLRv4Language;
import org.antlr.intellij.plugin.parser.ANTLRv4Lexer;
import org.antlr.intellij.plugin.parser.ANTLRv4Parser;
import org.antlr.v4.runtime.Lexer;

/** Adapt ANTLR needs to intellij */
public class ANTLRv4LexerAdaptor extends ANTLRLexerAdaptor {

// In case a lexer was created outside our ParserDefinition
static {
PSIElementTypeFactory.defineLanguageIElementTypes(
ANTLRv4Language.INSTANCE,
ANTLRv4Lexer.tokenNames,
ANTLRv4Parser.ruleNames
);
}

private static final ANTLRv4LexerState INITIAL_STATE = new ANTLRv4LexerState(Lexer.DEFAULT_MODE, null, 0);

public ANTLRv4LexerAdaptor(ANTLRv4Lexer lexer) {
Expand Down

0 comments on commit a67e6db

Please sign in to comment.