This project is read-only.

Custom Language Syntax Highlighting

Topics: Developer Forum
Mar 7, 2013 at 10:27 AM
Edited Mar 7, 2013 at 10:27 AM

I created a cusotm language called "cup", and now I try to set it's highlingting.
I created an XML file:

<?xml version="1.0" encoding="utf-8" ?>
<Language Name="cup">
<Lexer LexerName="cpp" LineCommentPrefix="//" >

  <Keywords List="0">
    if while for end switch case return break continue func

  <Keywords List="1">
    sin sos tan abs sqrt atan atan2 asin acos ceil floor round exp log log10 power

  <Keywords List="2">



  <Style Name="DOCUMENT_DEFAULT" ForeColor="black" />
  <Style Name="NUMBER" ForeColor="steelblue" />
  <Style Name="WORD" ForeColor="green" />
  <Style Name="WORD1" ForeColor="turquoise" />
  <Style Name="STRING" ForeColor="red" />
  <Style Name="CHARACTER" ForeColor="red" />
  <Style Name="PREPROCESSOR" ForeColor="brown" />
  <Style Name="OPERATOR" ForeColor="black" />
  <Style Name="IDENTIFIER" ForeColor="black" />
  <Style Name="BLOCK" ForeColor="blue" />
  <Style Name="END" ForeColor="blue" />
  <Style Name="COMMENT" ForeColor="green" />


and in my code I wrought this:
m_Scintilla.Lexing.Lexer = ScintillaNET.Lexer.Cpp;
m_Scintilla.Lexing.LexerLanguageMap["cup"] = "cpp";
m_Scintilla.ConfigurationManager.CustomLocation = System.IO.Path.GetFullPath("cup.xml");
m_Scintilla.ConfigurationManager.Language = "cup";

And yet, the keyword of both groups are not colored as I requested. What am I duing wrong?
Mar 7, 2013 at 1:44 PM
First off, you don't need to manually set the lexer in your code, it's already set in your xml file, but that's not the actual issue where. The actual issue is simply that your trying to set the custom location to the name of a file, it needs to be the directory the file is in instead, so you want System.IO.Path.GetDirectoryName().
Mar 10, 2013 at 4:36 AM
Thank you for your reply, but I'm afraid it's still not working! It seems like its taking the defaults of CPP and totaly egnore my directives...
Mar 14, 2013 at 1:38 AM
Also note, you don't actually have to set the location to the directory of the file. It will work if you set the location to the file itself. This is not the issue, however. You do have to keep in mind that you can't change the behavior of the lexer, either, you can only change the colors. This seems to be what you're trying to do, though, so it shouldn't matter. Try looking at the source to see what the built in XML file for C++ is; if you haven't done this, you may find that the "Name" attribute has to be different than WORD and WORD1 (I don't know, though). Another thing, do you try to set the font of the control anywhere later in code? This will clash with your styling.
Mar 14, 2013 at 7:32 AM
I give up using custom file for my language. Instead I set the syntax Highlighting dynamically:

m_Scintilla.Lexing.Lexer = ScintillaNET.Lexer.Cpp;

m_Scintilla.Lexing.Keywords[0] = "if else while for end switch case return break continue func";
m_Scintilla.Lexing.Keywords[1] = "sin sos tan abs sqrt atan atan2 asin acos ceil floor round exp log log10 power";

m_Scintilla.Lexing.LineCommentPrefix = "//";

m_Scintilla.Styles[m_Scintilla.Lexing.StyleNameMap["DOCUMENT_DEFAULT"]].ForeColor = System.Drawing.Color.Black;
m_Scintilla.Styles[m_Scintilla.Lexing.StyleNameMap["NUMBER"]].ForeColor = System.Drawing.Color.SteelBlue;
m_Scintilla.Styles[m_Scintilla.Lexing.StyleNameMap["WORD"]].ForeColor = System.Drawing.Color.Blue;
m_Scintilla.Styles[m_Scintilla.Lexing.StyleNameMap["WORD2"]].ForeColor = System.Drawing.Color.Turquoise;
m_Scintilla.Styles[m_Scintilla.Lexing.StyleNameMap["STRING"]].ForeColor = System.Drawing.Color.Red;
m_Scintilla.Styles[m_Scintilla.Lexing.StyleNameMap["CHARACTER"]].ForeColor = System.Drawing.Color.Red;
m_Scintilla.Styles[m_Scintilla.Lexing.StyleNameMap["PREPROCESSOR"]].ForeColor = System.Drawing.Color.Brown;
m_Scintilla.Styles[m_Scintilla.Lexing.StyleNameMap["OPERATOR"]].ForeColor = System.Drawing.Color.Black;
m_Scintilla.Styles[m_Scintilla.Lexing.StyleNameMap["IDENTIFIER"]].ForeColor = System.Drawing.Color.Black;
m_Scintilla.Styles[m_Scintilla.Lexing.StyleNameMap["COMMENT"]].ForeColor = System.Drawing.Color.Green;

It works!
Mar 14, 2013 at 11:27 AM
Congrats. The files can always be a bit tricky. The way the documentation is set up, it seems like using the XML files would be the obvious choice, while in reality, I think setting styles in code is preferable in many cases.