[Xtext] Default values for optional assignments? [message #468198] |
Tue, 04 August 2009 13:44  |
Eclipse User |
|
|
|
Dear xtext folks,
is there a way to specify default values for attribute assignments that
are optional?
Like in:
Node:
(attr=INT)? name=ID
If one omits the INT token, then attr will be initialized with 0 as I
understand. Is there a way to set this default value to some other value?
Cheers,
HAuke
|
|
|
|
|
|
|
Re: [Xtext] Default values for optional assignments? [message #986747 is a reply to message #985923] |
Wed, 21 November 2012 14:19   |
Eclipse User |
|
|
|
Hi again,
Your blog post helped in that I got to the point where I realised that for that particular problem I could just set the default value literal of the attribute.
The problem I have now is that I need to override the getter of an attribute so that if the attribute is null i return a value derived from one of the other attributes.
Obviously I can't just add an operation to override the getter as that invalidates the genmodel, so I'm a little stuck... any suggestions?
Thanks in advance.
|
|
|
|
|
|
Re: [Xtext] Default values for optional assignments? [message #1863563 is a reply to message #1863231] |
Fri, 09 February 2024 17:12  |
Eclipse User |
|
|
|
This can now be handled using a ValueConverter. In the example below, note that the `toValue()` methods check to see if the string is empty - this is how you handle the default value. This class must also be registered in the <DSL>RuntimeModule - see second code snippet
/**
*
*/
package com.epistimis.uddl;
import org.eclipse.xtext.common.services.DefaultTerminalConverters;
import org.eclipse.xtext.conversion.IValueConverter;
import org.eclipse.xtext.conversion.ValueConverter;
import org.eclipse.xtext.conversion.ValueConverterException;
import org.eclipse.xtext.nodemodel.INode;
/**
*
*/
public class UddlValueConverters extends DefaultTerminalConverters {
IValueConverter<Boolean> realRangeLowerValueConverter = new IValueConverter<Boolean>() {
@Override
public Boolean toValue(String string, INode node) throws ValueConverterException {
if ((string.startsWith("[")) || (string.isEmpty())) {
return Boolean.TRUE;
}
if (string.startsWith("(")) {
return Boolean.FALSE;
}
// default
return Boolean.TRUE;
}
@Override
public String toString(Boolean value) throws ValueConverterException {
return value.booleanValue()? "[" : "(" ;
}
};
IValueConverter<Boolean> realRangeUpperValueConverter = new IValueConverter<Boolean>() {
@Override
public Boolean toValue(String string, INode node) throws ValueConverterException {
if ((string.startsWith("]")) || (string.isEmpty())) {
return Boolean.TRUE;
}
if (string.startsWith(")")) {
return Boolean.FALSE;
}
// default
return Boolean.TRUE;
}
@Override
public String toString(Boolean value) throws ValueConverterException {
return value.booleanValue()? "]" : ")" ;
}
};
@ValueConverter(rule = "RRLOWER")
public IValueConverter<Boolean> RRLOWER() {
return realRangeLowerValueConverter;
}
@ValueConverter(rule = "RRUPPER")
public IValueConverter<Boolean> RRUPPER() {
return realRangeUpperValueConverter;
}
}
Register it in the <DSL>RuntimeModule like this:
public Class<? extends IValueConverterService> bindIValueConverterService() {
return UddlValueConverters.class ;
}
|
|
|
Powered by
FUDForum. Page generated in 0.03965 seconds