Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Separate java application that parses a file
Separate java application that parses a file [message #1806941] Sat, 18 May 2019 10:26 Go to next message
Eclipse UserFriend
sorry for the trivial question...

I've read the XText getting started documentation, I've found any kind of thing, but not the answer to one very simple question.

i've created a new XText project, a new grammar, processed it, everything's OK !

but now

I do not need to work inside Ecplise (code completions, highlighting, and so on)

I need to run a Java software that reads a file that follows my grammar and parse (or execute it).
I've done the same using directly ANTLR4 and g4 grammars --> everything OK.

public static void main(String[] args) throws Exception {

if (args==null || args.length == 0) {
args = new String[]{"src/testfile"};
}

TestLexer lexer = new TestLexer(new ANTLRFileStream(args[0]));
TestParser parser = new TestParser(new CommonTokenStream(lexer));
ParseTree tree = parser.parse();
EvalVisitor visitor = new EvalVisitor();
visitor.visit(tree);
}


can someone help me to do the same with the classes generated in a XText Eclipse project ?

thanks in advance
Andrea
Re: Separate java application that parses a file [message #1807148 is a reply to message #1806941] Thu, 23 May 2019 13:35 Go to previous message
Eclipse UserFriend
this snippet may help you to build such an app

import java.io.IOException;
import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.util.CancelIndicator;
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.IResourceValidator;
import org.eclipse.xtext.validation.Issue;

import com.google.inject.Injector;

public class Main {

    public static void main(String[] args) throws IOException {
        Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
        ResourceSet rs = injector.getInstance(ResourceSet.class);
        Resource resource = rs.getResource(URI.createURI("test.mydsl"), true);
        resource.load(null);
        IResourceValidator validator = injector.getInstance(IResourceValidator.class);
        List<Issue> issues = validator.validate(resource,
                CheckMode.ALL, CancelIndicator.NullImpl);
        for (Issue issue: issues) {
            switch (issue.getSeverity()) {
                case ERROR:
                    System.out.println("ERROR: " + issue.getMessage());
                case WARNING:
                        System.out.println("WARNING: " + issue.getMessage());
            }
        }
    }

}
Previous Topic:Using Xtext Parser/Java Models in Server Side (Spring Boot) App
Next Topic:How to import implicitly the namespace from importURI
Goto Forum:
  


Current Time: Thu Mar 27 01:19:05 EDT 2025

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

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

Back to the top