-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnode.pl
237 lines (219 loc) · 4.48 KB
/
node.pl
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
use strict;
use warnings;
use feature qw/say/;
use Data::Dump qw/pp/;
use Storable qw/dclone/;
use Carp qw/carp croak confess/;
my $definitions = defs();
my $attributes = attributes();
my $nodeinfo = nodeinfo();
sub get_node_info {
my ($name) = @_;
foreach my $key ( $name, qw/*/ ) {
exists $nodeinfo->{$key} and return $nodeinfo->{$key};
}
return undef;
}
sub get_node_definition {
my ($link) = @_;
if ( exists $definitions->{$link} ) {
return $definitions->{$link};
}
else {
carp sprintf "undefined '\$%s'", $link;
}
}
sub read_attrs {
my ($ref) = @_;
my @attrs = ();
for ($$ref) {
push @attrs, $2 while (/\G(\s*:\s*(\w+)\s*)/gc);
}
return \@attrs;
}
sub read_left {
my ($ref) = @_;
my $name = '';
my $attrs = [];
for ($$ref) {
/\G(\s*(\w+)\s*)/gc or last;
$name = $2;
if (/\G(:)/) {
$attrs = read_attrs( \$_ );
$attrs or last;
}
return ( $name, $attrs );
}
return ( undef, undef );
}
sub read_hash {
my ($ref) = @_;
my $array = read_nodes( \$_ );
my $hash = {};
foreach my $item (@$array) {
my $type = $item->{type};
$hash->{$type} = $item;
}
return $hash;
}
sub read_array {
my ($ref) = @_;
my $array = read_nodes( \$_ );
$array or return undef;
my $hash = { items => $array, };
return $hash;
}
sub read_right {
my ($ref) = @_;
for ($$ref) {
/\G(\s+)/gc;
if (/\G(\{)/gc) {
my $value = read_hash( \$_ );
/\G(\s*\})/gc or return undef;
return $value;
}
elsif (/\G(\[)/gc) {
my $value = read_array( \$_ );
/\G(\s*\])/gc or return undef;
return $value;
}
elsif (/\G(\$(\w+))/gc) {
my $value = get_node_definition($2);
if ( ref($value) =~ /HASH/ ) {
$value = dclone $value;
}
elsif ( ref($value) =~ /ARRAY/ ) {
$value = dclone $value;
}
elsif ( !ref($value) ) {
$value = { value => $value };
}
return $value;
}
m/\G('([^']*)')/gc and return { text => $2 };
if (/\G([^\{\}\[\]\s']+)/gc) {
my $what = $1;
$what =~ /^[\-\+]?\d+\.\d+$/ and return { fpvalue => $what };
return { value => $what };
}
}
return undef;
}
sub read_node {
my ($ref) = @_;
for ($$ref) {
/\G(\s+)/gc;
my ( $left, $attrs ) = read_left( \$_ );
$left or return undef;
/\G(\s+)/gc;
/\G(=)/gc or return undef;
my $right = read_right( \$_ );
defined $right or return undef;
$right->{type} = $left;
@$attrs and apply_attrs( $right, $attrs );
return fix_node($right);
}
return undef;
}
sub read_sequence {
my ( $ref, $pattern ) = @_;
my @array = ();
my $result = $pattern->($ref);
while ($result) {
push @array, $result;
$result = $pattern->($ref);
}
return \@array;
}
sub read_nodes {
my ($ref) = @_;
my $array = read_sequence( $ref, \&read_node );
return $array;
}
sub expand_macros {
local ($_) = @_;
my $MAX_ALLOWED_DEPTH = 100;
my $depth = 0;
again:
$depth++;
my $again = 0;
my @symbols = /\$(\w+)/g;
foreach my $symbol (@symbols) {
if ( exists $definitions->{$symbol} ) {
my $value = $definitions->{$symbol};
if ( !ref($value) ) {
s/\$\Q$symbol\E/$value/;
$again++;
}
}
}
if($again){
$depth > $MAX_ALLOWED_DEPTH and die "abused use of macros.";
goto again;
}
return $_;
}
sub parse_node($) {
local ($_) = @_;
$_ = expand_macros($_);
my $ref = \$_;
my $nodes = read_nodes($ref);
for ($$ref) {
/\G(\s+)/gc;
/\G(.+)/ and return undef;
}
return $nodes;
}
sub apply_attrs {
my ( $hash, $attrs ) = @_;
foreach my $attr (@$attrs) {
my $definition = $attributes->{$attr} // $attributes->{'*'};
if ($definition) {
if ( ref($definition) =~ /HASH/ ) {
$definition = dclone $definition;
foreach my $key ( keys %$definition ) {
my $value = $definition->{$key};
$hash->{$key} = $value;
}
}
elsif ( $definition =~ /CODE/ ) {
$definition->( $hash, $attr );
}
else {
$hash->{$attr} = $definition;
}
}
else {
warn sprintf "unkown used attribute '%s'", $attr;
}
}
}
sub get_right_value {
my ($right) = @_;
foreach my $key (qw/value fpvalue text/) {
exists $right->{$key} and return $right->{$key};
}
return undef;
}
sub fix_node {
my ($node) = @_;
my @keys = keys %$node;
foreach my $key (@keys) {
my $right = $node->{$key};
my $info = get_node_info($key);
$info or next;
$info->{terminal} and $right = get_right_value($right);
if ( $info->{mapto} ) {
delete $node->{$key};
$key = $info->{mapto};
}
elsif ( $info->{lowercase} ) {
delete $node->{$key};
$key = lc $key;
}
$info->{handler} and $right = $info->{handler}->($right);
$node->{$key} = $right;
}
return $node;
}
1;