Skip to content

Commit

Permalink
Merge pull request #25 from unv-unv/unv/code-reformat-2
Browse files Browse the repository at this point in the history
Code reformat using IJ4 code style (part 2).
  • Loading branch information
VISTALL authored Jul 24, 2024
2 parents 1011acc + e351818 commit 283caf1
Show file tree
Hide file tree
Showing 25 changed files with 2,768 additions and 2,660 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.intellij.grammar.psi.BnfFile;
import org.intellij.grammar.psi.BnfRule;
import org.jetbrains.annotations.Nls;

import javax.annotation.Nonnull;

import java.util.ArrayList;
Expand All @@ -40,60 +41,63 @@
*/
@ExtensionImpl
public class BnfLeftRecursionInspection extends LocalInspectionTool {
@Nls
@Nonnull
@Override
public String getGroupDisplayName() {
return "Grammar/BNF";
}

@Nls
@Nonnull
@Override
public String getGroupDisplayName() {
return "Grammar/BNF";
}

@Nls
@Nonnull
@Override
public String getDisplayName() {
return "Left recursion";
}
@Nls
@Nonnull
@Override
public String getDisplayName() {
return "Left recursion";
}

@Nonnull
@Override
public String getShortName() {
return "BnfLeftRecursionInspection";
}
@Nonnull
@Override
public String getShortName() {
return "BnfLeftRecursionInspection";
}

@Nonnull
@Override
public HighlightDisplayLevel getDefaultLevel() {
return HighlightDisplayLevel.WARNING;
}
@Nonnull
@Override
public HighlightDisplayLevel getDefaultLevel() {
return HighlightDisplayLevel.WARNING;
}

public boolean isEnabledByDefault() {
return true;
}
public boolean isEnabledByDefault() {
return true;
}

public ProblemDescriptor[] checkFile(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean isOnTheFly) {
if (file instanceof BnfFile bnfFile) {
ExpressionHelper expressionHelper = ExpressionHelper.getCached(bnfFile);
BnfFirstNextAnalyzer analyzer = new BnfFirstNextAnalyzer();
ArrayList<ProblemDescriptor> list = new ArrayList<>();
for (BnfRule rule : bnfFile.getRules()) {
if (ParserGeneratorUtil.Rule.isFake(rule)) continue;
String ruleName = rule.getName();
boolean exprParsing = ExpressionGeneratorHelper.getInfoForExpressionParsing(expressionHelper, rule) != null;
public ProblemDescriptor[] checkFile(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean isOnTheFly) {
if (file instanceof BnfFile bnfFile) {
ExpressionHelper expressionHelper = ExpressionHelper.getCached(bnfFile);
BnfFirstNextAnalyzer analyzer = new BnfFirstNextAnalyzer();
ArrayList<ProblemDescriptor> list = new ArrayList<>();
for (BnfRule rule : bnfFile.getRules()) {
if (ParserGeneratorUtil.Rule.isFake(rule)) {
continue;
}
String ruleName = rule.getName();
boolean exprParsing = ExpressionGeneratorHelper.getInfoForExpressionParsing(expressionHelper, rule) != null;

if (!exprParsing && analyzer.asStrings(analyzer.calcFirst(rule)).contains(ruleName)) {
list.add(manager.createProblemDescriptor(
rule.getId(),
"'" + ruleName + "' employs left-recursion unsupported by generator",
isOnTheFly,
LocalQuickFix.EMPTY_ARRAY,
ProblemHighlightType.GENERIC_ERROR_OR_WARNING
));
if (!exprParsing && analyzer.asStrings(analyzer.calcFirst(rule)).contains(ruleName)) {
list.add(manager.createProblemDescriptor(
rule.getId(),
"'" + ruleName + "' employs left-recursion unsupported by generator",
isOnTheFly,
LocalQuickFix.EMPTY_ARRAY,
ProblemHighlightType.GENERIC_ERROR_OR_WARNING
));
}
}
if (!list.isEmpty()) {
return list.toArray(new ProblemDescriptor[list.size()]);
}
}
}
if (!list.isEmpty()) return list.toArray(new ProblemDescriptor[list.size()]);
}

return ProblemDescriptor.EMPTY_ARRAY;
}
return ProblemDescriptor.EMPTY_ARRAY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,40 @@
* @author gregsh
*/
public class BnfRemoveExpressionFix implements LocalQuickFix {
@Nonnull
@Override
public String getName() {
return getFamilyName();
}

@Nonnull
@Override
public String getFamilyName() {
return "Remove expression";
}

@Override
public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
if (!element.isValid()) return;
PsiElement parent = element.getParent();
if (element instanceof BnfExpression && parent instanceof BnfChoice) {
ASTNode node = element.getNode();
ASTNode nextOr = TreeUtil.findSibling(node, BnfTypes.BNF_OP_OR);
ASTNode prevOr = TreeUtil.findSiblingBackward(node, BnfTypes.BNF_OP_OR);
assert nextOr != null || prevOr != null: "'|' missing in choice";
if (nextOr != null && prevOr != null) {
parent.deleteChildRange(prevOr.getTreeNext().getPsi(), nextOr.getPsi());
}
else {
parent.deleteChildRange(prevOr == null? element : prevOr.getPsi(), prevOr == null? nextOr.getPsi() : element);
@Nonnull
@Override
public String getName() {
return getFamilyName();
}

}
@Nonnull
@Override
public String getFamilyName() {
return "Remove expression";
}
else {
element.delete();

@Override
public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
if (!element.isValid()) {
return;
}
PsiElement parent = element.getParent();
if (element instanceof BnfExpression && parent instanceof BnfChoice) {
ASTNode node = element.getNode();
ASTNode nextOr = TreeUtil.findSibling(node, BnfTypes.BNF_OP_OR);
ASTNode prevOr = TreeUtil.findSiblingBackward(node, BnfTypes.BNF_OP_OR);
assert nextOr != null || prevOr != null : "'|' missing in choice";
if (nextOr != null && prevOr != null) {
parent.deleteChildRange(prevOr.getTreeNext().getPsi(), nextOr.getPsi());
}
else {
parent.deleteChildRange(prevOr == null ? element : prevOr.getPsi(), prevOr == null ? nextOr.getPsi() : element);
}
}
else {
element.delete();
}
BnfExpressionOptimizer.optimize(parent);
}
BnfExpressionOptimizer.optimize(parent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.intellij.grammar.psi.BnfRule;
import org.intellij.grammar.psi.impl.BnfRefOrTokenImpl;
import org.jetbrains.annotations.Nls;

import javax.annotation.Nonnull;

import java.util.Set;
Expand All @@ -47,79 +48,82 @@
*/
@ExtensionImpl
public class BnfSuspiciousTokenInspection extends LocalInspectionTool {
@Nls
@Nonnull
@Override
public String getGroupDisplayName() {
return "Grammar/BNF";
}

@Nls
@Nonnull
@Override
public String getGroupDisplayName() {
return "Grammar/BNF";
}

@Nls
@Nonnull
@Override
public String getDisplayName() {
return "Suspicious token";
}
@Nls
@Nonnull
@Override
public String getDisplayName() {
return "Suspicious token";
}

@Nonnull
@Override
public String getShortName() {
return "BnfSuspiciousTokenInspection";
}
@Nonnull
@Override
public String getShortName() {
return "BnfSuspiciousTokenInspection";
}

@Nonnull
@Override
public HighlightDisplayLevel getDefaultLevel() {
return HighlightDisplayLevel.WARNING;
}
@Nonnull
@Override
public HighlightDisplayLevel getDefaultLevel() {
return HighlightDisplayLevel.WARNING;
}

@Override
public boolean isEnabledByDefault() {
return true;
}
@Override
public boolean isEnabledByDefault() {
return true;
}

@Override
public ProblemDescriptor[] checkFile(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean isOnTheFly) {
ProblemsHolder problemsHolder = new ProblemsHolder(manager, file, isOnTheFly);
checkFile(file, problemsHolder);
return problemsHolder.getResultsArray();
}
@Override
public ProblemDescriptor[] checkFile(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean isOnTheFly) {
ProblemsHolder problemsHolder = new ProblemsHolder(manager, file, isOnTheFly);
checkFile(file, problemsHolder);
return problemsHolder.getResultsArray();
}

private static void checkFile(final PsiFile file, final ProblemsHolder problemsHolder) {
if (!(file instanceof BnfFile)) return;
final Set<String> tokens = RuleGraphHelper.getTokenNameToTextMap((BnfFile)file).keySet();
file.accept(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof BnfRule rule) {
// do not check external rules
if (ParserGeneratorUtil.Rule.isExternal(rule)) return;
}
else if (element instanceof BnfExternalExpression) {
// do not check external expressions
return;
}
else if (element instanceof BnfRefOrTokenImpl) {
PsiReference reference = element.getReference();
Object resolve = reference == null ? null : reference.resolve();
final String text = element.getText();
if (resolve == null && !tokens.contains(text) && isTokenTextSuspicious(text)) {
problemsHolder.registerProblem(
element,
"'" + text + "' token looks like a reference to a missing rule",
new CreateRuleFromTokenFix(text)
);
}
private static void checkFile(final PsiFile file, final ProblemsHolder problemsHolder) {
if (!(file instanceof BnfFile)) {
return;
}
super.visitElement(element);
}
});
}
final Set<String> tokens = RuleGraphHelper.getTokenNameToTextMap((BnfFile)file).keySet();
file.accept(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof BnfRule rule) {
// do not check external rules
if (ParserGeneratorUtil.Rule.isExternal(rule)) {
return;
}
}
else if (element instanceof BnfExternalExpression) {
// do not check external expressions
return;
}
else if (element instanceof BnfRefOrTokenImpl) {
PsiReference reference = element.getReference();
Object resolve = reference == null ? null : reference.resolve();
final String text = element.getText();
if (resolve == null && !tokens.contains(text) && isTokenTextSuspicious(text)) {
problemsHolder.registerProblem(
element,
"'" + text + "' token looks like a reference to a missing rule",
new CreateRuleFromTokenFix(text)
);
}
}
super.visitElement(element);
}
});
}

public static boolean isTokenTextSuspicious(String text) {
boolean isLowercase = text.equals(text.toLowerCase());
boolean isUppercase = !isLowercase && text.equals(text.toUpperCase());
return !isLowercase && !isUppercase || isLowercase && StringUtil.containsAnyChar(text, "-_");
}
public static boolean isTokenTextSuspicious(String text) {
boolean isLowercase = text.equals(text.toLowerCase());
boolean isUppercase = !isLowercase && text.equals(text.toUpperCase());
return !isLowercase && !isUppercase || isLowercase && StringUtil.containsAnyChar(text, "-_");
}
}
Loading

0 comments on commit 283caf1

Please sign in to comment.