CSharpSyntaxLanguage + MyOwnLanguage merged.

SyntaxEditor for Windows Forms Forum

Posted 17 years ago by Erik Pepping - RADVenture B.V
Version: 4.0.0259
I am trying to implement the example "Merging Two Languages at Run-Time Using a Direct Lexical State Transition ".
The tutorial implementation in my project worked OK but off course without intellisense and the other nice features of the AddOn.

Therefore , i've replaced the dynamic language with the CsharpSyntaxLanguage (from the .NET languages AddOn) :

//DynamicSyntaxLanguage csharp = GetCSharpLanguage()
CSharpSyntaxLanguage csharp = new CSharpSyntaxLanguage();

The following exception ocurrs are the moment of testing (.text property). CSharpSyntaxLanguage is derived from Mergable so i was expecting this to work.

System.InvalidCastException was unhandled
Message="Unable to cast object of type 'ActiproSoftware.SyntaxEditor.Addons.CSharp.CSharpSyntaxLanguage'
to type 'ActiproSoftware.SyntaxEditor.Addons.Dynamic.DynamicSyntaxLanguage'."

at ActiproSoftware.SyntaxEditor.Addons.Dynamic.LexicalPatternGroup.b()
at ActiproSoftware.SyntaxEditor.Addons.Dynamic.LexicalPatternGroup.a(ITextBufferReader A_0, LexicalPattern& A_1)
at ActiproSoftware.SyntaxEditor.Addons.Dynamic.DynamicLexicalScope.IsScopeStart(ITextBufferReader reader, ITokenLexicalParseData& lexicalParseData)
at o.a(Document A_0, TextRange A_1, Int32 A_2, Int32 A_3, ILexicalParseTarget A_4)
at ActiproSoftware.SyntaxEditor.MergableSyntaxLanguage.PerformLexicalParse(Document document, TextRange parseTextRange, ILexicalParseTarget parseTarget)
at ActiproSoftware.SyntaxEditor.Document.a(DocumentModification A_0, Boolean A_1)
at ActiproSoftware.SyntaxEditor.Document.set_Text(String value)

Am i doing something wrong? Did I've overlooked something?

Thanks in advance.

Comments (4)

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Not sure... I went into the DynamicallyCreateForm and did this single line code change and it worked ok:
private void CreateDirectiveXmlToCSharpLanguage() {
    // Load the two languages
    language = DynamicSyntaxLanguage.LoadFromXml(Program.DynamicLexersPath + "ActiproSoftware.XML.xml", 0);
    // DynamicSyntaxLanguage cSharpLanguage = DynamicSyntaxLanguage.LoadFromXml(Program.DynamicLexersPath + "ActiproSoftware.CSharp.xml", 0);
    ActiproSoftware.SyntaxEditor.Addons.CSharp.CSharpSyntaxLanguage cSharpLanguage = new ActiproSoftware.SyntaxEditor.Addons.CSharp.CSharpSyntaxLanguage();
However, please note that the C# add-on probably will not work correctly when in a merged scenario. Although it is capable of merging, the context determination code and probably a couple other areas, aren't able to correctly process it being mixed with other languages. So you may or may not run into issues with this. This is something we hope to address in the future for the add-on.

Actipro Software Support

Posted 17 years ago by Erik Pepping - RADVenture B.V
Thanks for the reply, but actually i am doing exactly the opposite.

I want to "extend" C# and make a transition to my own language.

So the "example" code should be inverted :

            CSharpSyntaxLanguage csharp = new CSharpSyntaxLanguage();
            DynamicSyntaxLanguage gwTemplate = GetGenWiseTemplateLanguage();

            csharp.IsUpdating = true;
            gwTemplate.IsUpdating = true;

            // Add a highlighting style
            csharp.HighlightingStyles.Add(new HighlightingStyle("TplDirectiveDelimiterStyle", null, Color.Black, Color.Yellow));

            // Create a new lexical state
            DynamicLexicalState lexicalState = new DynamicLexicalState("TplDirectiveState");
            lexicalState.DefaultTokenKey = "TplDirectiveDefaultToken";
            lexicalState.DefaultHighlightingStyle = csharp.HighlightingStyles["DefaultStyle"];
            lexicalState.LexicalStateTransitionLexicalState = gwTemplate.LexicalStates["DefaultState"];

            // Add the new lexical state at the beginning of the child states...
            // Remember that with an NFA regular expression, the first match is taken...
            // So since a < scope pattern is already in place, we must insert the new one before it
            csharp.LexicalStates["DefaultState"].ChildLexicalStates.Insert(0, lexicalState);

            // Create a lexical scope with a lexical state transition
            DynamicLexicalScope lexicalScope = new DynamicLexicalScope();
            lexicalScope.StartLexicalPatternGroup = new LexicalPatternGroup(LexicalPatternType.Explicit, "TplDirectiveStartToken",
                csharp.HighlightingStyles["TplDirectiveDelimiterStyle"], @"<%");
            lexicalScope.EndLexicalPatternGroup = new LexicalPatternGroup(LexicalPatternType.Explicit, "TplDirectiveEndToken",
                csharp.HighlightingStyles["TplDirectiveDelimiterStyle"], @"%>");

            csharp.IsUpdating = false;
            gwTemplate.IsUpdating = false;

            syntaxEditor1.Document.Language = csharp;
I thought that by doing it in this way, the context would still be valid.
Because my requirement is that i want to keep Code completion working.

Example Text

using System.Reflection;
using System.Runtime.CompilerServices;
using GenWise.Framework.Templates;

namespace MyProject 
   public class <% U.Quote(Options.ClassName) %> 
[Modified at 08/29/2007 10:26 AM]
Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Eric,

The problem you are having is that you are adding a "dynamic" lexical state to the add-on language, but the add-on language is not a dynamic language so it blows up.

You need to use the more base programmatic classes for adding the state, like DefaultLexicalState.

But still even when you do get the highlight transitioning working, you may or may not have random issues with context/IntelliPrompt.

Actipro Software Support

Posted 17 years ago by Erik Pepping - RADVenture B.V
Ok, thanks for the reply.

I will start an example of a derived language from CSharpLanguage so i can experiment with DefaultLexicalState and ProgrammaticLexicalScope.
The latest build of this product (v24.1.0) was released 4 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.