Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Xtext/Documentation/ValueConverter"

(New page: Value converters are registered to convert parsed text into a certain data type instance and back. The primary hook is called org.eclipse.xtext.conversion.IValueConverterService and the co...)
 
Line 8: Line 8:
 
The implementation for the default token grammar looks like so ([http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.tmf/org.eclipse.xtext/plugins/org.eclipse.xtext/src/org/eclipse/xtext/common/services/DefaultTerminalConverters.java?root=Modeling_Project&view=markup org/eclipse/xtext/common/services/DefaultTerminalConverters.java]):
 
The implementation for the default token grammar looks like so ([http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.tmf/org.eclipse.xtext/plugins/org.eclipse.xtext/src/org/eclipse/xtext/common/services/DefaultTerminalConverters.java?root=Modeling_Project&view=markup org/eclipse/xtext/common/services/DefaultTerminalConverters.java]):
 
<code>
 
<code>
  public class DefaultTerminalConverters extends AbstractAnnotationBasedValueConverterService {
+
public class DefaultTerminalConverters extends AbstractAnnotationBasedValueConverterService {
 
+
 
private Grammar grammar;
+
  private Grammar grammar;
+
 
@Inject
+
  @Inject
public void setGrammar(IGrammarAccess grammarAccess) {
+
  public void setGrammar(IGrammarAccess grammarAccess) {
this.grammar = grammarAccess.getGrammar();
+
    this.grammar = grammarAccess.getGrammar();
}
+
  }
+
 
protected Grammar getGrammar() {
+
  protected Grammar getGrammar() {
return grammar;
+
    return grammar;
}
+
  }
+
 
@ValueConverter(rule = "ID")
+
  @ValueConverter(rule = "ID")
public IValueConverter<String> ID() {
+
  public IValueConverter<String> ID() {
return new AbstractNullSafeConverter<String>() {
+
    return new AbstractNullSafeConverter<String>() {
@Override
+
      @Override
protected String internalToValue(String string, AbstractNode node) {
+
      protected String internalToValue(String string, AbstractNode node) {
return string.startsWith("^") ? string.substring(1) : string;
+
        return string.startsWith("^") ? string.substring(1) : string;
}
+
      }
 
+
 
@Override
+
      @Override
protected String internalToString(String value) {
+
      protected String internalToString(String value) {
if (GrammarUtil.getAllKeywords(getGrammar()).contains(value)) {
+
        if (GrammarUtil.getAllKeywords(getGrammar()).contains(value)) {
return "^"+value;
+
          return "^"+value;
}
+
        }
return value;
+
        return value;
}
+
      }
};
+
    };
}
+
  }
 
+
 
 
       ...  some other value converter
 
       ...  some other value converter
 
</code>
 
</code>
Line 46: Line 46:
 
Imagine, you would want to add a rule creating BigDecimals:
 
Imagine, you would want to add a rule creating BigDecimals:
 
<code>
 
<code>
        @ValueConverter(rule = "BIG_DECIMAL")
+
  @ValueConverter(rule = "BIG_DECIMAL")
public IValueConverter<String> BIG_DECIMAL() {
+
  public IValueConverter<String> BIG_DECIMAL() {
return new AbstractToStringConverter<BigDecimal>() {
+
    return new AbstractToStringConverter<BigDecimal>() {
@Override
+
      @Override
protected BigDecimal internalToValue(String string, AbstractNode node) {
+
      protected BigDecimal internalToValue(String string, AbstractNode node) {
return BigDecimal.valueOf(string);
+
        return BigDecimal.valueOf(string);
}
+
      }
};
+
    };
}
+
  }
 
</code>
 
</code>

Revision as of 05:22, 16 March 2009

Value converters are registered to convert parsed text into a certain data type instance and back. The primary hook is called org.eclipse.xtext.conversion.IValueConverterService and the concrete implementation can be registered via the runtime Guice module (TODO reference to framework description).

Annotation based value converters

The most simple way to register additional value converters is to make use of org.eclipse.xtext.conversion.impl.AbstractAnnotationBasedValueConverterService, which allows to declaratively register IValueConverter via annotated methods.

The implementation for the default token grammar looks like so (org/eclipse/xtext/common/services/DefaultTerminalConverters.java): public class DefaultTerminalConverters extends AbstractAnnotationBasedValueConverterService {

 private Grammar grammar;
 	
 @Inject
 public void setGrammar(IGrammarAccess grammarAccess) {
   this.grammar = grammarAccess.getGrammar();
 }
 	
 protected Grammar getGrammar() {
   return grammar;
 }
 	
 @ValueConverter(rule = "ID")
 public IValueConverter<String> ID() {
   return new AbstractNullSafeConverter<String>() {
     @Override
     protected String internalToValue(String string, AbstractNode node) {
       return string.startsWith("^") ? string.substring(1) : string;
     }
 
     @Override
     protected String internalToString(String value) {
       if (GrammarUtil.getAllKeywords(getGrammar()).contains(value)) {
         return "^"+value;
       }
       return value;
     }
   };
 }
 
      ...  some other value converter

If you use the common terminals grammar (org.eclipse.xtext.common.Terminals) you should subclass DefaultTerminalConverters and overwrite or add addition value converter by adding the respective methods.

Imagine, you would want to add a rule creating BigDecimals:

 @ValueConverter(rule = "BIG_DECIMAL")
 public IValueConverter<String> BIG_DECIMAL() {
   return new AbstractToStringConverter<BigDecimal>() {
     @Override
     protected BigDecimal internalToValue(String string, AbstractNode node) {
       return BigDecimal.valueOf(string);
     }
   };
 }

Back to the top