Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Binary and hex signed validation
Binary and hex signed validation [message #1152060] Wed, 23 October 2013 19:58 Go to next message
Tom Mealey is currently offline Tom MealeyFriend
Messages: 7
Registered: October 2013
Junior Member
Hi all,

I am trying to validate hex and binary input, but the problem I have is that I want to interpret the numbers as signed (2's complement) in some cases, and unsigned in other cases, depending on the context. The relevant grammar looks like this:

ValueRef:
	ref=[Macro] | value = (SIGNED_INT | HEX | BINARY)
;

Macro:
	name=ID '=' value=(SIGNED_INT | HEX | BINARY)
;

terminal fragment DIGIT: ('0'..'9');
	
terminal SIGNED_INT returns ecore::EIntegerObject:
	'-'* DIGIT*;

terminal fragment HEX_DIGIT: (DIGIT|'a'..'f'|'A'..'F');

terminal HEX returns ecore::EIntegerObject: 
	'0x' HEX_DIGIT*;

terminal fragment BINARY_DIGIT: ('0' | '1');

terminal BINARY returns ecore::EIntegerObject:
	'0b' (BINARY_DIGIT('_')*)*;


I then have a value converter that uses the Java Integer.parseInt to convert each of the three rules into an integer value. My validator performs a check on each ValueRef object, making sure its value is within the expected range for the object's context. However, I am running into difficulties with hex and binary input, because they are always parsed as unsigned values. For the case where I expect a signed value, this becomes a problem.

For example, if I expect a 4-bit signed value, then anywhere within the range {-8,7} works fine. In this context, I want to interpret 0b1111 and 0xf as -1, rather than 15, but I have no way of telling them apart from regular base 10 input. I need to somehow determine if the raw input was a hex or binary string, and then handle it differently in the validation logic. Is this at all possible, to grab the "raw" input for use in validation? Or am I better off going a different route? Thanks for any input or suggestions you have.

Here is the check I perform in my validator, if it helps:
def Boolean checkValueInRange (int value, int nBits, Boolean isSigned) {
		if (isSigned) {
			if (value < 0) return checkValueInRange(Math.abs(value)-1,nBits-1,false)
			else return checkValueInRange(value,nBits-1,false)
		}
		if (value < 0)
			return false
		return (value < Math.pow(2,nBits))
	}


Cheers,
Tom
Re: Binary and hex signed validation [message #1152067 is a reply to message #1152060] Wed, 23 October 2013 20:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

you may have a look at nodemodelutil to get the node and thus the text for a certain feature (VALUE_REF__VALUE)
of course you could use a more sophisticated valueconverter as well.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Binary and hex signed validation [message #1152068 is a reply to message #1152067] Wed, 23 October 2013 20:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
P:S: therefore you should introduce an actual datatype rule for the value:

VALUE returns ecore::EIntegerObject: (SIGNED_INT | HEX | BINARY)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Binary and hex signed validation [message #1153360 is a reply to message #1152068] Thu, 24 October 2013 15:05 Go to previous message
Tom Mealey is currently offline Tom MealeyFriend
Messages: 7
Registered: October 2013
Junior Member
Christian,

Thanks, this worked great. I used NodeModelUtils.getNode(ValueRef).text to get the "raw" input string at that node.
Previous Topic:problem with adding hover support
Next Topic:Waiting for Resource Descriptions to be updated after modification
Goto Forum:
  


Current Time: Thu Mar 28 08:27:38 GMT 2024

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

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

Back to the top