Skip to content

Commit 5254be3

Browse files
hurricupmaxmedvedev
authored andcommitted
#403 introduced fallbackStubElementType
Currently, the fallback element type is just hardcoded into parser generator. Now we can override it for a parser. Technically we can introduce the per-rule option, allowing us to specify the element type for each stub based psi element, but this feels good enough.
1 parent 6f8662b commit 5254be3

File tree

8 files changed

+879
-7
lines changed

8 files changed

+879
-7
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
<body>
3+
<p>Allows specifying a fallback class to use for an elementType in the constructor of stub elements.</p>
4+
5+
<p>Usually the parser generator looks into the parent class file to detect which class to use.
6+
But in some cases a parent class file may not be available (not yet compiled).
7+
In this case, it uses `IStubElementType` by default, which may not be the case in the modern code when `IElementType` should be used.
8+
</p>
9+
10+
<p>This is per parser option. It does not allow overriding the default per rule.</p>
11+
</body>
12+
</html>

src/org/intellij/grammar/KnownAttribute.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2020 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.
2+
* Copyright 2011-2025 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.
33
*/
44

55
package org.intellij.grammar;
@@ -52,6 +52,8 @@ public class KnownAttribute<T> {
5252
public static final KnownAttribute<String> EXTENDS = create(false, String.class, "extends", BnfConstants.AST_WRAPPER_PSI_ELEMENT_CLASS);
5353
public static final KnownAttribute<ListValue> IMPLEMENTS = create(false, ListValue.class, "implements", ListValue.singleValue( null, BnfConstants.PSI_ELEMENT_CLASS));
5454
public static final KnownAttribute<String> ELEMENT_TYPE = create(false, String.class, "elementType", null);
55+
public static final KnownAttribute<String> FALLBACK_STUB_ELEMENT_TYPE =
56+
create(false, String.class, "fallbackStubElementType", BnfConstants.ISTUBELEMENTTYPE_CLASS);
5557
public static final KnownAttribute<String> ELEMENT_TYPE_CLASS = create(false, String.class, "elementTypeClass", BnfConstants.IELEMENTTYPE_CLASS);
5658
public static final KnownAttribute<String> ELEMENT_TYPE_FACTORY = create(false, String.class, "elementTypeFactory", null);
5759
public static final KnownAttribute<Object> PIN = create(false, Object.class, "pin", -1);

src/org/intellij/grammar/generator/GenOptions.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2020 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.
2+
* Copyright 2011-2025 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.
33
*/
44

55
package org.intellij.grammar.generator;
@@ -11,6 +11,7 @@
1111

1212
import java.util.Map;
1313

14+
import static org.intellij.grammar.KnownAttribute.FALLBACK_STUB_ELEMENT_TYPE;
1415
import static org.intellij.grammar.generator.ParserGeneratorUtil.getGenerateOption;
1516
import static org.intellij.grammar.generator.ParserGeneratorUtil.getRootAttribute;
1617

@@ -35,6 +36,7 @@ public class GenOptions {
3536
public final Case generateElementCase;
3637
public final boolean generateTokenAccessors;
3738
public final boolean generateTokenAccessorsSet;
39+
public final String fallbackStubElementType;
3840
public final int javaVersion;
3941

4042
public GenOptions(BnfFile myFile) {
@@ -48,6 +50,8 @@ public GenOptions(BnfFile myFile) {
4850
generateTokenSets = generateTokenTypes && "yes".equals(genOptions.get("token-sets"));
4951
generateElementTypes = !"no".equals(genOptions.get("elements"));
5052
generateExactTypes = StringUtil.notNullize(genOptions.get("exact-types"));
53+
fallbackStubElementType = StringUtil.notNullize(getGenerateOption(myFile, FALLBACK_STUB_ELEMENT_TYPE, genOptions),
54+
FALLBACK_STUB_ELEMENT_TYPE.getDefaultValue());
5155
generateFirstCheck = getGenerateOption(myFile, KnownAttribute.GENERATE_FIRST_CHECK, genOptions, "first-check", "firstCheck");
5256
generateExtendedPin = getGenerateOption(myFile, KnownAttribute.EXTENDED_PIN, genOptions, "extended-pin", "extendedPin");
5357
generateTokenAccessors = getGenerateOption(myFile, KnownAttribute.GENERATE_TOKEN_ACCESSORS, genOptions, "token-accessors", "tokenAccessors");

src/org/intellij/grammar/generator/ParserGenerator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2020 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.
2+
* Copyright 2011-2025 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.
33
*/
44

55
package org.intellij.grammar.generator;
@@ -31,9 +31,9 @@
3131
import org.jetbrains.annotations.Nullable;
3232

3333
import java.io.IOException;
34+
import java.util.*;
3435
import java.util.HashMap;
3536
import java.util.HashSet;
36-
import java.util.*;
3737

3838
import static com.intellij.util.containers.ContainerUtil.emptyList;
3939
import static com.intellij.util.containers.ContainerUtil.map;
@@ -1615,7 +1615,7 @@ private void generatePsiImpl(BnfRule rule, RuleInfo info) {
16151615
for (NavigatablePsiElement m : constructors) {
16161616
collectMethodTypesToImport(Collections.singletonList(m), false, imports);
16171617
}
1618-
if (stubName != null && constructors.isEmpty()) imports.add(ISTUBELEMENTTYPE_CLASS);
1618+
if (stubName != null && constructors.isEmpty()) imports.add(G.fallbackStubElementType);
16191619
if (stubName != null) imports.add(stubName);
16201620
}
16211621

@@ -1642,7 +1642,7 @@ private void generatePsiImpl(BnfRule rule, RuleInfo info) {
16421642
if (stubName != null) {
16431643
out("public " + shortName + "(" +
16441644
shorten(stubName) + " stub, " +
1645-
shorten(ISTUBELEMENTTYPE_CLASS) + " stubType) {");
1645+
shorten(G.fallbackStubElementType) + " stubType) {");
16461646
out("super(stub, stubType);");
16471647
out("}");
16481648
newLine();

0 commit comments

Comments
 (0)