Skip to main content



      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 00:51 Go to next message
Eclipse UserFriend
#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 00:53] by Moderator

Re: Constraining customer terminal INT or REAL to a specific range [message #1130098 is a reply to message #1129940] Wed, 09 October 2013 04:23 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Constraining customer terminal INT or REAL to a specific range [message #1130338 is a reply to message #1130098] Wed, 09 October 2013 09:00 Go to previous messageGo to next message
Eclipse UserFriend
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] Thu, 10 October 2013 23:51 Go to previous messageGo to next message
Eclipse UserFriend
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 01:28] by Moderator

Re: Constraining customer terminal INT or REAL to a specific range [message #1132436 is a reply to message #1132271] Fri, 11 October 2013 02:32 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Constraining customer terminal INT or REAL to a specific range [message #1132954 is a reply to message #1132436] Fri, 11 October 2013 10:00 Go to previous message
Eclipse UserFriend
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: Wed Jul 02 02:42:50 EDT 2025

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

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

Back to the top