OSDN Git Service

collect some files' permissions
[yamy/yamy.git] / tools / makefunc
1 #!/usr/bin/perl -w
2 # -*- cperl -*-
3
4 binmode STDOUT;
5
6 while (<>) {
7   last if (m#BEGINING OF FUNCTION DEFINITION#);
8 }
9
10 $line = "";
11
12 while (<>) {
13   last if (m#END OF FUNCTION DEFINITION#);
14   chomp;
15   $line .= $_ unless (m{^\s*//});
16 }
17
18 $line =~ s/\s+/ /g;
19
20 print <<"__EOM__";
21 // DO NOT EDIT
22 // this file is automatically generated by makefunc
23 // see dependency information in mayu-common.mak
24
25 #ifdef FUNCTION_DATA
26 __EOM__
27 foreach $functionDefinition ( split(/;/, $line) ) {
28   # void funcPrefix(FunctionParam *i_param, const Keymap *i_keymap,
29   #                 bool i_doesIgnoreModifiers = true);
30   next unless ( $functionDefinition =~ m{^
31         \s*     \w+             # void
32         \s+     func(\w+)       # funcPrefix
33         \s*     \(              # (
34         \s*     FunctionParam   # FunctionParam
35         \s*     \*              # *
36         \s*     \w+             # i_param
37         \s*     ,?              # ,
38         \s*     (.*?)           # const Keymap *i_keymap,
39                                 # bool i_doesIgnoreModifiers = true
40         \s*     \)              # )
41         \s*$}x );
42   my($name) = $1;
43   push(@names, $name);
44   print <<"__EOM__";
45 class FunctionData_$name : public FunctionData
46 {
47 public:
48 __EOM__
49   my(@args) = split(/\s*,\s*/, $2);
50   
51   my $argc = 0;
52   my @argIsReference = ();
53   my @argTypes = ();
54   my @argNames = ();
55   my @argDefaultValues = ();
56   
57   foreach $arg ( @args ) {
58     my $isReference = 0;
59     my $type = "";
60     my $argName = "";
61     my $defaultValue = "";
62     # bool i_doesIgnoreModifiers = true
63     if ( $arg =~ m{^
64                 (.*\S)          # bool i_doesIgnoreModifiers
65         \s*     =               # =
66         \s*     (\S.*)          # true
67         $}x ) {
68       $arg = $1;
69       $defaultValue = $2;
70     }
71     # const Keymap *i_keymap
72     if ( $arg =~ m{^
73                 (.*\S           # const Keymap
74         \s*     \*)             # *
75         \s*     i_(\w+)         # i_keymap
76         $}x ) {
77       # const Hoge *i_hoge
78       $type = $1;
79       $argName = $2;
80     } elsif ( $arg =~ m{^
81                 const           # const
82         \s*     (.*\S)          # ...
83         \s*     &               # &
84         \s*     i_(\w+)         # i_...
85         $}x ) {
86       # const Hoge &i_hoge
87       $type = $1;
88       $argName = $2;
89       $isReference = 1;
90     } elsif ( $arg =~ m{^
91                 (.*\S)          # ...
92         \s*     i_(\w+)         # i_...
93         $}x ) {
94       # Hoge i_hoge
95       $type = $1;
96       $argName = $2;
97     } else {
98       die;
99     }
100     
101     push(@argIsReference, $isReference);
102     push(@argTypes, $type);
103     push(@argNames, $argName);
104     push(@argDefaultValues, $defaultValue);
105     $argc ++;
106   }
107   
108   for ($i = 0; $i < $argc; $i ++) {
109     print <<"__EOM__";
110   $argTypes[$i] m_$argNames[$i];
111 __EOM__
112   }
113   print <<"__EOM__";
114
115 public:
116   static FunctionData *create()
117   {
118     FunctionData_$name *fd
119       = new FunctionData_$name;
120 __EOM__
121   for ($i = 0; $i < $argc; $i ++) {
122     if ($argDefaultValues[$i]) {
123       print "    fd->m_$argNames[$i] = $argDefaultValues[$i];\n";
124     }
125   }
126   print <<"__EOM__";
127     return fd;
128   }
129   
130   virtual void load(SettingLoader *i_sl)
131   {
132 __EOM__
133   if ($argc == 0 || $argDefaultValues[0]) {
134     print <<"__EOM__";
135     if (!i_sl->getOpenParen(false, FunctionData_${name}::getName()))
136       return;
137 __EOM__
138   } else {
139     print <<"__EOM__";
140     i_sl->getOpenParen(true, FunctionData_${name}::getName()); // throw ...
141 __EOM__
142   }
143   for ($i = 0; $i < $argc; $i ++) {
144     if ($argDefaultValues[$i]) {
145       print <<"__EOM__";
146     if (i_sl->getCloseParen(false, FunctionData_${name}::getName()))
147       return;
148 __EOM__
149     }
150     if (0 < $i) {
151       print <<"__EOM__";
152     i_sl->getComma(false, FunctionData_${name}::getName()); // throw ...
153 __EOM__
154     }
155     print "    i_sl->load_ARGUMENT(&m_$argNames[$i]);\n";
156   }
157   print <<"__EOM__";
158     i_sl->getCloseParen(true, FunctionData_${name}::getName()); // throw ...
159   }
160
161   virtual void exec(Engine *i_engine, FunctionParam *i_param) const
162   {
163 __EOM__
164   print "    i_engine->func$name(i_param";
165   for ($i = 0; $i < $argc; $i ++) {
166     print ", m_$argNames[$i]";
167   }
168   print ");\n";
169   
170   print <<"__EOM__";
171   }
172
173   inline virtual const _TCHAR *getName() const
174   {
175     return _T("$name");
176   }
177
178   virtual tostream &output(tostream &i_ost) const
179   {
180     i_ost << _T("&") << getName();
181 __EOM__
182   if ( 0 < $argc ) {
183     print <<"__EOM__";
184     i_ost << _T("(");
185 __EOM__
186   }
187   for ($i = 0; $i < $argc; $i ++) {
188     if ($i == $argc - 1) {
189     print <<"__EOM__";
190     i_ost << m_$argNames[$i];
191 __EOM__
192     } else {
193     print <<"__EOM__";
194     i_ost << m_$argNames[$i] << _T(", ");
195 __EOM__
196     }
197   }
198   if ( 0 < $argc ) {
199     print <<"__EOM__";
200     i_ost << _T(") ");
201 __EOM__
202   }
203   print <<"__EOM__";
204     return i_ost;
205   }
206
207   virtual FunctionData *clone() const
208   {
209     return new FunctionData_${name}(*this);
210   }
211 };
212
213 __EOM__
214 }
215
216 print <<"__EOM__";
217 #endif // FUNCTION_DATA
218
219 #ifdef FUNCTION_FRIEND
220 __EOM__
221 foreach $name ( @names ) {
222   print <<"__EOM__";
223 friend class FunctionData_$name;
224 __EOM__
225 }
226 print <<"__EOM__";
227 #endif // FUNCTION_FRIEND
228
229 #ifdef FUNCTION_CREATOR
230 FunctionCreator functionCreators[] = {
231 __EOM__
232 foreach $name ( @names ) {
233   print <<"__EOM__";
234   { _T("$name"), FunctionData_${name}::create },
235 __EOM__
236 }
237 print <<"__EOM__";
238 };
239 #endif // FUNCTION_CREATOR
240 __EOM__