also rename .h to .hpp in miniswig dir
[supertux.git] / tools / miniswig / create_docu.cpp
1 #include "tree.hpp"
2 #include <iostream>
3 #include <sstream>
4 #include <stdexcept>
5 #include "create_docu.hpp"
6 #include "globals.hpp"
7
8 void
9 DocuCreator::create_docu(Namespace* ns)
10 {
11     std::string fromfile = original_file != "" ? original_file : inputfile;
12     
13     writer.openTag("documentation");
14     writer.openTag("namespace");
15     writer.writeAttribute("name", ns->name);
16
17     for(std::vector<AtomicType*>::iterator i = ns->types.begin();
18             i != ns->types.end(); ++i) {
19         AtomicType* type = *i;
20         Class* _class = dynamic_cast<Class*> (type);
21         if(_class != 0)
22             create_class_docu(_class);
23     }
24     for(std::vector<Function*>::iterator i = ns->functions.begin();
25             i != ns->functions.end(); ++i) {
26         create_function_docu(0, *i);
27     }
28
29     writer.closeTag("namespace");
30     writer.closeTag("documentation");
31 }
32
33 void
34 DocuCreator::create_class_docu(Class* _class)
35 {
36     writer.openTag("class");
37     writer.writeAttribute("name", _class->name);
38
39     if(_class->docu_comment != "") {
40         writer.writeTag("documentation");
41         writer.write(_class->docu_comment);
42     }
43     
44     for(std::vector<ClassMember*>::iterator i = _class->members.begin();
45             i != _class->members.end(); ++i) {
46         ClassMember* member = *i;
47         if(member->visibility != ClassMember::PUBLIC)
48             continue;
49         Function* function = dynamic_cast<Function*> (member);
50         if(!function)
51             continue;
52         if(function->type != Function::FUNCTION)
53             continue;
54         create_function_docu(_class, function);
55     }
56
57     writer.closeTag("class");
58 }
59
60 void
61 DocuCreator::create_function_docu(Class* _class, Function* function)
62 {
63     writer.openTag("function");
64     
65     writer.writeAttribute("return_type",
66             get_type(function->return_type));
67     writer.writeAttribute("name", function->name);
68
69     if(function->docu_comment != "") {
70         writer.writeTag("documentation");
71         writer.write(function->docu_comment);
72     }
73   
74     for(std::vector<Parameter>::iterator p = function->parameters.begin();
75             p != function->parameters.end(); ++p) {
76         if(p == function->parameters.begin() 
77                 && p->type.atomic_type == HSQUIRRELVMType::instance())
78             continue;
79
80         writer.writeTag("param");
81         writer.writeAttribute("type", get_type(p->type));
82         writer.writeAttribute("name", p->name);
83     }
84
85     writer.closeTag("function");
86 }
87
88 std::string
89 DocuCreator::get_type(const Type& type)
90 {
91     if(type.ref > 0 && type.atomic_type != StringType::instance())
92         throw std::runtime_error("References not handled yet");
93     if(type.pointer > 0)
94         throw std::runtime_error("Pointers not handled yet");
95     if(type.atomic_type == &BasicType::VOID) {
96         return "void";
97     } else if(type.atomic_type == &BasicType::INT) {
98         return "int";
99     } else if(type.atomic_type == &BasicType::FLOAT) {
100         return "float";
101     } else if(type.atomic_type == &BasicType::BOOL) {
102         return "bool";
103     } else if(type.atomic_type == StringType::instance()) {
104         return "string";
105     } 
106     
107     std::ostringstream msg;
108     msg << "Type '" << type.atomic_type->name << "' not supported yet.";
109     throw std::runtime_error(msg.str());
110 }
111