Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IFIleElementType Generation in Main #392

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/org/intellij/grammar/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.DebugUtil;
import org.intellij.grammar.fleet.FleetBnfAttributePostprocessor;
import org.intellij.grammar.generator.ParserGenerator;
import org.intellij.grammar.fleet.FleetBnfFileWrapper;
import org.intellij.grammar.fleet.FleetFileTypeGenerator;
import org.intellij.grammar.psi.BnfFile;

Expand Down Expand Up @@ -127,7 +127,7 @@ public static void main(String[] args) {
for (File file : files) {
if (file.isDirectory() || !grammarPattern.matcher(file.getName()).matches()) continue;
PsiFile psiFile = LightPsi.parseFile(file, parserDefinition);
if (!(psiFile instanceof BnfFile)) continue;
if (!(psiFile instanceof BnfFile bnfFile)) continue;

// for light-psi-all building:
if (args[0].contains("lightpsi")) {
Expand All @@ -139,7 +139,9 @@ public static void main(String[] args) {
}
count++;

BnfFile bnfFile = (generateForFleet) ? FleetBnfFileWrapper.wrapBnfFile((BnfFile)psiFile) : (BnfFile)psiFile;
if (generateForFleet) {
FleetBnfAttributePostprocessor.prepareForFleetGeneration(bnfFile);
}
new ParserGenerator(bnfFile, grammarDir.getAbsolutePath(), output.getAbsolutePath(), "").generate();
if (generateFileTypeElement) {
new FleetFileTypeGenerator((BnfFile)psiFile,
Expand Down
85 changes: 85 additions & 0 deletions src/org/intellij/grammar/fleet/FleetBnfAttributePostprocessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2011-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

package org.intellij.grammar.fleet;

import com.intellij.openapi.util.Key;
import org.intellij.grammar.KnownAttribute;
import org.intellij.grammar.generator.IAttributePostProcessor;
import org.intellij.grammar.psi.BnfFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

import static org.intellij.grammar.KnownAttribute.*;
import static org.intellij.grammar.fleet.FleetConstants.*;

//Wraps BnfFile to produce fleet-related attribute values.
//Implemented as BnfFileImpl extension to avoid implementation of all methods in BnfFile interface
public class FleetBnfAttributePostprocessor implements IAttributePostProcessor {

public static final Key<Boolean> GENERATE_FOR_FLEET = Key.create("GENERATE_FOR_FLEET");

private final Map<String, String> myFleetAttributeValuesSubstitution = Map.of(
PARSER_UTIL_CLASS.getName(), GPUB_CLASS,
ELEMENT_TYPE_CLASS.getName(), IELEMENTTYPE_CLASS,
TOKEN_TYPE_CLASS.getName(), IELEMENTTYPE_CLASS
);

private final Map<String, String> myDefaultGeneratedNames = Map.of(
PARSER_CLASS.getName(), PARSER_CLASS_DEFAULT,
ELEMENT_TYPE_HOLDER_CLASS.getName(), ELEMENT_TYPE_HOLDER_DEFAULT
);

private final List<String> mySuppressedFactories = List.of(ELEMENT_TYPE_FACTORY.getName(),
TOKEN_TYPE_FACTORY.getName());

@Override
public <T> @Nullable T postProcessValue(@NotNull KnownAttribute<T> knownAttribute, @Nullable T value) {
return findAttributeValue(value, knownAttribute);
}

public static BnfFile prepareForFleetGeneration(@NotNull BnfFile bnfFile) {
bnfFile.putUserData(GENERATE_FOR_FLEET, true);
bnfFile.putUserData(ATTRIBUTE_POSTPROCESSOR, new FleetBnfAttributePostprocessor());
return bnfFile;
}

private <T> T findAttributeValue(@Nullable T value,
@NotNull KnownAttribute<T> knownAttribute) {
//Bypass adjustment logic for the GENERATE attribute
if (knownAttribute.getName().equals(GENERATE.getName())) {
return value;
}

if (myFleetAttributeValuesSubstitution.containsKey(knownAttribute.getName())) {
if (!knownAttribute.getDefaultValue().equals(value)) {
return adjustedValue(value);
}
else {
return (T)myFleetAttributeValuesSubstitution.get(knownAttribute.getName());
}
}

////If a generated element name has been requested, return value adjusted accordingly
if (myDefaultGeneratedNames.containsKey(knownAttribute.getName())) {
return adjustedValue(value);
}

//If a factory attribute is requested, return null to force generation of non-factory methods
if (mySuppressedFactories.contains(knownAttribute.getName())) {
return null;
}

return value;
}

private static <T> T adjustedValue(@Nullable T origin) {
if (origin instanceof String && !((String)origin).startsWith(FLEET_NAMESPACE_PREFIX)) {
return (T)(FLEET_NAMESPACE_PREFIX + origin);
}
return origin;
}
}
126 changes: 0 additions & 126 deletions src/org/intellij/grammar/fleet/FleetBnfFileWrapper.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/org/intellij/grammar/fleet/GenerateFleetAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public class GenerateFleetAction extends GenerateAction {
protected @Nullable PsiFile getBnfFile(VirtualFile file, PsiManager psiManager) {
var psiFile = super.getBnfFile(file, psiManager);
if (psiFile == null) return null;
return FleetBnfFileWrapper.wrapBnfFile((BnfFile)psiFile);
return FleetBnfAttributePostprocessor.prepareForFleetGeneration((BnfFile)psiFile);
}
}
4 changes: 2 additions & 2 deletions src/org/intellij/grammar/generator/GenOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import com.intellij.openapi.util.text.StringUtil;
import org.intellij.grammar.KnownAttribute;
import org.intellij.grammar.fleet.FleetBnfFileWrapper;
import org.intellij.grammar.fleet.FleetBnfAttributePostprocessor;
import org.intellij.grammar.psi.BnfFile;

import java.util.Map;
Expand Down Expand Up @@ -38,7 +38,7 @@ public class GenOptions {
public final int javaVersion;

public GenOptions(BnfFile myFile) {
var generateForFleet = myFile instanceof FleetBnfFileWrapper;
var generateForFleet = Boolean.TRUE.equals(myFile.getUserData(FleetBnfAttributePostprocessor.GENERATE_FOR_FLEET));
Map<String, String> genOptions = getRootAttribute(myFile, KnownAttribute.GENERATE).asMap();
names = Names.forName(genOptions.get("names"));
generatePsi = getGenerateOption(myFile, KnownAttribute.GENERATE_PSI, genOptions, "psi") && !generateForFleet;
Expand Down
4 changes: 2 additions & 2 deletions src/org/intellij/grammar/generator/GeneratorBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.containers.JBIterable;
import org.intellij.grammar.KnownAttribute;
import org.intellij.grammar.fleet.FleetBnfFileWrapper;
import org.intellij.grammar.fleet.FleetBnfAttributePostprocessor;
import org.intellij.grammar.psi.BnfFile;
import org.intellij.grammar.psi.BnfRule;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -58,7 +58,7 @@ protected GeneratorBase(@NotNull BnfFile psiFile,
@NotNull String outputPath,
@NotNull String packagePrefix) {
myFile = psiFile;
myGenerateForFleet = psiFile instanceof FleetBnfFileWrapper;
myGenerateForFleet = psiFile.getUserData(FleetBnfAttributePostprocessor.GENERATE_FOR_FLEET);

G = new GenOptions(psiFile);
mySourcePath = sourcePath;
Expand Down
19 changes: 19 additions & 0 deletions src/org/intellij/grammar/generator/IAttributePostProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2011-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

package org.intellij.grammar.generator;

import com.intellij.openapi.util.Key;
import org.intellij.grammar.KnownAttribute;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface IAttributePostProcessor {
Key<IAttributePostProcessor> ATTRIBUTE_POSTPROCESSOR = Key.create("ATTRIBUTE_POSTPROCESSOR");

@Nullable
default <T> T postProcessValue(@NotNull KnownAttribute<T> knownAttribute, @Nullable T value) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package org.intellij.grammar.generator;

import org.intellij.grammar.fleet.FleetBnfFileWrapper;
import org.intellij.grammar.fleet.FleetBnfAttributePostprocessor;
import org.intellij.grammar.fleet.FleetConstants;
import org.intellij.grammar.psi.BnfFile;

Expand Down Expand Up @@ -52,6 +52,6 @@ private IntelliJPlatformConstants(String builder,
FleetConstants.TOKEN_SET_CLASS);

public static IntelliJPlatformConstants getConstantSetForBnf(BnfFile file) {
return (file instanceof FleetBnfFileWrapper) ? FleetConstantSet : IdeaConstantSet;
return (Boolean.TRUE.equals(file.getUserData(FleetBnfAttributePostprocessor.GENERATE_FOR_FLEET))) ? FleetConstantSet : IdeaConstantSet;
}
}
2 changes: 1 addition & 1 deletion src/org/intellij/grammar/generator/ParserGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,7 @@ private void generateElementTypesHolder(String className, Map<String, BnfRule> s
out("}");
}

protected boolean useFactory(String factory) {
protected static boolean useFactory(String factory) {
return factory != null;
}

Expand Down
18 changes: 9 additions & 9 deletions src/org/intellij/grammar/psi/impl/BnfFileImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@

import com.intellij.extapi.psi.PsiFileBase;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.util.AtomicClearableLazyValue;
import com.intellij.openapi.util.ClearableLazyValue;
import com.intellij.openapi.util.Conditions;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.*;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.JBIterable;
import org.intellij.grammar.BnfFileType;
import org.intellij.grammar.BnfLanguage;
import org.intellij.grammar.KnownAttribute;
import org.intellij.grammar.generator.IAttributePostProcessor;
import org.intellij.grammar.generator.ParserGeneratorUtil;
import org.intellij.grammar.psi.*;
import org.jetbrains.annotations.NotNull;
Expand All @@ -32,7 +30,7 @@
* Time: 23:55
*/
public class BnfFileImpl extends PsiFileBase implements BnfFile {

private final ClearableLazyValue<Map<String, BnfRule>> myRules = lazyValue(this::calcRules);
private final ClearableLazyValue<List<BnfAttrs>> myGlobalAttributes = lazyValue(this::calcAttributes);
private final ClearableLazyValue<Map<String, List<AttributeInfo>>> myAttributeValues = lazyValue(this::calcAttributeValues);
Expand Down Expand Up @@ -73,6 +71,12 @@

@Override
public <T> T findAttributeValue(@Nullable BnfRule rule, @NotNull KnownAttribute<T> knownAttribute, @Nullable String match) {
var postProcessor = getUserData(IAttributePostProcessor.ATTRIBUTE_POSTPROCESSOR);
if (postProcessor == null) return findAttributeValueInner(rule, knownAttribute, match);

Check warning on line 75 in src/org/intellij/grammar/psi/impl/BnfFileImpl.java

View workflow job for this annotation

GitHub Actions / Inspect code

Constant values

Condition `postProcessor == null` is always `true`
else return postProcessor.postProcessValue(knownAttribute, findAttributeValueInner(rule, knownAttribute, match));
}

private <T> T findAttributeValueInner(@Nullable BnfRule rule, @NotNull KnownAttribute<T> knownAttribute, @Nullable String match) {
T combined = null;
boolean copied = false;
for (AttributeInfo info : getMatchingAttributes(rule, knownAttribute, match)) {
Expand Down Expand Up @@ -191,10 +195,6 @@
return result;
}

protected boolean hasAttributeValue(@Nullable BnfRule rule, @NotNull KnownAttribute<?> attribute, @Nullable String match) {
return getMatchingAttributes(rule, attribute, match).isNotEmpty();
}

protected static @NotNull <T> AtomicClearableLazyValue<T> lazyValue(Supplier<T> producer) {
return new AtomicClearableLazyValue<>() {
@Override
Expand Down
Loading
Loading