1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use Data::Dumper; |
---|
6 | use Getopt::Long; |
---|
7 | |
---|
8 | my $GIVEN_CLASS = 'bdm::DS'; |
---|
9 | GetOptions( |
---|
10 | 'class=s' => \$GIVEN_CLASS, |
---|
11 | ) or die('Wrong options format'); |
---|
12 | |
---|
13 | my $prev_class = { |
---|
14 | name => '', |
---|
15 | daughters => [], |
---|
16 | }; |
---|
17 | $prev_class->{parent} = $prev_class; |
---|
18 | |
---|
19 | my $class; |
---|
20 | |
---|
21 | my $prev_indent = 0; |
---|
22 | my $indent; |
---|
23 | |
---|
24 | my $classes_list = {}; |
---|
25 | my $classes_hier = $prev_class; |
---|
26 | |
---|
27 | while (my $line = <>) { |
---|
28 | # parse the current line |
---|
29 | chomp $line; |
---|
30 | $line =~ m{ |
---|
31 | ^ # start of the line |
---|
32 | ([ ]+) # indentation |
---|
33 | (.+) # class name itself |
---|
34 | $ # end of the line |
---|
35 | }xms; |
---|
36 | $indent = length $1; # number of indent spaces |
---|
37 | my $class_name = $2; # the rest of the line |
---|
38 | |
---|
39 | # find the parent class: this simple while covers all situations |
---|
40 | while ($indent <= $prev_indent) { |
---|
41 | $prev_class = $prev_class->{parent}; |
---|
42 | $prev_indent--; |
---|
43 | } |
---|
44 | |
---|
45 | $class = { |
---|
46 | name => $class_name, |
---|
47 | daughters => [], |
---|
48 | parent => $prev_class, |
---|
49 | }; |
---|
50 | |
---|
51 | push @{ $prev_class->{daughters} }, $class; # place into the hierarchy |
---|
52 | $classes_list->{$class_name} = $class; # store in hash by name |
---|
53 | |
---|
54 | # store current values as previous for the next iteration |
---|
55 | $prev_indent = $indent; |
---|
56 | $prev_class = $class; |
---|
57 | } |
---|
58 | |
---|
59 | #;;print STDERR "==Hierarchy"; |
---|
60 | #;;print_class($classes_hier, 0, *STDERR); |
---|
61 | |
---|
62 | #;;print STDERR "\n==List\n"; |
---|
63 | #;;map { print STDERR "$_\n" } sort keys %{ $classes_list }; |
---|
64 | |
---|
65 | #print "\n==Given\n"; |
---|
66 | print_class($classes_list->{$GIVEN_CLASS}, 0); |
---|
67 | |
---|
68 | sub print_class { |
---|
69 | my $class = shift; |
---|
70 | my $indent = shift; |
---|
71 | my $out = shift || *STDOUT; |
---|
72 | |
---|
73 | print $out " " x $indent; |
---|
74 | print $out $class->{name}, "\n"; |
---|
75 | |
---|
76 | map { print_class($_, $indent+1, $out) } @{ $class->{daughters} }; |
---|
77 | } |
---|