Results 1 to 7 of 7

Thread: C# Syntax Highlighter

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188

    C# Syntax Highlighter

    I am having some problems with multiline comments stripping the color from other text.

    PHP Code:
    function cshilite($code)
    {

        
    $code str_replace("<br>"""$code);
        
    $code str_replace("<br />"""$code);
        
    $code str_replace("&gt;"">"$code);
        
    $code str_replace("&lt;""<"$code);
        
    $code str_replace("&quot;""\\"", $code);
        
    $code = str_replace("&amp;", "&", $code);
        
    $code = str_replace('$', '\\$', $code);
        
    $code = str_replace('\\n', '\\\\\\\\n', $code);
        
    $code = str_replace('\\r', '\\\\\\\\r', $code);
        
    $code = str_replace('\\t', '\\\\\\\\t', $code);

        
    $code = stripslashes($code);

        
    $code = htmlentities($code);

        
    $keywords = array('abstract', 'event', 'new', 'struct', 'as', 'explicit', 'null', 'switch', 'base', 'extern', 'object', 'this', 'bool', 'false', 'operator', 'throw', 'break', 'finally', 'out', 'true', 'byte', 'fixed', 'override', 'try', 'case', 'float', 'params', 'typeof', 'catch', 'for', 'private', 'uint', 'char', 'foreach', 'protected', 'ulong', 'checked', 'goto', 'public', 'unchecked', 'class', 'if', 'readonly', 'unsafe', 'const', 'implicit', 'ref', 'ushort', 'continue', 'in', 'return', 'using', 'decimal', 'int', 'sbyte', 'virtual', 'default', 'interface', 'sealed', 'volatile', 'delegate', 'internal', 'short', 'void', 'do', 'is', 'sizeof', 'while', 'double', 'lock', 'stackalloc', 'else', 'long', 'static', 'enum', 'namespace', 'string');

        for (
    $x = 0; $x < count($keywords); $x++) { 
            
    $pattern = '/(^|[^a-zA-Z0-9#@_\\'\\"])(' . $keywords[$x] . ')([^a-zA-Z0-9#_\\'\\"]|$)/'; 
            
    $code preg_replace($pattern'\\\\1<font color="blue">\\\\2</font>\\\\3'$code); 
        }

        
    $single '/((^|[^(https?|ftp|gopher|irc|news|telnet):])\\/\\/(.*?))([\\r\\n]|$)/is'

        if (
    preg_match_all($single$code$match)) 
        { 
            
    $return preg_replace('/<font color=(.*?)>(.*?)<\\/font>/','\\\\2',$match[0]); 
            
    $return preg_replace($single,'<font color="green">\\\\1</font>\\\\4',$return); 
            
    $code str_replace($match[0],$return,$code); 
        }

        
    $multi '/(\\/\\*(.*?)(\\*\\/|$))/is'

        if (
    preg_match_all($multi$code$match)) 
        { 
            
    $return preg_replace('/<font color=(.*?)>(.*?)<\\/font>/','\\\\2',$match[0]); 
            
    $return preg_replace($multi,'<font color="green">\\\\1</font>',$return); 
            
    $code str_replace($match[0],$return,$code); 
        }

        
    $spattern '/(\\&quot;(.*?)[^\\\\\\]&quot;)/is';

        if (
    preg_match_all($spattern$code$match)) 
        { 
            
    $return preg_replace('/<font color=(.*?)>(.*?)<\\/font>/','\\\\2',$match[0]); 
            
    $return preg_replace($spattern,'\\\\1',$return); 
            
    $code str_replace($match[0],$return,$code); 
        }

        
    $code str_replace("&quot;""\\"", $code);
        
    $code = str_replace("<br>", "", str_replace("<br />", "", $code));

        return "
    <blockquote><pre><smallfont>CSharp Code:</smallfont><hr size=1><br />" . $code . "<hr size=1></pre></blockquote>";

    Last edited by Tewl; Mar 25th, 2004 at 12:57 PM.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    care to provide any C# code so that i can see what it looks like when you are submitting your script? i don't know C#..
    Like Archer? Check out some Sterling Archer quotes.

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by kows
    care to provide any C# code so that i can see what it looks like when you are submitting your script? i don't know C#..
    I'm giving his script a wirl right now to see if I can figure it out, but all you need is a block comment, same as C++ and PHP:

    PHP Code:
    /*
       multiline block comment
    */ 
    Only the /* will be green, instead of the whole thing.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    Example:
    Code:
    private void test(object obj)
    {
      // single line comment 1
      string[] mystring = new string[10];
      /* multiline
          comment */
      // another single line comment
    }
    The pattern in the comment regexp needs to be rewritten. I am at work atm but I will start on it again when I get home.
    Last edited by Tewl; Mar 24th, 2004 at 03:51 PM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    Ok I have 2 patterns made for commenting multiline and single line comments an they appear to work fine apart from each other. Problem is I need to have one expression to do this otherwise it will not be following standards if someone adds comment code inside a comment. Heres what I have now.

    Patterns:

    PHP Code:
    $cpattern[0] = '/(\\/\\*(.*?)(\\*\\/|$))/is';
    $cpattern[1] = '/((^|[^(https?|ftp|gopher|irc|news|telnet):])\\/\\/(.*?)([\\r\\n]|$))/is';

    for (
    $x 0$x 2$x++)
    {
        if (
    preg_match_all($cpattern[$x], $code$match))
        {
            
    $return preg_replace('/<font color=(.*?)>(.*?)<\\/font>/','\\\\2',$match[0]);
            
    $return preg_replace($cpattern[$x],'<font color="green">\\\\1</font>',$return);
            
    $code str_replace($match[0],$return,$code);
            }
        }

    Last edited by Tewl; Mar 24th, 2004 at 08:27 PM.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    I forgot to parse strings.
    Last edited by Tewl; Mar 24th, 2004 at 10:05 PM.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    So far I know of 2 errors.

    1. /* in a single line comment will be read as multiline until it hits */ or end of code

    2. // or /* in a string causes color disfunction

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width