summaryrefslogtreecommitdiff
path: root/lib/sbin/parameter_parser.pl
blob: adb6dc5d20d50734e677ce2cebe4e1ebffa727f4 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#! /usr/bin/perl

#%implementations = ("flesh", "flesh", "test1", "test1", "test2", "test2");

#%parameter_database = create_parameter_database(%implementations);

#&print_parameter_database(%parameter_database);

#/*@@
#  @routine    create_parameter_database
#  @date       Wed Sep 16 11:45:18 1998
#  @author     Tom Goodale
#  @desc 
#  Creates a database of all the parameters
#  @enddesc 
#  @calls     
#  @calledby   
#  @history 
#
#  @endhistory 
#@@*/

sub create_parameter_database
{
    local(%implementations) = @_;
    local($imp, @indata);
    local(@new_parameter_data);
    local(@parameter_data);

#  Loop through each implementation's parameter file.
    foreach $imp (keys %implementations)
    {
#       Read the data
	@indata = read_file("$implementations{$imp}/param.ccl");
	
#       Get the parameters from it
	@new_parameter_data = &parse_param_ccl($imp, @indata);

#       Add the parameters to the master parameter database
	push (@parameter_data, @new_parameter_data);

    }

    @parameter_data = &cross_index_parameters(scalar(keys %implementations), (keys %implementations), @parameter_data);

    return @parameter_data;
}

