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
|
#!perl
################################################################################
=comment
Perl Weekly Challenge 259
=========================
TASK #2
-------
*Line Parser*
Submitted by: Gabor Szabo
You are given a line like below:
{% id field1="value1" field2="value2" field3=42 %}
Where
a) "id" can be \w+.
b) There can be 0 or more field-value pairs.
c) The name of the fields are \w+.
b) The values are either number in which case we don't need double quotes or
string in which case we need double quotes around them.
The line parser should return structure like below:
{
name => id,
fields => {
field1 => value1,
field2 => value2,
field3 => value3,
}
}
It should be able to parse the following edge cases too:
{% youtube title="Title \"quoted\" done" %}
and
{% youtube title="Title with escaped backslash \\" %}
BONUS: Extend it to be able to handle multiline tags:
{% id filed1="value1" ... %}
LINES
{% endid %}
You should expect the following structure from your line parser:
{
name => id,
fields => {
field1 => value1,
field2 => value2,
field3 => value3,
}
text => LINES
}
=cut
################################################################################
#--------------------------------------#
# Copyright © 2024 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=comment
Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. A single command-line argument specifies the (path and) filename of the file
from which the input data is to be read.
Assumptions and Notes
---------------------
1. Within fields, "number" values are integers.
2. Within fields, "string" values do not contain control codes.
3. Non-record lines are silently ignored.
4. The BONUS has not been attempted.
=cut
#===============================================================================
use v5.32.1; # Enables strictures
use warnings;
use Const::Fast;
use Regexp::Common qw( number );
use Test::More;
const my $BS_CODE => chr 1;
const my $QU_CODE => chr 2;
const my $USAGE => <<END;
Usage:
perl $0 <file>
perl $0
<file> Filename of the input data
END
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 259, Task #2: Line Parser (Perl)\n\n";
}
#===============================================================================
MAIN:
#===============================================================================
{
my $argc = scalar @ARGV;
if ($argc == 0)
{
run_tests();
}
elsif ($argc == 1)
{
my $file = $ARGV[ 0 ];
open( my $fh, '<', $file )
or error( qq[Can't open file "$file" for reading] );
my $records = parse_lines( $fh );
my $number = 1;
for my $record (@$records)
{
if (defined $record)
{
print "\n" if $number > 1;
print "Record $number\n";
print_record( $record );
++$number;
}
}
close $fh or die qq[Can't close file "$file"\n];
}
else
{
error( "Expected 0 or 1 command-line arguments, found $argc" );
}
}
#-------------------------------------------------------------------------------
sub parse_lines
#-------------------------------------------------------------------------------
{
my ($fh) = @_;
my @records;
while (my $line = <$fh>)
{
if ($line =~ / ^ \s* \{ \% \s+ (\w+) (?: \s+ (.*) )? \s+ \% \} \s* $ /x)
{
my %record;
$record{ name } = $1;
if (my $fields = $2)
{
$fields =~ s/ \\ \\ /$BS_CODE/gx;
$fields =~ s/ \\ \" /$QU_CODE/gx;
while ($fields)
{
$fields = parse_field( $fields, \%record );
$fields =~ s/ ^ \s+ //gx;
}
}
push @records, { %record };
}
else
{
push @records, undef; # Required for testing
}
}
return \@records;
}
#-------------------------------------------------------------------------------
sub parse_field
#-------------------------------------------------------------------------------
{
my ($fields, $record) = @_;
my $field_length = 0;
if ($fields =~ / ^ (\w+) \= ($RE{num}{int}) /x)
{
my ($name, $number) = ($1, $2);
push $record->{ fields }->@*, [ $name => $number ];
$field_length = length "$name=$number";
}
elsif ($fields =~ / ^ (\w+) \= \" (.*?) \" /x)
{
my ($name, $string) = ($1, $2);
$string =~ s/ $BS_CODE /\\/gx;
$string =~ s/ $QU_CODE /"/gx;
push $record->{ fields }->@*, [ $name => $string ];
$field_length = length qq[$name="$string"];
}
else
{
die qq[Invalid field in "$fields"];
}
substr $fields, 0, $field_length, '';
return $fields;
}
#-------------------------------------------------------------------------------
sub print_record
#-------------------------------------------------------------------------------
{
my ($record) = @_;
print "{\n";
printf " name => %s\n", $record->{ name };
print " fields =>\n";
print " {\n";
for my $field (@{ $record->{ fields } })
{
my $key = $field->[ 0 ];
my $value = $field->[ 1 ];
if ($value =~ / ^ $RE{num}{int} $ /x)
{
print qq[ $key => $value\n];
}
else
{
print qq[ $key => "$value"\n];
}
}
print " }\n";
print "}\n";
}
#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
print "Running the test suite\n";
my $records = parse_lines( *DATA );
for my $i (1 .. 6)
{
my $expected = get_expected( $i );
is_deeply $records->[ $i - 1 ], $expected, "Test $i";
}
done_testing;
}
#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
#-------------------------------------------------------------------------------
sub get_expected
#-------------------------------------------------------------------------------
{
my ($num) = @_;
if ($num == 1)
{
return {
name => 'id',
fields =>
[
[ field1 => 'value1' ],
[ field2 => 'value2' ],
[ field3 => 42 ]
]
}
}
elsif ($num == 2)
{
return {
name => 'youtube',
fields => [ [ title => 'Title "quoted" done' ] ]
}
}
elsif ($num == 3)
{
return {
name => 'youtube',
fields => [ [ title => 'Title with escaped backslash \\' ] ]
}
}
elsif ($num == 4)
{
return {
name => 'empty'
}
}
elsif ($num == 5)
{
return;
}
elsif ($num == 6)
{
return {
name => 'id',
fields => [ [ filed1 => 'value1' ] ],
}
}
die "The expected result for test $num is missing";
}
################################################################################
__DATA__
{% id field1="value1" field2="value2" field3=42 %}
{% youtube title="Title \"quoted\" done" %}
{% youtube title="Title with escaped backslash \\" %}
{% empty %}
Non-record line
{% id filed1="value1" %}
|