Skip to content

Instantly share code, notes, and snippets.

@kthoms
Last active March 21, 2016 09:15
Show Gist options
  • Save kthoms/769b3d77eeb87c09f4c3 to your computer and use it in GitHub Desktop.
Save kthoms/769b3d77eeb87c09f4c3 to your computer and use it in GitHub Desktop.
Operator overloading in Xbase
OpOr:
'||' | 'OR';
OpAnd:
'&&' | 'AND';
grammar org.eclipse.xtext.example.domainmodel.Domainmodel with org.eclipse.xtext.xbase.Xbase
...
Operation:
'op' name=ValidID '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')' (':' type=JvmTypeReference)?
body=XBlockExpression;
public class DomainmodelRuntimeModule extends AbstractDomainmodelRuntimeModule {
[...]
public Class bindOperatorMapping() {
return OperatorMappingCustom.class;
}
}
@Singleton
public class OperatorMappingCustom extends OperatorMapping {
public static final QualifiedName AND_2 = create("AND");
public static final QualifiedName OR_2 = create("OR");
@Override
public QualifiedName getMethodName(QualifiedName operator) {
if (AND_2.equals(operator)) {
return getMethodName(AND);
}
if (OR_2.equals(operator)) {
return getMethodName(OR);
}
return super.getMethodName(operator);
}
}
@Singleton
public class OperatorMappingCustom extends OperatorMapping {
public static final QualifiedName AND_2 = create("AND");
public static final QualifiedName OR_2 = create("OR");
}
return s!= null && s.length > 0 AND s.startsWith("bar")
@Test
def void testOverriddenKeyword() {
val model = '''
package example {
entity MyEntity {
property : String
op foo(String s) {
return s!= null && s.length > 0 AND s.startsWith("bar")
}
}
}
'''.parse
val pack = model.elements.head as PackageDeclaration
val entity = pack.elements.head as Entity
val op = entity.features.last as Operation
val method = op.jvmElements.head as JvmOperation
model.eResource.assertNoErrors
Assert::assertEquals("boolean", method.returnType.simpleName)
}
OpOr:
'||';
OpAnd:
'&&';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment