Simplify charset.cpkg a bit
[apps/madmutt.git] / tools / cpkg2c.mll
index 0eccf47..57e8258 100644 (file)
   module H = Hashtbl
   module B = Buffer
 
-  type 'a anchor = ('a * string * int)
-
+  type 'a anchor   = ('a * string * int)
   let t1 (a, _, _) = a
-  let t2 (_, a, _) = a
-  let t3 (_, _, a) = a
 
   let die lpos fpos s =
     output_string stderr (sprintf "%s:%d: %s\n" fpos lpos s);
 
   let put_line = printf "#line %d \"%s\"\n"
 
+  let isspace = function |' '|'\t' -> true | _ -> false
+
+  let strip s =
+    let l = ref 0 and r = ref (String.length s) in
+    while (isspace s.[!l]     && !l < !r) do incr l done;
+    while (isspace s.[!r - 1] && !l < !r) do decr r done;
+    String.sub s !l (!r - !l)
+
 (* @types related {{{ *)
 
   type typedef =
@@ -94,9 +99,9 @@
       | [t]          -> (false, type_find lpos fpos t)
       | _ -> assert false
     in
-    match s with
+    match strip s with
     | "void" -> []
-    | s      -> List.map aux (Str.split (Str.regexp "[ \t]+,[ \t]+") s)
+    | s      -> List.map aux (Str.split (Str.regexp "[ \t]*,[ \t]*") s)
 
   let parse_args lpos fpos s =
     let aux t =
       | [t; n]          -> ((false, type_find lpos fpos t), n)
       | _ -> assert false
     in
-    match s with
+    match strip s with
     | "void" -> []
-    | s      -> List.map aux (Str.split (Str.regexp "[ \t]+,[ \t]+") s)
+    | s      -> List.map aux (Str.split (Str.regexp "[ \t]*,[ \t]*") s)
 
 (* }}} *)
 (* parsing helpers {{{ *)
@@ -160,7 +165,6 @@ let sp     = [' ''\t']
 let typdecl = sp* ("const" sp+)? ident sp*
 
 (* }}} *)
-
 (* entry point {{{ *)
 
 rule cLine lpos fpos buf = parse
@@ -332,6 +336,8 @@ and ext_bodycode buf = parse
 (* }}} *)
 
 {
+(* templating functions {{{ *)
+
   let upper = String.uppercase
   let tplize tpl v =
     Str.global_replace (Str.regexp_string "$L") "L" (
@@ -417,7 +423,7 @@ and ext_bodycode buf = parse
     ) 0 fn.args);
     printf "\n#define RAISE(s)  luaL_error(L, (s))\n";
     if fn.rettype = [] then (
-      printf "#define RETURN return luaM_ret_%s_%s(L)\n" pkg.name fn.fname
+      printf "#define RETURN()  return luaM_ret_%s_%s(L)\n" pkg.name fn.fname
     ) else (
       printf "#define RETURN(luaM_x1";
       for i = 2 to retlen do printf ", luaM_x%d" i done;
@@ -526,7 +532,7 @@ int luaopen_%s(lua_State *L)
     luaL_newmetatable(L, \"%s\");
     mt = lua_gettop(L);
 
-    lua_pushliteral(L, \"%s\");
+    lua_pushliteral(L, \"__index\");
     lua_pushvalue(L, mt);                       /* upvalue 1         */
     lua_pushvalue(L, methods);                  /* upvalue 2         */
     lua_pushcclosure(L, &luaM_%s_index, 2);
@@ -543,13 +549,15 @@ int luaopen_%s(lua_State *L)
 
     lua_setmetatable(L, methods);
 
-    lua_pop(L, 1);                              /* drop mt           */
-    return 1;                                   /* return methods    */
+    lua_pop(L, 2);                              /* drop mt + methods */
+    return 1;
 }
 
 ")
     in List.iter do_c_aux
 
+(* }}} *)
+
   let usage () =
     output_string stderr "usage: cpkg2c (-h | -c) file.cpkg\n";
     exit 1