-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdexp.pl
executable file
·75 lines (64 loc) · 1.1 KB
/
cmdexp.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/perl
use strict;
use warnings;
use 5.10.0;
use File::Slurp qw/slurp/;
use Data::Dumper;
my $data = @ARGV ? slurp $ARGV[0] : slurp \*DATA;
$data =~ s/\s*#.*$//gm; # remove comments
$data =~ s/^\s*\n//gm; # remove blank lines
$data =~ s/\{\s*\n/{/g; # remove post whitespace
$data =~ s/\n\s*\}/}/g; # remove pre whitespace
print join "\n", expand($data), '';
exit;
sub expand {
my $data = shift;
return ()
unless $data;
die "syntax error" unless
$data =~ /
^
(?<PRE>[^{}]*+\n)?
(?<PRELINE>[^{}\n]*)
(?<TERM>
\{
(?<ITERM>
(?:
[^{}]*+
(?&TERM)*
[^{}]*+
)++
)
\}
)?
(?<POSTLINE>
(?:
[^{}\n]*+
(?&TERM)*
[^{}\n]*+
)*
)
\n?+
(?<POST>
(?:
[^{}]*+
(?&TERM)
[^{}]*+
)*+
[^{}]*+
)
$
/sx;
my ($preline, $postline, $pre, $post, $iterm) = @+{qw/PRELINE POSTLINE PRE POST ITERM/};
return split(/\n/, $pre // ''), ($iterm ? map {expand($preline . $_ . $postline)} expand($iterm) : $preline), expand($post);
}
__DATA__
example
line {
subline1
subline2 {
sublinepart1
sublinepart2
}
} continuation
dsdfsdf