简体   繁体   中英

Make a own interpreter for a Xtext grammar

i have a grammar made in Xtext , where i can launch an eclipse application from the plugin.xml and test my grammar . Now i need make an interpreter for be able to launch my code of my DSL .

I made a package with a class interpreter but i dont know how to access to the file opened in the eclipse editor for be launched . On the other hand , I think that the interpreter read line by line the file in the editor and run the sentence , is that way right?

My final question is , if you know a tutorial or a better way to implement an interpreter for a Xtext grammar and all work together ? i try to understand the Tortoise example but i dont understand anything .

Thanks!!!

Well this is a question that is quite impossible to give a general answer to. It highly depends on what your interpreter does and how it gives feedback. Even working line by line might not make sense at all instead simply iterate over the model content. You could imagine to do this on "autoedit" when the user hits enter in the file. This is what thy arithmetics example that ships with xtext does. Or you can toggle a view with the editor - that is what the tortoise example does ( https://github.com/xtext/seven-languages-xtext/blob/c04e8d56e362bfb8d6163f4b001b22ab878686ca/languages/org.xtext.tortoiseshell.lib/src/org/xtext/tortoiseshell/lib/view/TortoiseView.xtend ). Or you could simply have a eclipse command that you call via rightclick from context menu (and or a shortcut). Here is a small snippet how to build a Eclipse Command Handler that works on a open file. (taken from https://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-generator-from-a-context-menu/ )

public class InterpretCodeHandler extends AbstractHandler implements IHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {

        IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
        IFile file = (IFile) activeEditor.getEditorInput().getAdapter(IFile.class);
        if (file != null) {
            IProject project = file.getProject();



            if (activeEditor instanceof XtextEditor) {
                ((XtextEditor)activeEditor).getDocument().readOnly(new IUnitOfWork<Boolean, XtextResource>() {

                    @Override
                    public Boolean exec(XtextResource state)
                            throws Exception {
                        // TODO your code here
                        return Boolean.TRUE;
                    }
                });

            }
        }
        return null;
    }

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

}

It basically nails down to call XtextEditor.getDocument().readOnly() which gives you access to the xtext resource and you can work with it.

and here its registration(s)

<extension point="org.eclipse.ui.menus">
    <menuContribution locationURI="popup:#TextEditorContext?after=additions">
        <command commandId="org.xtext.example.mydsl.ui.handler.InterpreterCommand" style="push">
            <visibleWhen checkEnabled="false">
                   <reference definitionId="org.xtext.example.mydsl.MyDsl.Editor.opened"></reference>
            </visibleWhen>
        </command>
    </menuContribution>
</extension>
<extension point="org.eclipse.ui.handlers">
     <handler class="org.xtext.example.mydsl.ui.MyDslExecutableExtensionFactory:org.xtext.example.mydsl.ui.handler.InterpretCodeHandler" commandId="org.xtext.example.mydsl.ui.handler.InterpreterCommand">
     </handler>  
</extension> 
<extension point="org.eclipse.ui.commands">
      <command name="Interpret Code" id="org.xtext.example.mydsl.ui.handler.InterpreterCommand">
      </command>
</extension> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM