File: Parser.pm

package info (click to toggle)
yaird 0.0.12-18etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 1,432 kB
  • ctags: 725
  • sloc: perl: 4,161; xml: 3,233; ansic: 3,105; sh: 876; makefile: 150
file content (467 lines) | stat: -rw-r--r-- 11,194 bytes parent folder | download | duplicates (2)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!perl -w
#
# Parser -- read the config file and template file
#   Copyright (C) 2005  Erik van Konijnenburg
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# This file contains two separate parsers: one for the overall config,
# one for the template specification.  The reason is that different
# configurations (eg disk or network based) can share the same templates.
#
use strict;
use warnings;
use Parse::RecDescent;

use Base;

package Parser;


		#
		# Comment processing: the part of
		# line after # is ignored.
		#
$Parse::RecDescent::skip = qr{(\s+|#.*\n)*};

		#
		# First grammar: the templates
		#
my $templateGrammar = <<'...';


		#
		# start_rule -- parse config file and
		# return errors.
		# It turns out that everything but the first
		# error is useless.
		#
start_rule :	config_file[fileName => $arg{fileName} ]
	      |	{
			my $fileName = $arg{fileName};
			my $errors = $thisparser->{errors};
			$thisparser->{errors} = undef;
			my $firstError = shift @{$errors};
			my $msg = $firstError->[0];
			my $line = $firstError->[1];
			Base::fatal ("$fileName:$line: $msg");
		}


config_file :	template_set[fileName => $arg{fileName}] end_of_file
		{ $return = $item{template_set}; }
	      |	<error>

end_of_file :	/\z/
	      |	<error: junk at end of file>


template_set :	<rulevar: $set = {} >
template_set :	'TEMPLATE' 'SET' <commit>
			template[fileName => $arg{fileName}, set => $set](s)
		'END' 'TEMPLATE' 'SET'
		{
			$return = $set;
		}
	    |   <error?>


template :	'TEMPLATE' <commit> identifier
		'BEGIN'
			template_directive[fileName => $arg{fileName}](s?)
		'END' 'TEMPLATE'
		{
			my $name = $item{identifier};

			if (exists ($arg{set}->{$name})) {
				my $old = $arg{set}->{$name}{origin};
 				Base::fatal ("$arg{fileName}:$prevline: redefinition of $name (earlier definition at $old)");
			}

			$arg{set}->{$name} = {
				origin => "$arg{fileName}:$prevline",
				identifier => $item{identifier},
				directives => $item{'template_directive(s?)'},
			};
			$return = 1;
		}
	    |	<error?>


template_directive :
		file_directive[fileName => $arg{fileName}]
	    |	dir_directive[fileName => $arg{fileName}]
	    |	tree_directive[fileName => $arg{fileName}]
	    |	script_directive[fileName => $arg{fileName}]
	    |	<error>


		#
		# Include this file from host on the image
		#
file_directive:	'FILE' <commit> pathname
		{
			$return = {
				type => 'file',
				value => $item{pathname},
				origin => "$arg{fileName}:$prevline",
			};
		}

		#
		# Create this directory on the image
		#
dir_directive :	'DIRECTORY' <commit> pathname
		{
			$return = {
				type => 'directory',
				value => $item{pathname},
				origin => "$arg{fileName}:$prevline",
			};
		}

		#
		# Copy this tree recursively from host to image
		#
tree_directive:	'TREE' <commit> pathname
		{
			$return = {
				type => 'tree',
				value => $item{pathname},
				origin => "$arg{fileName}:$prevline",
			};
		}

		#
		# Add this fragment to named script on the image
		#
script_directive: 'SCRIPT' <commit> pathname
		'BEGIN'
			inline_fragment
		'END' 'SCRIPT'
		{
			$return = {
				type => 'script',
				pathname => $item{pathname},
				value => $item{inline_fragment},
				origin => "$arg{fileName}:$prevline",
			};
		}
	    |	<error?>


		#
		# Identifier in config file; normally corresponds to
		# variable in calling perl code.  No reason to allow
		# these identifiers to have same name as reserved
		# words in config language, so no need to quote.
		#
identifier :	/[A-Za-z0-9][A-Za-z0-9_-]*/
	      |	<error>


		#
		# Pathname on generated image.  This will be subjected
		# to template substitution, so can contain stuff not
		# commonly found in pathnames
		# Let's not allow newline, to give error recovery some chance.
		#
pathname :	/"[^"\n]+"/
		{
			$return = $item{__PATTERN1__};
			$return =~ s/^"(.*)"$/\1/;
		}
	      |	<error: Invalid pathname: use double quoted string>


		#
		# A here document.  Twist: leading space plus '!'
		# is removed.  This means no terminator is needed,
		# and the here doc can be indented, and (unlike YAML)
		# it's insensitive to tabs vs space problems.
		#
inline_fragment: /!.*\n(\s*!.*\n)*/
		{
			$return = $item{__PATTERN1__};
			$return =~ s/^\s*!//mg;
		}
	      |	<error: Invalid inline fragment: start lines with exclamation mark (!)>

...


		#
		# Second grammar, this one for the overall config file.
		# This has some stuff in common with the template grammar.
		# Can we avoid repeating the code?  Options:
		# - merge config files at install time - too many variants
		# - concat the shared part to each grammar - ugly
		# - parameter to select real start_rule - hmm; possible
		# - recdescent include mechanism - ugly: call can be
		#   only where syntax allows it, but replacement text
		#   can straddle syntactic boundaries.
		# For now, just duplicate.
		#
my $configGrammar = <<'...';


		#
		# start_rule -- parse config file and return errors.
		# It turns out that everything but the first error is useless.
		#
start_rule :	config_file[fileName => $arg{fileName} ]
	      |	{
			my $fileName = $arg{fileName};
			my $errors = $thisparser->{errors};
			$thisparser->{errors} = undef;
			my $firstError = shift @{$errors};
			my $msg = $firstError->[0];
			my $line = $firstError->[1];
			Base::fatal ("$fileName:$line: $msg");
		}


config_file :	config_set[fileName => $arg{fileName}] end_of_file
		{ $return = $item{config_set}; }
	      |	<error>

end_of_file :	/\z/
	      |	<error: junk at end of file>


config_set :	<rulevar: $set = {} >
config_set :	'CONFIG'  <commit>
			config_item[fileName => $arg{fileName}, set => $set](s)
		'END' 'CONFIG'
		{
			if (! exists ($set->{templateFileName})) {
 				Base::fatal ("$arg{fileName}:$prevline: missing templatefile specification");
			}
			if (! exists ($set->{goals})) {
 				Base::fatal ("$arg{fileName}:$prevline: missing goal list");
			}
			$return = $set;
		}
	    |   <error?>


		# do these need origin?  seems a bit small for that.
config_item :	goal_list[fileName => $arg{fileName}, set => $arg{set}]
	    |	format_specification[fileName => $arg{fileName}, set => $arg{set}]
	    |	template_file_name[fileName => $arg{fileName}, set => $arg{set}]
	    |	<error>


format_specification :
		'FORMAT' <commit> identifier
		{
			if (exists ($arg{set}->{format})) {
 				Base::fatal ("$arg{fileName}:$prevline: duplicate format specification");
			}
			$arg{set}->{format} = $item{identifier};
			$return = 1;
		}
	    |	<error?>



template_file_name :
		'TEMPLATE' <commit> 'FILE' pathname
		{
			if (exists ($arg{set}->{templateFileName})) {
 				Base::fatal ("$arg{fileName}:$prevline: duplicate templatefile specification");
			}
			$arg{set}->{templateFileName} = $item{pathname};
			$return = 1;
		}
	    |	<error?>


		#
		# Goal_list - what the initial image should do.
		#
goal_list :	'GOALS' <commit>
			goal_directive[fileName => $arg{fileName}](s?)
		'END' 'GOALS'
		{
			if (exists ($arg{set}->{goals})) {
 				Base::fatal ("$arg{fileName}:$prevline: duplicate goal list");
			}
			$arg{set}->{goals} = $item{'goal_directive(s?)'};
			$return = 1;
		}
	    |	<error?>


goal_directive :
		template_directive[fileName => $arg{fileName}]
	    |	input_directive[fileName => $arg{fileName}]
	    |	network_directive[fileName => $arg{fileName}]
	    |	module_directive[fileName => $arg{fileName}]
	    |	optional_module_directive[fileName => $arg{fileName}]
	    |	mountdir_directive[fileName => $arg{fileName}]
	    |	mountdev_directive[fileName => $arg{fileName}]
	    |	<error>


		#
		# Expand this template.  No parameters supported.
		#
template_directive:	'TEMPLATE' <commit> identifier
		{
			$return = {
				type => 'template',
				value => $item{identifier},
				origin => "$arg{fileName}:$prevline",
			};
		}

		#
		# Load modules for all input devices
		#
input_directive :	'INPUT'
		{
			$return = {
				type => 'input',
				origin => "$arg{fileName}:$prevline",
			};
		}

		#
		# Load modules for all network devices
		#
network_directive :	'NETWORK'
		{
			$return = {
				type => 'network',
				origin => "$arg{fileName}:$prevline",
			};
		}

		#
		# Load this module
		#
module_directive:	'MODULE' <commit> identifier
		{
			$return = {
				type => 'module',
				value => $item{identifier},
				optional => 0,
				origin => "$arg{fileName}:$prevline",
			};
		}

		#
		# Load this module, if you can find it
		#
optional_module_directive:	'OPTIONAL' 'MODULE' <commit> identifier
		{
			$return = {
				type => 'module',
				value => $item{identifier},
				optional => 1,
				origin => "$arg{fileName}:$prevline",
			};
		}

		#
		# Mount the fs that fstab lists for pathname
		#
mountdir_directive:	'MOUNTDIR' <commit> pathname mount_point
		{
			$return = {
				type => 'mountdir',
				value => $item{pathname},
				mountPoint => $item{mount_point},
				origin => "$arg{fileName}:$prevline",
			};
		}

		#
		# Mount the fs in blockdev
		#
mountdev_directive:	'MOUNTDEV' <commit> pathname mount_point
		{
			$return = {
				type => 'mountdev',
				value => $item{pathname},
				mountPoint => $item{mount_point},
				origin => "$arg{fileName}:$prevline",
			};
		}

mount_point:	pathname

		#
		# Identifier in config file; normally corresponds to
		# variable in calling perl code.  No reason to allow
		# these identifiers to have same name as reserved
		# words in config language, so no need to quote.
		#
identifier :	/[A-Za-z0-9][A-Za-z0-9_-]*/
	      |	<error>


		#
		# Pathname.
		# Let's not allow newline, to give error recovery some chance.
		#
pathname :	/"[^"\n]+"/
		{
			$return = $item{__PATTERN1__};
			$return =~ s/^"(.*)"$/\1/;
		}
	      |	<error: Invalid pathname: use double quoted string>

...


#
# parse -- given grammar and filename, return parse tree or die.
# what the tree looks like is completely determined by the grammar.
#
sub parse ($$) {
	my ($grammar, $fileName) = @_;
	my $parser = new Parse::RecDescent ($grammar);

	if (! open (IN, "<", $fileName)) {
		Base::fatal ("could not open $fileName");
	}
	my $slurp = $/;
	$/ = undef;
	my $text = <IN>;
	$/ = $slurp;
	if (! close (IN)) {
		Base::fatal ("could not read $fileName");
	}

	# $::RD_TRACE = 1;
	my $tree = $parser->start_rule ($text, 0, 'fileName' => $fileName);
	return $tree;
}

#
# parseConfig -- parse config file and template file
#
sub parseConfig ($) {
	my ($fileName) = @_;
	my $config = parse ($configGrammar, $fileName);
	$config->{templates} = parse ($templateGrammar,
		$config->{templateFileName});
	return $config;
}


1;