commit 5ec633b077889d4da1e3e8e4fecf2ec1837930f3
parent 2f40e857f3b7f0bc73319f39ed2d67299b9dd06a
Author: Tomas Hlavaty <tom@logand.com>
Date: Wed, 7 Dec 2011 23:01:25 +0100
parser fixes and unoidl2java improvements
Diffstat:
M | parse.y | | | 10 | ++++++---- |
M | unoidl2java.c | | | 230 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- |
2 files changed, 205 insertions(+), 35 deletions(-)
diff --git a/parse.y b/parse.y
@@ -38,7 +38,7 @@ Any ast;
%token OUT INOUT ONEWAY GET SET PUBLISHED ELLIPSIS
%token REMOVABLE
-%token METHOD CONSTRUCTOR RELATIVE ABSOLUTE TEMPLATE
+%token METHOD CONSTRUCTOR RELATIVE ABSOLUTE TEMPLATE EXP
%%
@@ -58,7 +58,8 @@ enum_member: identifier | identifier EQ exp {$$ = list2($1, $3);};
defstruct: published STRUCT identifier single_inheritance
LCURLY struct_members RCURLY SEMICOLON {$$ = cons5($2, $3, $1, $4, $6);};
single_inheritance: nil | COLON name {$$ = $2};
-struct_members: struct_member | struct_member struct_members {$$ = cons($1, $2);};
+struct_members: struct_member {$$ = list1($1);}
+ | struct_member struct_members {$$ = cons($1, $2);};
struct_member: type identifier SEMICOLON {$$ = list2($2, $1);};
deftemplate: published STRUCT identifier struct_params
LCURLY template_members RCURLY SEMICOLON {$$ = cons5($2, $3, $1, $4, $6);};
@@ -147,7 +148,7 @@ interface_singleton: published SINGLETON identifier COLON name SEMICOLON
service_singleton: published SINGLETON identifier
LCURLY SERVICE name SEMICOLON RCURLY SEMICOLON
{$$ = list5($2, $3, $1, $6, T);};
-type: simple_type | sequence_type | template_type | name;
+type: simple_type {$$ = list1($1);} | sequence_type | template_type | name;
simple_type: VOID | BOOLEAN | BYTE | SHORT | UNSIGNED SHORT | LONG
| UNSIGNED LONG | HYPER | UNSIGNED HYPER | FLOAT | DOUBLE | CHAR
| STRING | TYPE | ANY;
@@ -155,7 +156,8 @@ sequence_type: SEQUENCE LT type GT {$$ = list2($1, $3);};
template_type: name LT type types GT
{$$ = list3(mk(TEMPLATE, "template"), $1, cons($3, $4));};
types: nil | COMMA type types {$$ = cons($2, $3);};
-exp: xor_exp | exp OR xor_exp {$$ = list3($2, $1, $3);};
+exp: exp_top {$$ = list2(mk(EXP, "exp"), $1);};
+exp_top: xor_exp | exp OR xor_exp {$$ = list3($2, $1, $3);};
xor_exp: and_exp | xor_exp XOR and_exp {$$ = list3($2, $1, $3);};
and_exp: shift_exp | and_exp AND shift_exp {$$ = list3($2, $1, $3);};
shift_exp: add_exp | shift_exp shift_op add_exp {$$ = list3($2, $1, $3);};
diff --git a/unoidl2java.c b/unoidl2java.c
@@ -11,57 +11,196 @@ extern const Any NIL;
static inline void pr(char *x) {printf("%s", x);}
static inline void pl(char *x) {printf("%s\n", x);}
-static void pr_exp(Any x) { // TODO
- print(x);
+static void pp(Any x);
+
+static void pp_list(Any x) {
+ for(; !null(x); x = cdr(x)) {
+ pr("#"); print(car(x)); pl("");
+ pp(car(x));
+ }
+}
+
+static void pp_clist(Any x) {
+ for(int i = 0; !null(x); x = cdr(x), i++) {
+ if(0 < i) pr(", ");
+ pp(car(x));
+ }
}
-static void pr_module() // TODO
-{
+static void pr_relative(Any x) {
+ pp(cadr(x));
+ x = cddr(x);
+ for(; !null(x); x = cdr(x)) {
+ pr(".");
+ pp(car(x));
+ }
+}
+
+static void pr_module() { // TODO
char *name = "PACKAGE";
pl("package "); pr(name); pl(";");
}
-static void pr_enum(Any x)
-{
- print(x);
- char *name = token(cadr(x));
+// http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Professional_UNO
+
+static void pr_enum(Any x) {
+ Any name = cadr(x);
Any published = caddr(x);
Any values = cdddr(x);
Any v0 = car(values);
Any v0k = consp(v0) ? car(v0) : v0;
pr_module();
- pl("");
- pr("public final class "); pr(name); pl(" extends com.sun.star.uno.Enum {");
- pl("");
- pr(" private "); pr(name); pl("(int value) {");
+ pr("public final class "); pp(name); pl(" extends com.sun.star.uno.Enum {");
+ pr(" private "); pp(name); pl("(int value) {");
pl(" super(value);");
pl(" }");
- pl("");
- pr(" public static "); pr(name); pl(" getDefault() {");
- pr(" return "); print(v0k); pl(";");
+ pr(" public static "); pp(name); pl(" getDefault() {");
+ pr(" return "); pp(v0k); pl(";");
pl(" }");
- pl("");
for(Any y = values; !null(y); y = cdr(y)) {
Any value = car(y);
char *k = token(consp(value) ? car(value) : value);
Any v = consp(value) ? cdr(value) : mk(INT, "?"); // TODO
- pr(" public static final "); pr(name); pr(" "); pr(k); pr(" = new "); pr(name); pr("("); pr_exp(v); pl(");");
+ pr(" public static final "); pp(name); pr(" "); pr(k); pr(" = new "); pp(name); pr("("); pp(v); pl(");");
}
- pl("");
- pr(" public static "); pr(name); pl(" fromInt(int value) {");
+ pr(" public static "); pp(name); pl(" fromInt(int value) {");
pl(" switch(value) {");
for(Any y = values; !null(y); y = cdr(y)) {
Any value = car(y);
char *k = token(consp(value) ? car(value) : value);
Any v = consp(value) ? cdr(value) : mk(INT, "?");
- pr(" case "); pr_exp(v); pr(": return "); pr(k); pl(";");
+ pr(" case "); pp(v); pr(": return "); pr(k); pl(";");
}
pl(" }");
- pr(" return "); print(v0k); pl(";");
+ pr(" return "); pp(v0k); pl(";");
pl(" }");
pl("}");
}
+static void pr_args(Any x) {
+ for(int i = 0; !null(x); x = cdr(x), i++) {
+ Any a = car(x);
+ if(0 < i) pr(", ");
+ pp(cadr(a)); pr(" "); pp(car(a));
+ }
+}
+
+static void pr_args2(Any x) { // for in, out, inout
+ for(int i = 0; !null(x); x = cdr(x), i++) {
+ Any a = car(x);
+ if(0 < i) pr(", ");
+ pp(caddr(a)); pr(" "); pp(cadr(a));
+ }
+}
+
+static void pr_struct(Any x) {
+ print(x);
+ Any name = cadr(x);
+ Any published = caddr(x);
+ Any super = cadddr(x);
+ Any slots = cddddr(x);
+ pr_module();
+ pr("public class "); pp(name);
+ if(!null(super)) {pr(" extends "); pp(super);}
+ pl(" {");
+ for(Any y = slots; !null(y); y = cdr(y)) {
+ Any slot = car(y);
+ pr(" public "); pp(cadr(slot)); pr(" "); pp(car(slot)); pl(";");
+ }
+ pr(" public "); pp(name); pl("() {");
+ // TODO init enum to default
+ pl(" }");
+ pr(" public "); pp(name); pr("("); pr_args(slots); pl(") {");
+ for(Any y = slots; !null(y); y = cdr(y)) {
+ Any slot = car(y);
+ // TODO if Object => super(slot);
+ pr(" this."); pp(car(slot)); pr(" = "); pp(car(slot)); pl(";");
+ }
+ pl(" }");
+}
+
+static void pr_exception(Any x) {
+ Any name = cadr(x);
+ Any published = caddr(x);
+ Any super = cadddr(x);
+ Any body = cddddr(x);
+ pr_module();
+ pr("public class "); pp(name);
+ if(!null(super)) {
+ pr(" extends "); pp(super);
+ }
+ pl(" {");
+ for(Any y = body; !null(y); y = cdr(y)) {
+ Any slot = car(y);
+ pr(" "); pp(cadr(slot)); pr(" "); pp(car(slot)); pl(";");
+ }
+ pr(" public "); pp(name); pr("("); pr_args(body); pl(") {");
+ for(Any y = body; !null(y); y = cdr(y)) {
+ Any slot = car(y);
+ pr(" this."); pp(car(slot)); pr(" = "); pp(car(slot)); pl(";");
+ }
+ pl(" }");
+ pl("}");
+}
+
+static void pr_interface(Any x) {
+ Any name = cadr(x);
+ Any published = caddr(x);
+ Any super = cadddr(x);
+ Any body = cddddr(x);
+ pr_module();
+ pr("public interface "); pp(name);
+ if(!null(super)) {pr(" extends "); pp(super);}
+ pl(" {");
+ pp_list(body);
+ pl("}");
+}
+
+static void pr_method(Any x) {
+ Any name = cadr(x);
+ Any published = caddr(x);
+ Any type = cadddr(x);
+ Any args = caddddr(x);
+ Any body = cdddddr(x); // TODO throws
+ pr(" "); pp(type); pr(" "); pp(name); pr("("); pr_args2(args); pr(")");
+ if(!null(body)) {
+ pr(" throws "); pp_clist(body);
+ }
+ pl(";");
+}
+
+static void pr_attribute(Any x) {
+ pr(" "); pp(cadr(x)); pr(" "); pp(caddr(x)); pl(";"); // TODO options
+}
+
+static void pr_arg(Any x) {
+ pp(caddr(x)); pr(" "); pp(cadr(x));
+}
+
+static void pr_service(Any x) {
+}
+
+static void pr_singleton(Any x) {
+}
+
+static void pr_constants(Any x) {
+ Any name = cadr(x);
+ Any published = caddr(x);
+ Any body = cdddr(x);
+ pr_module();
+ pr("public final class "); pp(name); pl(" {");
+ pp_list(body);
+ pl("}");
+}
+
+static void pr_const(Any x) {
+ pr(" public static final "); pp(cadr(x)); pr(" ");
+ pp(caddr(x)); pr(" = "); pp(cadddr(x)); pl(";");
+}
+
+static void pr_typedef(Any x) {
+}
+
Any module;
static void pp(Any x) {
@@ -74,11 +213,45 @@ static void pp(Any x) {
print(module);
pp(caddr(x));
break;
- case ENUM:
- pr_enum(x);
- break;
- /* default: */
- /* print(x); */
+ case ENUM: pr_enum(x); break;
+ case STRUCT: pr_struct(x); break;
+ case INTERFACE: pr_interface(x); break;
+ case METHOD: pr_method(x); break;
+ //case IN:
+ //case OUT:
+ //case INOUT: pr_arg(x); break;
+ case RELATIVE:
+ case ABSOLUTE: pr_relative(x); break;
+ case CONSTANTS: pr_constants(x); break;
+ case CONST: pr_const(x); break;
+ case SEQUENCE: pr("sequence<"); pp(cadr(x)); pr(">"); break;
+ case ATTRIBUTE: pr_attribute(x); break;
+ case EXCEPTION: pr_exception(x); break;
+ case VOID:
+ case BOOLEAN:
+ case BYTE:
+ case SHORT:
+ case LONG:
+ case HYPER:
+ case FLOAT:
+ case DOUBLE:
+ case CHAR:
+ case STRING:
+ case TYPE:
+ case ANY: print(car(x)); break;
+ case UNSIGNED: pp_list(x); break;
+ case EXP: pp(cadr(x)); break;
+ case XOR: pp(cadr(x)); pr("^"); pp(caddr(x)); break;
+ case AND: pp(cadr(x)); pr("&"); pp(caddr(x)); break;
+ case LSHIFT:
+ case RSHIFT:
+ case MUL:
+ case DIV:
+ case MOD: pp(cadr(x)); pp(car(x)); pp(caddr(x)); break;
+ //case PLUS: unary?
+ //case MINUS:
+ case NOT: pr("!"); pp(cadr(x)); break;
+ default: print(x);
}
} else
print(x);
@@ -86,11 +259,6 @@ static void pp(Any x) {
printf("NIL");
}
-static void pp_list(Any x) {
- for(; !null(x); x = cdr(x))
- pp(car(x));
-}
-
int main() {
yyparse();
module = NIL;