Constraining custom terminal INT or REAL to a specific range [message #1129940] |
Wed, 09 October 2013 00:51  |
Eclipse User |
|
|
|
#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 #1132271 is a reply to message #1130338] |
Thu, 10 October 2013 23:51   |
Eclipse User |
|
|
|
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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.11768 seconds