Reputation: 615
Building a standalone program parsing a file, I used the first option of http://www.davehofmann.de/?p=101
A validation is defined that give an error (red under line in eclipse) if text does not start with a capital letter.
The standalone parser does not give an error of an input file that does not pass the validation. How can I ask the standalone implementation also the verify the validations?
Update 1
The classed used to parse the grammar with checkResource
included:
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)
}
}
}
}
Upvotes: 4
Views: 1564
Reputation: 11868
if you dont call the validator it wont validate. so call it (pseudo code)
@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)
}
}
}
since the blog you posted is very vague on how to call
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());
}
}
}
}
Upvotes: 6