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 =~ /^'([bis])'$/) {
92 fatal($src, "error: .kind should be [ibs] 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 'b') {
105 $t{check} = '(luaL_checkint($L, $$) == 0)';
108 if ($t{kind} eq 'i') {
109 $t{check} = 'luaL_checkint($L, $$)';
112 if ($t{kind} eq 's') {
113 $t{check} = 'luaL_checkstring($L, $$)';
117 $t{ctypefmt} .= ' %s' unless (($t{ctypefmt} = $t{ctype}) =~ s/:/ \%s :/);
118 $t{ctype} =~ s/:.*//;
120 return $types{$type} = \%t;
123 sub parse_package($$) {
124 my ($src, $pkg) = @_;
127 file => $src->{file},
128 line => $src->{line},
131 if (defined $pkgs{$pkg}) {
132 my $pos = get_pos(\%pkgs, $pkg);
133 fatal($src, "already defined package: `$pkg' at $pos");
136 while (stream_getline($src)) {
137 if (/^\s*\}\s*(\w+)\s*;\s*$/) {
142 if (/^\s*(.*?)\s*=\s*(.*?)\s*;\s*$/) {
143 my ($lhs, $rhs) = ($1, $2);
144 my %m = ( file => $src->{file}, line => $src->{line} );
146 if ($lhs =~ /^((?:const\s+)?\w+?)\s*(\w+)$/) {
150 push @{$p{props}}, $2;
151 } elsif ($lhs =~ /^((?:const\s+)?\w+?)\s*(\w*)\((.*)\)$/) {
156 push @{$p{meths}}, $2;
158 fatal($src, "syntax error");
161 if (defined $p{members}{$m{name}}) {
162 my $pos = get_pos($p{members}{$m{name}}, 0);
163 fatal($src, "already defined package member: `$m{name}' at $pos");
166 $p{members}{$m{name}} = \%m;
171 fatal($src, "syntax error");
175 return $pkgs{$pkg} = \%p;
179 my ($ref, $typ) = @_;
181 if ($typ =~ /^const\s+(\w*)$/) {
182 fatal($ref, "undefined type `$1'") unless defined $types{$1};
183 return (const => 1, type => $types{$1});
186 fatal($ref, "undefined type `$typ'") unless defined $types{$typ};
187 return (const => 0, type => $types{$typ});
196 my %t = find_type($f, $f->{type});
197 my $call = $f->{cfun};
200 print "static int luaM_${pkg}_$f->{name}(lua_State *L) {\n";
202 if ($f->{proto} ne "void") {
204 for my $tmp (split /,/,$f->{proto}) {
208 my %pt = find_type($f, $tmp);
209 my $check = $pt{type}->{check};
211 $check =~ s/\$\$/\%d/;
213 put_line($pt{type}, 0);
214 printf " $pt{type}->{ctype} var$i = $check\n", $i++;
218 if (defined $t{type}->{push}) {
219 $call =~ s/\$\$/$t{type}->{push}/;
222 if ($t{type}->{kind} eq 'b') {
224 print " lua_pushboolean(L, $call);\n";
227 if ($t{type}->{kind} eq 'i') {
229 print " lua_pushint(L, $call);\n";
232 if ($t{type}->{kind} eq 's') {
235 print " lua_pushstring(L, $call);\n";
239 print " char *s = $call;\n";
240 print " lua_pushstring(L, s);\n";
241 print " p_delete(&s);\n";
246 printf " return %d;\n", ($f->{type} ne "void");
254 sub dump_struct_full($) {
258 print "struct luaM_$pkg->{name}_t $pkg->{cname} = {\n";
260 foreach (@{$pkg->{props}}) {
261 my $p = $pkg->{members}{$_};
262 my %t = find_type($p, $p->{type});
266 print " $p->{init},\n";
268 print " 0,\n" if ($t{type}->{kind} eq 'b');
269 print " 0,\n" if ($t{type}->{kind} eq 'i');
270 print " NULL,\n" if ($t{type}->{kind} eq 's');
277 static void $pkg->{cname}_init(void)
281 foreach (@{$pkg->{props}}) {
282 my $p = $pkg->{members}{$_};
283 my %t = find_type($p, $p->{type});
284 my $var = $pkg->{cname}.".".$p->{name};
289 print " $var = $p->{init};\n" if ($t{type}->{kind} eq 'b');
290 print " $var = $p->{init};\n" if ($t{type}->{kind} eq 'i');
291 print " m_strreplace(&$var, $p->{init});\n" if ($t{type}->{kind} eq 's');
296 sub dump_struct_short($) {
299 print "struct luaM_$pkg->{name}_t {\n";
300 foreach (@{$pkg->{props}}) {
301 my $p = $pkg->{members}{$_};
302 my %t = find_type($p, $p->{type});
303 printf " $t{type}->{ctypefmt};\n", $p->{name};
309 extern struct luaM_$pkg->{name}_t $pkg->{cname};
317 sub dump_package_full($) {
320 dump_struct_full($pkg);
321 map { print "\n"; dump_fun($pkg->{name}, $pkg->{members}{$_}); } @{$pkg->{meths}};
324 static const luaL_reg luaM_$pkg->{name}_methods[] = {
326 map { print " { \"$_\", luaM_$pkg->{name}_$_ },\n"; } @{$pkg->{meths}};
331 static int luaM_$pkg->{name}_index(lua_State *L)
333 const char *idx = luaL_checkstring(L, 2);
335 switch (mlua_which_token(idx, -1)) {
338 foreach (@{$pkg->{props}}) {
339 my $p = $pkg->{members}{$_};
340 my %t = find_type($p, $p->{type});
341 my $call = $pkg->{cname}.".".$p->{name};
343 my $tok = $p->{name};
346 if ($t{type}->{kind} eq 'b') {
347 $call = "lua_pushboolean(L, $call)";
350 if ($t{type}->{kind} eq 'i') {
351 $call = "lua_pushinteger(L, $call)";
354 if ($t{type}->{kind} eq 's') {
355 $call = "lua_pushstring(L, $call)";
358 if (defined $t{type}->{push}) {
359 $call =~ s/\$\$/$t{type}->{push}/;
363 print " case LTK_$tok:\n";
365 print " return 1;\n";
370 lua_rawget(L, lua_upvalueindex(2)); /* try methods */
375 static int luaM_$pkg->{name}_newindex(lua_State *L)
377 const char *idx = luaL_checkstring(L, 2);
379 switch (mlua_which_token(idx, -1)) {
382 foreach (@{$pkg->{props}}) {
383 my $p = $pkg->{members}{$_};
384 my %t = find_type($p, $p->{type});
388 my $tok = $p->{name};
389 my $var = $pkg->{cname}.".".$p->{name};
390 my $check = $t{type}->{check};
396 print " case LTK_$tok: \n";
397 print " $var = $check" if ($t{type}->{kind} eq 'b');
398 print " $var = $check" if ($t{type}->{kind} eq 'i');
399 print " m_strreplace(&$var, $check)" if ($t{type}->{kind} eq 's');
401 print ";\n return 1;\n";
410 int luaopen_$pkg->{name}(lua_State *L)
414 $pkg->{cname}_init();
416 /* create methods table, add it the the table of globals */
417 luaL_openlib(L, "$pkg->{name}", luaM_$pkg->{name}_methods, 0);
418 methods = lua_gettop(L);
420 /* create metatable for $pkg->{name}, add it to the registry */
421 luaL_newmetatable(L, "$pkg->{name}");
424 lua_pushliteral(L, "__index");
425 lua_pushvalue(L, mt); /* upvalue 1 */
426 lua_pushvalue(L, methods); /* upvalue 2 */
427 lua_pushcclosure(L, &luaM_$pkg->{name}_index, 2);
428 lua_rawset(L, mt); /* set mt.__index */
430 lua_pushliteral(L, "__newindex");
431 lua_newtable(L); /* for new members */
432 lua_pushcclosure(L, &luaM_$pkg->{name}_newindex, 1);
433 lua_rawset(L, mt); /* set mt.__newindex */
435 lua_pushliteral(L, "__metatable");
436 lua_pushvalue(L, methods); /* dup methods table */
437 lua_rawset(L, mt); /* hide metatable */
439 lua_setmetatable(L, methods);
441 lua_pop(L, 1); /* drop mt */
442 return 1; /* return methods */
448 sub dump_package_short($) {
450 my $upp = $pkg->{name};
454 #ifndef MUTT_LUA_${upp}_H
455 #define MUTT_LUA_${upp}_H
458 dump_struct_short($pkg);
461 int luaopen_$pkg->{name}(lua_State *L);
463 #endif /* MUTT_LUA_${upp}_H */
470 my %src = stream_open(shift);
473 while (stream_getline(\%src)) {
474 if (/^\s*\@type\s+([a-zA-Z]\w*)\s*=\s*{\s*$/) {
475 parse_type(\%src, $1);
477 } elsif (/^\s*\@package\s+([a-zA-Z]\w*)\s+{\s*$/) {
478 dump_package_full(parse_package(\%src, $1));
480 } elsif (/^\s*(\@\w*)/) {
481 fatal(\%src, "syntax error: unknown directive `$1'");
483 next if ($resync && /^\s+$/);
496 my %src = stream_open(shift);
499 while (stream_getline(\%src)) {
500 if (/^\s*\@type\s+([a-zA-Z]\w*)\s*=\s*{\s*$/) {
501 parse_type(\%src, $1);
502 } elsif (/^\s*\@package\s+([a-zA-Z]\w*)\s+{\s*$/) {
503 dump_package_short(parse_package(\%src, $1));
504 } elsif (/^\s*(\@\w*)/) {
505 fatal(\%src, "syntax error: unknown directive `$1'");
512 if ($#ARGV < 1 || ($ARGV[0] ne "-h" && $ARGV[0] ne "-c")) {
514 usage: mluapkg (-h | -c) file.pkg
520 /***** THIS FILE IS AUTOGENERATED DO NOT MODIFY DIRECTLY ! *****/
523 map { do_h($ARGV[$_]); } (1 .. $#ARGV) if ($ARGV[0] eq '-h');
524 map { do_c($ARGV[$_]); } (1 .. $#ARGV) if ($ARGV[0] eq '-c');