Commit | Line | Data |
2c631ee0 |
1 | #!/bin/env perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
e8f4c506 |
6 | use DBI qw(looks_like_number); |
b2fc39a5 |
7 | use DBD::SQLite; |
f60f09e5 |
8 | use JSON::XS; |
b2fc39a5 |
9 | |
fc6614ee |
10 | use Getopt::Long; |
11 | |
12 | GetOptions( |
13 | 'json!' => \my $opt_json, |
ee2793c1 |
14 | 'dot!' => \my $opt_dot, |
b2fc39a5 |
15 | 'db=s' => \my $opt_db, |
e8f4c506 |
16 | 'verbose|v!' => \my $opt_verbose, |
17 | 'debug|d!' => \my $opt_debug, |
fc6614ee |
18 | ) or exit 1; |
94fab3d1 |
19 | |
f60f09e5 |
20 | my $j = JSON::XS->new->ascii->pretty(0); |
21 | |
1915946b |
22 | my ($dbh, $node_ins_sth); |
23 | if ($opt_db) { |
24 | $dbh = DBI->connect("dbi:SQLite:dbname=$opt_db","","", { |
25 | RaiseError => 1, PrintError => 0, AutoCommit => 0 |
26 | }); |
27 | $dbh->do("PRAGMA synchronous = OFF"); |
28 | $dbh->do("DROP TABLE IF EXISTS node"); |
29 | $dbh->do(q{ |
30 | CREATE TABLE node ( |
31 | id integer primary key, |
32 | name text, |
33 | title text, |
34 | depth integer, |
35 | parent_id integer, |
36 | |
37 | self_size integer, |
38 | kids_size integer, |
39 | kids_node_count integer, |
40 | child_ids text, |
41 | attr_json text, |
42 | leaves_json text |
43 | ) |
44 | }); |
45 | $node_ins_sth = $dbh->prepare(q{ |
46 | INSERT INTO node VALUES (?,?,?,?,?, ?,?,?,?,?,?) |
47 | }); |
48 | } |
b2fc39a5 |
49 | |
2c631ee0 |
50 | my @stack; |
51 | my %seqn2node; |
52 | |
ee2793c1 |
53 | my $dotnode = sub { |
54 | my $name = shift; |
55 | $name =~ s/"/\\"/g; |
56 | return '"'.$name.'"'; |
57 | }; |
58 | |
59 | print "memnodes = [" if $opt_json; |
60 | |
61 | if ($opt_dot) { |
62 | print "digraph {\n"; # } |
63 | print "graph [overlap=false]\n"; # target="???", URL="???" |
64 | } |
65 | |
66 | |
94fab3d1 |
67 | sub enter_node { |
68 | my $x = shift; |
69 | if ($opt_json) { |
70 | print " " x $x->{depth}; |
b2fc39a5 |
71 | print qq({ "id": "$x->{id}", "name": "$x->{name}", "depth":$x->{depth}, "children":[ \n); |
94fab3d1 |
72 | } |
ee2793c1 |
73 | if ($opt_dot) { |
74 | #printf $fh qq{\tn%d [ %s ]\n}, $x->{id}, $dotnode->($x->{name}); |
75 | #print qq({ "id": "$x->{id}", "name": "$x->{name}", "depth":$x->{depth}, "children":[ \n); |
76 | } |
94fab3d1 |
77 | return; |
78 | } |
79 | |
80 | sub leave_node { |
81 | my $x = shift; |
b2fc39a5 |
82 | delete $seqn2node{$x->{id}}; |
ee2793c1 |
83 | |
94fab3d1 |
84 | my $self_size = 0; $self_size += $_ for values %{$x->{leaves}}; |
85 | $x->{self_size} = $self_size; |
ee2793c1 |
86 | |
87 | my $parent = $stack[-1]; |
88 | if ($parent) { |
2c631ee0 |
89 | # link to parent |
5a78486c |
90 | $x->{parent_id} = $parent->{id}; |
2c631ee0 |
91 | # accumulate into parent |
92 | $parent->{kids_node_count} += 1 + ($x->{kids_node_count}||0); |
94fab3d1 |
93 | $parent->{kids_size} += $self_size + $x->{kids_size}; |
5a78486c |
94 | push @{$parent->{child_id}}, $x->{id}; |
2c631ee0 |
95 | } |
96 | # output |
97 | # ... |
94fab3d1 |
98 | if ($opt_json) { |
99 | print " " x $x->{depth}; |
100 | my $size = $self_size + $x->{kids_size}; |
101 | print qq(], "data":{ "\$area": $size } },\n); |
102 | } |
ee2793c1 |
103 | if ($opt_dot) { |
1915946b |
104 | printf "// n%d parent=%s(type=%s)\n", $x->{id}, |
105 | $parent ? $parent->{id} : "", |
106 | $parent ? $parent->{type} : ""; |
107 | if ($x->{type} != 2) { |
108 | my @node_attr = (sprintf "label=%s", $dotnode->($x->{name})); |
109 | if ($parent) { # probably a link |
110 | my @link_attr; |
111 | my $parent_id = $parent->{id}; |
112 | if ($parent->{type} == 2) { # link |
113 | (my $link_name = $parent->{name}) =~ s/->$//; |
114 | push @link_attr, (sprintf "label=%s", $dotnode->($link_name)); |
115 | $parent_id = ($stack[-2]||die "panic")->{id}; |
116 | } |
117 | printf qq{n%d -> n%d [%s];\n}, |
118 | $parent_id, $x->{id}, join(",", @link_attr); |
119 | } |
120 | printf qq{n%d [ %s ];\n}, $x->{id}, join(",", @node_attr); |
121 | } |
122 | |
ee2793c1 |
123 | } |
b2fc39a5 |
124 | if ($dbh) { |
f60f09e5 |
125 | my $attr_json = $j->encode($x->{attr}); |
e78b28ca |
126 | my $leaves_json = $j->encode($x->{leaves}); |
b2fc39a5 |
127 | $node_ins_sth->execute( |
e8f4c506 |
128 | $x->{id}, $x->{name}, $x->{title}, $x->{depth}, $x->{parent_id}, |
b2fc39a5 |
129 | $x->{self_size}, $x->{kids_size}, $x->{kids_node_count}, |
f60f09e5 |
130 | $x->{child_id} ? join(",", @{$x->{child_id}}) : undef, |
e78b28ca |
131 | $attr_json, $leaves_json, |
b2fc39a5 |
132 | ); |
133 | # XXX attribs |
134 | } |
94fab3d1 |
135 | return; |
2c631ee0 |
136 | } |
137 | |
94fab3d1 |
138 | |
2c631ee0 |
139 | while (<>) { |
140 | chomp; |
b2fc39a5 |
141 | my ($type, $id, $val, $name, $extra) = split / /, $_, 5; |
ee2793c1 |
142 | if ($type =~ s/^-//) { # Node type ($val is depth) |
2c631ee0 |
143 | while ($val < @stack) { |
94fab3d1 |
144 | leave_node(my $x = pop @stack); |
e8f4c506 |
145 | warn "N $id d$val ends $x->{id} d$x->{depth}: size $x->{self_size}+$x->{kids_size}\n" |
146 | if $opt_verbose; |
2c631ee0 |
147 | } |
148 | die 1 if $stack[$val]; |
ee2793c1 |
149 | my $node = $stack[$val] = { id => $id, type => $type, name => $name, extra => $extra, attr => {}, leaves => {}, depth => $val, self_size=>0, kids_size=>0 }; |
94fab3d1 |
150 | enter_node($node); |
b2fc39a5 |
151 | $seqn2node{$id} = $node; |
2c631ee0 |
152 | } |
153 | elsif ($type eq "L") { # Leaf name and memory size |
b2fc39a5 |
154 | my $node = $seqn2node{$id} || die; |
2c631ee0 |
155 | $node->{leaves}{$name} += $val; |
156 | } |
e8f4c506 |
157 | elsif (looks_like_number($type)) { # Attribute type, name and value |
b2fc39a5 |
158 | my $node = $seqn2node{$id} || die; |
e8f4c506 |
159 | my $attr = $node->{attr} || die; |
160 | if ($type == 1) { # NPattr_NAME |
161 | warn "Node $id already has attribute $type:$name (value $attr->{$type}{$name})\n" |
162 | if exists $attr->{$type}{$name}; |
163 | $attr->{$type}{$name} = $val || $id; |
164 | warn "A \@$id: '$name' $val\n"; |
165 | $node->{title} = $name if $type == 1 and !$val; |
166 | } |
167 | elsif (2 <= $type and $type <= 4) { # NPattr_PAD* |
168 | warn "Node $id already has attribute $type:$name (value $attr->{$type}[$val])\n" |
169 | if defined $attr->{$type}[$val]; |
170 | $attr->{$type}[$val] = $name; |
171 | } |
172 | else { |
173 | warn "Invalid attribute type '$type' on line $. ($_)"; |
174 | } |
2c631ee0 |
175 | } |
176 | else { |
177 | warn "Invalid type '$type' on line $. ($_)"; |
e8f4c506 |
178 | next; |
2c631ee0 |
179 | } |
b2fc39a5 |
180 | $dbh->commit if $dbh and $id % 10_000 == 0; |
2c631ee0 |
181 | } |
182 | |
183 | my $x; |
184 | while (@stack > 1) { |
94fab3d1 |
185 | leave_node($x = pop @stack) while @stack; |
b2fc39a5 |
186 | warn "EOF ends $x->{id} d$x->{depth}: size $x->{self_size}+$x->{kids_size}\n"; |
2c631ee0 |
187 | } |
94fab3d1 |
188 | print " ];\n" if $opt_json; |
ee2793c1 |
189 | print "}\n" if $opt_dot; |
94fab3d1 |
190 | |
b2fc39a5 |
191 | $dbh->commit if $dbh; |
192 | |
2c631ee0 |
193 | use Data::Dumper; |
194 | warn Dumper(\$x); |
195 | warn Dumper(\%seqn2node); |
196 | |
197 | =for |
198 | SV(PVAV) fill=1/1 [#1 @0] |
199 | : +64 sv =64 |
200 | : +16 av_max =80 |
201 | : AVelem-> [#2 @1] |
202 | : : SV(RV) [#3 @2] |
203 | : : : +24 sv =104 |
204 | : : : RV-> [#4 @3] |
205 | : : : : SV(PVAV) fill=-1/-1 [#5 @4] |
206 | : : : : : +64 sv =168 |
207 | : AVelem-> [#6 @1] |
208 | : : SV(IV) [#7 @2] |
209 | : : : +24 sv =192 |
210 | 192 at -e line 1. |
211 | =cut |
212 | __DATA__ |
213 | N 1 0 SV(PVAV) fill=1/1 |
214 | L 1 64 sv |
215 | L 1 16 av_max |
216 | N 2 1 AVelem-> |
217 | N 3 2 SV(RV) |
218 | L 3 24 sv |
219 | N 4 3 RV-> |
220 | N 5 4 SV(PVAV) fill=-1/-1 |
221 | L 5 64 sv |
222 | N 6 1 AVelem-> |
223 | N 7 2 SV(IV) |
224 | L 7 24 sv |