Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 14:26 Go to next message
Andrea Bigi is currently offline Andrea BigiFriend
Messages: 2
Registered: May 2019
Junior Member
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 17:35 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13067
Registered: July 2009
Senior Member
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());
            }
        }
    }

}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
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: Wed Aug 21 05:54:06 GMT 2019

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

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

Back to the top