adding keywords to DynamicSyntaxLanguage

SyntaxEditor for Windows Forms Forum

Posted 17 years ago by shark92651
Version: 4.0.0246
Avatar
How would I go about adding additional keywords, datatypes, etc... to a DynamicSyntaxLanguage instance after it has been initialized from an XML file? For example lets say I have a generic SQL language xml file and after it is initialized I want to add in data types that I get from an ODBC connection call.

Comments (7)

Posted 17 years ago by Matthew Smith - Developer, One Plus One Solutions Pty Limited
Avatar
Hi,

I do the following with mine:

'Turn on updating
SyntaxLanguage.IsUpdating = True

Dim lexicalStates As DynamicLexicalState = syntaxEditor.LexicalStates("DefaultState")
Dim keywordPatternGroup As LexicalPatternGroup = lexicalStates.LexicalPatternGroup("ReservedWordToken")

'Add items you items in a loop using the following:
keywordPatternGroup.Add(New LexicalPatternGroup("<NameOfToken>")

'Turn off updating
SyntaxLanguage.IsUpdating = False
My xml language file is a variation of the VB.Net dynamic language.

[Modified at 04/12/2007 06:24 PM]

Regards, Matt

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Good info Matthew. Yes that is how you can do it programmatically.

But you do need to make sure you set your language's SyntaxLanguage.IsUpdating = true before changing the patterns and setting IsUpdating = false after the change is complete. That allows the parser to apply the updates.


Actipro Software Support

Posted 17 years ago by Matthew Smith - Developer, One Plus One Solutions Pty Limited
Avatar
Yep - sorry left that out!

Regards, Matt

Posted 17 years ago by shark92651
Avatar
Thanks for the replies - exactly what I was looking for.
Posted 17 years ago by Robert Muir
Avatar
I'm having trouble with this. I'm using C# so I attempted to translate it as follows

syntaxEditor.Document.Language = language;
language.IsUpdating = true;
DynamicLexicalState lexicalStates = (DynamicLexicalState)language.LexicalStates["DefaultState"];
LexicalPatternGroup keywordPatternGroup = lexicalStates.LexicalPatternGroups["ReservedWordToken"];
keywordPatternGroup.Add(new LexicalPatternGroup("test"));
language.IsUpdating = false;
Several problems
1. new LexicalPatternGroup("test") doesn't compile as there's no overload for it.
2. When I execute the code (without the above) keywordPatternGroup is null

My xml language file is is much reduced version of the C#.net example.

What I really wanted to do was, I think, slightly different but I couldn't get past this point. I need to color user defined tokens, and added another ExplicitPatternGroup with an empty ExplicitPatterns to which I intended to add the new tokens (and later delete then add new ones). The code sample appears not to use ExplicitPatterns which I would have thought preferable.

I'd really appreciate some pointers.

[Modified at 09/11/2007 10:21 AM]
Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Robert,

You are almost there...

1) You mean to be using LexicalPattern, not LexicalPatternGroup.
2) The LexicalPatternGroups collection is indexed on LexicalPatternGroup.Key, not the token key. So you will have to define a Key attribute in the dynamic language xml with the name "ReservedWord" and use that as your indexer lookup instead.
3) LexicalPatternType.PatternType defines whether a pattern group is explicit or regex.

Hope that helps!


Actipro Software Support

Posted 17 years ago by Robert Muir
Avatar
Thanks, its working now.

I'll show what I did for the benefit of others
In XML I added the Key UserDefinedToken.

<!-- User Defined Identifiers -->
<ExplicitPatternGroup Key="UserDefinedToken"  TokenKey="UserDefinedIdentifierToken" Style="UserDefinedIdentifierStyle" LookAhead="{NonWordMacro}">
  <ExplicitPatterns>
  </ExplicitPatterns>
</ExplicitPatternGroup>
And in C#

syntaxEditor.Document.Language = language;
language.IsUpdating = true;
DynamicLexicalState lexicalStates = (DynamicLexicalState)language.LexicalStates["DefaultState"];
LexicalPatternGroup keywordPatternGroup = lexicalStates.LexicalPatternGroups["UserDefinedToken"];
foreach (UserToken token in userTokens)
    keywordPatternGroup.Add(new LexicalPattern(token.Name));
language.IsUpdating = false;
syntaxEditor.Document.LoadFile(value);
The only thing I'm puzzled about it LexicalPatternType.PatternType, which I had no reason to use. I suspect I did it a different way to your suggestion, and added to an existing group that I defined in XML.
The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.