IBabelPackage::LoadScope Method
Returns the IScope Interface object for a file given a file name and project.
HRESULT LoadScope (
long reserved,
BSTR fileName,
IBabelProject* project,
IScope** scope
);
Parameters
reserved
[in] Reserved for internal use. Always set to 0.fileName
[in] File name containing the scope.project
[in] Project containing the file. This value can be NULL.scope
[out] Returns the scope for the file. For more information, see the IScope Interface.
Return Value
If the method succeeds, it returns S_OK. If it fails, it returns an error code.
Remarks
To determine the project value, you can call the IParseSink::GetProject Method.
The Babel package attempts to load a scope for the source file given a file name and a project. If the file name can be loaded by a Babel service (as determined by examining the Extensions registry entry; see Babel Registry Information for information about the Extensions registry entry), the file is loaded and then parsed, and the resulting IScope Interface object is returned in the scope parameter.
It is possible to load an IScope object for files that are processed by your own implementation. These scopes are delay loaded. That is, as long as you do not call any method on the scope, the IBabelService::ParseSource Method is not called recursively. This can be useful to import chasing (see the Example). For another example, see How to: Add Import Chasing.
Example
This is a fragment from a Pascal parser.y file that demonstrates how to do include source from another file. g_service points to the StdService class that represents a default Babel implementation (see The Default Babel Implementation in the Language Service Package). The g_service->loadScope method eventually calls the IBabelPackage::LoadScope method.
Because the IBabelPackage::LoadScope method is called through the StdService::loadScope method, the file being imported in this example does not trigger a parse at this point (which would cause a recursive parse). Instead, the file is not parsed until any method on the IScope Interface that represents the imported file is called.
/* represents Pascal statement import "filename"; */
import_statement : KWIMPORT CHARACTER_STRING SEMICOLON
{ char fileName[1024];
const char * tokenstring;
//copy and remove the quotes
tokenstring = g_service->tokenText(&$2) + 1;
int i = 0;
while (*tokenstring != '\0')
fileName[i++] = *(tokenstring+);
fileName[i-1] = '\0';
//find & load source
IScope* scope = NULL;
//searchFile does ParseSink::GetProject & BabelProject::SearchFile
g_service->searchFile(fileName,1024);
g_service->loadScope( fileName, &scope );
if (scope)
{
g_service->addExtern( $1, $2, scope );
scope->Release();
}
}
;
See Also
Concepts
The Default Babel Implementation in the Language Service Package