Parsing code document with header and footer set

SyntaxEditor .NET Languages Add-on for WPF Forum

Posted 11 years ago by Nick
Version: 13.2.0592
Avatar

Hi,

I'm trying to parse some VB code using the following extension method, but it fails with errors regarding imports section:

public static IParseData Parse(this ISyntaxLanguage language, string header, string body, string footer)
{
  language.ThrowArgumentNullException("language");
  var parser = language.GetParser();
  if (parser == null)
  {
    returnnull;
  }
  var codeDocument = newCodeDocument { Language = language };
  codeDocument.SetHeaderAndFooterText(header, footer);
  codeDocument.SetText(body);
  var snapshot = codeDocument.CurrentSnapshot;
  var reader = snapshot.GetReader(0); // should this be: -snapshot.HeaderText.Length?
  var parseRequest = newParseRequest(Guid.NewGuid().ToString(), reader.BufferReader, language, codeDocument) { Snapshot = snapshot };
  return parser.Parse(parseRequest);
}

 The header is:

Public Partial Class MyClass

The body:

Sub MySub
End Sub

And the footer

End Class

I think it is failing because it does not consider the header when parsing the code.  When the syntax editor returns the parse result for the same VB code, I note the snapshot start point is -27 and the EndPoint is larger than the snapshot I get from the code above.  Is there some way to create a new snapshot for document or to modify the current one to use the header and footer?

Thanks in advance,

 

Nick

[Modified 11 years ago]

Comments (3)

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

Hi Nick,

A common mistake with header/footer and VB is that people don't remember that they need a line terminator at the end of the header and at the start of the footer.  The parser will look at the string returned by "header + body + footer" all concatinated without line terminators in between.  So it's up to you to add them to the header/footer.  Let us know if that helps.


Actipro Software Support

Posted 11 years ago by Nick
Avatar

Hi,

The source header and footer do both contain CR/LF (which I understand is normalised when set against the document).  My contrived example and the -27 value I mentioned aren't from the same code, it was just unfortunate that the class name I chose coincided with that value.

When I examine the reader, it appears that it will only cover the text (body) value.  If I try using the commented out code to pass a negative start point, it then doesn't seem to read enough of the data. I can't see any way to control the length/endpoint for the reader.

Regards,

Nick

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

Hi Nick,

Sorry I should have mentioned this too... Try this line instead: 

var reader = snapshot.GetMergedBufferReader(); 

That method will make a reader that allows scanning over the header/footer as well. 


Actipro Software Support

The latest build of this product (v25.1.0) was released 29 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.