sub cross_index_parameters
{
  local($n_implementations, @indata) = @_;
  local(@implementations);
  local(%parameter_database);
  local(@module_file);
  local($line);
  local(@data);

  @implementations = @indata[0..$n_implementations-1];
  %parameter_database = @indata[$n_implementations..$#indata];

  foreach $imp (@implementations)
  {
    foreach $parameter (split(/ /, $parameter_database{"\U$imp\E PUBLIC variables"}))
    {
      if($public_parameters{"\U$parameter\E"})
      {
	print STDERR "Duplicate public parameter $parameter\n";
	print STDERR "Parameter defined in $imp and in " . 
	  $public_parameters{"\Uparameter\E"};
	die("****Fatal error***");
      }
      else
      {
	$public_parameters{"\Uparameter\E"} = "$imp";
	
	$parameter_database{"PUBLIC PARAMETERS"} .= "$imp\::$parameter ";
      }
    }
  }

  return %parameter_database;
}




#/*@@
#  @routine    read_file
#  @date       Wed Sep 16 11:54:38 1998
#  @author     Tom Goodale
#  @desc 
#  Reads a file deleting comments and blank lines. 
#  @enddesc 
#  @calls     
#  @calledby   
#  @history 
# 
#  @endhistory 
#@@*/

sub read_file
{
    local($file) = @_;
    local(@indata);

    open(IN, "<$file") || die("Can't open $file\n");

    while(<IN>)
    {
	$_ =~ s/\#.*//;
	
	next if(m/^\s+$/);
	
	chop;
	
	push(@indata, $_);
    }

    close IN;

    return @indata;
}


#/*@@
#  @routine    parse_param_ccl
#  @date       Wed Sep 16 11:55:33 1998
#  @author     Tom Goodale
#  @desc 
#  Parses a param.ccl file and generates a database of the values.
#  @enddesc 
#  @calls     
#  @calledby   
#  @history 
#
#  @endhistory 
#@@*/

sub parse_param_ccl
{
    local($implementation, @data) = @_;
    local($linenum, $line, $block, $type, $variable, $description, $nerrors);
    local($current_friend, $new_ranges, $new_desc);
    local($data, %parameter_db);
    local(%friends);
    local(%defined_parameters);


#   The default block is private.
    $block = "PRIVATE";

    for($linenum = 0; $linenum < @data; $linenum++)
    {
	$line = $data[$linenum];

#       Parse the line
	if($line =~ m/(PUBLIC|PROTECTED|PRIVATE|FRIEND)\s*:(.*)/i)
	{
#           It's a new block.
	    $block = "\U$1\E";
	    if($block eq "FRIEND")
	    {
		$current_friend = $2;
		$current_friend =~ s:\s::;

#               It's a friend block.
		$block .= " \U$current_friend\E";
#               Remember this friend, but make the memory unique.
		$friends{"\U$current_friend\E"} = 1;
	    }
	}
	elsif($line =~ m:(EXTENDS )?\s*(INTEGER|REAL|KEYWORD|STRING)\s*([a-zA-Z]+[a-zA-Z0-9_]*) \s*(\"[^\"]*\"):i)
	{

#           This is a parameter definition.
	    $type = "\U$2\E";
	    $variable = $3;
	    $description = $4;

	    if($defined_parameters{"\U$variable\E"})
	    {
	      print STDERR "Duplicate parameter $variable in implementation $implementation\n";
	      print STDERR "Ignoring second definition.\n";
		$nerrors++;
		$linenum++ until ($data[$linenum] =~ m:\}:);
	    }
	    elsif($1 =~ m:EXTENDS:i && $block ne "FRIEND")
	    {
#               Can only extend a friend variable.
		print STDERR "Parse error at line $linenum\n";
		$nerrors++;
		$linenum++ until ($data[$linenum] =~ m:\}:);
	    }
	    elsif(! $data[$linenum+1] =~ m:^\s*\{\s*$:)
	    {
#               Since the data should have no blank lines, the next
#               line should have { on it.
		print STDERR "Parse error at line $linenum\n";
		$nerrors++;
#               Move past the end of this block.
		$linenum++ until ($data[$linenum] =~ m:\}:);
	    }
	    else
	    {
#               Move past {
		$linenum++;
		$linenum++;

#               Store data about this variable.
		$defined_parameters{"\U$variable\E"} = 1;

		$parameter_db{"\U$implementation $block\E variables"} .= $variable." ";
		$parameter_db{"\U$implementation $variable\E type"} = $type;
		$parameter_db{"\U$implementation $variable\E description"} = $description;
		$parameter_db{"\U$implementation $variable\E ranges"} = 0;

#               Parse the allowed values and their descriptions.
		while(($new_ranges, $new_desc) = $data[$linenum] =~ m/(.*)::(.*)/)
		{
		    $parameter_db{"\U$implementation $variable\E ranges"}++;
		    $parameter_db{"\U$implementation $variable\E range $parameter_db{\"\U$implementation $variable\E ranges\"} range"} = $new_ranges;
		    $parameter_db{"\U$implementation $variable\E range $parameter_db{\"\U$implementation $variable\E ranges\"} description"} = $new_desc;
		    $linenum++;
		}
		if($block !~ m:FRIEND:)
		{
		    if($data[$linenum] =~ m:\s*\}\s*(.+):)
		    {
			$parameter_db{"\U$implementation $variable\E default"} = $1;
		    }
		    else
		    {
			print STDERR "Unable to find default for $variable\n";
			$nerrors++;
		    }		
		}
	    }
	}
	else
	{
	    if($line =~ m:\{:)
	    {
		print STDERR "...Skipping block with missing keyword....\n";
		$linenum++ until ($data[$linenum] =~ m:\}:);
	    }
	    else
	    {
		print STDERR "Unknown line $line!!!\n";
	    }
	}
    }

    $parameter_db{"\U$implementation\E FRIEND implementations"} = join(" ", keys %friends);

    return %parameter_db;
}

#/*@@
#  @routine    print_parameter_database
#  @date       Wed Sep 16 14:58:52 1998
#  @author     Tom Goodale
#  @desc 
#  Prints out a parameter database.
#  @enddesc 
#  @calls     
#  @calledby   
#  @history 
#
#  @endhistory 
#@@*/
sub print_parameter_database
{
    local(%parameter_database) = @_;
    local($field);

    foreach $field ( sort keys %parameter_database ){
	print "$field has value $parameter_database{$field}\n";
    }
}

1;