Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Constraining custom terminal INT or REAL to a specific range(noob)
Constraining custom terminal INT or REAL to a specific range [message #1129940] Wed, 09 October 2013 04:51 Go to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 176
Registered: September 2013
Senior Member
#1 I would like to specify a terminal "REGISTER" that accepts INT type values from 32 to 63.

#2 I made my own 'REAL' Xtext terminal using the INT '.' INT approach. I need to constrain this to the range -2.0 to 1.9999804.

How to?

Thanks,

GW

[Updated on: Wed, 09 October 2013 04:53]

Report message to a moderator

Re: Constraining customer terminal INT or REAL to a specific range [message #1130098 is a reply to message #1129940] Wed, 09 October 2013 08:23 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
I'd write a validation rule (see docs). This way you make sure the user
gets proper error messages when value is out of range.

Am 09.10.13 06:51, schrieb Gary Worsham:
> I made my own 'REAL' Xtext terminal using the INT '.' INT approach.
>
> I need to constrain this to the range -2.0 to 1.9999804. How to?
>
> Thanks,
>
> GW


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


---
Get professional support from the Xtext committers at www.typefox.io
Re: Constraining customer terminal INT or REAL to a specific range [message #1130338 is a reply to message #1130098] Wed, 09 October 2013 13:00 Go to previous messageGo to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 176
Registered: September 2013
Senior Member
Thanks for the tip! Will investigate.
Re: Constraining customer terminal INT or REAL to a specific range [message #1132271 is a reply to message #1130338] Fri, 11 October 2013 03:51 Go to previous messageGo to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 176
Registered: September 2013
Senior Member
OK here's my grammar file:

grammar com.holycityaudio.spincad.SpinCAD with org.eclipse.xtext.common.Terminals

generate spinCAD "http://www.holycityaudio.com/spincad/SpinCAD"

Program:
	instructions+=Instruction*;
	
Instruction:
	ReadRegister |
	WriteRegister |
	ReadRegisterFilter |
	Mulx |
	WriteDelay
	;
	
ReadRegister: 'RDAX' arg1 = INT ',' arg2 = SPINREAL;
WriteRegister: 'WRAX' arg1 = INT ',' arg2 = SPINREAL;
ReadRegisterFilter: 'RDFX' arg1 = INT ',' arg2 = SPINREAL;
Mulx: 'MULX' arg1 = INT;
WriteDelay: 'WRA' arg1 = INT ',' arg2 = SPINREAL;
//------------------------
terminal SPINREAL : ('0'..'1') '.' INT;


and then I wrote this little validator, and just put in some Java debug output for fun:

/*
 * generated by Xtext
 */
package com.holycityaudio.spincad.validation
import org.eclipse.xtext.validation.Check
import com.holycityaudio.spincad.spinCAD.Instruction

/**
 * Custom validation rules. 
 *
 * see http://www.eclipse.org/Xtext/documentation.html#validation
 */
class SpinCADValidator extends AbstractSpinCADValidator {
	@Check
	def checkArg1LT64(Instruction inst) {
		if (inst.arg1 > 64) {
			System.out.printf('Arg 1 is greater than 64! %d\n', inst.arg1)
		}
	}

	@Check
	def checkWRA_Arg1LT32768(Instruction inst) {
		if (inst.arg1 > 32768) {
			System.out.printf('Arg 1 is greater than 32768! %d\n', inst.arg1)
		}
	}
}


So as far as I can deduce, my entire SpinCAD file gets run through these rules every time I type in the editor. Way cool!

So here's my next problem.

The source statement:

WRA ADDR,VALUE maps to the instruction "WriteDelay". The first argument should be validated as an ADDR which is an INT between 0 to 37627. The second argument VALUE should be validated as an S1.14 float, which has a valid range of -2.0 to 1.999-something. Well that's fine, I could write that arg1 should be less than 37628 and arg2 should be in the proper range.

However, RDAX REGISTER, VALUE needs to be validated with arg1 being a REGISTER between 32 and 63. I tried defining a custom terminal REGISTER which was the same as INT, but Xtext didn't seem to like that.

The question starts here
What I'd like to be able to access in my validator code is the actual name of the parsed instruction, such as "WriteDelay" or "ReadRegister" so that I can set up a case statement to validate the arguments properly.

Can I do this without redefining my model, or do I explicitly have to add the instruction name somehow?

I could also define different groups of instructions which would use the same validation patterns for arg1 and arg2. Would that work?

Many thanks,

GW

[Updated on: Fri, 11 October 2013 05:28]

Report message to a moderator

Re: Constraining customer terminal INT or REAL to a specific range [message #1132436 is a reply to message #1132271] Fri, 11 October 2013 06:32 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

couldn't you just make the checks more specific (type)

def checkArg1LT64(ReadRegister inst)
instead of/in addition to
def checkArg1LT64(Instruction inst)

You can also define multiple validation rules for the same type (just use different method names). That way you can implement general constraints for the super type and specific ones for the specific types...

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Constraining customer terminal INT or REAL to a specific range [message #1132954 is a reply to message #1132436] Fri, 11 October 2013 14:00 Go to previous message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 176
Registered: September 2013
Senior Member
Hi Alex,

You are so correct!

I had tried that but got a "could not resolve type WriteRegister" error - I should have just taken the Eclipse suggestion to import it!

Onward and upward!

Thanks,

GW
Previous Topic:CanTerminal rules be available as EObjects in the generated model?
Next Topic:Question about cross-linking and creation of instances
Goto Forum:
  


Current Time: Thu Apr 18 11:34:03 GMT 2024

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

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

Back to the top