123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- use strict;
- use warnings;
- use List::Util qw/all/;
- my $symbol_name_prefix_regex = '(?:[a-z]*:)';
- my $symbol_name_regex = "($symbol_name_prefix_regex?(?:[a-zA-Z0-9_]+))";
-
- my %config;
-
- sub interpolate {
- my $string = shift;
- my %options = @_;
-
-
- $string =~ s/(?<!\\)\$$symbol_name_regex/\${$1}/gs;
-
-
- $string =~ s/\$\{$symbol_name_regex\}/\$config{\'$1\'}->{value}/g;
-
-
- $string =~ s/\\\$/\$/g;
-
-
- $string =~ s/\\qq/\\"/g;
-
- return $string;
- }
-
-
- while ($ARGV[0]) {
- if ($ARGV[0] =~ /^-([D|d])$symbol_name_regex=(.*)$/) {
- $config{$2} = { value => $3, quoted => $1 eq 'D' };
- shift(@ARGV);
- }
- else {
- last;
- }
- }
-
-
- my @condition_stack = ( 1 );
- for my $file (@ARGV) {
- open(my $fh, '<', $file) or die "Can't open $file: $!\n";
- while (my $line = <$fh>) {
- chomp($line);
-
- if ($line =~ /^\s*$symbol_name_regex=(.*)$/) {
- if (all {$_ == 1} @condition_stack) {
- my $symbol = $1;
- (my $quote_begin, my $value, my $quote_end) = $2 =~ /^(['|"])?([^'"]*)(['|"])?$/;
-
- $quote_begin = '' unless defined $quote_begin;
- $quote_end = '' unless defined $quote_end;
- die "$file:$.: Unmatched quotes in \"$line\"\n" unless $quote_begin eq $quote_end;
-
- if ($quote_begin eq '"') {
- $config{$symbol} = { value => eval('"' . interpolate($2) . '"'), quoted => 1 };
- }
- else {
- $config{$symbol} = { value => $2, quoted => $quote_begin eq '\'' };
- }
- }
- }
- elsif ($line =~ /^\s*\((.*)\)\s?{$/) {
- if (eval(interpolate($1))) {
- push(@condition_stack, 1);
- }
- else {
- push(@condition_stack, 0);
- }
- }
- elsif ($line =~ /^\s*}$/) {
- pop(@condition_stack);
- die "$file:$.: Closing non-existent block\n" unless @condition_stack;
- }
- elsif ($line =~ (/^\s*$/) || ($line =~ /^\s*#/)) {
- }
- else {
- die "$file:$.: Malformed line: \"$line\"\n";
- }
- }
- }
-
-
- for (sort(keys(%config))) {
- my $quote = $config{$_}->{quoted} ? '\'' : '';
- print("$_=$quote$config{$_}->{value}$quote\n") unless $_ =~ /^$symbol_name_prefix_regex/;
- }
|