1

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.

user-517752
  • 1,188
  • 5
  • 21
  • 54
  • Possible duplicate of [ANTLR and Xtext integration for developing plugin](http://stackoverflow.com/questions/23290093/antlr-and-xtext-integration-for-developing-plugin) – Alexey Romanov Nov 08 '15 at 18:02
  • Thanks Alexey! This question is comprehensive and specific, derived from my some experiments with xText. So, it is bit different. – user-517752 Nov 09 '15 at 05:23

3 Answers3

0

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

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • Alexey I know that xTexT uses ANTLR. But, for me the - how can I integrate with ANTLR parser is missing. Could you please help me to resolve this problem using one simple example? This would help me a lot. – user-517752 Nov 09 '15 at 05:24
0

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.

Gaetan
  • 101
  • 3
  • Thanks for the answer! I have already ANTLR grammar and code generator written in StringTemplate. My question is the linking between xtext grammar (that I have written because of nice editor support) and already written ANTLR grammar. How can I link between these two pieces; ANTLR and xtext ? – user-517752 Nov 09 '15 at 08:38
  • In fact if you want to use your own grammar and your generator, you will need to develop inside your Xtext plugin : * An Action to launch your code generation, * Or you can branch your code generation component inside the class generator generated by Xtext. But at the end if you want to provide completion, linking, validation etc. You will need to redevelop your grammar in Xtext. And if you need to change your grammar you will need to maintain two grammars at the same time. – Gaetan Nov 09 '15 at 09:20
  • @Geetan: Thanks for the answer ! Could you please help me with a small code snippet? I am getting your text theoretically, but not in coding terms. – user-517752 Nov 12 '15 at 08:02
  • 1
    In fact the code will depends on how you want to integrate your functionnality for the user. By example if it's an Action you will need to defined a command and a handler (see this tutorial :http://www.vogella.com/tutorials/EclipseCommands/article.html). Inside your handler you can manage your action. If your action is on a selection, a project etc. I will add an example if the selection is on a File. – Gaetan Nov 13 '15 at 07:27
  • I have posted a separate questions that give you an idea on - what kind of actions I want ? the link of the question is : http://stackoverflow.com/questions/33684328/linking-xtext-editor-support-with-external-antlr-parser – user-517752 Nov 13 '15 at 14:55
0

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;
}

}

Gaetan
  • 101
  • 3
  • But if you want to make an incremental generation, you can branch your code inside the xtend file. This file is contains inside the package "generator" in the "src" folder with the name "YourDslName"Generator.xtend. Just add your code inside the method "doGenerate". – Gaetan Nov 13 '15 at 07:37