Adding DynamicLexicalPatternGroup at runtime

SyntaxEditor for WPF Forum

Posted 8 years ago by Mick George - Software Engineer, CNC Software, Inc.
Version: 11.1.0542
Avatar
Hello,

I have a syntax language class that needs to have DynamicLexicalPatternGroup's created and added to the language at run time that will be populated by a number DynamicLexicalPattern keywords that are accessed at run time.

In my test project I have added the code below in the syntax language constructor which I assume is an OK location to do so. If I watch postLexer the new pattern group has been added but when I view my sample text none of the lexical patterns are highlighted in red as I have specified, what am I missing?

Thanks

 public PostSyntaxLanguage() :
            base("Mastercam Post Language")
        {
            // Create a classification type provider and register its classification types
            PostClassificationTypeProvider classificationTypeProvider = new PostClassificationTypeProvider();
            classificationTypeProvider.RegisterAll();

            ClassificationType systemClassificationType = new ClassificationType("System", "ClassificationType for system tokens");
            classificationTypeProvider.Registry.Register(systemClassificationType, new HighlightingStyle(System.Windows.Media.Brushes.Red));

            PostLexer postLexer = new PostLexer(classificationTypeProvider);

            foreach (DynamicLexicalState s in postLexer.LexicalStates)
            {
                switch (s.Key)
                {
                    case "Default":
                        
                        DynamicLexicalPatternGroup systemPatternGroup = new DynamicLexicalPatternGroup(DynamicLexicalPatternType.Explicit, "System", systemClassificationType);

                        systemPatternGroup.CaseSensitivity = ActiproSoftware.Text.CaseSensitivity.Sensitive;
                        
                        // Add some test data (TODO: pull in live list)
                        systemPatternGroup.Patterns.Add(new DynamicLexicalPattern("ToolTableDescription"));
                        systemPatternGroup.Patterns.Add(new DynamicLexicalPattern("ToolTableMillDiameter"));
                        systemPatternGroup.Patterns.Add(new DynamicLexicalPattern("PToolTableMillIteration"));
                        systemPatternGroup.Patterns.Add(new DynamicLexicalPattern("ToolTableLatheInsertName"));
                        systemPatternGroup.Patterns.Add(new DynamicLexicalPattern("ToolTableLatheInsertName"));

                        s.LexicalPatternGroups.Add(systemPatternGroup);
                        break;
                }
            }

            // Register an ILexer service that can tokenize text
            this.RegisterService<ILexer>(postLexer);

            // Register an ICodeDocumentTaggerProvider service that creates a token tagger for
            //   each document using the language
            this.RegisterService(new PostTokenTaggerProvider(classificationTypeProvider));
        }

Comments (3)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Mick,

I would think that it would be working ok based on what I see there but it's hard to know for sure without debugging it. Maybe check the ambient highlighting style registry to see if you find an entry for your classification type that has the style you specified.

If you can't figure it out, maybe make a very simple new sample project that shows it and email it over. Be sure to rename the .zip file extension so it doesn't get blocked.


Actipro Software Support

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Mick,

Thanks for the sample. The problem is that you added your systemPatternGroup to the end of the LexicalPatternGroups, and the Identifier token was being matched first since it's higher in the collection. If you look at the token under the caret when you have the caret over one of your system keywords, you can see that was an Identifier.

To fix it, just move up the pattern group with an Insert instead of an Add:
s.LexicalPatternGroups.Insert(0, systemPatternGroup);


Actipro Software Support

Posted 8 years ago by Mick George - Software Engineer, CNC Software, Inc.
Avatar
Works great, thank you very much.
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.