+ if (def->type->strct) {
+ P->structs = add_decl_struct(P->structs, def->type->strct);
+ }
+ if (def->type->enm) {
+ P->enums = add_decl_enum(P->enums, def->type->enm);
+ }
+}
+block ::= constant(constant). {
+ P->consts = add_constant(P->consts, constant);
+}
+block ::= decl_struct(strct). {
+ P->structs = add_decl_struct(P->structs, strct);
+}
+block ::= decl_union(u). {
+ P->unions = add_decl_union(P->unions, u);
+}
+block ::= decl_enum(e). {
+ P->enums = add_decl_enum(P->enums, e);
+}
+
+optional_name(n) ::= .{
+ n = NULL;
+}
+optional_name(n) ::= NAME(N). {
+ n = N;
+}
+
+enum_name(n) ::= ENUM(E) optional_name(N). {
+ if (N) {
+ n = N;
+ free(E);
+ } else {
+ char digest[17];
+
+ PSI_TokenHash(E, digest);
+ n = PSI_TokenTranslit(PSI_TokenAppend(E, 1, digest), " ", "@");
+ }
+}
+
+%type decl_enum {decl_enum *}
+%destructor decl_enum {free_decl_enum($$);}
+decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
+ e = init_decl_enum(N->text, list);
+ e->token = N;
+}
+
+%type decl_enum_items {decl_enum_items*}
+%destructor decl_enum_items {free_decl_enum_items($$);}
+decl_enum_items(l) ::= decl_enum_item(i). {
+ l = init_decl_enum_items(i);
+}
+decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). {
+ l = add_decl_enum_item(l_, i);
+}
+
+%type decl_enum_item {decl_enum_item*}
+%destructor decl_enum_item {free_decl_enum_item($$);}
+decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). {
+ i = init_decl_enum_item(N->text, num);
+ i->token = N;
+}
+decl_enum_item(i) ::= NAME(N). {
+ i = init_decl_enum_item(N->text, NULL);
+ i->token = N;
+}
+
+union_name(n) ::= UNION(U) optional_name(N). {
+ if (N) {
+ n = N;
+ free(U);
+ } else {
+ char digest[17];
+
+ PSI_TokenHash(U, digest);
+ n = PSI_TokenTranslit(PSI_TokenAppend(U, 1, digest), " ", "@");
+ }
+}
+
+struct_name(n) ::= STRUCT(S) optional_name(N). {
+ if (N) {
+ n = N;
+ free(S);
+ } else {
+ char digest[17];
+
+ PSI_TokenHash(S, digest);
+ n = PSI_TokenTranslit(PSI_TokenAppend(S, 1, digest), " ", "@");
+ }
+}
+
+%type decl_struct_args_block {decl_args*}
+%destructor decl_struct_args_block {free_decl_args($$);}
+decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. {
+ args_ = args;
+}
+%type decl_struct_args {decl_args*}
+%destructor decl_struct_args {free_decl_args($$);}
+decl_struct_args(args_) ::= decl_struct_args_block(args). {
+ args_ = args;
+}
+decl_struct_args(args_) ::= EOS. {
+ args_ = init_decl_args(NULL);
+}
+
+
+%type decl_struct {decl_struct*}
+%destructor decl_struct {free_decl_struct($$);}
+decl_struct(strct) ::= STRUCT NAME(N) align_and_size(as) decl_struct_args(args). {
+ strct = init_decl_struct(N->text, args);
+ strct->align = as.a;
+ strct->size = as.s;
+ strct->token = N;
+}
+
+%type align_and_size { struct {size_t a; size_t s; } }
+align_and_size(as) ::= . {
+ as.a = 0;
+ as.s = 0;
+}
+align_and_size(as) ::= COLON COLON LPAREN NUMBER(A) COMMA NUMBER(S) RPAREN. {
+ as.a = atol(A->text);
+ as.s = atol(S->text);
+ free(A);
+ free(S);
+}
+
+%type decl_union {decl_union*}
+%destructor decl_union {free_decl_union($$);}
+decl_union(u) ::= UNION NAME(N) align_and_size(as) decl_struct_args(args). {
+ u = init_decl_union(N->text, args);
+ u->align = as.a;
+ u->size = as.s;
+ u->token = N;
+}
+
+%token_class const_type_token BOOL INT FLOAT STRING.
+%type const_type {const_type*}
+%destructor const_type {free_const_type($$);}
+const_type(type_) ::= const_type_token(T). {
+ type_ = init_const_type(T->type, T->text);
+ free(T);
+}
+%type constant {constant*}
+%destructor constant {free_constant($$);}
+constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
+ constant = init_constant(type, T->text, val);
+ free(T);