Xtext独立和验证

时间:2017-11-04 11:36:32

标签: xtext

构建一个解析文件的独立程序,我使用了http://www.davehofmann.de/?p=101

的第一个选项

如果文本不以大写字母开头,则定义验证会给出错误(eclipse中的红线)。

独立解析器不会给出未通过验证的输入文件的错误。如何询问独立实现还验证验证?

更新1

用于解析包含checkResource的语法的分类:

class XtextParser {
    @Inject
    private IParser parser;
    new() {
        var injector = new MyDslStandaloneSetup().
                          createInjectorAndDoEMFRegistration();
        injector.injectMembers(this)
    }
    def EObject parse(Reader reader) throws IOException {
        var result = parser.parse(reader)
        if (result.hasSyntaxErrors()) {
            throw new ParseException("Provided input contains syntax errors.")
        }
        var resource = result.getRootASTElement().eResource() 
        //resource is null
        //checkResource(resource)
        return result.getRootASTElement();
    }
    @Inject IResourceValidator resourceValidator

    def void checkResource(Resource resource) {
        val issues = resourceValidator.validate(resource, 
                                 CheckMode.ALL, CancelIndicator.NullImpl)
        for (issue : issues) {
            switch issue.severity {
                case ERROR:
                    println("ERROR: " + issue.message)
                case WARNING:
                    println("WARNING: " + issue.message)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

如果你不打电话给验证器它不会验证。所以称之为(伪代码)

@Inject IResourceValidator resourceValidator

def void checkResource(Resource resource) {
    val issues = resourceValidator.validate(resource,
            CheckMode.ALL, CancelIndicator.NullImpl)
    for (issue: issues) {
        switch issue.severity {
            case ERROR:
                println("ERROR: " + issue.message)
            case WARNING:
                println("WARNING: " + issue.message)
        }
    }
}

因为您发布的博客对如何调用

非常模糊
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());
            }
        }
    }

}