Skip to content
Snippets Groups Projects
Commit ebc4edf6 authored by Johannes Mey's avatar Johannes Mey
Browse files

add some analysis

parent b39631e0
No related branches found
No related tags found
No related merge requests found
......@@ -21,7 +21,7 @@ declaration ::= (type_decl | relation) comment*
comment ::= (WHITESPACE | MULTILINECOMMENT | DOCCOMMENT | SINGLELINECOMMENT)
type_decl ::= ABSTRACT? ID (COL ID)? (ASSIGN (component | nta_component)*)? SCOL
type_decl ::= ABSTRACT? ID (COL ID)? (ASSIGN (component | nta_component)*)? SCOL {methods=[getName]}
nta_component ::= SLASH component SLASH
......
package org.jastadd.tooling;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.FileTypeIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import org.jastadd.tooling.psi.RelAstGrammarFile;
import org.jastadd.tooling.psi.RelAstGrammarTypeDecl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class RelAstGrammarUtil {
/**
* Searches the entire project for RelAst grammar files with TypeDecls with the given name.
*
* @param project current project
* @param name to check
* @return matching TypeDecls
*/
public static List<RelAstGrammarTypeDecl> findTypeDecl(Project project, String name) {
List<RelAstGrammarTypeDecl> result = new ArrayList<>();
Collection<VirtualFile> virtualFiles =
FileTypeIndex.getFiles(RelAstGrammarFileType.INSTANCE, GlobalSearchScope.allScope(project));
for (VirtualFile virtualFile : virtualFiles) {
RelAstGrammarFile simpleFile = (RelAstGrammarFile) PsiManager.getInstance(project).findFile(virtualFile);
if (simpleFile != null) {
// TODO check if file is ignored or generated!
RelAstGrammarTypeDecl[] typeDecls = PsiTreeUtil.getChildrenOfType(simpleFile, RelAstGrammarTypeDecl.class);
if (typeDecls != null) {
for (RelAstGrammarTypeDecl typeDecl : typeDecls) {
// FIXME workaround because mixins are not working with gradle-grammar-kit-plugin
// see https://github.com/JetBrains/gradle-grammar-kit-plugin/issues/23
if (name.equals(org.jastadd.tooling.psi.impl.RelAstGrammarPsiImplUtil.getName(typeDecl))) {
result.add(typeDecl);
}
}
}
}
}
return result;
}
/**
* Collects all TypeDecls from all RelAst grammar files in the entire project.
*
* @param project current project
* @return all TypeDecls
*/
public static List<RelAstGrammarTypeDecl> findTypeDecl(Project project) {
List<RelAstGrammarTypeDecl> result = new ArrayList<>();
Collection<VirtualFile> virtualFiles =
FileTypeIndex.getFiles(RelAstGrammarFileType.INSTANCE, GlobalSearchScope.allScope(project));
for (VirtualFile virtualFile : virtualFiles) {
RelAstGrammarFile simpleFile = (RelAstGrammarFile) PsiManager.getInstance(project).findFile(virtualFile);
if (simpleFile != null) {
// TODO check if file is ignored or generated!
RelAstGrammarTypeDecl[] typeDecls = PsiTreeUtil.getChildrenOfType(simpleFile, RelAstGrammarTypeDecl.class);
if (typeDecls != null) {
result.addAll(Arrays.asList(typeDecls));
}
}
}
return result;
}
}
package org.jastadd.tooling.psi.impl;
import com.intellij.lang.ASTNode;
import org.jastadd.tooling.parser.RelAstGrammarTypes;
import org.jastadd.tooling.psi.RelAstGrammarTypeDecl;
public class RelAstGrammarPsiImplUtil {
public static String getName(RelAstGrammarTypeDecl element) {
// this finds the *first* ID, which is what we want
ASTNode keyNode = element.getNode().findChildByType(RelAstGrammarTypes.ID);
if (keyNode != null) {
// IMPORTANT: Convert embedded escaped spaces to simple spaces
return keyNode.getText();
} else {
return null;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment