blib/script/zuzu-highlight.pl untested

filter: Statementstmt 0.0% 0 / 184 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 72 MC/DCmcdc 0.0% 0 / 41 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 432 CC 52 SCAR 51.1
CC
52
Cov
0%
extract_lexeme
380.0
class_for_token
240.0
extract_regexp_lexeme
72.0
CRAP 165.1
1 #!/usr/bin/env perl
2  
3 use strict;
4 use utf8;
5 use warnings;
6 use FindBin qw( $Bin );
7 use lib "$Bin/../lib";
8  
9 use Zuzu::Lexer;
10 use Zuzu::Parser;
11  
12 my %WORD_OPERATOR_KW = map { $_ => 1 } qw(
13 and and? or or? xor xor? nand nand? nor nor? xnor xnor? onlyif onlyif? butnot butnot? not
14 eq ne gt ge lt le cmp eqi nei gti gei lti lei cmpi
15 mod abs sqrt floor ceil round int length uc lc typeof instanceof does can
16 union intersection subsetof supersetof equivalentof divides
17 in
18 );
19  
20 binmode STDOUT, ':encoding(UTF-8)';
21 binmode STDERR, ':encoding(UTF-8)';
22  
23 my $source = read_source(@ARGV);
24 my ( $body, $parsed, $parse_error ) = highlight($source);
25 print render_html( $body, $parsed, $parse_error );
26  
27 sub read_source {
28 my (@args) = @_;
29  
30 if ( @args > 1 ) {
31 die "usage: $0 [file.zzs]\n";
32 }
33  
34 if ( @args == 1 and $args[0] ne '-' ) {
35 my $path = $args[0];
36 open my $fh, '<:encoding(UTF-8)', $path
37 or die "failed to read '$path': $!\n";
38 local $/ = undef;
39  
40 return <$fh>;
41 }
42  
43 binmode STDIN, ':encoding(UTF-8)';
44 local $/ = undef;
45  
46 return <STDIN> // '';
47 }
48  
49 sub highlight {
50 my ($src) = @_;
51 my @out;
52 my @tokens = lex_tokens($src);
53 my @line_offsets = line_offsets($src);
54 my $cursor = 0;
55  
56 for my $idx ( 0 .. $#tokens ) {
57 my $entry = $tokens[$idx];
58 my $tok = $entry->{token};
59 my $start = offset_for_token( \@line_offsets, $entry );
60 if ( $start > $cursor ) {
61 push @out, esc_html( substr( $src, $cursor, $start - $cursor ) );
62 }
63  
64 my $lexeme = extract_lexeme( $src, $start, $tok );
65 $lexeme = substr( $src, $start, 1 ) if $lexeme eq '';
66 $cursor = $start + length($lexeme);
67  
68 my $class = class_for_token( $tok, \@tokens, $idx );
69 if ( defined $class ) {
70 push @out, span( $class, $lexeme );
71 }
72 else {
73 push @out, esc_html($lexeme);
74 }
75 }
76  
77 push @out, esc_html( substr( $src, $cursor ) ) if $cursor < length($src);
78 my ( $parsed, $parse_error ) = parse_check($src);
79  
80 return ( join( '', @out ), $parsed, $parse_error );
81 }
82  
83 sub lex_tokens {
84 my ($src) = @_;
85 my $lexer = Zuzu::Lexer->new( src => $src, filename => '<highlight>' );
86 my @tokens;
87  
88 while (1) {
89 my $tok = $lexer->next_token;
90 last if $tok->type eq 'EOF';
91 push @tokens, {
92 token => $tok,
93 line => $tok->line,
94 col => $tok->col,
95 };
96 }
97  
98 return @tokens;
99 }
100  
101 sub parse_check {
102 my ($src) = @_;
103 my $parser = Zuzu::Parser->new;
104 my $ok = eval {
105 $parser->parse( $src, '<highlight>' );
106 1;
107 };
108 if ($ok) {
109 return ( 1, '' );
110 }
111 else {
112 my $err = $@;
113 $err = "$err";
114 $err =~ s/\s+\z//;
115  
116 ( 0, $err );
117 }
118 }
119  
120 sub line_offsets {
121 my ($src) = @_;
122 my @offsets = ( 0 );
123 my $pos = 0;
124  
125 while ( $src =~ /\n/g ) {
126 $pos = pos($src);
127 push @offsets, $pos;
128 }
129  
130 return @offsets;
131 }
132  
133 sub offset_for_token {
134 my ( $line_offsets, $entry ) = @_;
135 my $line_start = $line_offsets->[ $entry->{line} - 1 ] // 0;
136  
137 return $line_start + $entry->{col} - 1;
138 }
139  
140 sub extract_lexeme {
141 my ( $src, $start, $tok ) = @_;
142 my $type = $tok->type // '';
143 my $rest = substr( $src, $start );
144  
145 if ( $type eq 'KW' ) {
146 return $tok->value // '';
147 }
148 if ( $type eq 'IDENT' ) {
149 return $tok->value // '';
150 }
151 if ( $type eq 'BOOL' ) {
152 return ( $rest =~ /\A(?:true|false|⊤|⊥)/u ) ? $& : '';
153 }
154 if ( $type eq 'NULL' ) {
155 return ( $rest =~ /\Anull/u ) ? 'null' : '';
156 }
157 if ( $type eq 'NUMBER' ) {
158 return (
159 $rest =~ /\A(?:0x[0-9A-Fa-f]+|0b[01]+|0o[0-7]+|[0-9]+(?:\.[0-9]+)?(?:E[+-]?[0-9]+)?)/u
160 ) ? $& : '';
161 }
162 if ( $type eq 'STRING' ) {
163 return ( $rest =~ /\A"""(?s:(?:\\.|(?!""").))*"""|\A"(?:\\.|[^"\\])*"?/u ) ? $& : '';
164 }
165 if ( $type eq 'BINARY_STRING' ) {
166 return ( $rest =~ /\A'''(?s:(?:\\.|(?!''').))*'''|\A'(?:\\.|[^'\\])*'?/u ) ? $& : '';
167 }
168 if ( $type eq 'TEMPLATE' ) {
169 return ( $rest =~ /\A```(?s:(?:\\.|(?!```).))*```|\A`(?:\\.|[^`\\])*`/u ) ? $& : '';
170 }
171 if ( $type eq 'REGEXP' ) {
172 return extract_regexp_lexeme($rest);
173 }
174 if ( $type eq 'EMPTY_SET' ) {
175 return '∅';
176 }
177 if ( $type eq 'OP' ) {
178 my $op = $tok->value // '';
179 return $op if $op ne '' and index( $rest, $op ) == 0;
180 }
181  
182 return '';
183 }
184  
185 sub extract_regexp_lexeme {
186 my ($rest) = @_;
187 return '' if substr( $rest, 0, 1 ) ne '/';
188  
189 my $len = length($rest);
190 my $pos = 1;
191 my $escaped = 0;
192 my $in_class = 0;
193  
194 while ( $pos < $len ) {
195 my $c = substr( $rest, $pos, 1 );
196 if ( !$escaped and !$in_class and $c eq '/' ) {
197 $pos++;
198 my $flags = '';
199 while ( $pos < $len ) {
200 my $flag = substr( $rest, $pos, 1 );
201 last if $flag ne 'i' and $flag ne 'g';
202 last if index( $flags, $flag ) >= 0;
203 $flags .= $flag;
204 $pos++;
205 }
206  
207 return substr( $rest, 0, $pos );
208 }
209 if ( !$escaped and $c eq '[' ) {
210 $in_class = 1;
211 }
212 elsif ( !$escaped and $c eq ']' ) {
213 $in_class = 0;
214 }
215 $escaped = !$escaped && $c eq "\\" ? 1 : 0;
216 $pos++;
217 }
218  
219 return '';
220 }
221  
222 sub class_for_token {
223 my ( $tok, $tokens, $idx ) = @_;
224 my $type = $tok->type // '';
225  
226 if ( $type eq 'KW' and ( $tok->value // '' ) eq 'default' ) {
227 my $next = next_significant_token( $tokens, $idx );
228 return 'keyword'
229 if defined $next and $next->type eq 'OP' and ( $next->value // '' ) eq ':';
230  
231 return 'operator';
232 }
233 if ( $type eq 'KW' and $WORD_OPERATOR_KW{ $tok->value // '' } ) {
234 return 'operator';
235 }
236 return 'keyword' if $type eq 'KW';
237 return 'boolean' if $type eq 'BOOL';
238 return 'null' if $type eq 'NULL';
239 return 'number' if $type eq 'NUMBER';
240 return 'string' if $type eq 'STRING' or $type eq 'BINARY_STRING' or $type eq 'TEMPLATE';
241 return 'regexp' if $type eq 'REGEXP';
242 return 'operator' if $type eq 'OP' or $type eq 'EMPTY_SET';
243 if ( $type eq 'IDENT' ) {
244 if ( $idx > 0 ) {
245 my $prev = $tokens->[ $idx - 1 ]{token};
246 if ( $prev->type eq 'KW' ) {
247 my $kw = $prev->value // '';
248 return 'ident-decl' if $kw =~ /\A(?:function|method|class|trait|catch)\z/u;
249 }
250 }
251  
252 return 'ident';
253 }
254  
255 return undef;
256 }
257  
258 sub next_significant_token {
259 my ( $tokens, $idx ) = @_;
260  
261 for my $look ( $idx + 1 .. $#$tokens ) {
262 my $tok = $tokens->[$look]{token};
263 my $type = $tok->type // '';
264 return $tok if $type ne 'COMMENT' and $type ne 'POD';
265 }
266  
267 return undef;
268 }
269  
270 sub span {
271 my ( $class, $text ) = @_;
272  
273 return qq{<span class="$class">} . esc_html($text) . '</span>';
274 }
275  
276 sub esc_html {
277 my ($text) = @_;
278  
279 $text =~ s/&/&amp;/g;
280 $text =~ s/</&lt;/g;
281 $text =~ s/>/&gt;/g;
282  
283 return $text;
284 }
285  
286 sub render_html {
287 my ( $body, $parsed, $parse_error ) = @_;
288 my $status_class = $parsed ? 'parse-ok' : 'parse-error';
289 my $status_msg = $parsed ? 'Parse check: ok' : 'Parse check: failed';
290 my $status_detail = $parsed ? '' : '<div class="parse-detail">' . esc_html($parse_error) . '</div>';
291 my $head = <<'HTML';
292 <!doctype html>
293 <html lang="en">
294 <head>
295 <meta charset="utf-8">
296 <meta name="viewport" content="width=device-width, initial-scale=1">
297 <title>Zuzu Syntax Highlight</title>
298 <style>
299 :root {
300 color-scheme: light dark;
301 --bg: #F4F1E8;
302 --surface: #F8F5EE;
303 --border: #D6D0C4;
304 --text: #2E2E2E;
305 --comment: #7C7670;
306 --keyword: #8A9EFF;
307 --string: #B06E8A;
308 --number: #A77755;
309 --operator: #6C78C8;
310 --ident: #2E2E2E;
311 --boolean: #F2A7A7;
312 --null: #D98989;
313 --regexp: #7BA6B0;
314 }
315  
316 @media (prefers-color-scheme: dark) {
317 :root {
318 --bg: #2E2E2E;
319 --surface: #363636;
320 --border: #4A4A4A;
321 --text: #F4F1E8;
322 --comment: #B9B2A5;
323 --keyword: #8A9EFF;
324 --string: #E0AFC4;
325 --number: #D6B28E;
326 --operator: #AAB6FF;
327 --ident: #F4F1E8;
328 --boolean: #F2A7A7;
329 --null: #FFC0C0;
330 --regexp: #9FC9D2;
331 }
332 }
333  
334 body {
335 margin: 0;
336 padding: 2rem;
337 background: var(--bg);
338 color: var(--text);
339 font-family: Inter, ui-sans-serif, system-ui, -apple-system,
340 "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
341 }
342  
343 pre.zuzu-code {
344 margin: 0;
345 padding: 1.25rem 1.5rem;
346 background: var(--surface);
347 border: 1px solid var(--border);
348 border-radius: 10px;
349 overflow-x: auto;
350 line-height: 1.55;
351 font-size: 0.95rem;
352 font-family: "JetBrains Mono", "SFMono-Regular", Menlo,
353 Consolas, "Liberation Mono", monospace;
354 }
355  
356 .comment { color: var(--comment); font-style: italic; }
357 .keyword { color: var(--keyword); font-weight: 600; }
358 .string { color: var(--string); }
359 .number { color: var(--number); }
360 .operator { color: var(--operator); }
361 .ident { color: var(--ident); }
362 .boolean { color: var(--boolean); font-weight: 600; }
363 .null { color: var(--null); font-weight: 600; }
364 .regexp { color: var(--regexp); }
365 .ident-decl { color: var(--keyword); font-weight: 700; }
366 .parse-status { margin-bottom: 0.8rem; font-size: 0.85rem; }
367 .parse-ok { color: #4C7A5C; }
368 .parse-error { color: #B85050; }
369 .parse-detail { font-family: monospace; margin-top: 0.25rem; white-space: pre-wrap; }
370 </style>
371 </head>
372 <body>
373 HTML
374 my $status = qq{<div class="parse-status $status_class">$status_msg$status_detail</div>\n};
375 my $open_pre = <<'HTML';
376 <pre class="zuzu-code">
377 HTML
378 my $tail = <<'HTML';
379 </pre>
380 </body>
381 </html>
382 HTML
383  
384 return $head . $status . $open_pre . $body . $tail;
385 }