custom numbers

Mar 23, 2010 at 7:30 PM

Hello, Just a quick thank you to Roman for creating such a useful and powerful project!

I have a newbie question about how to restrict/customize a NumberLiteral to certain range. That is, I wish to have a grammar something like this "psuedo-BNF" example:


<restrictedNumber>::=<restrictedInt>.<restrictedInt> | <restrictedInt>
<restrictedInt> ::= MakePlusRule(restrictedInt, intRange);
<intRange> ::= 1|2|3|4|5
// ...same as ExpressionEvaluatorGrammar example from here on, only using "restrictedNumber" for the NumberLiteral
expression.Rule = binexpr | restrictedNumber;
binexpr.Rule = expression + binop + expression;


So in this grammar, restrictedNumber could only take on values between [1, 5.55*].  I have had a few attempts at a solution without my success, so suspect I am missing somthing fundamental.

Any advice about how to go about doing this in Irony would be much appreciated!

Thanks again,

Mar 23, 2010 at 9:50 PM

Use NumberLiteral.ValidateToken event, in case number is out of range set Context.CurrentToken to error token:

Context.CurrentToken = Context.Source.CreateErrorToken("invalid number");

Mar 24, 2010 at 9:22 AM

Excellent that works, thanks Roman.