2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 2 of the License, or (at
5 # your option) any later version.
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # General Public License for more details.
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 # Copyright © 2007 Pierre Habouzit
29 return (file => $name, line => 0, f => \*FILE);
37 sub stream_getline($) {
39 my $file = $stream->{f};
49 #{{{ file:line functions
52 my ($stream, $msg) = @_;
53 printf STDERR "%s:%d: %s\n", $stream->{file}, $stream->{line}, $msg;
59 return $h->{$k}{file}.':'.$h->{$k}{line};
64 printf "#line %d \"%s\"\n", $src->{line} + $inc, $src->{file};
72 my ($src, $type) = @_;
79 if (defined $types{$type}) {
80 my $pos = get_pos(\%types, $type);
81 fatal($src, "already defined type: `$type' at $pos");
84 while (stream_getline($src)) {
85 last if (/^\s*\}\s*;\s*/);
86 if (/^\s*\.(push|kind|ctype|check)\s*=\s*(.*?)\s*;\s*$/) {
89 if ($2 =~ /^'([si])'$/) {
92 fatal($src, "error: .kind should be 'i' or 's' got $2)");
96 fatal($src, "syntax error: unknown type directive");
100 for my $i ('kind', 'ctype') {
101 fatal($src, "incomplete type $type: missing .$i") unless defined $t{$i};
103 unless (defined $t{check}) {
104 if ($t{kind} eq 's') {
105 $t{check} = 'luaL_checkstring($L, $$)';
108 if ($t{kind} eq 'i') {
109 $t{check} = 'luaL_checkint($l, $$)';
113 return $types{$type} = \%t;
116 sub parse_package($$) {
117 my ($src, $pkg) = @_;
120 file => $src->{file},
121 line => $src->{line},
124 if (defined $pkgs{$pkg}) {
125 my $pos = get_pos(\%pkgs, $pkg);
126 fatal($src, "already defined package: `$pkg' at $pos");
129 while (stream_getline($src)) {
130 if (/^\s*\}\s*(\w+)\s*;\s*$/) {
135 if (/^\s*(.*?)\s*=\s*(.*?)\s*;\s*$/) {
136 my ($lhs, $rhs) = ($1, $2);
137 my %m = ( file => $src->{file}, line => $src->{line} );
139 if ($lhs =~ /^((?:const\s+)?\w+?)\s*(\w+)$/) {
143 push @{$p{props}}, $2;
144 } elsif ($lhs =~ /^((?:const\s+)?\w+?)\s*(\w*)\((.*)\)$/) {
149 push @{$p{meths}}, $2;
151 fatal($src, "syntax error");
154 if (defined $p{members}{$m{name}}) {
155 my $pos = get_pos($p{members}{$m{name}}, 0);
156 fatal($src, "already defined package member: `$m{name}' at $pos");
159 $p{members}{$m{name}} = \%m;
164 fatal($src, "syntax error");
168 return $pkgs{$pkg} = \%p;
172 my ($ref, $typ) = @_;
174 if ($typ =~ /^const\s+(\w*)$/) {
175 fatal($ref, "undefined type `$1'") unless defined $types{$1};
176 return (const => 1, type => $types{$1});
179 fatal($ref, "undefined type `$typ'") unless defined $types{$typ};
180 return (const => 0, type => $types{$typ});
189 my %t = find_type($f, $f->{type});
190 my $call = $f->{cfun};
193 print "static int luaM_${pkg}_$f->{name}(lua_State *L) {\n";
195 if ($f->{proto} ne "void") {
197 for my $tmp (split /,/,$f->{proto}) {
201 my %pt = find_type($f, $tmp);
202 my $check = $pt{type}->{check};
204 $check =~ s/\$\$/\%d/;
206 put_line($pt{type}, 0);
207 printf " $pt{type}->{ctype} var$i = $check\n", $i++;
211 if (defined $t{type}->{push}) {
212 $call =~ s/\$\$/$t{type}->{push}/;
215 if ($t{type}->{kind} eq 's') {
218 print " lua_pushstring(L, $call);\n";
222 print " char *s = $call;\n";
223 print " lua_pushstring(L, s);\n";
224 print " p_delete(&s);\n";
229 if ($t{type}->{kind} eq 'i') {
231 print " lua_pushint(L, $call);\n";
234 printf " return %d;\n", ($f->{type} ne "void");
242 sub dump_struct_full($) {
246 print "struct luaM_$pkg->{name}_t $pkg->{cname} = {\n";
248 foreach (@{$pkg->{props}}) {
249 my $p = $pkg->{members}{$_};
250 my %t = find_type($p, $p->{type});
254 print " $p->{init},\n";
256 print " 0,\n" if ($t{type}->{kind} eq 'i');
257 print " NULL,\n" if ($t{type}->{kind} eq 's');
264 static void $pkg->{cname}_init(void)
268 foreach (@{$pkg->{props}}) {
269 my $p = $pkg->{members}{$_};
270 my %t = find_type($p, $p->{type});
271 my $var = $pkg->{cname}.".".$p->{name};
276 print " m_strreplace(&$var, $p->{init});\n" if ($t{type}->{kind} eq 's');
277 print " $var = $p->{init};\n" if ($t{type}->{kind} eq 'i');
282 sub dump_struct_short($) {
285 print "struct luaM_$pkg->{name}_t {\n";
286 foreach (@{$pkg->{props}}) {
287 my $p = $pkg->{members}{$_};
288 my %t = find_type($p, $p->{type});
289 print " $t{type}->{ctype} $p->{name};\n";
295 extern struct luaM_$pkg->{name}_t $pkg->{cname};
303 sub dump_package_full($) {
306 dump_struct_full($pkg);
307 map { print "\n"; dump_fun($pkg->{name}, $pkg->{members}{$_}); } @{$pkg->{meths}};
310 static const luaL_reg luaM_$pkg->{name}_methods[] = {
312 map { print " { \"$_\", luaM_$pkg->{name}_$_ },\n"; } @{$pkg->{meths}};
317 static int luaM_$pkg->{name}_index(lua_State *L)
319 const char *idx = luaL_checkstring(L, 2);
321 switch (mlua_which_token(idx, -1)) {
324 foreach (@{$pkg->{props}}) {
325 my $p = $pkg->{members}{$_};
326 my %t = find_type($p, $p->{type});
327 my $call = $pkg->{cname}.".".$p->{name};
329 my $tok = $p->{name};
332 if ($t{type}->{kind} eq 's') {
333 $call = "lua_pushstring(L, $call)";
336 if ($t{type}->{kind} eq 'i') {
337 $call = "lua_pushinteger(L, $call)";
340 if (defined $t{type}->{push}) {
341 $call =~ s/\$\$/$t{type}->{push}/;
345 print " case LTK_$tok:\n";
347 print " return 1;\n";
352 lua_rawget(L, lua_upvalueindex(2)); /* try methods */
357 static int luaM_$pkg->{name}_newindex(lua_State *L)
359 const char *idx = luaL_checkstring(L, 2);
361 switch (mlua_which_token(idx, -1)) {
364 foreach (@{$pkg->{props}}) {
365 my $p = $pkg->{members}{$_};
366 my %t = find_type($p, $p->{type});
370 my $tok = $p->{name};
371 my $var = $pkg->{cname}.".".$p->{name};
372 my $check = $t{type}->{check};
378 print " case LTK_$tok: \n";
379 print " m_strreplace(&$var, $check)" if ($t{type}->{kind} eq 's');
380 print " $var = $check" if ($t{type}->{kind} eq 'i');
382 print ";\n return 1;\n";
391 int luaopen_$pkg->{name}(lua_State *L)
395 $pkg->{cname}_init();
397 /* create methods table, add it the the table of globals */
398 luaL_openlib(L, "$pkg->{name}", luaM_$pkg->{name}_methods, 0);
399 methods = lua_gettop(L);
401 /* create metatable for $pkg->{name}, add it to the registry */
402 luaL_newmetatable(L, "$pkg->{name}");
405 lua_pushliteral(L, "__index");
406 lua_pushvalue(L, mt); /* upvalue 1 */
407 lua_pushvalue(L, methods); /* upvalue 2 */
408 lua_pushcclosure(L, &luaM_$pkg->{name}_index, 2);
409 lua_rawset(L, mt); /* set mt.__index */
411 lua_pushliteral(L, "__newindex");
412 lua_newtable(L); /* for new members */
413 lua_pushcclosure(L, &luaM_$pkg->{name}_newindex, 1);
414 lua_rawset(L, mt); /* set mt.__newindex */
416 lua_pushliteral(L, "__metatable");
417 lua_pushvalue(L, methods); /* dup methods table */
418 lua_rawset(L, mt); /* hide metatable */
420 lua_setmetatable(L, methods);
422 lua_pop(L, 1); /* drop mt */
423 return 1; /* return methods */
429 sub dump_package_short($) {
431 my $upp = $pkg->{name};
435 #ifndef MUTT_LUA_${upp}_H
436 #define MUTT_LUA_${upp}_H
439 dump_struct_short($pkg);
442 int luaopen_$pkg->{name}(lua_State *L);
444 #endif /* MUTT_LUA_${upp}_H */
451 my %src = stream_open(shift);
454 while (stream_getline(\%src)) {
455 if (/^\s*\@type\s+([a-zA-Z]\w*)\s*=\s*{\s*$/) {
456 parse_type(\%src, $1);
458 } elsif (/^\s*\@package\s+([a-zA-Z]\w*)\s+{\s*$/) {
459 dump_package_full(parse_package(\%src, $1));
461 } elsif (/^\s*(\@\w*)/) {
462 fatal(\%src, "syntax error: unknown directive `$1'");
464 next if ($resync && /^\s+$/);
472 print "/* vi"."m:set ft=c: */\n";
478 my %src = stream_open(shift);
481 while (stream_getline(\%src)) {
482 if (/^\s*\@type\s+([a-zA-Z]\w*)\s*=\s*{\s*$/) {
483 parse_type(\%src, $1);
484 } elsif (/^\s*\@package\s+([a-zA-Z]\w*)\s+{\s*$/) {
485 dump_package_short(parse_package(\%src, $1));
486 } elsif (/^\s*(\@\w*)/) {
487 fatal(\%src, "syntax error: unknown directive `$1'");
494 if ($#ARGV < 1 || ($ARGV[0] ne "-h" && $ARGV[0] ne "-c")) {
496 usage: mluapkg (-h | -c) file.pkg
502 /***** THIS FILE IS AUTOGENERATED DO NOT MODIFY DIRECTLY ! *****/
505 map { do_h($ARGV[$_]); } (1 .. $#ARGV) if ($ARGV[0] eq '-h');
506 map { do_c($ARGV[$_]); } (1 .. $#ARGV) if ($ARGV[0] eq '-c');