fix namespace problems
[supertux.git] / tools / miniswig / parser.yy
index 6c25c9e..8728b87 100644 (file)
@@ -3,8 +3,8 @@
 #include <iostream>
 #include <sstream>
 #include <stdexcept>
-#include "tree.h"
-#include "globals.h"
+#include "tree.hpp"
+#include "globals.hpp"
 
 %}
 
@@ -15,6 +15,7 @@
     float       fval;
     Class*      _class;
     Function*   function;
+    Field*      field;
     Type*       type;
     AtomicType* atomic_type;
     Namespace*  _namespace;
@@ -30,8 +31,9 @@ bool search_down = true;
 Namespace* search_namespace = 0;
 Namespace* current_namespace = 0;
 static Class* current_class = 0;
-static Function* currentFunction = 0;
+static Function* current_function = 0;
 static Type* current_type = 0;
+static Field* current_field = 0;
 static ClassMember::Visbility current_visibility;
 
 class ParseError : public std::exception
@@ -40,7 +42,8 @@ public:
     ParseError(const std::string& message) throw()
     {
         std::ostringstream msg;
-        msg << "Parse error in line " << yylineno << ": "
+        msg << "Parse error in '" << current_file
+            << "' line " << getCurrentLine() << ": "
             << message;
         this->message = msg.str();
     }
@@ -62,11 +65,10 @@ private:
 %token <str>  T_STRING
 %token <str>  T_ID
 %token <atomic_type> T_ATOMIC_TYPE
-%token <_namespace> T_NAMESPACEREF;
+%token <_namespace> T_NAMESPACEREF
 %token T_CLASS
 %token T_STRUCT
 %token T_STATIC
-%token T_VIRTUAL
 %token T_CONST
 %token T_UNSIGNED
 %token T_SIGNED
@@ -88,6 +90,7 @@ private:
 %type <function> function_declaration
 %type <function> constructor_declaration;
 %type <function> destructor_declaration;
+%type <field> field_declaration;
 %type <type>   type
 %type <atomic_type> type_identifier
 
@@ -133,22 +136,53 @@ namespace_member:
     | function_declaration
         { current_namespace->functions.push_back($1); }
     | namespace_declaration
+    | field_declaration
+        { current_namespace->fields.push_back($1); }
 ;  
 
 class_declaration:
-    T_CLASS T_ID '{' 
+    T_CLASS T_ID 
         {
             current_class = new Class();
             current_class->name = $2;
             free($2);
+            current_class->docu_comment = last_docucomment;
+            last_docucomment = "";
             current_visibility = ClassMember::PROTECTED;
         }
-    class_body '}' ';'
+    superclass_list '{' class_body '}' ';'
         {
             $$ = current_class;
         }
 ;
 
+superclass_list:
+    /* empty */
+    | ':' superclasses
+;
+
+superclasses:
+      superclass
+    | superclasses ',' superclass
+;
+
+superclass:
+    superclass_visibility type_identifier
+        {
+            Class* superclass = dynamic_cast<Class*> ($2);
+            if(superclass == 0)
+                throw ParseError("SuperClass is not a Class type");
+            current_class->super_classes.push_back(superclass);
+            superclass->sub_classes.push_back(current_class);
+        }
+;
+
+superclass_visibility:
+    T_PUBLIC
+    | T_PROTECTED
+    | T_PRIVATE
+;
+
 class_body: /* empty */
         | class_body class_body_element
 ;
@@ -170,7 +204,11 @@ class_body_element:
                 $1->visibility = current_visibility;
                 current_class->members.push_back($1);
             }
-        | variable_declaration
+        | field_declaration
+            {
+                $1->visibility = current_visibility;
+                current_class->members.push_back($1);
+            }
 ;
 
 visibility_change:
@@ -185,51 +223,92 @@ visibility_change:
 constructor_declaration:    
     T_ID '('
         {
-            currentFunction = new Function();
-            currentFunction->type = Function::CONSTRUCTOR;
+            current_function = new Function();
+            current_function->type = Function::CONSTRUCTOR;
+            current_function->docu_comment = last_docucomment;
+            last_docucomment = "";
             free($1);
         }
     parameter_list ')' ';'
         {
-            $$ = currentFunction;
+            $$ = current_function;
         }
 ;
 
 destructor_declaration:
-    maybe_virtual '~' T_ID '(' ')' abstract_declaration ';'
+    '~' T_ID '(' ')' abstract_declaration ';'
         {
-            currentFunction = new Function();
-            currentFunction->type = Function::DESTRUCTOR;
-            free($3);
-            $$ = currentFunction;
+            current_function = new Function();
+            current_function->type = Function::DESTRUCTOR;
+            current_function->docu_comment = last_docucomment;
+            last_docucomment = "";
+            free($2);
+            $$ = current_function;
         }
 ;
 
-maybe_virtual:
-    /* empty */
-    | T_VIRTUAL
+field_declaration:
+    type T_ID 
+        {
+            current_field = new Field();
+            current_field->type = $1;
+            current_field->docu_comment = last_docucomment;
+            last_docucomment = "";
+            current_field->name = $2;
+            free($2);
+        }
+    maybe_const_initialisation ';'
+        {
+            $$ = current_field;
+        }
 ;
 
-variable_declaration:
-    type T_ID ';'
-;
+maybe_const_initialisation:
+    /* empty */
+    | '=' T_INT
+        {
+            if(current_field->type->atomic_type == &BasicType::FLOAT) {
+                current_field->const_float_value = (float) $2;
+            } else {
+                current_field->const_int_value = $2;
+            }
+            current_field->has_const_value = true;
+        }
+    | '=' T_FLOAT
+        {
+            current_field->const_float_value = $2;
+            current_field->has_const_value = true;
+        }
+    | '=' T_STRING
+        {
+            current_field->const_string_value = $2;
+            current_field->has_const_value = true;
+        }
+;          
 
 function_declaration:
-    maybe_virtual type T_ID '(' 
+    type T_ID '(' 
         {
-            currentFunction = new Function();
-            currentFunction->type = Function::FUNCTION;
-            currentFunction->return_type = *($2);
-            delete $2;
-            currentFunction->name = $3;
-            free($3);
+            current_function = new Function();
+            current_function->type = Function::FUNCTION;
+            current_function->return_type = *($1);
+            delete $1;
+            current_function->name = $2;
+            free($2);
+            current_function->docu_comment = last_docucomment;
+            last_docucomment = "";
         }                           
-    parameter_list ')' abstract_declaration ';'
+    parameter_list ')' maybe_const abstract_declaration ';'
         {
-            $$ = currentFunction;
+            $$ = current_function;
         }
 ;
 
+maybe_const:
+    /* empty */
+    | T_CONST
+;
+
 abstract_declaration:
     /* empty */
     | '=' T_INT
@@ -251,16 +330,16 @@ parameter:
             Parameter parameter;
             parameter.type = *($1);
             delete $1;
-            currentFunction->parameters.push_back(parameter);
+            current_function->parameters.push_back(parameter);
         }
     | type T_ID
         {
             Parameter parameter;
             parameter.type = *($1);
             delete $1;
-            parameter.name = *($2);
+            parameter.name = $2;
             free($2);
-            currentFunction->parameters.push_back(parameter);
+            current_function->parameters.push_back(parameter);
         }
 ;
 
@@ -328,7 +407,6 @@ atomic_type:
 type_identifier:
     T_ATOMIC_TYPE
         {
-            // search for type in current compilation unit...
             $$ = $1;
         }
     | namespace_refs "::" T_ATOMIC_TYPE