#!/usr/bin/env perl use 5.014; use warnings; { my %entity_for = qw{& & < < > >}; sub chars_to_ents { $_[0] =~ s/([&<>])/$entity_for{$1}/gr } } my @plain_captures = qw{white_space remainder}; my @highlight_captures = qw{operator variable function constant statement datatype comment string integer float pragma}; my $re = qr{ (?> (? \s+ ) | (? (?> \/\* (?: . (?! \*\/ ) )*+ (?: . (?= \*\/ ) )?+ \*\/ | \/\/ [^\n]* $ ) ) | (? (?> [#]include \s+ " \w+ " \s* $ | [#]define \s+ \w+ \s+ \w+ \s* $ ) ) | (? " (?: [^"\\]++ | \\. )*+ " ) | (? \b \d+ \. \d+ f? \b ) | (? \b \d+ \b ) | (? \b [A-Z0-9_]+ \b ) | (? \b (?> action | const | effect | event | float | int | itemproperty | location | object | string | struct \s+ \w+ | talent | vector | void ) \b ) | (? \b (?> break | continue | do | for | if | else | return | switch | case | default | while ) \b ) | (? \b [A-Za-z_] \w* (?= \s*\( ) ) | (? \b [A-Za-z_] \w* \b ) | (? (?> \>\>\>\= | \>\>\> | \>\>\= | \<\<\= | \>\> | \<\< | \+\+ | \-\- | \&\= | \|\= | \^\= | \*\= | \/\= | \%\= | \+\= | \-\= | \=\= | \!\= | \<\= | \>\= | \&\& | \|\| | \< | \> | \! | \& | \| | \^ | \~ | \* | \/ | \% | \+ | \- | \= | \? | \: | \; | \. | \{ | \} | \( | \) | \, | \@ ) ) | (? .*? ) ) }msx; my $init_code = do { local $/; <> }; say '
';

MATCH:
while ($init_code =~ /$re/g) {
    for my $plain_capture (@plain_captures) {
        if (exists $+{$plain_capture}) {
            print $+{$plain_capture};
            next MATCH;
        }
    }

    for my $highlight_capture (@highlight_captures) {
        if (exists $+{$highlight_capture}) {
            print '',
                    chars_to_ents($+{$highlight_capture}), '';
            next MATCH;
        }
    }
}

say '
'; exit;