summaryrefslogtreecommitdiff
path: root/bin/MakeProjectCreator/modules/TemplateInputReader.pm
blob: 40f663ca253a32896a0df2cfea7bcca04cce43a2 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package TemplateInputReader;

# ************************************************************
# Description   : Reads the template input and stores the values
# Author        : Chad Elliott
# Create Date   : 5/16/2002
# ************************************************************

# ************************************************************
# Pragmas
# ************************************************************

use strict;

use Parser;

use vars qw(@ISA);
@ISA = qw(Parser);

# ************************************************************
# Data Section
# ************************************************************

my($mpt)  = 'mpt';

# ************************************************************
# Subroutine Section
# ************************************************************

sub new {
  my($class) = shift;
  my($inc)   = shift;
  my($self)  = Parser::new($class, $inc);

  $self->{'values'}  = {};
  $self->{'cindex'}  = 0;
  $self->{'current'} = [ $self->{'values'} ];

  return $self;
}


sub parse_line {
  my($self)        = shift;
  my($ih)          = shift;
  my($line)        = shift;
  my($status)      = 1;
  my($errorString) = '';
  my($current)     = $self->{'current'};

  if ($line eq '') {
  }
  elsif ($line =~ /^([\w\s]+)\s*{$/) {
    ## Entering a new scope
    my($name) = $1;
    $name =~ s/\s+$//;
    if (!defined $$current[$self->{'cindex'}]->{$name}) {
      $$current[$self->{'cindex'}]->{$name} = {};
    }
    push(@$current, $$current[$self->{'cindex'}]->{$name});
    $self->{'cindex'}++;
  }
  elsif ($line =~ /^}$/) {
    if ($self->{'cindex'} > 0) {
      pop(@$current);
      $self->{'cindex'}--;
    }
    else {
      $status = 0;
      $errorString = 'ERROR: Unmatched curly brace';
    }
  }
  elsif ($line =~ /^(\w+)\s*(\+=|=)\s*(.*)?/) {
    my($name)  = $1;
    my($op)    = $2;
    my($value) = $3;

    if (defined $value) {
      $value = $self->create_array($value);
    }
    else {
      $value = '';
    }

    if ($op eq '+=') {
      my($ref) = $$current[$self->{'cindex'}]->{$name};
      if (defined $ref) {
        if (UNIVERSAL::isa($ref, 'ARRAY')) {
          if (UNIVERSAL::isa($value, 'ARRAY')) {
            push(@$ref, @$value);
          }
          else {
            push(@$ref, $value);
          }
        }
        else {
          if (UNIVERSAL::isa($value, 'ARRAY')) {
            $$current[$self->{'cindex'}]->{$name} .= " @$value";
          }
          else {
            $$current[$self->{'cindex'}]->{$name} .= $value;
          }
        }
      }
      else {
        $$current[$self->{'cindex'}]->{$name} = $value;
      }
    }
    else {
      if (!$$current[$self->{'cindex'}]->{$name}) {
        $$current[$self->{'cindex'}]->{$name} = $value;
      }
      else {
        $status = 0;
        $errorString = "ERROR: Redifinition of '$name'";
      }
    }
  }
  elsif ($line =~ /^conditional_include\s+"([\w\s\-\+\/\\\.]+)"$/) {
    my($file) = $self->search_include_path("$1.$mpt");
    if (defined $file) {
      my($ol) = $self->{'line_number'};
      ($status, $errorString) = $self->read_file($file);
      $self->{'line_number'} = $ol;
    }
  }
  else {
    $status = 0;
    $errorString = "ERROR: Unrecognized line: $line";
  }

  return $status, $errorString;
}


sub get_value {
  my($self) = shift;
  my($tag)  = shift;
  return $self->{'values'}->{$tag};
}


1;