Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C++ syntax highlight with Regular Expressions

Posted on 2006-04-16
6
Medium Priority
?
286 Views
Last Modified: 2008-03-10
I want to colorize a C++ code by highlighting keywords in blue before outputting it inside HTML page. The problem is that if two keywords are one just after another, the followed word is not colorized (ex: in "const int i = 0;" just "const" will be in blue). So what I must do to show to the preg_replace to return to one character back after a match ?

The code I use is :

<?php
[...]
$delimiters = "\\r|\\n|\\t|\\x20|\\x21|\\x25|\\x40|\\x7E|\\x21|\\x22|\\x25|\\x26|\\x27|\\x28|\\x29|\\x2A|\\x2B|\\x2C|\\x2D|\\x2E|\\x2F|\\x3A|\\x3B|\\x3C|\\x3D|\\x3E|\\x3F|\\x40|\\x5B|\\x5C|\\x5D|\\x5E|\\x7B|\\x7C|\\x7D|\\x7E";
$from = "\"(" . $delimiters . ")(auto|int|bool|short|long|void|...|#ifndef|#undef)(" . $delimiters . ")\"";
$to = "$1<font color=blue>$2</font>$3";
$new_code = preg_replace($from, $to, $new_code);
[...]
?>
0
Comment
Question by:MainMa
  • 3
  • 3
6 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 240 total points
ID: 16466230
Isn't it easier to tokenize the string and then highlight words from your functionlist accordingly?

-r-
0
 

Author Comment

by:MainMa
ID: 16468236
Sorry, what do you mean exactly by tokenize the string ?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16468299
Tokenizing is splitting a large body of text into tokens,ie:
a = 1 + 5 + pow(1,2)
tokenized:
IDENTIFIER, EQUALS, NUM, ADD, IDENTIFIER, ANCHOR_LEFT, NUM, COMMA, NUM, ANCHOR_RIGHT.

Then you can give all NUM tokens a certain color, decide to filter all IDENTIFIER tokens with your function list and give the function one color, and other items another color.

With some work you could have this parser colorize the whole code in a single pass.

-r-
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MainMa
ID: 16468356
Ok. I see.
It seems to me quite difficult to realize the task this way, but I will try to do it nextly...

Also, I've just tried to colorize the script my way, but by adding "??" at the end:

<?php
[...]
$from = "\"(" . $delimiters . ")(auto|int|bool|short|long|void|...|#ifndef|#undef)(" . $delimiters . ")??\"";
[...]
?>

Seems that it works, but will it slow down the process ? I will use the function on a large amounts of code, so the speed is quite important...
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16468390
You should be fine with a single questionmark I think.

-r-
0
 

Author Comment

by:MainMa
ID: 16468417
You should be fine with a single questionmark I think.>

No, it doesn't work. With a single questionmark, the effect is the same as without questionmark at all... :(
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question