Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Problem with importURI and standalone generator
Problem with importURI and standalone generator [message #995180] Sat, 29 December 2012 09:39 Go to next message
Damien Martin-Guillerez is currently offline Damien Martin-GuillerezFriend
Messages: 10
Registered: December 2012
Junior Member
Hello all,

I have the following grammar (the url of eclipse.org have been replaced to escape forum restrictions) :

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

import "eclipse-org-site/xtext/common/JavaVMTypes" as jvmTypes 

generate myDsl "xtext-org-site/example/mydsl/MyDsl"

Model: (greetings+=Greeting | imports+=Import | goodbyes+=Goodbye)*;

Greeting: "Hello" name=ID;
Import: "import" importURI=STRING;
Goodbye: "Bye" ref=[Greeting] ;


The generator is a simple one, just to test:
public class MyDslGenerator implements IGenerator {

    @Override
    public void doGenerate(Resource resource, IFileSystemAccess fsa) {
        StringBuilder output = new StringBuilder();
        Iterator<?> it = resource.getAllContents();
        while (it.hasNext()) {
            Object o = it.next();
            if (o instanceof Greeting) {
                output.append("Hello " + ((Greeting) o).getName() + "\n");
            } else if (o instanceof Goodbye) {
                output.append("Goodbye " + ((Goodbye) o).getRef().getName() + "\n");
            }
        }
        fsa.generateFile(resource.getURI().lastSegment() + ".output", output);
    }
}


With the following file test3.mydsl:
import "test2/test2.mydsl"

Hello aek

Bye aek 
Bye eak


test2/test2.mydsl simply contains "Hello eak". It works perfectly fine in the Eclipse launch but if I call org.xtext.example.mydsl.generator.Main on test3.mydsl, I get the following error:
ERROR:Couldn't resolve reference to Greeting 'eak'. (src/test3.mydsl line : 6)

Has anyone any clue on how to resolve that?

A subsidiary thing which is not blocking but that I'd like to have working is to be able to use jvmTypes in the same times that I can use importURI but it seems that the TypesGeneratorFragment is needed for jvmTypes but it stop importURI feature from working.

Thank you all for the help.
Re: Problem with importURI and standalone generator [message #995253 is a reply to message #995180] Sat, 29 December 2012 14:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

the java main is not meant for such a scenario. you have to modify it that you load all resources to the resourceset manually.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Problem with importURI and standalone generator [message #995306 is a reply to message #995253] Sat, 29 December 2012 18:09 Go to previous messageGo to next message
Damien Martin-Guillerez is currently offline Damien Martin-GuillerezFriend
Messages: 10
Registered: December 2012
Junior Member
Hello, thank you for the quick reply,

I've tried to do the thing but I just can make it work. I modified the runGenerator method:
    protected void runGenerator(String string) {
        ResourceSet set = resourceSetProvider.get();
        set.getResource(URI.createURI("src/test2/test2.mydsl"), true);

        Resource resource = set.getResource(URI.createURI(string), true);
        for (Resource r : set.getResources())
            System.out.println("loaded " + r.getURI());

        // validate the resource
        List<Issue> list = validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
       
        if (!list.isEmpty()) {
            for (Issue issue : list) {
                System.err.println(issue);
            }
            System.exit(-1);
        }

        // configure and start the generator
        fileAccess.setOutputPath("src-gen/");
        generator.doGenerate(resource, fileAccess);

        System.out.println("Code generation finished");
    }


And I still get:
loaded src/test2/test2.mydsl
loaded src/test3.mydsl
ERROR:Couldn't resolve reference to Greeting 'eak'. (src/test3.mydsl line : 6)


I'm probably not loading things right but I can't figure out what.
Re: Problem with importURI and standalone generator [message #995308 is a reply to message #995306] Sat, 29 December 2012 18:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

shoulnd the import be src/test2/test2.mydsl
which version do you use?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Sat, 29 December 2012 18:56]

Report message to a moderator

Re: Problem with importURI and standalone generator [message #995323 is a reply to message #995308] Sat, 29 December 2012 19:16 Go to previous messageGo to next message
Damien Martin-Guillerez is currently offline Damien Martin-GuillerezFriend
Messages: 10
Registered: December 2012
Junior Member
Hello, that is it (current directory was '.', it fixed by going into src).

However, the problem is that I'm doing an Xtext generator for a language that is already existing and is using a relative path for the import statement. The Xtext language was already existing for the Eclipse editor plugin but we'd like to remove our existing ANTLR generator to have only an Xtext grammar. For now, the only remaining problem I have is that include with command-line generator (plus the jvmType with importURI but it is not blocking).

Is there is any way to do that?

My eclipse is Indigo SR2 (20120216-1857) with Xtext SDK 2.3.1.v20120821094.
Re: Problem with importURI and standalone generator [message #995324 is a reply to message #995323] Sat, 29 December 2012 19:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

are you sure relative imports dont work?
you might digg into the code to get relative imports working. (org.eclipse.xtext.scoping.impl.ImportUriGlobalScopeProvider)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Problem with importURI and standalone generator [message #995328 is a reply to message #995324] Sat, 29 December 2012 19:33 Go to previous messageGo to next message
Damien Martin-Guillerez is currently offline Damien Martin-GuillerezFriend
Messages: 10
Registered: December 2012
Junior Member
Well it does work if I execute the generator from the directory of the importing file but it does not work when I'm out of it, which is really weird.

If I'm running the plugin in Eclipse then I can do whatever import I'd like (using .., or going into a sub-directory). However, if I'm doing an import (file3) in an imported file (file2) then I cannot refer to objects imported from file3 in the first file (which is logical, the first file has to import file3).

Anyway, I'm going to dig a bit more into ImportUriGlobalScopeProvider.
Re: Problem with importURI and standalone generator [message #995370 is a reply to message #995328] Sat, 29 December 2012 22:27 Go to previous message
Damien Martin-Guillerez is currently offline Damien Martin-GuillerezFriend
Messages: 10
Registered: December 2012
Junior Member
Ok I've solved the problem by using absolute URIs when requesting resources so that the resolver works correctly. Thank you for the help
Previous Topic:Dynamic template proposals in reference context
Next Topic:XText/XTend: JVM type inferrer with multiple classes
Goto Forum:
  


Current Time: Tue Apr 23 13:56:47 GMT 2024

Powered by FUDForum. Page generated in 0.03848 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top