[antlr-interest] Value types - can't be set to null in C#
Gavin Lambert
antlr at mirality.co.nz
Mon Jan 7 17:14:00 PST 2008
Mere moments ago, I wrote:
>Of course from a Java-based code-generation standpoint you have
no
>way of knowing whether a given type is a value type or a
reference
>type, which complicates matters. So if the 'default' keyword
thing
>I suggested a moment ago doesn't work (or if you don't want to
>restrict yourself to C# 2.0) then you'll have to have some kind
of
>syntax to tell ANTLR whether types are value or reference.
Actually, another thing that could work is simply detecting
whether the variables are used within an @init block. If so, you
could assume that they're being initialised properly there and
simply not do any initialisation at declaration time.
So for the original example, it'd have to be rewritten slightly to
this:
datetime returns [DateTime value]
@init {
$value = DateTime.MinValue;
}
: (h=hour24 {Debug.WriteLine("hour24=" + $h.value.ToString());}
':' m=minute {Debug.WriteLine("minute=" +
$m.value.ToString());}
':' s=second {Debug.WriteLine("second=" +
$s.value.ToString());}
'.' ms=millisecond {Debug.WriteLine("millisecond=" +
$ms.value.ToString());})
{
$value = new DateTime(2008, 1, 4, $h.value, $m.value,
$s.value,
$ms.value);
}
;
... which would generate:
//...
DateTime value;
//...
value = DateTime.MinValue;
//...
That'd work. It could be mildly annoying if these types were used
commonly (so a global "this is how you init DateTimes" specifier
or getting "default" to work would be better), but it'd be a
simple quick-fix solution.
More information about the antlr-interest
mailing list