Monday, 22 September 2014

Import objects should link to proper source MIB definition - Part 3

From last Mib.xtext, the main change need to be done is to break Mib Objects from Import Objects. i.e The Mib Object identifier does not need to directly point to import section's identifier. Import works more as 'namespace' lookup for each identifier. Therefore Mib grammar should be modified as follow:

Mib.xtext
Import:
 name='IMPORTS' defs+=ImpDef+ ';';

ImpDef:
 objects+=ImpObject (',' objects+=ImpObject)* 'FROM' name=ID;

ImpObject:
 name=ID;

And re-point imp to macro as well as simplify Identifier:

Mib.xtext
Identifier:
 ObjectType | (name=ID mibType value=OidValue);
// ObjectType | (name=MibObject mibType value=OidValue);

ObjectType:
 name=ID imp=[Macro] 'SYNTAX' mibType 'ACCESS' ID 'STATUS' ID
// name=MibObject imp=[MibObject] 'SYNTAX' mibType 'ACCESS' ID 'STATUS' ID
 ('DESCRIPTION' STRING)?
 ('REFERENCE' STRING)?
 ('INDEX' '{' mibType (',' mibType)* '}')?
 ('DEFVAL' '{' INT | STRING '}')?
 value=OidValue;

OidValue:
 '::=' '{' parent=[Identifier]? oidnum=INT '}';
// '::=' '{' parent=[MibObject]? oidnum=INT '}';


And since we are not using "importedNamespace" attribute, we need to complete the namespace by overriding "internalGetImportedNamespaceResolvers()" - note that I used "getImportedNamespace()" on earlier example - but that can't be used here as Xtext not able to know all the imported object since the structure is pretty complex in MIB. Hence overriding the "internalGetImportedNamespaceResolvers()" which is in fact the caller for "getImportedNamespace()" is more appropriate here.


MibImportedNamespaceAwareLocalScopeProvider.xtend
class MibImportedNamespaceAwareLocalScopeProvider extends ImportedNamespaceAwareLocalScopeProvider {
 override List internalGetImportedNamespaceResolvers(EObject context, boolean ignoreCase) {
  val list = new LinkedList
  if (context instanceof Definition) {
   if ((context as Definition).imports != null) {
    for (x : EcoreUtil2.getAllContentsOfType((context as Definition).imports, ImpObject)) {
     val qname = (x.eContainer as ImpDef).name + "." + x.name
     list.add(createImportedNamespaceResolver(qname, ignoreCase));
    }
   }
  } 
  list
 }
}


And of course bind this class by:

MibRuntimeModule.java
public class MibRuntimeModule extends
  com.ravi.mib.xtext.AbstractMibRuntimeModule {
 @Override
 public void configureIScopeProviderDelegate(com.google.inject.Binder binder) {
  binder.bind(org.eclipse.xtext.scoping.IScopeProvider.class)
    .annotatedWith(
      com.google.inject.name.Names
        .named(org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider.NAMED_DELEGATE))
    .to(MibImportedNamespaceAwareLocalScopeProvider.class);
 }
}


Now, "Open-Declaration" (i.e F3) feature should work for both "Mib object identifier" and "OBJECT-TYPE" macro, open up file that contains those definitions.

No comments:

Post a Comment