Use Grammar-Kit parser standalone

Answered

Hello,

I am developing a language plugin for Intellij IDEA.

I want to use my parser in conjunction with other applications outside of the IDE as well. 

  • Am I allowed to do this?
  • How do I achieve: "file.x" -> PsiFile and use it (read-only) in other Java applications?

Best Regards

2 comments
Comment actions Permalink

The main dependencies are PsiBuilderImpl and GeneratedParserUtilBase classes.

You can tear off unrelated code leaving only what's needed in your case.

 

You can also play with standalone Grammar-Kit light-psi-all.jar and LightPsi.java.

There're expression-console-sample artifact and light-psi run configuration in Grammar-Kit project.

0
Comment actions Permalink

Thanks I got it working by using 

com.intellij.lang.parser.GeneratedParserUtilBase
com.intellij.lang.impl.PsiBuilderImpl

and with light-psi-all.jar.

When I run my simple example the instance is still blocking. How can I close it programmatically? Is there something like .close() .disconnect() I do not see? Or is System.exit(0); the way to go?

public static void main(String[] args){
try {
File file = new File(CountKeywords.class.getResource("/example.x").getFile());

PsiFile psiFile = XStandaloneParser.parse(file);

PsiElement root = psiFile.getNode().getPsi();

Collection<PsiElement> keywords = PsiTreeUtil.findChildrenOfType(root, XKeyword.class);

System.out.println("There are " + keywords.size() + " keywords.");

}catch (IOException ex){
ex.printStackTrace();
}
}


public static PsiFile parse(File file) throws IOException {

try {
return LightPsi.parseFile(file, new XParserDefinition());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

return null;
}
0

Please sign in to leave a comment.