Filename and SourceKey

SyntaxEditor for Windows Forms Forum

Posted 15 years ago by Brad S. - MedAssets, Inc.
Version: 4.0.0282
Avatar
Background:
I'm working on a full-fledged mergable syntax language. So far so good--grammar and AST are working great. Now It must process include files. I'm having trouble piecing together a clear strategy for handling multiple source files from the documentation but I think I'm heading in the right direction. I can't really tell, because...

Question:
I'm having trouble getting the filename/sourcekey (or any access to the SemanticParserServiceRequest) from within MySyntaxLanguage.PerformSemanticParse -- all I get there is a MergableLexicalParserManager. The manager object doesn't seem to have much of the context or the original request.

The ISemanticParserServiceProcessor.Process (in MySyntaxLanguage) looks like this:

        void ISemanticParserServiceProcessor.Process(SemanticParserServiceRequest request)
        {
            request.SemanticParseData = MergableLexicalParserManager.PerformSemanticParse( this, request.TextBufferReader, request.Filename ) as ISemanticParseData;
        }
Which ultimately calls MySyntaxLanguage with a stripped down manager object:

        protected override object PerformSemanticParse(MergableLexicalParserManager manager)
        {
                // Blah blah blah...
        }
which is where I need the current scope's filename/directory in order to know how to interpret any contained relative paths (in include directives). In a previous post, you mentioned that you use the SourceKey for your C#/VB add ons. My question is, how do you pass it through the static MergableLexicalParserManager.PerformSemanticParse method or otherwise get back to the request's SourceKey? Am I missing something?

Thanks,
Brad

Comments (3)

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Brad,

When PerformSemanticParse(MergableLexicalParserManager) completes, if the object returned is an ICompilationUnit, the MergableLexicalParserManager fills in its SourceKey property. That should be filled in by the time it gets back up to the Process method. But unfortunately that is probably too late for you.

We'll add a property to the next build on MergableLexicalParserManager that gives you the SourceKey.


Actipro Software Support

Posted 15 years ago by Brad S. - MedAssets, Inc.
Avatar
For now, I did this:

private class MyLexicalParserManager : MergableLexicalParserManager
{

    public SemanticParserServiceRequest Request { get; set; }

    public MyLexicalParserManager(MergableSyntaxLanguage rootLanguage, SemanticParserServiceRequest request) :
        base( request.TextBufferReader, rootLanguage, request.Filename )
    {
        this.Request = request;
    }

}

void ISemanticParserServiceProcessor.Process(SemanticParserServiceRequest request)
{
    MyLexicalParserManager manager = new MyLexicalParserManager( this, request );
    request.SemanticParseData = PerformSemanticParse( manager ) as ISemanticParseData;

    // Previous code:
    //request.SemanticParseData = MergableLexicalParserManager.PerformSemanticParse( this, request.TextBufferReader, request.Filename ) as ISemanticParseData;
}
And then I do this:

protected override object PerformSemanticParse(MergableLexicalParserManager manager)
{
    MyLexicalParserManager newManager = null;
    if (manager is MyLexicalParserManager) newManager = manager as MyLexicalParserManager;

    // Now I can access newManager.Request
    // blah blah blah...
}
It works, but I'm not sure what the risks are. I've effectively redefined an interface method to get what I need. So long as I can control who calls PerformSemanticParse, I should always have what I need. I just don't know if I actually have that control. I guess I'm asking, "I know it's ugly, but do you think it is stupid?"
Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
That's basically the same code we have. Our code also does this:
// Set the source key
if (semanticParseResult is ICompilationUnit)
    ((ICompilationUnit)semanticParseResult).SourceKey = sourceKey;
            
if (semanticParseResult is IAstNode)
    semanticParseResult = manager.MergeLanguageTransitionNodes((IAstNode)semanticParseResult);
So you can use that code snippet until the next release.


Actipro Software Support

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.