EOF
[m6w6/ext-psi] / src / parser_proc.y
index f3a51f46ef3c319b5875304872c7f972f33b7d8a..ac7c1172838d586b46a4a516bfc932456e58475c 100644 (file)
 %extra_argument {PSI_Parser *P}
 /* TOKEN is defined inside syntax_error */
 %syntax_error {
-       PSI_ParserSyntaxError(P, P->psi.file.fn, P->line, "Unexpected token '%s'", TOKEN->text);
+       if (TOKEN) {
+               PSI_ParserSyntaxError(P, P->psi.file.fn, P->line, "Unexpected token '%s'", TOKEN->text);
+       } else {
+               PSI_ParserSyntaxError(P, P->psi.file.fn, P->line, "Unexpected end of input");
+       }
 }
 
 %nonassoc NAME.
-%fallback NAME FREE SET LET RETURN LIB INT.
+%fallback NAME FREE SET LET RETURN LIB INT UNSIGNED.
 
 file ::= blocks.
 
 blocks ::= block.
 blocks ::= blocks block.
 
+block ::= EOF.
+
 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
        if (P->psi.file.ln) {
                PSI_ParserSyntaxError(P, P->psi.file.ln, T->line, "Extra 'lib %s' statement has no effect", libname->text);
@@ -92,6 +98,12 @@ decl_typedef(def) ::= TYPEDEF decl_type(type) NAME(ALIAS) EOS. {
        def = init_decl_typedef(ALIAS->text, type);
        free(ALIAS);
 }
+/* support opaque types */
+decl_typedef(def) ::= TYPEDEF VOID(V) NAME(ALIAS) EOS. {
+       def = init_decl_typedef(ALIAS->text, init_decl_type(V->type, V->text));
+       free(V);
+       free(ALIAS);
+}
 decl_typedef(def) ::= TYPEDEF STRUCT(S) NAME(N) NAME(ALIAS) EOS. {
        def = init_decl_typedef(ALIAS->text, init_decl_type(S->type, N->text));
        free(ALIAS);
@@ -155,8 +167,8 @@ decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
 
 %type decl_arg {decl_arg*}
 %destructor decl_arg {free_decl_arg($$);}
-decl_arg(arg_) ::= decl_type(type) decl_var(var). {
-       arg_ = var->arg = init_decl_arg(type, var);
+decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
+       arg_ = init_decl_arg(type, var);
 }
 /* void pointers need a specific rule */
 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
@@ -164,7 +176,14 @@ decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
                init_decl_type(T->type, T->text),
                init_decl_var(N->text, p, 0)
        );
-       arg_->var->arg = arg_;
+       free(T);
+       free(N);
+}
+decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
+       arg_ = init_decl_arg(
+               init_decl_type(T->type, T->text),
+               init_decl_var(N->text, p, 0)
+       );
        free(T);
        free(N);
 }
@@ -212,6 +231,15 @@ decl_type(type_) ::= decl_type_token(T). {
        type_ = init_decl_type(T->type, T->text);
        free(T);
 }
+/* unsigned, urgh */
+decl_type(type_) ::= UNSIGNED NAME(T). {
+       type_ = init_decl_type(T->type, T->text);
+       type_->name = realloc(type_->name, T->size + sizeof("unsigned"));
+       memmove(type_->name + sizeof("unsigned"), type_->name, T->size);
+       memcpy(type_->name, "unsigned", sizeof("unsigned")-1);
+       type_->name[sizeof("unsigned")] = ' ';
+       type_->name[T->size + sizeof("unsigned")] = 0;
+}
 /* we have to support plain int here because we have it in our lexer rules */
 decl_type(type_) ::= INT(T). {
        type_ = init_decl_type(PSI_T_NAME, T->text);
@@ -224,6 +252,15 @@ decl_type(type_) ::= STRUCT(S) NAME(T). {
        free(T);
 }
 
+%type const_decl_type {decl_type*}
+%destructor const_decl_type {free_decl_type($$);}
+const_decl_type(type) ::= decl_type(type_). {
+       type = type_;
+}
+const_decl_type(type) ::= CONST decl_type(type_). {
+       type = type_;
+}
+
 %type impl {impl*}
 %destructor impl {free_impl($$);}
 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {