Access to the document properties while parsing synchronously

SyntaxEditor for WPF Forum

Posted 2 years ago by Daisuke Nakada
Version: 17.2.0664
Avatar

Hi,

 

 

In our product, there are some scenarios where a synchronous parsing is needed,

and then, in the grammar class's EbnfTerm callbacks, we want to access the ICodeDocument.Properties.

 

I know you provide the SyntaxLanguageParsingExtensions.Parse method for performing a synchronous parsing

but the ICodeDocument.Properties is not accessible inside the grammar class's EbnfTerm callback as this method is called without the document.

 

Please tell me how to achieve this.

 

 

Thank you.

Comments (6)

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

Hello,

In your parser class that inherits LLParserBase, you can add an override like this:

protected override void InitializeParserState(IParserState state, IParseRequest request) {
  base.InitializeParserState(state, request);

  state.CustomData = ((ICodeDocument)request.Snapshot.Document).Properties;
}

That will allow your properties to be accessible in the grammar parser wherever you have access to an IParserState.


Actipro Software Support

Posted 2 years ago by Daisuke Nakada
Avatar

Yes, my parser inherits LLParserBase.

I tried that but inside the InitializeParserState method, the ICodeDocument.Properties was not what I needed: It contained only "ITagger".

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

Hello,

You said you wanted to access the document.Properties from within parser grammar callback methods.  I was saying if you store the document.Properties collection in the state.CustomData then you can access that in any grammar callback method.  You could store or update whatever data you want there.  Or you could use a completely custom object in state.CustomData if you would prefer.

Or if I'm misunderstanding what you are trying to do, please give some more detail on the scenario.  Thanks!


Actipro Software Support

Posted 2 years ago by Daisuke Nakada
Avatar

Sorry for my not clearing the points.

When a syntax editor is created, we store some information within the ICodeDocument.Properties. We use this information in grammar callback methods in order to access our database. Even during a synchronous parsing as well, we need to access that information in the grammar callback methods.

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

Hello,

What I suggested previously should normally work.  When you use the Parse extension method for synchronous parsing though, that actually creates a new CodeDocument and so in that case, the Properties won't be available since those are on the other instance.

Perhaps you could just do something like this to use your original document (instead of a new one) for the synchronized parse instead?

var parser = document.Language.GetService<IParser>();
ITextBufferReader reader = document.CurrentSnapshot.GetReader(0).BufferReader;
ParseRequest request = new ParseRequest(Guid.NewGuid().ToString(), reader, language, document);
request.Snapshot = document.CurrentSnapshot;
var parseData = parser.Parse(request);


Actipro Software Support

Posted 2 years ago by Daisuke Nakada
Avatar

Sorry for the late reply and thank you very much. The code you showed works perfectly.

I really appreciate it!

The latest build of this product (v2019.1 build 0683) 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.