1 |
#!/usr/bin/perl -w |
2 |
use strict; |
3 |
use Message::Util::QName::Filter { |
4 |
d => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#dis-->, |
5 |
dis2pm => q<http://suika.fam.cx/~wakaba/archive/2004/11/8/dis2pm#>, |
6 |
DOMCore => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/dom-core#>, |
7 |
DOMMain => q<http://suika.fam.cx/~wakaba/archive/2004/dom/main#>, |
8 |
lang => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#>, |
9 |
Perl => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#Perl-->, |
10 |
license => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/license#>, |
11 |
ManakaiDOM => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/manakai-dom#>, |
12 |
owl => q<http://www.w3.org/2002/07/owl#>, |
13 |
rdf => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#>, |
14 |
rdfs => q<http://www.w3.org/2000/01/rdf-schema#>, |
15 |
}; |
16 |
|
17 |
use Getopt::Long; |
18 |
use Pod::Usage; |
19 |
my %Opt; |
20 |
GetOptions ( |
21 |
'for=s' => \$Opt{For}, |
22 |
'help' => \$Opt{help}, |
23 |
'verbose!' => $Opt{verbose}, |
24 |
) or pod2usage (2); |
25 |
if ($Opt{help}) { |
26 |
pod2usage (0); |
27 |
exit; |
28 |
} |
29 |
$Opt{file_name} = shift; |
30 |
|
31 |
BEGIN { |
32 |
require 'manakai/genlib.pl'; |
33 |
require 'manakai/dis.pl'; |
34 |
} |
35 |
our $State; |
36 |
our $result = ''; |
37 |
|
38 |
eval q{ |
39 |
sub impl_msg ($;%) { |
40 |
warn shift () . "\n"; |
41 |
} |
42 |
} unless $Opt{verbose}; |
43 |
|
44 |
sub perl_change_package (%) { |
45 |
my %opt = @_; |
46 |
my $fn = $opt{full_name}; |
47 |
unless ($fn eq $State->{ExpandedURI q<dis2pm:currentPackage>}) { |
48 |
$State->{ExpandedURI q<dis2pm:currentPackage>} = $fn; |
49 |
return perl_statement qq<package $fn>; |
50 |
} else { |
51 |
return ''; |
52 |
} |
53 |
} # perl_change_package |
54 |
|
55 |
$State->{DefaultFor} = $Opt{For}; |
56 |
|
57 |
my $source = dis_load_module_file (module_file_name => $Opt{file_name}, |
58 |
For => $Opt{For}, |
59 |
use_default_for => 1); |
60 |
$State->{for_def_required}->{$State->{DefaultFor}} ||= 1; |
61 |
|
62 |
dis_check_undef_type_and_for () unless $Opt{no_undef_check}; |
63 |
|
64 |
if (dis_uri_for_match (ExpandedURI q<ManakaiDOM:Perl>, $State->{DefaultFor})) { |
65 |
dis_perl_init ($source, For => $State->{DefaultFor}); |
66 |
} |
67 |
|
68 |
$State->{ExpandedURI q<dis2pm:currentPackage>} = 'main'; |
69 |
$result .= "#!/usr/bin/perl \n"; |
70 |
$result .= perl_comment q<This file is automatically generated from> . "\n" . |
71 |
q<"> . $Opt{file_name} . q<" at > . |
72 |
rfc3339_date (time) . qq<.\n> . |
73 |
q<Don't edit by hand!>; |
74 |
$result .= perl_statement q<use strict>; |
75 |
$result .= perl_change_package |
76 |
(full_name => $State->{Module}->{$State->{module}} |
77 |
->{ExpandedURI q<dis2pm:packageName>}); |
78 |
$result .= perl_statement |
79 |
perl_assign |
80 |
perl_var (type => '$', local_name => 'VERSION', |
81 |
scope => 'our') |
82 |
=> perl_literal version_date time; |
83 |
|
84 |
for my $pack (values %{$State->{Module}->{$State->{module}} |
85 |
->{ExpandedURI q<dis2pm:package>}||{}}) { |
86 |
next unless defined $pack->{Name}; |
87 |
if ({ |
88 |
ExpandedURI q<ManakaiDOM:Class> => 1, |
89 |
ExpandedURI q<ManakaiDOM:IF> => 1, |
90 |
ExpandedURI q<ManakaiDOM:ExceptionClass> => 1, |
91 |
ExpandedURI q<ManakaiDOM:ExceptionIF> => 1, |
92 |
ExpandedURI q<ManakaiDOM:WarningIF> => 1, |
93 |
}->{$pack->{ExpandedURI q<dis2pm:type>}}) { |
94 |
## Package name and version |
95 |
$result .= perl_change_package |
96 |
(full_name => $pack->{ExpandedURI q<dis2pm:packageName>}); |
97 |
$result .= perl_statement |
98 |
perl_assign |
99 |
perl_var (type => '$', local_name => 'VERSION', |
100 |
scope => 'our') |
101 |
=> perl_literal version_date time; |
102 |
## Inheritance |
103 |
my @isa; |
104 |
for my $uri (@{$pack->{ISA}||[]}, @{$pack->{Implement}||[]}) { |
105 |
my $pack = $State->{Type}->{$uri}; |
106 |
if (defined $pack->{ExpandedURI q<dis2pm:packageName>}) { |
107 |
push @isa, $pack->{ExpandedURI q<dis2pm:packageName>}; |
108 |
} else { |
109 |
impl_msg ("Inheriting package name for <$uri> not defined", |
110 |
node => $pack->{src}) if $Opt{verbose}; |
111 |
} |
112 |
} |
113 |
$result .= perl_inherit \@isa; |
114 |
## Members |
115 |
if ({ |
116 |
ExpandedURI q<ManakaiDOM:Class> => 1, |
117 |
ExpandedURI q<ManakaiDOM:ExceptionClass> => 1, |
118 |
ExpandedURI q<ManakaiDOM:WarningClass> => 1, |
119 |
}->{$pack->{ExpandedURI q<dis2pm:type>}}) { |
120 |
for my $method (values %{$pack->{ExpandedURI q<dis2pm:method>}}) { |
121 |
next unless defined $method->{Name}; |
122 |
if ($method->{ExpandedURI q<dis2pm:type>} eq |
123 |
ExpandedURI q<ManakaiDOM:DOMMethod>) { |
124 |
$result .= perl_sub |
125 |
(name => $method->{ExpandedURI q<dis2pm:methodName>}, |
126 |
code => ''); |
127 |
} elsif ($method->{ExpandedURI q<dis2pm:type>} eq |
128 |
ExpandedURI q<ManakaiDOM:DOMAttribute>) { |
129 |
$result .= perl_sub |
130 |
(name => $method->{ExpandedURI q<dis2pm:methodName>}, |
131 |
'prototype' |
132 |
=> (defined $method->{ExpandedURI q<dis2pm:setter>} |
133 |
->{Name} ? '$;$' : '$'), |
134 |
code => ''); |
135 |
} |
136 |
} # package method |
137 |
## TODO: Const |
138 |
} |
139 |
## TODO: Const |
140 |
} # root object |
141 |
} |
142 |
|
143 |
## Export |
144 |
if (keys %{$State->{perl_primary_module}->{perl_export_ok}||{}}) { |
145 |
$result .= perl_change_package |
146 |
full_name => $State->{perl_primary_module}->{perl_package_name}; |
147 |
$result .= perl_statement 'require Exporter'; |
148 |
$result .= perl_inherit ['Exporter']; |
149 |
$result .= perl_statement |
150 |
perl_assign |
151 |
perl_var (type => '@', scope => 'our', |
152 |
local_name => 'EXPORT_OK') |
153 |
=> '(' . perl_list (keys %{$State->{perl_primary_module} |
154 |
->{perl_export_ok}}) . ')'; |
155 |
if (keys %{$State->{perl_primary_module}->{perl_export_tags}||{}}) { |
156 |
$result .= perl_statement |
157 |
perl_assign |
158 |
perl_var (type => '%', scope => 'our', |
159 |
local_name => 'EXPORT_TAGS') |
160 |
=> '(' . perl_list (map { |
161 |
$_ => [keys %{$State->{perl_primary_module} |
162 |
->{perl_export_tags}->{$_}}] |
163 |
} keys %{$State->{perl_primary_module} |
164 |
->{perl_export_tags}}) . ')'; |
165 |
} |
166 |
} |
167 |
|
168 |
$result .= perl_statement 1; |
169 |
|
170 |
output_result $result; |
171 |
|
172 |
1; |