unoidl2

Unnamed repository; edit this file to name it for gitweb.
git clone https://logand.com/git/unoidl2.git/
Log | Files | Refs

commit 34e1f1810997d18f60d80cfec089417b9343c978
parent 6a5f084f657a73620b0e0e6fa2e07d5c8dc47ec7
Author: Tomas Hlavaty <tom@logand.com>
Date:   Fri,  2 Dec 2011 02:41:08 +0100

parser improved

Diffstat:
Mparse.y | 61+++++++++++++++++++++++++++++++++++--------------------------
Mscan.ll | 12+++++++++++-
Munoidl2.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
Munoidl2.h | 11++++++++++-
4 files changed, 112 insertions(+), 38 deletions(-)

diff --git a/parse.y b/parse.y @@ -16,11 +16,11 @@ int yywrap() extern int yylex(void); -Any ast; - extern const Any NIL; extern const Any T; +Any ast; + #define YYSTYPE Any %} @@ -37,18 +37,20 @@ extern const Any T; %token OUT INOUT ONEWAY GET SET PUBLISHED ELLIPSIS FLOATING_PT_LITERAL %token REMOVABLE +%token METHOD CONSTRUCTOR + %% -ast: idl_specification {ast = $1}; -idl_specification: nil | declaration; -nil: {$$ = NIL;}; +ast: declarations {ast = $1}; +nil: /* empty */ {$$ = NIL;}; declaration: defenum | defstruct | deftemplate | defexception | interface_forward | definterface | deftype | constant | constants | defmodule | interface_service | accumulated_service | interface_singleton | service_singleton; published: nil | PUBLISHED {$$ = T;}; -defenum: published ENUM identifier LCURLY enum_members RCURLY SEMICOLON; +defenum: published ENUM identifier LCURLY enum_members RCURLY SEMICOLON + {$$ = cons4($2, $3, $1, $4)}; enum_members: enum_member {$$ = list1($1);} | enum_member COMMA enum_members {$$ = cons($1, $2);}; enum_member: identifier | identifier EQ exp {$$ = list3($2, $1, $3);}; @@ -65,14 +67,15 @@ identifiers: identifier {$$ = list1($1);} template_members: template_member | template_member template_members; template_member: struct_member | parametric_member; parametric_member: identifier identifier SEMICOLON; -defexception: published EXCEPTION identifier single_inheritance LCURLY struct_members_opt RCURLY SEMICOLON; +defexception: published EXCEPTION identifier single_inheritance + LCURLY struct_members_opt RCURLY SEMICOLON; struct_members_opt: nil | struct_members; -interface_forward: published INTERFACE identifier SEMICOLON; +interface_forward: published INTERFACE identifier SEMICOLON {$$ = list3($2, $3, $1);}; definterface: published INTERFACE identifier single_inheritance - LCURLY interface_members RCURLY SEMICOLON; -interface_members: nil | interface_member interface_members; + LCURLY interface_members RCURLY SEMICOLON {$$ = cons5($2, $3, $1, $4, $5);}; +interface_members: nil | interface_member interface_members {$$ = cons($1, $2);}; interface_member: interface_inheritance | attribute | defmethod; -interface_inheritance: optional INTERFACE name SEMICOLON {$$ = list2($3, $1);}; +interface_inheritance: optional INTERFACE name SEMICOLON {$$ = list3($2, $3, $1);}; optional: nil | LSQUARE OPTIONAL RSQUARE {$$ = $2;}; attribute: attribute_flags type identifier attribute_accesses_opt SEMICOLON; attribute_accesses_opt: nil | LCURLY attribute_accesses2 RCURLY {$$ = $2}; @@ -80,19 +83,23 @@ attribute_accesses2: attribute_access {$$ = list1($1);} | attribute_access attribute_accesses2 {$$ = cons($1, $2);}; attribute_flags: LSQUARE attribute_flags ATTRIBUTE attribute_flags_cdr RSQUARE; attribute_flags: nil | attribute_flag | attribute_flag attribute_flags_cdr; -attribute_flags_cdr: COMMA attribute_flag | COMMA attribute_flag attribute_flags_cdr; +attribute_flags_cdr: COMMA attribute_flag + | COMMA attribute_flag attribute_flags_cdr; attribute_flag: BOUND | READONLY; attribute_access: attribute_get | attribute_set; attribute_get: GET exception_spec SEMICOLON; attribute_set: SET exception_spec SEMICOLON; exception_spec: RAISES LPAR names RPAR; names: name | name COMMA names; -defmethod: oneway type_opt identifier LPAR method_params RPAR exception_spec SEMICOLON; -oneway: nil | LSQUARE ONEWAY RSQUARE {$$ = T;}; -type_opt: nil | type; -method_params: method_param method_params_cdr; -method_params_cdr: nil | COMMA method_param; -method_param: LSQUARE direction RSQUARE type identifier; +defmethod: /* oneway */ type/* _opt */ identifier LPAR method_params_opt RPAR + exception_spec SEMICOLON + {$$ = list6(mk(METHOD, "method"), $3, $2, $1, $5, $7);}; +/* oneway: nil | LSQUARE ONEWAY RSQUARE {$$ = T;}; */ +/* type_opt: nil | type; */ +method_params_opt: nil | method_params {$$ = $1;}; +method_params: method_param {$$ = list1($1);} + | method_param COMMA method_params {$$ = cons($1, $3);}; +method_param: LSQUARE direction RSQUARE type identifier {$$ = list3($3, $5, $4);}; exception_spec_opt: nil | exception_spec; direction: IN | OUT | INOUT; deftype: published TYPEDEF type identifier SEMICOLON; @@ -100,18 +107,20 @@ constant: published const_decl; const_decl: CONST type identifier EQ exp SEMICOLON; constants: published CONSTANTS identifier LCURLY const_decls RCURLY SEMICOLON; const_decls: nil | const_decl const_decls; -defmodule: MODULE identifier LCURLY declarations RCURLY SEMICOLON; -declarations: nil | declaration declarations; +defmodule: MODULE identifier LCURLY declarations RCURLY SEMICOLON + {$$ = cons3($1, $2, $4)}; +declarations: nil | declaration declarations {$$ = cons($1, $2);}; interface_service: published SERVICE identifier colon_name_opt - constructors_block SEMICOLON; -colon_name_opt: nil | COLON name; + constructors_block SEMICOLON {$$ = cons5($2, $3, $1, $4, $5);}; +colon_name_opt: nil | COLON name {$$ = $2;}; constructors: nil | constructor constructors; constructor: identifier LPAR constructor_params_opt RPAR - exception_spec_opt SEMICOLON; -constructors_block: nil | LCURLY constructors RCURLY; + exception_spec_opt SEMICOLON + {$$ = list4(mk(CONSTRUCTOR, "constructor"), $1, $3, $5);}; +constructors_block: nil | LCURLY constructors RCURLY {$$ = $2;}; constructor_params_opt: nil | constructor_params; constructor_params: rest_param | ctor_params; -ctor_params: ctor_param | ctor_param COMMA ctor_params; +ctor_params: ctor_param | ctor_param COMMA ctor_params {$$ = cons($1, $2);}; rest_param: LSQUARE IN RSQUARE ANY ELLIPSIS identifier; ctor_param: LSQUARE IN RSQUARE type identifier; accumulated_service: published SERVICE identifier colon_name_opt @@ -132,7 +141,7 @@ type: simple_type | 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; -sequence_type: SEQUENCE LT type GT; +sequence_type: SEQUENCE LT type GT {$$ = list2($1, $2);}; template_type: name LT types GT; types: nil | type COMMA types; exp: xor_exp | exp OR xor_exp {$$ = list3($2, $1, $3);}; diff --git a/scan.ll b/scan.ll @@ -33,6 +33,7 @@ IDENTIFIER_NEW ({ALPHA}({ALPHA}|{DIGIT})*)|({CAPITAL}("_"?({ALPHA}|{DIGIT})+)*) IDENTIFIER ("_"?({ALPHA}|{DIGIT})+)* %% + [ \t\r]+ ; /* eat up whitespace */ [\n] { yycolumn = 1; @@ -56,6 +57,15 @@ IDENTIFIER ("_"?({ALPHA}|{DIGIT})+)* ">>" return tok(RSHIFT, ">>"); "~" return tok(NOT, "~"); "::" return tok(SCOPE, "::"); +"{" return tok(LCURLY, "{"); +"}" return tok(RCURLY, "}"); +"[" return tok(LSQUARE, "["); +"]" return tok(RSQUARE, "]"); +"(" return tok(LPAR, "("); +")" return tok(RPAR, ")"); +"," return tok(COMMA, ","); +";" return tok(SEMICOLON, ";"); +":" return tok(COLON, ":"); attribute return tok(ATTRIBUTE, "attribute"); bound return tok(BOUND, "bound"); @@ -120,6 +130,6 @@ published return tok(PUBLISHED, "published"); {DIGIT}+ return tok(INT, strdup(yytext)); -. return yytext[0]; +{IDENTIFIER} return tok(ID, strdup(yytext)); %% diff --git a/unoidl2.c b/unoidl2.c @@ -18,8 +18,9 @@ struct any { } u; }; -const Any NIL = NULL; -static struct any _T = {.tag = CONS, .u = {.t = {.kind = 0, .token = "T"}}}; +static struct any _NIL = {.tag = CONS, .u = {.c = {.car = &_NIL, .cdr = &_NIL}}}; +static struct any _T = {.tag = TOKEN, .u = {.t = {.kind = 0, .token = "T"}}}; +const Any NIL = &_NIL; const Any T = &_T; Any cons(Any car, Any cdr) { @@ -38,8 +39,12 @@ Any mk(Kind kind, char *token) { return x; } -Any consp(Any x) { - return CONS == x->tag ? T : NIL; +int null(Any x) { + return NIL == x; +} + +int consp(Any x) { + return CONS == x->tag; } Any car(Any x) { @@ -50,6 +55,31 @@ Any cdr(Any x) { return x->u.c.cdr; } +Any cons3(Any a, Any b, Any c) +{ + return cons(a, cons(b, c)); +} + +Any cons4(Any a, Any b, Any c, Any d) +{ + return cons(a, cons(b, cons(c, d))); +} + +Any cons5(Any a, Any b, Any c, Any d, Any e) +{ + return cons(a, cons(b, cons(c, cons(d, e)))); +} + +Any cons6(Any a, Any b, Any c, Any d, Any e, Any f) +{ + return cons(a, cons(b, cons(c, cons(d, cons(e, f))))); +} + +Any cons7(Any a, Any b, Any c, Any d, Any e, Any f, Any g) +{ + return cons(a, cons(b, cons(c, cons(d, cons(e, cons(f, g)))))); +} + Any list1(Any a) { return cons(a, NIL); @@ -57,21 +87,38 @@ Any list1(Any a) Any list2(Any a, Any b) { - return cons(a, cons(b, NIL)); + return cons3(a, b, NIL); } Any list3(Any a, Any b, Any c) { - return cons(a, cons(b, cons(c, NIL))); + return cons4(a, b, c, NIL); +} + +Any list4(Any a, Any b, Any c, Any d) +{ + return cons5(a, b, c, d, NIL); +} + +Any list5(Any a, Any b, Any c, Any d, Any e) +{ + return cons6(a, b, c, d, e, NIL); +} + +Any list6(Any a, Any b, Any c, Any d, Any e, Any f) +{ + return cons7(a, b, c, d, e, f, NIL); } void print(Any x) { - if(x) { + if(null(x)) + printf("NIL"); + else { if(consp(x)) { printf("("); print(car(x)); Any d; - for(d = cdr(x); d; d = cdr(d)) { + for(d = cdr(x); !null(d); d = cdr(d)) { printf(" "); if(consp(d)) print(car(d)); @@ -84,6 +131,5 @@ void print(Any x) { printf(")"); } else printf("%s", x->u.t.token); - } else - printf("NIL"); + } } diff --git a/unoidl2.h b/unoidl2.h @@ -6,10 +6,19 @@ typedef enum yytokentype Kind; Any cons(Any car, Any cdr); Any mk(Kind kind, char *token); -Any consp(Any x); +int null(Any x); +int consp(Any x); Any car(Any x); Any cdr(Any x); +Any cons3(Any a, Any b, Any c); +Any cons4(Any a, Any b, Any c, Any d); +Any cons5(Any a, Any b, Any c, Any d, Any e); +Any cons6(Any a, Any b, Any c, Any d, Any e, Any f); +Any cons7(Any a, Any b, Any c, Any d, Any e, Any f, Any g); Any list1(Any a); Any list2(Any a, Any b); Any list3(Any a, Any b, Any c); +Any list4(Any a, Any b, Any c, Any d); +Any list5(Any a, Any b, Any c, Any d, Any e); +Any list6(Any a, Any b, Any c, Any d, Any e, Any f); void print(Any x);