Thursday, November 5, 2009

Colorize C# code with RegEx and HTML

Hi guys.
This time I needed to colorize some basic sample c# code and haven't found any simple and usable componente to do that, so I decided to code one, but very simple and basic.
I know it lacks some functionality, like coloring a class on a static method call, because this is not so simple as that, but it's a start, here is the code:
private string words = "abstract|as|base|bool|break|byte|case|catch|char|checked|class|const|continue|decimal|default|delegate|do|double|else|enum|event|explicit|extern|false|finally|fixed|float|for|foreach|goto|if|implicit|in|int|interface|internal|is|lock|long|namespace|new|null|object|operator|out|override|params|private|protected|public|readonly|ref|return|sbyte|sealed|short|sizeof|stackalloc|static|string|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|virtual|volatile|void|while";
//this is a list of reserved words

private string ColorizeCode(string code)
{
  Regex regExInst = new Regex(@"(?\w+)\s\w+\s=\snew\s(?\w+)\(");//regex to colorize the object creations

  MatchCollection mcInst = regExInst.Matches(code);

  foreach (Match match in mcInst)
  {
    code = code.Remove(match.Groups["TYPE"].Index, match.Groups["TYPE"].Length);
    code = code.Insert(match.Groups["TYPE"].Index, "" + match.Groups["TYPE"].Value + "");

    Regex regExInstAux = new Regex(@"\s=\snew\s(?\w+)\(");
    MatchCollection mcInstAux = regExInstAux.Matches(code);
    foreach (Match matchAux in oMatchCollectionAux)
    {
      code = code.Remove(matchAux.Groups["TYPE"].Index,    matchAux.Groups["TYPE"].Length);
      code = code.Insert(matchAux.Groups["TYPE"].Index, "" + matchAux.Groups["TYPE"].Value + "");
    }
  }

  Regex regExCall = new Regex(@"(?\w+)\s\w+\s=\s\w+");
  //regex to colorize an object received from a method call

  MatchCollection mcCall = regExCall.Matches(code);

  foreach (Match match in mcCall)
  {
    code = code.Remove(match.Groups["TYPE"].Index, match.Groups["TYPE"].Length);
    code = code.Insert(match.Groups["TYPE"].Index, "" + match.Groups["TYPE"].Value + "");
  }

  string[] reservedWordArr = words.Split('|');

  for (int i = 0; i <= reservedWordArr.Length - 1; i++)
  {
    Regex regexRW = new Regex(@"\s(?" + reservedWordArr[i] + @")\s");
    //regex to colorize reserved words
    MatchCollection matchColRW = regexRW.Matches(code);
    foreach (Match match in matchColRW)
    {
      code = code.Remove(match.Groups["RW"].Index, match.Groups["RW"].Length);
      code = code.Insert(match.Groups["RW"].Index, "" + match.Groups["RW"].Value + "");
    }
  }

  return code;
}
and that's it! of course for your case, you change the markup I added..

See you soon :)

2 comments:

Rahul said...

hi,

First of all. Thanks very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I

was with the information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful

for .Net community.

There is a good C# resource site, Have alook

http://www.csharptalk.com

simi

Ricardo Rodrigues said...

Thank you very much for your reply, I'm sorry I didn't replied earlier, but I didn't have the alerts in place. Thanks again, I'll check it out!