Last active
March 21, 2016 09:15
-
-
Save kthoms/769b3d77eeb87c09f4c3 to your computer and use it in GitHub Desktop.
Operator overloading in Xbase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OpOr: | |
'||' | 'OR'; | |
OpAnd: | |
'&&' | 'AND'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DomainmodelRuntimeModule extends AbstractDomainmodelRuntimeModule { | |
[...] | |
public Class bindOperatorMapping() { | |
return OperatorMappingCustom.class; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Singleton | |
public class OperatorMappingCustom extends OperatorMapping { | |
public static final QualifiedName AND_2 = create("AND"); | |
public static final QualifiedName OR_2 = create("OR"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return s!= null && s.length > 0 AND s.startsWith("bar") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OpOr: | |
'||'; | |
OpAnd: | |
'&&'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment