G'day All,
I use a scripting language, called NWScript, for some CRPG development that I do from time to time.
I wrote the following Perl script to syntax-highlight NWScript code for HTML rendering:
#!/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 statem
+ent
datatype comment string integer float prag
+ma};
my $re = qr{
(?>
(?<white_space>
\s+
)
|
(?<comment>
(?>
\/\* (?: . (?! \*\/ ) )*+ (?: . (?= \*\/ ) )?+ \*\/
|
\/\/ [^\n]* $
)
)
|
(?<pragma>
(?>
[#]include \s+ " \w+ " \s* $
|
[#]define \s+ \w+ \s+ \w+ \s* $
)
)
|
(?<string>
" (?: [^"\\]++ | \\. )*+ "
)
|
(?<float>
\b \d+ \. \d+ f? \b
)
|
(?<integer>
\b \d+ \b
)
|
(?<constant>
\b [A-Z0-9_]+ \b
)
|
(?<datatype>
\b (?>
action | const | effect | event | float | int
| itemproperty | location | object | string
| struct \s+ \w+ | talent | vector | void
) \b
)
|
(?<statement>
\b (?>
break | continue | do | for | if | else
| return | switch | case | default | while
) \b
)
|
(?<function>
\b [A-Za-z_] \w* (?= \s*\( )
)
|
(?<variable>
\b [A-Za-z_] \w* \b
)
|
(?<operator>
(?>
\>\>\>\= | \>\>\> | \>\>\= | \<\<\= | \>\> | \<\< | \+
+\+ | \-\-
| \&\= | \|\= | \^\= | \*\= | \/\= | \%\= | \+\= | \-\
+=
| \=\= | \!\= | \<\= | \>\= | \&\& | \|\|
| \< | \> | \! | \& | \| | \^ | \~ | \* | \/ | \% | \+
+ | \- | \=
| \? | \: | \; | \. | \{ | \} | \( | \) | \, | \@
)
)
|
(?<remainder>
.*?
)
)
}msx;
my $init_code = do { local $/; <> };
say '<pre class="syntax-highlight">';
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 '<span class="', $highlight_capture, '">',
chars_to_ents($+{$highlight_capture}), '</span>';
next MATCH;
}
}
}
say '</pre>';
exit;
NWScript uses a C-like syntax.
I'm aware that a few monks use NWScript; however, I'd guess most don't and have probably never heard of it.
So, purely to provide an example that's looks a little more familiar to most, here's a slightly fudged (just the #include pragma) hello.c:
/* hello.c */
#include "stdio"
main()
{
printf("hello, world\n");
}
And here's the output after running that through my script:
<pre class="syntax-highlight">
<span class="comment">/* hello.c */</span>
<span class="pragma">#include "stdio"
</span>
<span class="function">main</span><span class="operator">(</span><span
+ class="operator">)</span>
<span class="operator">{</span>
<span class="function">printf</span><span class="operator">(</span
+><span class="string">"hello, world\n"</span><span class="operator">)
+</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
For anyone wishing to use this script, here's the CSS I use (in the Spoiler):