Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Get all syntax errors (resource.getErrors() gives only the first error in my language file)
Get all syntax errors [message #1805024] Thu, 04 April 2019 12:08 Go to next message
Eclipse UserFriend
Hey,
i am getting only the first syntax error of my language file but i have typed 4 syntactical mistakes in my language file. What am i missing?
/*
 * class for parsing the language file to a model/abstract syntax tree
 */
public class A2bParser {

	Injector injector;
	XtextResourceSet resourceSet;
	Resource resource;
	

	public A2bParser(){
		initializeParser();
	}

	private void initializeParser(){
		injector = new A2BStandaloneSetup().createInjectorAndDoEMFRegistration();
		resourceSet = injector.getInstance(XtextResourceSet.class);
		resourceSet.addLoadOption(XtextResource.OPTION_ENCODING, "UTF-8");
		resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
	}

	public Model parse(String uriPath) throws A2BParseErrorException{
		
		resource = resourceSet.getResource(URI.createFileURI(uriPath), true);
		
		if(resource.getErrors().isEmpty()) {

			Model model = (Model) resource.getContents().get(0);
			return model;

		}else {	
			System.out.println(resource.getErrors().size());
			EList<Diagnostic> errorList = resource.getErrors();
			throw new A2BParseErrorException(errorList);
		}

	}

}


So my println gives me every time only the size of 1.
Re: Get all syntax errors [message #1805025 is a reply to message #1805024] Thu, 04 April 2019 12:09 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

can you please provide a complete minimal grammar and unit test?
is this about parse errors or validation errors?
how do you execute that code?

[Updated on: Thu, 04 April 2019 12:10] by Moderator

Re: Get all syntax errors [message #1805026 is a reply to message #1805025] Thu, 04 April 2019 12:17 Go to previous messageGo to next message
Eclipse UserFriend
Hey,
my minimal grammar and the language file with the syntax mistakes

grammar a2b.A2B with org.eclipse.xtext.common.Terminals

generate a2B "http://www.A2B.a2b"
import "http://www.eclipse.org/emf/2002/Ecore" as ecore

Model:
	element+=Instruction*
;

Instruction:
	DB | DW | DD | {CRC} CRC
;

DB:
	'DB' (stringValue=BYTE | intValue=INT) (crcValue=CRCCHECK)?
;

DW:
	'DW' (stringValue=WORD | intValue=INT) (crcValue=CRCCHECK)?
;

DD:
	'DD' (stringValue=DOUBLEWORD | longValue=LONG) (crcValue=CRCCHECK)?
;

CRC:
	'CRC'
;

LONG returns ecore::ELong:
	INT
;

terminal BINARY_:
	(('0' | '1')('0' | '1')('0' | '1')('0' | '1')('0' | '1')('0' | '1')('0' | '1')('0' | '1'))
;

terminal HEX_:
	(('A'..'F' | 'a'..'f' | '0'..'9')('A'..'F' | 'a'..'f' | '0'..'9'))
;

terminal BYTE:
	'0b'BINARY_ |
	'0x'HEX_
;

terminal WORD:
	'0b'BINARY_ BINARY_ | 
	'0x'HEX_ HEX_
;

terminal DOUBLEWORD:
	'0b'BINARY_ BINARY_ BINARY_ BINARY_ | 
	'0x'HEX_ HEX_ HEX_ HEX_
;

terminal CRCCHECK:
	'#'
;


This is my language file i am loading
DI 0x45ff003c #
ED 0x1c464000 # w
DW 0x4006 #
CRC
DD 0xac100a63 # +
DD 0xac100a0c #
Re: Get all syntax errors [message #1805028 is a reply to message #1805025] Thu, 04 April 2019 12:24 Go to previous messageGo to next message
Eclipse UserFriend
Hey,
this are the errors i get in the second eclipse runtime environment.
I am executing throug my eclipse ide. I made an maven project and added the dependency to my dsl project.
Re: Get all syntax errors [message #1805034 is a reply to message #1805028] Thu, 04 April 2019 13:17 Go to previous messageGo to next message
Eclipse UserFriend
No I mean how do you execute your util
Re: Get all syntax errors [message #1805037 is a reply to message #1805034] Thu, 04 April 2019 13:39 Go to previous messageGo to next message
Eclipse UserFriend
I have another class called Compiler which instantiates the Parser and goes through the model to check each instruction and calls for each instruction a method.

In my main i have one argument which gives the path and the file to my language file and another argument what file should be generated.

I was experimenting some more with my instruction and noticed that my custom validation errors aren't presented in the resource.getError().

Is there another object which handles the custom errors?

Here is my language validator.
/*
 * generated by Xtext 2.17.0
 */
package a2b.validation

import a2b.a2B.DW
import org.eclipse.xtext.validation.Check
import a2b.a2B.A2BPackage
import a2b.a2B.DB
import a2b.a2B.DD

/**
 * This class contains custom validation rules. 
 *
 * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#validation
 */
class A2BValidator extends AbstractA2BValidator {
	public static val INVALID_NAME = 'invalidName'
	@Check
	def checkDDIntegerValue(DD dd){
		if(dd.longValue > 4294967295L || dd.longValue < 0){
			error('Value for integer is between 0 and 4294967295', A2BPackage.Literals.DD__LONG_VALUE, INVALID_NAME)
		}
	}
	
	@Check
	def checkDWShortValue(DW dw){
		if(dw.intValue > 65535 || dw.intValue < 0){
			error('Value for short is between 0 and 65535', A2BPackage.Literals.DW__INT_VALUE, INVALID_NAME)
		}	
	}
	
	@Check
	def checkDBByteValue(DB db){
		if(db.intValue > 255 || db.intValue < 0){
			error('Value for byte is between 0 and 255', A2BPackage.Literals.DB__INT_VALUE, INVALID_NAME)
		}
	}

	
}

Re: Get all syntax errors [message #1805039 is a reply to message #1805037] Thu, 04 April 2019 13:54 Go to previous messageGo to next message
Eclipse UserFriend
Is this about parse or validator issues?

For the latter see this from the docs

@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)
}
}
}
Re: Get all syntax errors [message #1805041 is a reply to message #1805039] Thu, 04 April 2019 14:02 Go to previous messageGo to next message
Eclipse UserFriend
Its a validation issue i think,

do i inject and implement this in my RuntimeModule?
Re: Get all syntax errors [message #1805042 is a reply to message #1805041] Thu, 04 April 2019 14:08 Go to previous messageGo to next message
Eclipse UserFriend
No at the place where you use it

So you would da a injector. Getinstance(iresourcevalidator.class)
In your case
Re: Get all syntax errors [message #1805043 is a reply to message #1805042] Thu, 04 April 2019 14:13 Go to previous message
Eclipse UserFriend
Yeah now its working thanks a lot christian for your support.
Previous Topic:Hello I'm new in this technology
Next Topic:Accessing parser source correspondence for AST elements in standalone setup
Goto Forum:
  


Current Time: Sat Apr 26 21:14:25 EDT 2025

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

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

Back to the top