rebendajiri
rebendajiri

Reputation: 21

Own syntax highlighter

I am about writing own simple syntax highlighter in PHP. I've done basic algorithm based on regular expressions and string replacement, but what I really don't know is way how to disable replacing keywords which are commented.

For example:

/**
 * Some class
 *
 * @property-read $foo
 */
 class Test
 {
     private $foo;

     public function __construct()
     {
     }
 }

Now my solution simply highlight defined keywords (like class or variables) but also those which are commented.

Any solution for this problem?

Upvotes: 2

Views: 209

Answers (3)

sashank
sashank

Reputation: 1541

Why not borrow lessons from how vi or vim already does this? long back I remember for some custom tag based language we developed, we wanted syntax highlighting in VI and VIM , that is when we changed few .vi sort of configuration files where we mentioned, all the meta data like which color to what kind of tag, what are tags possible etc.

Looking more into how vi or vim or any text editor does this might be more helpful!

Upvotes: 1

Victor Welling
Victor Welling

Reputation: 1897

Why not use PHP's tokenizer to do the job for you? That way, your syntax highlighter will parse the PHP code the exact same way the Zend Engine does, which is probably going to give you a lot better results than a regular expression.

Upvotes: 5

DKSan
DKSan

Reputation: 4197

You could exclude the commented lines by this logic:

if line starts with /** disable highlight
if next line starts with * do nothing and check next line
if line starts with */ reenable highlight

Just a quick guess and can be defined more precise, but should work as a logic.

Upvotes: 0

Related Questions