Skip to content

Commit

Permalink
Merge branch 'master' into issue_2_highlighting_again
Browse files Browse the repository at this point in the history
  • Loading branch information
bjansen authored Apr 1, 2023
2 parents 46e9a53 + 2b2d1b4 commit 4dd89b8
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 83 deletions.
18 changes: 9 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ buildscript {
}

plugins {
id 'org.jetbrains.intellij' version '0.3.12'
id 'org.jetbrains.intellij' version '1.13.3'
id 'maven-publish'
id 'signing'
}

wrapper {
gradleVersion = '4.10.2'
gradleVersion = '8.0.2'
}

group 'org.antlr'
Expand All @@ -26,31 +26,31 @@ compileJava {
}

intellij {
version = ideaVersion
version.set(ideaVersion)

pluginName 'antlr4-intellij-adaptor'
downloadSources true
updateSinceUntilBuild false
pluginName.set('antlr4-intellij-adaptor')
downloadSources.set(true)
updateSinceUntilBuild.set(false)
}

repositories {
mavenCentral()
}

dependencies {
compile("org.antlr:antlr4-runtime:$antlr4Version") {
implementation("org.antlr:antlr4-runtime:$antlr4Version") {
exclude group:'com.ibm.icu', module:'icu4j'
}
}

task sourcesJar(type: Jar) {
from sourceSets.main.allJava
classifier = 'sources'
archiveClassifier.set('sources')
}

task javadocJar(type: Jar) {
from javadoc
classifier = 'javadoc'
archiveClassifier.set('javadoc')
}

publishing {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ libraryVersion=0.2-SNAPSHOT
ideaVersion=2018.3.2

# The version of ANTLR v4 that will be used to generate the parser
antlr4Version=4.7.2
antlr4Version=4.12.0

# Variables required to allow build.gradle to parse,
# override in ~/.gradle/gradle.properties
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Sun Oct 21 15:06:30 MSK 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ public class ANTLRLexerAdaptor extends com.intellij.lexer.LexerBase {
* index tracked by IntelliJ. This field provides for an
* efficient implementation of {@link #getState}.
*/
private final Map<ANTLRLexerState, Integer> stateCacheMap = new HashMap<ANTLRLexerState, Integer>();
private final Map<ANTLRLexerState, Integer> stateCacheMap = new HashMap<>();

/**
* Provides a map from a state index tracked by IntelliJ
* &rarr; {@code ANTLRLexerState} object describing the ANTLR lexer
* state. This field provides for an efficient implementation
* of {@link #toLexerState}.
*/
private final List<ANTLRLexerState> stateCache = new ArrayList<ANTLRLexerState>();
private final List<ANTLRLexerState> stateCache = new ArrayList<>();

/**
* Caches the {@code buffer} provided in the call to {@link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.Vocabulary;
import org.antlr.v4.runtime.misc.Utils;
import org.jetbrains.annotations.NotNull;

Expand All @@ -12,6 +13,10 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

import static java.util.function.Function.*;
import static java.util.stream.Collectors.*;

/** The factory that automatically maps all tokens and rule names into
* IElementType objects: {@link TokenIElementType} and {@link RuleIElementType}.
Expand All @@ -29,42 +34,19 @@ public class PSIElementTypeFactory {
private PSIElementTypeFactory() {
}

public static void defineLanguageIElementTypes(Language language,
String[] tokenNames,
String[] ruleNames)
public static void defineLanguageIElementTypes(Language language, Vocabulary vocabulary, String[] ruleNames)
{
synchronized (PSIElementTypeFactory.class) {
if ( tokenIElementTypesCache.get(language)==null ) {
List<TokenIElementType> types = tokenIElementTypesCache.get(language);
if ( types==null ) {
types = createTokenIElementTypes(language, tokenNames);
tokenIElementTypesCache.put(language, types);
}
}
if ( ruleIElementTypesCache.get(language)==null ) {
List<RuleIElementType> result = ruleIElementTypesCache.get(language);
if ( result==null ) {
result = createRuleIElementTypes(language, ruleNames);
ruleIElementTypesCache.put(language, result);
}
}
if ( tokenNamesCache.get(language)==null ) {
tokenNamesCache.put(language, createTokenTypeMap(tokenNames));
}
if ( ruleNamesCache.get(language)==null ) {
ruleNamesCache.put(language, createRuleIndexMap(ruleNames));
}
tokenIElementTypesCache.computeIfAbsent(language, l -> createTokenIElementTypes(l, vocabulary));
ruleIElementTypesCache.computeIfAbsent(language, l -> createRuleIElementTypes(l, ruleNames));
tokenNamesCache.computeIfAbsent(language, l -> createTokenTypeMap(vocabulary));
ruleNamesCache.computeIfAbsent(language, l -> createRuleIndexMap(ruleNames));
}
}

public static TokenIElementType getEofElementType(Language language) {
TokenIElementType result = eofIElementTypesCache.get(language);
if (result == null) {
result = new TokenIElementType(Token.EOF, "EOF", language);
eofIElementTypesCache.put(language, result);
}

return result;
return eofIElementTypesCache.computeIfAbsent(language,
l -> new TokenIElementType(Token.EOF, "EOF", l));
}

public static List<TokenIElementType> getTokenIElementTypes(Language language) {
Expand All @@ -84,8 +66,9 @@ public static Map<String, Integer> getTokenNameToTypeMap(Language language) {
}

/** Get a map from token names to token types. */
public static Map<String, Integer> createTokenTypeMap(String[] tokenNames) {
return Utils.toMap(tokenNames);
public static Map<String, Integer> createTokenTypeMap(Vocabulary vocabulary) {
return IntStream.rangeClosed(0, vocabulary.getMaxTokenType()).boxed()
.collect(toMap(vocabulary::getDisplayName, identity()));
}

/** Get a map from rule names to rule indexes. */
Expand All @@ -94,17 +77,10 @@ public static Map<String, Integer> createRuleIndexMap(String[] ruleNames) {
}

@NotNull
public static List<TokenIElementType> createTokenIElementTypes(Language language, String[] tokenNames) {
List<TokenIElementType> result;
TokenIElementType[] elementTypes = new TokenIElementType[tokenNames.length];
for (int i = 0; i < tokenNames.length; i++) {
if ( tokenNames[i]!=null ) {
elementTypes[i] = new TokenIElementType(i, tokenNames[i], language);
}
}

result = Collections.unmodifiableList(Arrays.asList(elementTypes));
return result;
public static List<TokenIElementType> createTokenIElementTypes(Language language, Vocabulary vocabulary) {
return IntStream.rangeClosed(0, vocabulary.getMaxTokenType()).boxed()
.map(i -> new TokenIElementType(i, vocabulary.getDisplayName(i), language))
.collect(toList());
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public class PSITokenSource implements TokenSource {
protected PsiBuilder builder;
protected TokenFactory tokenFactory = CommonTokenFactory.DEFAULT;
protected TokenFactory<?> tokenFactory = CommonTokenFactory.DEFAULT;

public PSITokenSource(PsiBuilder builder) {
this.builder = builder;
Expand Down Expand Up @@ -52,7 +52,7 @@ public Token nextToken() {
int type = ideaTType!=null ? ideaTType.getANTLRTokenType() : Token.EOF;

int channel = Token.DEFAULT_CHANNEL;
Pair<TokenSource, CharStream> source = new Pair<TokenSource, CharStream>(this, null);
Pair<TokenSource, CharStream> source = new Pair<>(this, null);
String text = builder.getTokenText();
int start = builder.getCurrentOffset();
int length = text != null ? text.length() : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ANTLRParseTreeToPSIConverter implements ParseTreeListener {
protected final Language language;
protected final PsiBuilder builder;
protected Map<RecognitionException, SyntaxError> syntaxErrors;
protected final Deque<PsiBuilder.Marker> markers = new ArrayDeque<PsiBuilder.Marker>();
protected final Deque<PsiBuilder.Marker> markers = new ArrayDeque<>();

protected final List<TokenIElementType> tokenElementTypes;
protected final List<RuleIElementType> ruleElementTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.IntervalSet;
import org.antlr.v4.runtime.tree.ErrorNode;

/** Adapt ANTLR's DefaultErrorStrategy so that we add error nodes
* for EOF if reached at start of resync's consumeUntil().
Expand All @@ -16,7 +17,8 @@ public class ErrorStrategyAdaptor extends DefaultErrorStrategy {
protected void consumeUntil(Parser recognizer, IntervalSet set) {
Token o = recognizer.getCurrentToken();
if ( o.getType()==Token.EOF ) {
recognizer.getRuleContext().addErrorNode(o);
ErrorNode errorNode = recognizer.createErrorNode(recognizer.getRuleContext(), o);
recognizer.getRuleContext().addErrorNode(errorNode);
}
super.consumeUntil(recognizer, set);
}
Expand Down
13 changes: 4 additions & 9 deletions src/main/java/org/antlr/intellij/adaptor/psi/Trees.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
package org.antlr.intellij.adaptor.psi;

import com.intellij.lang.Language;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.project.Project;
Expand Down Expand Up @@ -226,14 +225,10 @@ public static PsiElement createLeafFromText(Project project, Language language,
public static void replacePsiFileFromText(final Project project, Language language, final PsiFile psiFile, String text) {
final PsiFile newPsiFile = createFile(project, language, text);
if ( newPsiFile==null ) return;
WriteCommandAction setTextAction = new WriteCommandAction(project) {
@Override
protected void run(@NotNull Result result) throws Throwable {
psiFile.deleteChildRange(psiFile.getFirstChild(), psiFile.getLastChild());
psiFile.addRange(newPsiFile.getFirstChild(), newPsiFile.getLastChild());
}
};
setTextAction.execute();
WriteCommandAction.runWriteCommandAction(project, () -> {
psiFile.deleteChildRange(psiFile.getFirstChild(), psiFile.getLastChild());
psiFile.addRange(newPsiFile.getFirstChild(), newPsiFile.getLastChild());
});
}

public static PsiFile createFile(Project project, Language language, String text) {
Expand Down
15 changes: 4 additions & 11 deletions src/main/java/org/antlr/intellij/adaptor/xpath/XPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@
import org.antlr.intellij.adaptor.lexer.RuleIElementType;
import org.antlr.intellij.adaptor.lexer.TokenIElementType;
import org.antlr.intellij.adaptor.psi.Trees;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.LexerNoViableAltException;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.xpath.XPathLexer;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -121,13 +120,7 @@ public XPath(Language language, String path) {
// TODO: check for invalid token/rule names, bad syntax

public XPathElement[] split(String path) {
ANTLRInputStream in;
try {
in = new ANTLRInputStream(new StringReader(path));
}
catch (IOException ioe) {
throw new IllegalArgumentException("Could not read path: "+path, ioe);
}
CharStream in = CharStreams.fromString(path);
XPathLexer lexer = new XPathLexer(in) {
public void recover(LexerNoViableAltException e) { throw e; }
};
Expand All @@ -145,7 +138,7 @@ public XPathElement[] split(String path) {

List<Token> tokens = tokenStream.getTokens();
// System.out.println("path="+path+"=>"+tokens);
List<XPathElement> elements = new ArrayList<XPathElement>();
List<XPathElement> elements = new ArrayList<>();
int n = tokens.size();
int i=0;
loop:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public XPathWildcardAnywhereElement() {

@Override
public Collection<PsiElement> evaluate(PsiElement t) {
if ( invert ) return new ArrayList<PsiElement>(); // !* is weird but valid (empty)
if ( invert ) return new ArrayList<>(); // !* is weird but valid (empty)
return Trees.getDescendants(t);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public XPathWildcardElement() {
@Override
public Collection<PsiElement> evaluate(final PsiElement t) {
if ( invert ) return new ArrayList<>(); // !* is weird but valid (empty)
List<PsiElement> kids = new ArrayList<PsiElement>();
List<PsiElement> kids = new ArrayList<>();
for (PsiElement c : t.getChildren()) {
kids.add(c);
}
Expand Down

0 comments on commit 4dd89b8

Please sign in to comment.