-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerateMarshaling.pl
175 lines (152 loc) · 3.59 KB
/
generateMarshaling.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
#!/usr/bin/perl -w
use v5.30;
use Data::Dumper;
use Tie::IxHash;
use List::Util qw(pairs);
no warnings qw(experimental::smartmatch);
use standard;
tie my %interface_srcs, 'Tie::IxHash';
tie my %struct_srcs, 'Tie::IxHash';
tie my %func_srcs, 'Tie::IxHash';
tie my %struct_members_refv, 'Tie::IxHash';
my @function_srcs;
sub unmarshal {
my ($name) = @_; # struct_name
my $ret = "func (x *$name) UnmarshalJSON(data []byte) error {\n";
$ret .= tmpStruct($name) . "\n";
$ret .= "var y tmp
json.Unmarshal(data, &y)\n";
for ($struct_members_refv{$name}->@*) {
my ($name, $type) = $_->@*;
$ret .= ($interface_srcs{$type} ?
unmarshalInterfaceAssign($name, $type) :
structAssign($name)) . "\n";
}
$ret .= "return nil
}";
return $ret;
}
sub marshal {
my ($name) = @_; # struct_name
return qq!func (x $name) MarshalJSON() ([]byte, error) {
type marshal $name
y := marshal(x)
y.Type = "$name"
return json.Marshal(y)
}!;
}
sub possibleStructs {
my $ret = "possibleStructs := map[string][]interface{}\{\n";
for (keys %interface_srcs) {
my @impls = implementations($_);
$ret .= qq!"$_": []$_\{\n!;
for my $impl (@impls) {
$ret .= "&$impl\{},\n";
}
$ret .= "},\n";
}
$ret .= '}';
return $ret;
}
sub implementations {
my ($iface_name) = @_;
my $suffix = substr $iface_name, 1;
return grep /$suffix/, keys %struct_srcs;
}
sub tmpStruct {
my ($struct_name) = @_;
my $ret = "type tmp struct {\n";
for ($struct_members_refv{$struct_name}->@*) {
my ($name, $type) = $_->@*;
$ret .= "$name " .
($interface_srcs{$type} ? 'json.RawMessage' : $type) . "\n";
}
$ret .= '}';
return $ret;
}
sub unmarshalInterfaceAssign {
my ($name, $type) = @_;
my $instance_name = "\l${type}Instance";
my $ret = "var $instance_name map[string]interface{}\n";
$ret .= "json.Unmarshal(y.$name, &$instance_name)\n";
for (implementations($type)) {
$ret .= qq<if $instance_name\["Type"] == "$_" {
var z $_
json.Unmarshal(y.$name, &z)
x.$name = &z
}\n>;
}
chomp $ret;
return $ret;
}
sub structAssign {
my ($name) = @_;
return "x.$name = y.$name";
}
sub addMember {
my ($src, $member) = @_;
substr $src, index($src, "\n"), 0, "\n$member";
return $src;
}
sub isValid {
my ($struct) = @_;
return qq[func (x *$struct) isValid() bool {
return x.Type == "$struct";
}];
}
$/ = '';
while (<DATA>) {
chomp;
if (/^type (\w+) interface \{$/m) {
$interface_srcs{$1} = $_;
} elsif (/^type (\w+) struct \{$/m) {
my $struct_name = $1;
$struct_srcs{$struct_name} = $_;
$struct_members_refv{$struct_name} = [pairs(split ' ', s/^.*\n((?:.*\n)*)\s*\}$/$1/r)];
} elsif (/^func/) {
push @function_srcs, $_;
} else {
die "Paragraph not recognized: $_";
}
}
say qq[package main
import (
"encoding/json"
)];
while (my ($_k, $v) = each %interface_srcs) {
say $v;
}
my @struct_srcs_keys = keys %struct_srcs;
for my $k (@struct_srcs_keys) {
my $v = $struct_srcs{$k};
say addMember($v, 'Type string');
say unmarshal($k);
say marshal($k);
}
for (@function_srcs) {
say $_;
}
__DATA__
type Define struct {
DefineName string
RegexSteps Step
}
type Step interface {
RegexStepMarker()
String() string
}
type PositionSaveStep struct {
}
type CallStep struct {
callee string
}
type MatchCombineStep struct {
combineRuleName string
depth int
}
type MatchSaveStep struct {
SaveRuleName string
}
type MatchStep struct {
MatchString string
}