|
| 1 | +#!/usr/bin/end perl |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | + |
| 5 | +# This script is used to preprocess the perl6 syntax file to reduce |
| 6 | +# repetition of common patterns. VimL requires cumbersome string |
| 7 | +# concatenation and eval to reuse patterns, which makes for a lot of |
| 8 | +# boilerplate code and a less readable regexes. So instead we preprocess |
| 9 | +# the file to keep the original source more readable and easier to edit. |
| 10 | +# |
| 11 | +# A "macro" is defined by including lines in the source file which start |
| 12 | +# with the VimL comment character (") followed by an identifier name |
| 13 | +# surrounded by two sets of at-signs (@@FOO@@). This is followed by |
| 14 | +# whitespace and then a double or single-quoted string containing the |
| 15 | +# replacement text. Earlier macros can be interpolated into later macros. |
| 16 | + |
| 17 | +my $preproc = '@@'; |
| 18 | +my %replacements; |
| 19 | +while (my $line = <>) { |
| 20 | + my $check_line = $line; |
| 21 | + while ($check_line =~ /$preproc/) { |
| 22 | + $check_line = substr($check_line, $+[0]); |
| 23 | + if ($check_line !~ /^\w+$preproc/) { |
| 24 | + warn "Missing '$preproc' on line $.\n"; |
| 25 | + } |
| 26 | + else { |
| 27 | + $check_line = substr($check_line, $+[0]); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + if ($line =~ /^"\s*$preproc(\w+)$preproc\s+ (?:"(.+)"|'(.*)')\s*$/) { |
| 32 | + my ($name, $content) = ($1, $2); |
| 33 | + $content =~ s/$preproc(\w+)$preproc/ |
| 34 | + die "Replacement for $preproc$1$preproc not found at line $.\n" if !$replacements{$1}; |
| 35 | + $replacements{$1} |
| 36 | + /eg; |
| 37 | + $replacements{$name} = $content; |
| 38 | + } |
| 39 | + else { |
| 40 | + $line =~ s/$preproc(\w+)$preproc/ |
| 41 | + die "Replacement for $preproc$1$preproc not found at line $.\n" if !$replacements{$1}; |
| 42 | + $replacements{$1} |
| 43 | + /eg; |
| 44 | + } |
| 45 | + print $line; |
| 46 | +} |
0 commit comments