0
votes

My current project is focusing on code generation from DSL ( i.e., high-level specification). More specifically, developers write high-level specifications and these specifications are parsed and generate code in Java and Android. For parser, I have used ANTLR grammar and for code generation I have used StringTemplateFiles.

However, developer writes high-level specifications in notepad. Because of it, I am not able to provide syntax highlighting, coloring , and error handling. To provide this support, I am thinking to use xText.

I am thinking to integrate xText as follows:

  • Developers will write high-level specifications into editor support provide by xtext (Basically, I will write xtext grammar and generate editor support). Here, Xtext editor will handle syntax coloring, syntax highlighting and error handling.
  • I will take all these specifications as .txt inputs and then ANTLR parse these files. And generate Java and Android code.

Need your suggestions on the following questions:

(1) How can I extract files, written in xtext editor, and provide input to ANTLR parser? OR (2) Should I stick with xText and try to integrate ANTLR parser and xtext? (kindly advise the - how could I integrate xtext and ANTLR with a simple example) OR (3) Should I use only ANTLR and StringTemplateFiles and try to create Eclipse plugin out of it?

Other alternative suggestions are also welcomed.

3
Thanks Alexey! This question is comprehensive and specific, derived from my some experiments with xText. So, it is bit different. - Pankesh

3 Answers

0
votes

You don't need to integrate XText and ANTLR; XText already uses ANTLR for actual parsing.

0
votes

Xtext is based on Antlr. So no need to integrate Antlr and Xtext. I advice you to create an Xtext project on Eclipse and to generate the artifacts using the mwe2 file. Then in the src-gen folder you will can find your Antlr grammar generated from your Xtext grammar.

If you want to generate code from your Xtext grammar you can use Xtend. It's provide already everything that you need. See : https://eclipse.org/Xtext/documentation/207_template.html.

Otherwise if you have already an antlr grammar and a generator, you will need to (re) write it in Xtext.

0
votes

By example:

public class CustomGenerator extends AbstractHandler{

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    ISelection selection = HandlerUtil.getCurrentSelection(event);

    //If your selection is an IFile
    //Selection from the Project Explorer
    if(selection instanceof IStructuredSelection){
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        Object element = structuredSelection.getFirstElement();
        if(element instanceof IFile){
            IFile file = (IFile) element;
            InputStream contentOfYouFile = ((IFile) element).getContents();

            //make your job
        }
    }

    return null;
}

}