Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

java - How to parse from DSL to Object with ParseHelper

I would like to parse a DSL program written to JsonObject with ParseHelper

public static JsonModel toJsonModel(CharSequence c) {
    ParseHelper<JsonModel> parseHelper = new ParseHelper<JsonModel>();
    try {
        return parseHelper.parse(c);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public static void main(String args[]) {
    
    String chaine = "jsonValue { "Nom" : "Toto",
"
            + "          "Adresse" : { 
"
            + "                      "Adresse 1" : "x avenue de xx",
"
            + "                      "Adresse 2" : " x rue de xx"
"
            + "                      },
"
            + "         }
"
            + "         filename = "jsonFileToRead.json"
"
            + "         SearchForKey("xx") in jsonValue
";
    CharSequence c = "'''"+chaine+"'''";
    
    JsonModel model = toJsonModel(c);


}

but I can't seem to parse, I get the following error:

java.lang.NullPointerException: Cannot invoke "org.eclipse.xtext.testing.util.ResourceHelper.createResourceSet()" because "this.resourceHelper" is null
at org.eclipse.xtext.testing.util.ParseHelper.parse(ParseHelper.java:65)
at org.istic.idm.tests.TempsMemoire.toJsonModel(TempsMemoire.java:14)
at org.istic.idm.tests.TempsMemoire.main(TempsMemoire.java:49)
question from:https://stackoverflow.com/questions/66069030/how-to-parse-from-dsl-to-object-with-parsehelper

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you need to use injection to obtain the parsehelper e.g.

import org.eclipse.xtext.testing.util.ParseHelper;
import org.xtext.example.mydsl5.MyDslStandaloneSetup;
import org.xtext.example.mydsl5.myDsl.Model;

import com.google.inject.Inject;
import com.google.inject.Injector;

public class Main {

    public static void main(String[] args) throws Exception {
        Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
        Main main = injector.getInstance(Main.class);
        System.out.println(main.doGetModel("Hello World!"));

    }
    @Inject ParseHelper<Model> parserHelper;
    
    public Model doGetModel(String content) throws Exception {
        return parserHelper.parse(content);
    }

}

or

import org.eclipse.xtext.testing.util.ParseHelper;
import org.xtext.example.mydsl5.MyDslStandaloneSetup;
import org.xtext.example.mydsl5.myDsl.Model;

import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
    
public class Main {

    public static void main(String[] args) throws Exception {
        System.out.println(doGetModel("Hello World!"));

    }
    public static Model doGetModel(String content) throws Exception {
        Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
        ParseHelper<Model> parserHelper = injector.getInstance(Key.get(new TypeLiteral<ParseHelper<Model>>() {}));
        return parserHelper.parse(content);
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...