I need to build a grammer containing a cross reference, which may be invalid, i.e. points to a nonexisting target. A file containing such a reference should not yield an error, but only a warning. The generator would handle this as as a special case.

How can I do this with XText?

有帮助吗?

解决方案

It's not possible to create valid cross references to non-existing targets in EMF.

I would suggest to go with EAttributes instead of EReferences:

  • Change the feature=[EClass|ID] by feature=ID in {YourDSL} grammar.
  • Provide a scope calculation utility like it's done in *scope_EClass_feature(context, reference)* method in the {YourDSL}ScopeProvider class. As this scoping methods simply use the eType of the given reference the reimplementation should be straightforward.
  • Use this scope calculation utility in {YourDSL}ProposalProvider to propose values for the introduced EAttribute.
  • Optionally you can use this utility in a validation rule to add a warning/info to this EAttribute if it's not "valid".
  • Finally use the utility in your generator to create output based on valid target eObjects.

其他提示

I also ran into this problem when creating a DSL to provide declerations of variables for a none-declerative language for a transition pahse. This method works but ask yourself if you realy want to have those nasty may-references.

You can drop the auto generated error in you UI module only. To do so, provide an ILinkingDiagnosticMessageProvider and override the function getUnresolvedProxyMessage:

class DSLLinkingDiagnosticMessageProvider extends LinkingDiagnosticMessageProvider {
    override getUnresolvedProxyMessage(ILinkingDiagnosticContext context) {
        if(context.context instanceof YourReference) {
            // return null so the your error is left out
            null
        } else {
            // use super implementation for others
            super.getUnresolvedProxyMessage(context)
        }
    }
}

All linker-errors for YourReference will be missed. But be aware that there will be a dummy referenced object with all fealds null. Exspecialy the name ist lost and you can not set it due to a CyclicLinkingException. But you may create a new method that sets the name directly.

Note that the dummy object will have the type you entered in your gramma. If its abstract you can easily check witch reference is not linked.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top