Posted 18 years ago by Dan Oren - Software Developer, Watchfire
Version: 4.0.0237
Avatar
Hi,

I'm moving to version 4.0 from version 3.1.
In 3.1 I used a SemanticParser in the following way:

I created a new SemanticParser class that implements the PostParse Method and changes the HighlightingStyle of some specific tokens.
public class SemanticHTTPRequestParser : SemanticDefaultParser
{
     ...
        public override void PostParse(Document document, DocumentModification modification) 
        {
              ...

private HighlightingStyle requestLineBoldItemsHighlightingStyle = new HighlightingStyle("RequestLineBoldItemsKey", "RequestLineBoldItems", Color.Black, Color.WhiteSmoke, true, false, false);

              ...

              Token token = document.Tokens.GetTokenAtOffset(document.Lines[lineNumber].StartOffset + charIndex);
                                if (token != null)
                                {
                                    token.CustomHighlightingStyle = requestErrorMessageHighlightingStyle;
                                    token.Modified = true;
                                }
              ...
        }
     ...

}
In the main class when the language is loaded an event is thrown and I set the semantic parser :
private void syntaxEditorControl_DocumentSyntaxLanguageLoading(object sender, SyntaxLanguageEventArgs e)
{
      e.Language.SemanticParser = semanticHTTPRequestParser; //already created in the Ctor.
}
The result was that any change in the text goes to the PostParse method and change the HighlightStyle of some tokens.


I understood that in version 4.0 there is no SemanticParser anymore and it was replaced in "DynamicSyntaxLanguage".

I made some changes in my old semantic parser class to support the changes:
public class SemanticHTTPRequestParser : DynamicSyntaxLanguage
{
    ...
private HighlightingStyle requestLineBoldItemsHighlightingStyle = new HighlightingStyle("RequestLineBoldItemsKey", "RequestLineBoldItems", Color.Black, Color.WhiteSmoke, DefaultableBoolean.True, DefaultableBoolean.True, HighlightingStyleLineStyle.DashDot);

    ...

        protected override void OnDocumentTextChanged(Document document, DocumentModificationEventArgs e)
        {
              IToken token = document.Tokens.GetTokenAtOffset(document.Lines[lineNumber].StartOffset + charIndex);
                                if (token != null)
                                {
                                    DynamicToken dynamicToken = token as DynamicToken;
                                    if(dynamicToken !=null)
                                    {
                                       dynamicToken.CustomHighlightingStyle = requestLineBoldItemsHighlightingStyle;
                                    }
                                }

        }

}
I am trying to do the following in the main class like before but the Language object does not have the SemanticParser property anymore:
private void syntaxEditorControl_DocumentSyntaxLanguageLoading(object sender, SyntaxLanguageEventArgs e)
{
      e.Language.SemanticParser = semanticHTTPRequestParser; //already created in the Ctor.
}
What should I do in order to support the previous behaviour?
What code is parallel to this in 4.0 ?

Regards,

Dan

Dan Oren

Comments (16)

Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Dan,

Check out the Language Encapsulation section in the Converting from Version 3.x to 4.0 documentation topic.
It maps where you can do these sorts of things in v4.0.


Actipro Software Support

Posted 18 years ago by Dan Oren - Software Developer, Watchfire
Avatar
Hi,

I tried to do it according to the Help and the SDI application and I didnt find a way to do it.
Could you please supply me with a code example or a sample application ?

Regards,
Dan

Dan Oren

Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
The code you moved to OnDocumentTextChanged should work fine. Is it being executed?


Actipro Software Support

Posted 18 years ago by Dan Oren - Software Developer, Watchfire
Avatar
The code is not being executed since I can't bind this Parser to the syntaxEditor. (but I know that the code should be OK).
I used to bind the Parser to the language in the language loaded event, using the 'e' of the event (e.Language.SemanticParser = mySemanticParser).
In version 4.0 the e.Language does not have a set property for SemanticParser.
I can assume that the interfaces were changed so I want to know what is parallel to this code or idea in 4.0

Regards,

Dan Oren

Dan Oren

Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Right the code-behind SyntaxLanguage can now be attached to a dynamic language with the SyntaxLanguageTypeName attribute in the root SyntaxLanguage tag in the XML definition. Look at our ActiproSoftware.CSharp.xml for an example and read the docs on SyntaxLanguage tag for its usage. Once you do that, the dynamic language XML definition you made will be "merged" with your code-behind and you will use that merged language for your parsing. Then your code will be executed.


Actipro Software Support

Posted 18 years ago by Dan Oren - Software Developer, Watchfire
Avatar
Hi,

Could you explain again in more low level ?

Dan Oren

Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
The XML definition files that you used in v3.1 and are using now define
the "lexical parser" engine for the language. When using these, you are
using a dynamic language. The alternative to doing this is going
lower level and writing a lexical parser programmatically but don't worry about
that since you are using a dynamic language.

The root tag in the dynamic language (look in the docs) has an attribute
that says load a certain type from a certain assembly and use that as my
codebehind class instead of a generic DynamicSyntaxLanguage class.
Therefore if you make a class that inherits DynamicSyntaxLanguage and specify
it in your XML definition, that will be loaded as your SyntaxLanguage
and the lexical parsing will be defined by your XML definition.
So the code you wrote will be executed and your XML definition will be controlling
the lexical parsing. It's the same as it was in v3.1 just instead of
setting a SemanticParser class, you are setting a SyntaxLanguage class instead,
which has a LOT more capabilities than the SemanticParser class did in 3.1.


