CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 13 123411 ... LastLast
Results 1 to 15 of 186
  1. #1
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Small utility for pasting nicer code

    Check out the full article about the Syntax Highlighter


    Hi,

    I wrote a small utility that allows you to do some basic syntax highlighting when pasting C++ code, instead of using the ugly PHP codes.

    Example :

    Plain code
    Code:
    int f(float d)
    // Checks whether the number is positive or negative
    {
      if (f > 0) {
        printf("postive\n");
        return 1;
      } else {
        printf("negative or zero\n");
        return -1;
      }
    }
    With PHP syntax highlighting:
    PHP Code:
    int f(float d)
    // Checks whether the number is positive or negative
    {
      if (
    0) {
        
    printf("postive\n");
        return 
    1;
      } else {
        
    printf("negative or zero\n");
        return -
    1;
      }

    With my small utility:
    Code:
    int f(float d)
    // Checks whether the number is positive or negative
    {
      if (f > 0) {
        printf("postive\n");
        return 1;
      } else {
        printf("negative or zero\n");
        return -1;
      }
    }
    Usage is pretty simple : You select the text you want to convert, copy it to the clipboard (for example with Ctrl-C) and then left-click on the task-bar icon for the program. Then paste it into codeguru Right-clicking will exit the program.

    You can edit the colors when you recompile the source code. I guess I should have added some configuration possibility but I was too lazy BTW, recompiling the source also allows you to paste HTML formatted C++ source code.

    New Version (check rest of thread to see the changes):
    Download it from here
    [Last updated on the 4.10.2003]

    Old Version:
    Source (30Kb)
    Executable (52Kb)

    I don't guarantee it'll work on your computer though, but it works fine for me
    Last edited by Yves M; October 4th, 2003 at 06:38 PM.

  2. #2
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673

    Thumbs up

    That's a good one!

    May I have your permission to use it on vBB forums other than CG's?

    Thanks anyway!

  3. #3
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Oh yeah, by the way it's junkware. That means as soon as you download it, it is as if you had programmed it. So feel free to use it anywhere anytime and change the code.

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Nice idea and nice job. Now you just have to convert it to a hack suitable for this board and we can save us the copy and paste...

  5. #5
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by Andreas Masur
    Nice idea and nice job. Now you just have to convert it to a hack suitable for this board and we can save us the copy and paste...
    lol TRue that would be even easier, but unfortunately I don't know much about hacking vBulletin

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Since it's only a small change, here it also for VB programmers.

    Example :
    Plain code:
    Code:
    Private Sub Form_Load()
        Dim x As Integer
        Dim b As New Collection
        'this is a comment
        
        For x = 1 To 10
            Debug.Print "H'""ello " & x
        Next
        If x = 10 Then
            Debug.Print "blah"
        ElseIf x = 11 Then
            Debug.Print x
        End If
    End Sub
    PHP:
    PHP Code:
    Private Sub Form_Load()
        
    Dim x As Integer
        Dim b 
    As New Collection
        
    'this is a comment
        
        For x = 1 To 10
            Debug.Print "H'""
    ello " & x
        Next
        If x = 10 Then
            Debug.Print "
    blah"
        ElseIf x = 11 Then
            Debug.Print x
        End If
    End Sub 
    VB Syntax highlighter:
    Code:
    Private Sub Form_Load()
        Dim x As Integer
        Dim b As New Collection
        'this is a comment
        
        For x = 1 To 10
            Debug.Print "H'""ello " & x
        Next
        If x = 10 Then
            Debug.Print "blah"
        ElseIf x = 11 Then
            Debug.Print x
        End If
    End Sub
    download it from here
    Last edited by Yves M; November 2nd, 2002 at 02:57 PM.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  7. #7
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    Just Testing Java Code

    Code:
    public class Test   {
       public test() {
          String sText = {"This is","Just a", "Test"};
          printText(sText);
       }
    
       public void printText(String sText[])   {
           for(int i=0; i<sTest.length; i++)
              System.out.println(sText[i]);         
       }
    }
    Very nice!

  8. #8
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Interesting

    Well let me explain a bit how the thing works.

    User Interface
    Nothing special there. I created a sample Win32 Hello World application, changed it so that it doesn't display the main window and added the icon to the taskbar through a (useless ) class in taskbar.h.

    The Clipboard functions are pretty straightforward too.

    The list of keywords
    This is a more interesting piece of code, although it's only for recreational use (i.e. not optimized). The class CFSM is actually not modelling a Finite State Machine (that was my initial goal, that's why it's still called that ) but rather a tree where each node can have n child nodes.

    This class is used for storing the keywords and parsing a string "quickly" to see whether it is in the set of keywords. One limitation is the fact that a keyword has to end in a non alphanumeric character and contain only alphanumeric characters. This is a bit of a problem with VB which has "supposedly" keywords with spaces.

    Special symbols
    I use a map to save a list of special symbols and their translation. This does not depend on the source language, but rather on the target format. When converting to HTML for example, you have to convert <, > to & lg and & gt (with the ; at the end) and spaces to & nbsp.

    For the vBulletin target, the only interesting rule is that tabs get converted to two spaces (really useful for VC programs)

    Colors
    There are only 3 colors used:
    • Keywords
    • Comments
    • string literal

    The color tags are defined in Highlighter.cpp, in case you want to change them.

    The Highlighter class (simple parser)
    This is where the work is being done. I use a std::string for the output, due to its simplicity. Most of the code is devoted to proper handling of string literals, line and multiline comments. Since this changes from C++ to VB, I had to rewrite this one.

    Of course, a nicer solution would be to allow a user-definable syntax for these things and then use a real finite state machine to do the actual parsing, but well, I couldn't be bothered

    Conclusion
    So what to do for a different language ? Well if it has the same (or simpler) comment and string literal structure as C++, then it works fine. The only thing you might want to do is to get a list of keywords for that language and add it (Highlighter.cpp).
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  9. #9
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Nice idea, nice job!
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  10. #10
    Join Date
    Feb 2002
    Location
    Makati City, Philippines
    Posts
    1,054
    I use this to highlight comments only. I didn't have much time lately to tinker on VB syntax color scheme. It's good to have one from you later.
    Marketing our skills - please participate in the survey and share your insights
    -

  11. #11
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Well, aio, I have to admit that the idea to write this utility first came to me when I read that post of yours

    Thanks Gabriel
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  12. #12
    Join Date
    Apr 2002
    Posts
    388
    Really great!!!
    mfg Ungi

    Music, music and VB. VB is like music: You never know how it is interpreted.

  13. #13
    Join Date
    Sep 2000
    Location
    Indianapolis
    Posts
    6,754
    Nice. If there was a "guru of the week" award, it would go to Yves this week!

    I'm going to have the description above clipped out as well as your downloads. I'll have this converted into a standard article that can be linked to on the site.

    Brad!
    -----------------------------------------------
    Brad! Jones,
    Yowza Publishing
    LotsOfSoftware, LLC

    -----------------------------------------------

  14. #14
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by Brad Jones
    Nice. If there was a "guru of the week" award, it would go to Yves this week!
    Thanks

    Right now I'm working on a version that allows you to change dynamically between the parsers and adds some configuration options. The basic new things are:

    Redesigned Parser
    The parser is now a bit smarter and can take any combination of keywords / formatting rules / colors tags / special symbols. This should allow for other languages like perl, ASP and java to get parsed correctly according to their syntax. The parser still remains simplistic though, since only one rule can be active at any time (this means no nesting of rules). But I guess that this should be sufficient for the basic job of syntax highlighting.

    Default Parsers
    The default parsers are right now :
    • C++ with rules for vBulletin
    • VB with rules for vBulletin
    • C++ with rules for HTML
    • VB with rules for HTML

    When you execute the program for the first time, the parsers get stored in the registry in HKEY_LOCAL_USER\Software\Syntax Highlighter. On subsequent executions the default parser and the others (when the user switches them) are read in from there. I have to find out whether this will prevent people who don't have write access to the registry from using the program.

    Custom Parsers
    You can define a custom parser in two ways. The first way is to use the configuration dialog to edit the keywords, rules, special symbols and color tags. This is probably cumbersome for a completely new parser, so that's why the second option comes in.

    The second option is to load a parser from a configuration file. The format will be document and it's pretty easy to understand. You can also save any installed parser to a configuration file so that you have a template to work from.

    You could also save yourself the work on the configuration file and work straight in the registry.

    User Interface
    There is now a user interface with a bit more things than just left and right-clicking on an icon. This is actually the bit that's not finished yet, as I have to fine-tune the configuration dialog (not that easy in straight Win32 ) All I can say is that you should be prepared for some weird things

    The usage will be simple though. Left-clicking on the taskbar icon will still get the current text in the clipboard, parse it and put the output back into the clipboard. Right-clicking will pop up a few buttons which let you specify one of the parsers or bring up the configuration dialog.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  15. #15
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Thanks!



    Cesare Imperiali
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Page 1 of 13 123411 ... LastLast

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

Featured