summaryrefslogtreecommitdiff
path: root/lib/sbin/parameter_parser.pl
blob: 63de8736d0f7a81a9ad59b63509df3756c041620 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#! /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
{
  my(%thorns) = @_;
  my($thorn, @indata);
  my(@new_parameter_data);
  my(@parameter_data);
  
  #  Loop through each implementation's parameter file.
  foreach $thorn (keys %thorns)
  {
    print "   $thorn\n";
    #       Read the data
    @indata = &read_file("$thorns{$thorn}/param.ccl");
    
    #       Get the parameters from it
    @new_parameter_data = &parse_param_ccl($thorn, @indata);

    &PrintParameterStatistics($thorn, @new_parameter_data);

    #       Add the parameters to the master parameter database
    push (@parameter_data, @new_parameter_data);
    
  }
  
  @parameter_data = &cross_index_parameters(scalar(keys %thorns), (keys %thorns), @parameter_data);
  
  return @parameter_data;
}

sub cross_index_parameters
{
  my($n_thorns, @indata) = @_;
  my(@thorns);
  my(%parameter_database);
  my(@module_file);
  my($line);
  my(@data);
  my($thorn);
  
  @thorns = @indata[0..$n_thorns-1];
  %parameter_database = @indata[$n_thorns..$#indata];
  
  $parameter_database{"GLOBAL PARAMETERS"} = "";
  
  foreach $thorn (@thorns)
  {
    foreach $parameter (split(/ /, $parameter_database{"\U$thorn\E GLOBAL variables"}))
    {
      if($public_parameters{"\U$parameter\E"})
      {
	  $message = "Duplicate public parameter $parameter, defined in $imp and ".$public_parameters{"\Uparameter\E"};
	  &CST_error(0,$message,__LINE__,__FILE__);
      }
      else
      {
	$public_parameters{"\Uparameter\E"} = "$thorn";
	
	$parameter_database{"GLOBAL PARAMETERS"} .= "$thorn\::$parameter ";
      }
    }
  }
  
  return %parameter_database;
}



#/*@@
#  @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
{
  my($thorn, @data) = @_;
  my($line_number, $line, $block, $type, $variable, $description);
  my($current_friend, $new_ranges, $new_desc);
  my($data, %parameter_db);
  my(%friends);
  my(%defined_parameters);
  
  
  #   The default block is private.
  $block = "PRIVATE";
  
  # Initialise, to prevent perl -w from complaining.
  $parameter_db{"\U$thorn PRIVATE\E variables"} = "";

  for($line_number = 0; $line_number < @data; $line_number++)
  {
    $line = $data[$line_number];
    
    #       Parse the line
    if($line =~ m/(GLOBAL|RESTRICTED|PRIVATE|SHARES)\s*:(.*)/i)
    {
      #           It's a new block.
      $block = "\U$1\E";
      
      if($block eq "SHARES")
      {
	$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;
      }
      
      # Do some initialisation to prevent perl -w from complaining.
      if(!$parameter_db{"\U$thorn $block\E variables"})
      {
	$parameter_db{"\U$thorn $block\E variables"} = "";
      }
    }
    elsif($line =~ m:(EXTENDS |USES )?\s*(?\:CCTK_)?(INT|REAL|LOGICAL|BOOLEAN|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;

      # Logical is depricated
      if ($type =~ /LOGICAL/i)
      {
	$type =~ s/LOGICAL/BOOLEAN/gi;
      }

      if($defined_parameters{"\U$variable\E"})
      {

	$message = "Duplicate parameter $variable in thorn $thorn. Ignoring second definition";
	&CST_error(1,$message,__LINE__,__FILE__);

	$line_number++ until ($data[$line_number] =~ m:\}:);
      }
      elsif($1 && $1 =~ m:(EXTENDS|USES):i && $block !~ m:SHARES\s*\S:)
      {
	# Can only extend a friend variable.
	$message =  "Parse error in $thorn/param.ccl";
	&CST_error(0,$message,__LINE__,__FILE__);
	$line_number++ until ($data[$line_number] =~ m:\}:);
      }
      elsif($data[$line_number+1] !~ m:^\s*\{\s*$:)

      {
	# Since the data should have no blank lines, the next
	# line should have { on it.
	$message = "Parse error in $thorn/param.ccl - missing \"{\" in definition of parameter \"$variable\"";
	&CST_error(0,$message,__LINE__,__FILE__);
	# Move past the end of this block.
	$line_number++ until ($data[$line_number] =~ m:\}:);
      }
      else
      {
	# Move past {
	$line_number++;
	$line_number++;
	
	# Store data about this variable.
	$defined_parameters{"\U$variable\E"} = 1;
	
	$parameter_db{"\U$thorn $block\E variables"} .= $variable." ";
	$parameter_db{"\U$thorn $variable\E type"} = $type;
	$parameter_db{"\U$thorn $variable\E description"} = $description;
	$parameter_db{"\U$thorn $variable\E ranges"} = 0;
	
	# Parse the allowed values and their descriptions.
        # The (optional) description is seperated by ::
	while($data[$line_number] !~ m:\s*\}:)
	{
	    ($new_ranges, $delim, $new_desc) = $data[$line_number] =~ m/(.*)(::)(.*)/;
	    # Increment the number of ranges found (ranges)
	    $parameter_db{"\U$thorn $variable\E ranges"}++;
	    # Strip out any spaces in the range for a numeric parameter.
	    if($type =~ m:INT|REAL:)
	    {
		$new_ranges =~ s/[ \t]+/ /g;
	    }
	    $parameter_db{"\U$thorn $variable\E range $parameter_db{\"\U$thorn $variable\E ranges\"} range"} = $new_ranges;
	    # Give a warning if no description has been given
	    if(! $delim)
	    {
		$message = "Missing description of range '$new_ranges' for parameter $thorn\::$variable";
		&CST_error(1,$message,__LINE__,__FILE__);
	    }
	    $parameter_db{"\U$thorn $variable\E range $parameter_db{\"\U$thorn $variable\E ranges\"} description"} = $new_desc;
	    $line_number++;
	}

        # Give a warning if no range was given and it was needed
        if ($parameter_db{"\U$thorn $variable\E ranges"}==0 && $type =~ m:INT|REAL:)
        {
	    $message = "No range given for $variable in $thorn";
	    &CST_error(0,$message,__LINE__,__FILE__);
        }
	if($block !~ m:SHARES:)
	{
	  if($data[$line_number] =~ m:\s*\}\s*(.+):)
	  {
	      $default = $1;
	      if ($type =~ m:INT|REAL: && $default =~ m:":)
	      {
		  $message = "String default given for $type $variable in $thorn";
                  &CST_error(0,$message,__LINE__,__FILE__);
	      }
              elsif ($type =~ m:STRING|KEYWORD: && $default !~ m:".*":)
              {
		  $message = "Default given for $type $variable in $thorn is not a string";
                  &CST_error(0,$message,__LINE__,__FILE__);
	      }
              elsif ($type =~ m:BOOLEAN: && $default =~ m:": && $default !~ m:".*":)
	      {
		  $message = "Default given for $type $variable in $thorn is missing a quote";
                  &CST_error(0,$message,__LINE__,__FILE__);
	      }
	      
	      $default = $1 if ($default =~ m:\"(((\\\")|[^\"])*)\":);

	      $parameter_db{"\U$thorn $variable\E default"} = $default;
	  }
	  else
	  {
	    $message =  "Unable to find default for $variable";
	    &CST_error(0,$message,__LINE,__FILE);
	  }		
	}
      }
    }
    else
    {
      if($line =~ m:\{:)
      {
	$message = "Skipping parameter block in $thorn with missing keyword";
        &CST_error(1,$message,__LINE__,__FILE__);
	$line_number++ until ($data[$line_number] =~ m:\}:);
      }
      else
      {
	$message = "Unknown line \"$line\" in $thorn/param.ccl";
        &CST_error(0,$message,__LINE__,__FILE__);
      }
    }
  }
  
  $parameter_db{"\U$thorn\E SHARES 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
{
  my(%parameter_database) = @_;
  my($field);
  
  foreach $field ( sort keys %parameter_database )
  {
    print "$field has value $parameter_database{$field}\n";
  }
}


#/*@@
#  @routine    PrintParameterStatistics
#  @date       Sun Sep 19 13:04:18 1999
#  @author     Tom Goodale
#  @desc 
#  Prints out some statistics about a thorn's param.ccl
#  @enddesc 
#  @calls     
#  @calledby   
#  @history 
#
#  @endhistory 
#
#@@*/
sub PrintParameterStatistics
{
  my($thorn, %parameter_database) = @_;
  my($block);
  my($sep);

  $sep = "          ";
  foreach $block ("Global", "Restricted", "Private")
  {
    print $sep . scalar(split(" ", $parameter_database{"\U$thorn $block\E variables"})) . " $block";
    $sep = ", ";
  }

  print " parameters\n";

  return;
}

1;