Actipro Software Support

Posted 18 years ago by Dan Oren - Software Developer, Watchfire
Avatar
Hi again,

I have no problem with XML language definition file.
I am working with them after converting them to 4.0. The problem is not here.
I do changes on the highlighting of the text in the syntaxEditor using a class that implements a SemanticParser class. (in 3.1).

I dont know how to connect this parser to the SyntaxEditor. (The syntax Editor should know that when there are changes on the document it should call the PostParse methos in the Parser class. No in 4.0 it has different name from PostParse and the implementation is of a different class from SemanticParser. No it implements DynamicSyntaxLanguage Class. I know all that...)

How I am telling the SyntxEditor that my class should be used for semantic Parsing ??
Regards,

Dan Oren

Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
If you have everything wired up as I've explained above then you simply place the code you used to have in SemanticParser.PostParse into the SyntaxLanguage.OnDocumentTextChanged override for your language class.


Actipro Software Support

Posted 18 years ago by Dan Oren - Software Developer, Watchfire
Avatar
Hi,

lets start from 0.....

In the XML files I will define expressions and other things that I already know in the design.
In the PostParse I will use in run time for customized changes.
Now - I used in 3.1 in the PostParse in order to make some changes on token that I could not do them in the XML definition. In order to change the highlight of these tokens I Created a class called MySemanticParser that implement the SemanticParser interface by overiding the PostParse method.
In order to apply the changes in the PostParse method I needed to tell the SyntaxEditor that MySemanticParser is the actuall SemanticParser. This I did by doing e.Language.SemanticParser = new MySemanticParser(); in the LanguageLoaded event.

This was worked fine in 3.1.

Now - in 4.0 the name of the interface that should be implemented was changed and also the method that should be overiden.
I change the old class according to 4.0 changes. Untill here all is OK.

The problem now is that I dont know how to tell the SyntaxEditor like before :
e.Language.SemanticParser = new MySemanticParser();
since the e.Language does not have '.SemanticParser' property anymore.

If you will read all the messages from start you will understan what I am talking about.

Regards,

Dan Oren

Posted 18 years ago by Dan Oren - Software Developer, Watchfire
Avatar
Hi

Now in 4.0 you can not do the following :
/// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void syntaxEditorControl_DocumentSyntaxLanguageLoading(object sender, SyntaxLanguageEventArgs e)
        {
            e.Language.SemanticParser = mySemanticParser;
        }
Please register this event and try to set it

Regards,

Dan Oren

Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
That's what I keep trying to explain here is that there is no more SemanticParser class
and all the functionality that was available in v3.1's SemanticParser class is now merged
into the SyntaxLanguage class.

PostParse maps over to SyntaxLanguage.OnDocumentTextChanged.

Look at these in the sample project:
Languages/Dynamic/Lexers/ActiproSoftware.CSharp.xml
Languages/Dynamic/CSharpDynamicSyntaxLanguage.cs

The first tag of the C# XML definition is this:
<SyntaxLanguage Key="C#" LanguageDefinitionVersion="4.0" Secure="True" WordContainsAdditionalCharacters="@_"
SyntaxLanguageTypeName="TestApplication.CSharpDynamicSyntaxLanguage, TestApplication"
xmlns="http://ActiproSoftware/SyntaxEditor/4.0/LanguageDefinition">
Look at how the SyntaxLanguageTypeName attribute tells SyntaxEditor to load the
CSharpDynamicSyntaxLanguage class as its base SyntaxLanguage type.

The CSharpDynamicSyntaxLanguage class inherits DynamicOutliningSyntaxLanguage. We've added
code that used to be in a v3.1 SemanticParser class for C# into this CSharpDynamicSyntaxLanguage
class. It is here that you can override OnDocumentTextChanged and put your old PostParse code in.

Those two files show exactly what you want to do.


Actipro Software Support

Posted 18 years ago by Dan Oren - Software Developer, Watchfire
Avatar
Ok, this is what I need. the line that telling that this will be the semantic parser is equal to the 'e.Language.SemanticParser = mySemanticParser' in 3.1.

But - how can I used in set properties in this method if the creator of the Class is the SyntaxEditor when its language being loaded ?
I dont have reference to this class in runtime?

In my program the SemanticParser implementation has ArrayList properties that holds the tokens that should be changed. I set these araayList in runtime when I know that some tokens should be highlighted and then when the PostParse/TextChanged fired it does it work on the arryList items. How can i 'communicate' with the SemanticParser in run time and sets its data and tasks ?

Thanks

Dan Oren

Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
When you load your language you get a SyntaxLanguage reference. If you wire it
up as I said, that reference will be an instance of your custom SyntaxLanguage
class. So you can add your own properties and methods, etc. It's the same
thing as before and you can do everything as before, it's just that the
stuff that was in the v3.1 SemanticParser class is now directly in the
SyntaxLanguage class.

You can customize it however you want. So add a property for your collection
of special items to be highlighted. Then update it at runtime as needed.


Actipro Software Support

Posted 18 years ago by Dan Oren - Software Developer, Watchfire
Avatar
Ok, so I need to get the instance of that class in order to set properties in it.
What line of code returns me the instance of the custom syntaxLanguage that was loaded from the instance of the SyntaxEditor ?
this.SyntaxEditor. ?

Dan Oren

Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
This tells you the language in use:
SyntaxLanguage language = editor.Document.Language;


Actipro Software Support

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

Add Comment

Please log in to a validated account to post comments.