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