diff options
Diffstat (limited to 'bin/MakeProjectCreator/modules/TemplateParser.pm')
-rw-r--r-- | bin/MakeProjectCreator/modules/TemplateParser.pm | 257 |
1 files changed, 123 insertions, 134 deletions
diff --git a/bin/MakeProjectCreator/modules/TemplateParser.pm b/bin/MakeProjectCreator/modules/TemplateParser.pm index 544264b3457..d34873a0b27 100644 --- a/bin/MakeProjectCreator/modules/TemplateParser.pm +++ b/bin/MakeProjectCreator/modules/TemplateParser.pm @@ -11,6 +11,7 @@ package TemplateParser; # ************************************************************ use strict; +use Cwd; use Parser; @@ -21,22 +22,11 @@ use vars qw(@ISA); # Data Section # ************************************************************ -my(%keywords) = ('if' => 1, - 'else' => 1, - 'endif' => 1, - 'noextension' => 1, - 'dirname' => 1, - 'basename' => 1, - 'basenoextension' => 1, - 'foreach' => 1, - 'forfirst' => 1, - 'fornotfirst' => 1, - 'fornotlast' => 1, - 'forlast' => 1, - 'endfor' => 1, - 'comment' => 1, - 'flag_overrides' => 1, - 'marker' => 1, +my(@keywords) = ('if', 'else', 'endif', + 'noextension', 'dirname', 'basename', 'basenoextension', + 'foreach', 'forfirst', 'fornotfirst', + 'fornotlast', 'forlast', 'endfor', + 'comment', 'flag_overrides', ); # ************************************************************ @@ -49,12 +39,10 @@ sub new { my($self) = Parser::new($class); $self->{'prjc'} = $prjc; - $self->{'ti'} = $prjc->get_template_input(); - $self->{'crlf'} = undef; $self->{'values'} = {}; $self->{'defaults'} = {}; $self->{'lines'} = []; - $self->{'built'} = ''; + $self->{'built'} = ""; $self->{'sstack'} = []; $self->{'lstack'} = []; $self->{'if_skip'} = 0; @@ -75,7 +63,7 @@ sub new { sub basename { my($self) = shift; my($file) = shift; - for(my $i = length($file) - 1; $i >= 0; --$i) { + for(my $i = length($file) - 1; $i >= 0; $i--) { my($ch) = substr($file, $i, 1); if ($ch eq '/' || $ch eq '\\') { ## The template file may use this value (<%basename_found%>) @@ -92,7 +80,7 @@ sub basename { sub dirname { my($self) = shift; my($file) = shift; - for(my $i = length($file) - 1; $i != 0; --$i) { + for(my $i = length($file) - 1; $i != 0; $i--) { my($ch) = substr($file, $i, 1); if ($ch eq '/' || $ch eq '\\') { ## The template file may use this value (<%dirname_found%>) @@ -102,7 +90,7 @@ sub dirname { } } delete $self->{'values'}->{'dirname_found'}; - return '.'; + return "."; } @@ -113,13 +101,26 @@ sub strip_line { ## Override strip_line() from Parser. ## We need to preserve leading space and ## there is no comment string in templates. - ++$self->{'line_number'}; + $self->{'line_number'}++; $line =~ s/\s+$//; return $line; } +sub is_keyword { + my($self) = shift; + my($name) = shift; + + foreach my $key (@keywords) { + if ($name eq $key) { + return 1; + } + } + return 0; +} + + ## Append the current value to the line that is being ## built. This line may be a foreach line or a general ## line without a foreach. @@ -166,9 +167,9 @@ sub adjust_value { $parts = $self->create_array($value); } - $value = ''; + $value = ""; foreach my $part (@$parts) { - if ($part ne $$val[1] && $part ne '') { + if ($part ne $$val[1] && $part ne "") { $value .= "$part "; } } @@ -192,18 +193,16 @@ sub set_current_values { ## If any value within a foreach matches the name ## of a hash table within the template input we will ## set the values of that hash table in the current scope - my($ti) = $self->{'ti'}; + my($ti) = $self->{'prjc'}->get_template_input(); if (defined $ti) { my($counter) = $self->{'foreach'}->{'count'}; - if ($counter >= 0) { - my($value) = $ti->get_value($name); - if (defined $value && UNIVERSAL::isa($value, 'HASH')) { - my(%copy) = (); - foreach my $key (keys %$value) { - $copy{$key} = $self->adjust_value($key, $$value{$key}); - } - $self->{'foreach'}->{'temp_scope'}->[$counter] = \%copy; + my($value) = $ti->get_value($name); + if (defined $value && $counter >= 0 && UNIVERSAL::isa($value, 'HASH')) { + my(%copy) = (); + foreach my $key (keys %$value) { + $copy{$key} = $self->adjust_value($key, $$value{$key}); } + $self->{'foreach'}->{'temp_scope'}->[$counter] = \%copy; } } } @@ -224,7 +223,7 @@ sub relative { $value = \@built; } else { - my($cwd) = $self->getcwd(); + my($cwd) = getcwd(); my($start) = 0; my($fixed) = 0; @@ -255,20 +254,20 @@ sub relative { if (index($cwd, $val) == 0) { my($count) = 0; - substr($cwd, 0, length($val)) = ''; + substr($cwd, 0, length($val)) = ""; while($cwd =~ /^\\/) { $cwd =~ s/^\///; } my($length) = length($cwd); - for(my $i = 0; $i < $length; ++$i) { + for(my $i = 0; $i < $length; $i++) { if (substr($cwd, $i, 1) eq '/') { - ++$count; + $count++; } } - $val = '../' x $count; + $val = "../" x $count; $val =~ s/\/$//; if ($self->{'prjc'}->convert_slashes()) { - $val = $self->slash_to_backslash($val); + $val =~ s/\//\\/g; } substr($value, $start) =~ s/\$\([^)]+\)/$val/; } @@ -291,15 +290,16 @@ sub get_value { ## First, check the temporary scope (set inside a foreach) if ($counter >= 0) { while(!defined $value && $counter >= 0) { - $value = $self->{'foreach'}->{'temp_scope'}->[$counter]->{$name}; - --$counter; + my($scope) = $self->{'foreach'}->{'temp_scope'}->[$counter]; + $value = $$scope{$name}; + $counter--; } $counter = $self->{'foreach'}->{'count'}; } if (!defined $value) { ## Next, check for a template value - my($ti) = $self->{'ti'}; + my($ti) = $self->{'prjc'}->get_template_input(); if (defined $ti) { $value = $ti->get_value($name); if (defined $value) { @@ -311,8 +311,9 @@ sub get_value { ## Next, check the inner to outer foreach ## scopes for overriding values while(!defined $value && $counter >= 0) { - $value = $self->{'foreach'}->{'scope'}->[$counter]->{$name}; - --$counter; + my($scope) = $self->{'foreach'}->{'scope'}->[$counter]; + $value = $$scope{$name}; + $counter--; } ## Then get the value from the project creator @@ -349,7 +350,7 @@ sub get_value_with_default { $value = $self->{'prjc'}->fill_value($name); if (!defined $value) { # print "DEBUG: WARNING: $name defaulting to empty string\n"; - $value = ''; + $value = ""; } } else { @@ -365,16 +366,17 @@ sub get_value_with_default { sub process_foreach { my($self) = shift; my($index) = $self->{'foreach'}->{'count'}; + my($name) = $self->{'foreach'}->{'names'}->[$index]; my($text) = $self->{'foreach'}->{'text'}->[$index]; my($status) = 1; - my($errorString) = ''; + my($errorString) = ""; my(@values) = (); - my($names) = $self->create_array($self->{'foreach'}->{'names'}->[$index]); - my($name) = undef; + my($names) = $self->create_array($name); + $name = undef; foreach my $n (@$names) { my($vals) = $self->get_value($n); - if (defined $vals && $vals ne '') { + if (defined $vals && $vals ne "") { if (!UNIVERSAL::isa($vals, 'ARRAY')) { $vals = $self->create_array($vals); } @@ -386,7 +388,7 @@ sub process_foreach { } ## Reset the text (it will be regenerated by calling parse_line - $self->{'foreach'}->{'text'}->[$index] = ''; + $self->{'foreach'}->{'text'}->[$index] = ""; if (defined $values[0]) { my($inner) = $name; @@ -398,7 +400,7 @@ sub process_foreach { $$scope{'forfirst'} = 1; $$scope{'fornotfirst'} = 0; - for(my $i = 0; $i <= $#values; ++$i) { + for(my $i = 0; $i <= $#values; $i++) { my($value) = $values[$i]; ## Set the corresponding values in the temporary scope @@ -427,9 +429,9 @@ sub process_foreach { ## Now parse the line of text, each time ## with different values - ++$self->{'foreach'}->{'processing'}; + $self->{'foreach'}->{'processing'}++; ($status, $errorString) = $self->parse_line(undef, $text); - --$self->{'foreach'}->{'processing'}; + $self->{'foreach'}->{'processing'}--; if (!$status) { last; } @@ -444,9 +446,11 @@ sub handle_end { my($self) = shift; my($name) = shift; my($status) = 1; - my($errorString) = ''; - my($end) = pop(@{$self->{'sstack'}}); - pop(@{$self->{'lstack'}}); + my($errorString) = ""; + my($sstack) = $self->{'sstack'}; + my($lstack) = $self->{'lstack'}; + my($end) = pop(@$sstack); + pop(@$lstack); if (!defined $end) { $status = 0; @@ -459,7 +463,7 @@ sub handle_end { my($index) = $self->{'foreach'}->{'count'}; ($status, $errorString) = $self->process_foreach(); if ($status) { - --$self->{'foreach'}->{'count'}; + $self->{'foreach'}->{'count'}--; $self->append_current($self->{'foreach'}->{'text'}->[$index]); } } @@ -471,7 +475,6 @@ sub handle_end { sub get_flag_overrides { my($self) = shift; my($name) = shift; - my($type) = shift; my($value) = undef; my($file) = $self->get_value($name); my($prjc) = $self->{'prjc'}; @@ -480,18 +483,12 @@ sub get_flag_overrides { foreach my $key (keys %$fo) { if ($key =~ /^$name/) { foreach my $of (keys %{$$fo{$key}}) { - my($cv) = $of; - if ($prjc->convert_slashes()) { - $cv = $prjc->slash_to_backslash($of); - } - if ($cv eq $file) { + if ($of eq $file) { foreach my $ma (keys %{$prjc->{'matching_assignments'}}) { if ($ma eq $key) { foreach my $aname (@{$prjc->{'matching_assignments'}->{$ma}}) { - if ($aname eq $type && - defined $$fo{$key}->{$of}->{$aname}) { + if (defined $$fo{$key}->{$of}->{$aname}) { $value = $$fo{$key}->{$of}->{$aname}; - last; } } last; @@ -510,20 +507,22 @@ sub get_flag_overrides { sub handle_if { my($self) = shift; my($val) = shift; + my($sstack) = $self->{'sstack'}; + my($lstack) = $self->{'lstack'}; my($name) = 'endif'; - push(@{$self->{'lstack'}}, $self->line_number() . " $val"); + push(@$lstack, $self->line_number() . " $val"); if (!$self->{'if_skip'}) { my($true) = 1; - push(@{$self->{'sstack'}}, $name); + push(@$sstack, $name); if ($val =~ /^!(.*)/) { $val = $1; $val =~ s/^\s+//; $true = 0; } - if ($val =~ /flag_overrides\(([^\)]+),\s*([^\)]+)\)/) { - $val = $self->get_flag_overrides($1, $2); + if ($val =~ /flag_overrides\(([^\)]+)\)/) { + $val = $self->get_flag_overrides($1); } else { $val = $self->get_value($val) @@ -537,19 +536,21 @@ sub handle_if { } } else { - push(@{$self->{'sstack'}}, "*$name"); + push(@$sstack, "*$name"); } } sub handle_else { - my($self) = shift; - my(@scopy) = @{$self->{'sstack'}}; + my($self) = shift; + my($sstack) = $self->{'sstack'}; + my(@scopy) = @$sstack; + my($name) = "endif"; ## This method does not take into account that ## multiple else clauses could be supplied to a single if. ## Someday, this may be fixed. - if (defined $scopy[$#scopy] && $scopy[$#scopy] eq 'endif') { + if (defined $scopy[$#scopy] && $scopy[$#scopy] eq $name) { $self->{'if_skip'} ^= 1; } } @@ -558,20 +559,22 @@ sub handle_else { sub handle_foreach { my($self) = shift; my($val) = shift; + my($sstack) = $self->{'sstack'}; + my($lstack) = $self->{'lstack'}; my($name) = 'endfor'; - push(@{$self->{'lstack'}}, $self->line_number()); + push(@$lstack, $self->line_number()); if (!$self->{'if_skip'}) { - push(@{$self->{'sstack'}}, $name); - ++$self->{'foreach'}->{'count'}; + push(@$sstack, $name); + $self->{'foreach'}->{'count'}++; my($index) = $self->{'foreach'}->{'count'}; $self->{'foreach'}->{'names'}->[$index] = $val; - $self->{'foreach'}->{'text'}->[$index] = ''; + $self->{'foreach'}->{'text'}->[$index] = ""; $self->{'foreach'}->{'scope'}->[$index] = {}; } else { - push(@{$self->{'sstack'}}, "*$name"); + push(@$sstack, "*$name"); } } @@ -639,25 +642,12 @@ sub handle_basenoextension { sub handle_flag_overrides { my($self) = shift; my($name) = shift; - my($type) = ''; - - ($name, $type) = split(/,\s*/, $name); - - if (!$self->{'if_skip'}) { - my($value) = $self->get_flag_overrides($name, $type); - if (defined $value) { - $self->append_current($value); - } - } -} - - -sub handle_marker { - my($self) = shift; - my($name) = shift; + my($file) = $self->get_value($name); + my($prjc) = $self->{'prjc'}; + my($fo) = $prjc->{'flag_overrides'}; if (!$self->{'if_skip'}) { - my($value) = $self->{'prjc'}->get_verbatim($name); + my($value) = $self->get_flag_overrides($name); if (defined $value) { $self->append_current($value); } @@ -675,14 +665,14 @@ sub split_name_value { my($name) = undef; my($val) = undef; - for(my $i = 0; $i < $length; ++$i) { + for(my $i = 0; $i < $length; $i++) { my($ch) = substr($line, $i, 1); if (!defined $name && $ch eq '(') { $name = substr($line, 0, $i); - $val = ''; + $val = ""; } elsif (!defined $name && $ch eq '%') { - if (substr($line, $i + 1, 1) eq '>') { + if (substr($line, $i + 1, 1) eq ">") { $name = substr($line, 0, $i); last; } @@ -691,7 +681,7 @@ sub split_name_value { $val .= $ch; } elsif (defined $val && $ch eq ')') { - if (substr($line, $i + 1, 2) eq '%>') { + if (substr($line, $i + 1, 2) eq "%>") { last; } else { @@ -699,7 +689,6 @@ sub split_name_value { } } } - return $name, $val; } @@ -709,11 +698,11 @@ sub process_name { my($line) = shift; my($length) = 0; my($status) = 1; - my($errorString) = ''; + my($errorString) = ""; - if ($line eq '') { + if ($line eq "") { } - elsif ($line =~ /^(\w+)(\(([^\)]+|\".*\"|flag_overrides\([^\)]+,\s*[^\)]+\))\))?%>/) { + elsif ($line =~ /^(\w+)(\(([^\)]+|\".*\"|flag_overrides\([^\)]+\))\))?%>/) { my($name, $val) = $self->split_name_value($line); $length += length($name); @@ -721,7 +710,7 @@ sub process_name { $length += length($val) + 2; } - if (defined $keywords{$name}) { + if ($self->is_keyword($name)) { if ($name eq 'endif' || $name eq 'endfor') { ($status, $errorString) = $self->handle_end($name); } @@ -744,9 +733,6 @@ sub process_name { elsif ($name eq 'flag_overrides') { $self->handle_flag_overrides($val); } - elsif ($name eq 'marker') { - $self->handle_marker($val); - } elsif ($name eq 'noextension') { $self->handle_noextension($val); } @@ -774,9 +760,9 @@ sub process_name { else { my($error) = $line; my($length) = length($line); - for(my $i = 0; $i < $length; ++$i) { + for(my $i = 0; $i < $length; $i++) { my($part) = substr($line, $i, 2); - if ($part eq '%>') { + if ($part eq "%>") { $error = substr($line, 0, $i + 2); last; } @@ -793,9 +779,6 @@ sub collect_data { my($self) = shift; my($prjc) = $self->{'prjc'}; - ## Save crlf so we don't have to keep going back to the prjc - $self->{'crlf'} = $prjc->crlf(); - ## Collect the components into {'values'} somehow foreach my $key (keys %{$prjc->{'valid_components'}}) { my(@list) = $prjc->get_component_list($key); @@ -822,18 +805,23 @@ sub collect_data { sub is_only_keyword { - my($self) = shift; - my($line) = shift; + my($self) = shift; + my($line) = shift; + my($status) = 0; ## Does the line contain only a keyword? if ($line =~ /^<%(.*)%>$/) { my($part) = $1; if ($part !~ /%>/) { - $part =~ s/\(.*//; - return (defined $keywords{$part} ? 1 : 0); + foreach my $keyword (@keywords) { + if ($part =~ /^$keyword/) { + $status = 1; + last; + } + } } } - return 0; + return $status; } @@ -842,12 +830,12 @@ sub parse_line { my($ih) = shift; my($line) = shift; my($status) = 1; - my($errorString) = ''; + my($errorString) = ""; my($length) = length($line); my($name) = 0; - my($crlf) = $self->{'crlf'}; + my($crlf) = $self->{'prjc'}->crlf(); my($clen) = length($crlf); - my($startempty) = ($line eq '' ? 1 : 0); + my($startempty) = ($line eq "" ? 1 : 0); my($append_name) = 0; ## If processing a foreach or the line only @@ -860,17 +848,17 @@ sub parse_line { } if ($self->{'foreach'}->{'count'} < 0) { - $self->{'built'} = ''; + $self->{'built'} = ""; } - for(my $i = 0; $i < $length; ++$i) { + for(my $i = 0; $i < $length; $i++) { my($part) = substr($line, $i, 2); - if ($part eq '<%') { - ++$i; + if ($part eq "<%") { + $i++; $name = 1; } - elsif ($part eq '%>') { - ++$i; + elsif ($part eq "%>") { + $i++; $name = 0; if ($append_name) { $append_name = 0; @@ -880,12 +868,12 @@ sub parse_line { } } elsif ($name) { - my($substr) = substr($line, $i); + my($substr) = substr($line, $i); my($efcheck) = ($substr =~ /^endfor\%\>/); my($focheck) = ($substr =~ /^foreach\(/); if ($focheck && $self->{'foreach'}->{'count'} >= 0) { - ++$self->{'foreach'}->{'nested'}; + $self->{'foreach'}->{'nested'}++; } if ($self->{'foreach'}->{'count'} < 0 || @@ -910,13 +898,13 @@ sub parse_line { else { $name = 0; if (!$self->{'if_skip'}) { - $self->append_current('<%' . substr($line, $i, 1)); + $self->append_current("<%" . substr($line, $i, 1)); $append_name = 1; } } if ($efcheck && $self->{'foreach'}->{'nested'} > 0) { - --$self->{'foreach'}->{'nested'}; + $self->{'foreach'}->{'nested'}--; } } else { @@ -930,8 +918,9 @@ sub parse_line { ## If the line started out empty and we're not ## skipping from the start or the built up line is not empty if ($startempty || - ($self->{'built'} ne $crlf && $self->{'built'} ne '')) { - push(@{$self->{'lines'}}, $self->{'built'}); + ($self->{'built'} ne $crlf && $self->{'built'} ne "")) { + my($lines) = $self->{'lines'}; + push(@$lines, $self->{'built'}); } } |