Commit 045d2416 authored by Davis King's avatar Davis King

Improved the XML output

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403719
parent 0604f4fc
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <fstream> #include <fstream>
#include <stack> #include <stack>
#include "dlib/cpp_tokenizer.h" #include "dlib/cpp_tokenizer.h"
#include "dlib/string.h"
using namespace dlib; using namespace dlib;
using namespace std; using namespace std;
...@@ -403,7 +404,6 @@ void process_file ( ...@@ -403,7 +404,6 @@ void process_file (
} }
else if (token == "namespace") else if (token == "namespace")
{ {
cout << "hit namespace" << endl;
recently_seen_namespace_keyword = true; recently_seen_namespace_keyword = true;
recently_seen_new_scope = true; recently_seen_new_scope = true;
} }
...@@ -553,7 +553,6 @@ void process_file ( ...@@ -553,7 +553,6 @@ void process_file (
// if we are entering a new scope // if we are entering a new scope
if (recently_seen_new_scope) if (recently_seen_new_scope)
{ {
cout << "new scope" << endl;
scopes.push(0); scopes.push(0);
at_top_of_new_scope = true; at_top_of_new_scope = true;
...@@ -750,7 +749,11 @@ string get_function_name ( ...@@ -750,7 +749,11 @@ string get_function_name (
{ {
if (i != 0 && !seen_operator) if (i != 0 && !seen_operator)
{ {
name = declaration[i-1].second; // if this is a destructor then include the ~
if (i > 1 && declaration[i-2].second == "~")
name = "~" + declaration[i-1].second;
else
name = declaration[i-1].second;
} }
break; break;
...@@ -785,15 +788,126 @@ string get_function_name ( ...@@ -785,15 +788,126 @@ string get_function_name (
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
string pretty_print_declaration (
const std::vector<std::pair<int,string> >& decl
)
{
string temp;
if (decl.size() == 0)
return temp;
else if (decl.size() >= 1)
{
temp = decl[0].second;
if (temp == "template")
temp += " ";
}
for (unsigned long i = 1; i < decl.size(); ++i)
{
bool add_trailing_space = false;
if (decl[i].first == tok_type::IDENTIFIER ||
decl[i].first == tok_type::KEYWORD)
{
if (decl[i-1].first == tok_type::IDENTIFIER ||
decl[i-1].first == tok_type::KEYWORD ||
decl[i-1].second == "," ||
decl[i-1].second == "&" ||
decl[i-1].second == "*" ||
decl[i-1].second == ">"
)
{
temp += " ";
}
}
else if (i+1 < decl.size())
{
if(decl[i].first == tok_type::OTHER)
{
if (decl[i].second == ":" && decl[i-1].second != ":" && decl[i+1].second != ":")
{
temp += " ";
add_trailing_space = true;
}
}
}
temp += decl[i].second;
if (add_trailing_space)
temp += " ";
}
return temp;
}
// ----------------------------------------------------------------------------------------
string format_comment (
const string& comment
)
{
if (comment.size() <= 6)
return "";
string temp = trim(trim(comment.substr(3,comment.size()-6), " \t"), "\n\r");
// now figure out what the smallest amount of leading white space is and remove it from each line.
unsigned long num_whitespace = 100000;
string::size_type pos1 = 0, pos2 = 0;
while (pos1 != string::npos)
{
// find start of non-white-space
pos2 = temp.find_first_not_of(" \t",pos1);
// if this is a line of just white space then ignore it
if (pos2 != string::npos && temp[pos2] != '\n' && temp[pos2] != '\r')
{
if (pos2-pos1 < num_whitespace)
num_whitespace = pos2-pos1;
}
// find end-of-line
pos1 = temp.find_first_of("\n\r", pos2);
// find start of next line
pos2 = temp.find_first_not_of("\n\r", pos1);
pos1 = pos2;
}
// now remove the leading white space
string temp2;
bool seen_newline = true;
unsigned long counter = 0;
for (unsigned long i = 0; i < temp.size(); ++i)
{
// if we are looking at a new line
if (temp[i] == '\n' || temp[i] == '\r')
{
counter = 0;
}
else if (counter < num_whitespace)
{
++counter;
continue;
}
temp2 += temp[i];
}
return temp2;
}
// ----------------------------------------------------------------------------------------
typedef_record convert_tok_typedef_record ( typedef_record convert_tok_typedef_record (
const tok_typedef_record& rec const tok_typedef_record& rec
) )
{ {
typedef_record temp; typedef_record temp;
for (unsigned long i = 0; i < rec.declaration.size(); ++i) temp.declaration = pretty_print_declaration(rec.declaration);
{
temp.declaration += rec.declaration[i].second + " ";
}
return temp; return temp;
} }
...@@ -804,10 +918,7 @@ variable_record convert_tok_variable_record ( ...@@ -804,10 +918,7 @@ variable_record convert_tok_variable_record (
) )
{ {
variable_record temp; variable_record temp;
for (unsigned long i = 0; i < rec.declaration.size(); ++i) temp.declaration = pretty_print_declaration(rec.declaration);
{
temp.declaration += rec.declaration[i].second + " ";
}
return temp; return temp;
} }
...@@ -819,13 +930,9 @@ method_record convert_tok_method_record ( ...@@ -819,13 +930,9 @@ method_record convert_tok_method_record (
{ {
method_record temp; method_record temp;
temp.comment = rec.comment; temp.comment = format_comment(rec.comment);
temp.name = get_function_name(rec.declaration); temp.name = get_function_name(rec.declaration);
temp.declaration = pretty_print_declaration(rec.declaration);
for (unsigned long i = 0; i < rec.declaration.size(); ++i)
{
temp.declaration += rec.declaration[i].second + " ";
}
return temp; return temp;
} }
...@@ -840,7 +947,7 @@ class_record convert_tok_class_record ( ...@@ -840,7 +947,7 @@ class_record convert_tok_class_record (
crec.scope = rec.scope; crec.scope = rec.scope;
crec.file = rec.file; crec.file = rec.file;
crec.comment = rec.comment; crec.comment = format_comment(rec.comment);
crec.name.clear(); crec.name.clear();
...@@ -857,12 +964,7 @@ class_record convert_tok_class_record ( ...@@ -857,12 +964,7 @@ class_record convert_tok_class_record (
} }
} }
crec.declaration.clear(); crec.declaration = pretty_print_declaration(rec.declaration);
for (unsigned long i = 0; i < rec.declaration.size(); ++i)
{
crec.declaration += rec.declaration[i].second + " ";
}
for (unsigned long i = 0; i < rec.public_typedefs.size(); ++i) for (unsigned long i = 0; i < rec.public_typedefs.size(); ++i)
crec.public_typedefs.push_back(convert_tok_typedef_record(rec.public_typedefs[i])); crec.public_typedefs.push_back(convert_tok_typedef_record(rec.public_typedefs[i]));
...@@ -890,13 +992,9 @@ function_record convert_tok_function_record ( ...@@ -890,13 +992,9 @@ function_record convert_tok_function_record (
temp.scope = rec.scope; temp.scope = rec.scope;
temp.file = rec.file; temp.file = rec.file;
temp.comment = rec.comment; temp.comment = format_comment(rec.comment);
temp.name = get_function_name(rec.declaration); temp.name = get_function_name(rec.declaration);
temp.declaration = pretty_print_declaration(rec.declaration);
for (unsigned long i = 0; i < rec.declaration.size(); ++i)
{
temp.declaration += rec.declaration[i].second + " ";
}
return temp; return temp;
} }
...@@ -930,17 +1028,36 @@ void convert_to_normal_records ( ...@@ -930,17 +1028,36 @@ void convert_to_normal_records (
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
string add_entity_ref (const string& str)
{
string temp;
for (unsigned long i = 0; i < str.size(); ++i)
{
if (str[i] == '&')
temp += "&amp;";
else if (str[i] == '<')
temp += "&lt;";
else if (str[i] == '>')
temp += "&gt;";
else
temp += str[i];
}
return temp;
}
// ----------------------------------------------------------------------------------------
void write_as_xml ( void write_as_xml (
const function_record& rec, const function_record& rec,
ostream& fout ostream& fout
) )
{ {
fout << " <function>\n"; fout << " <function>\n";
fout << " <name>" << rec.name << "</name>\n"; fout << " <name>" << add_entity_ref(rec.name) << "</name>\n";
fout << " <scope>" << rec.scope << "</scope>\n"; fout << " <scope>" << add_entity_ref(rec.scope) << "</scope>\n";
fout << " <declaration>" << rec.declaration << "</declaration>\n"; fout << " <declaration>" << add_entity_ref(rec.declaration) << "</declaration>\n";
fout << " <file>" << rec.file << "</file>\n"; fout << " <file>" << add_entity_ref(rec.file) << "</file>\n";
fout << " <comment>" << rec.comment << "</comment>\n"; fout << " <comment>" << add_entity_ref(rec.comment) << "</comment>\n";
fout << " </function>\n"; fout << " </function>\n";
} }
...@@ -948,54 +1065,69 @@ void write_as_xml ( ...@@ -948,54 +1065,69 @@ void write_as_xml (
void write_as_xml ( void write_as_xml (
const class_record& rec, const class_record& rec,
ostream& fout ostream& fout,
unsigned long indent
) )
{ {
fout << " <class>\n"; const string pad(indent, ' ');
fout << " <name>" << rec.name << "</name>\n";
fout << " <scope>" << rec.scope << "</scope>\n";
fout << " <declaration>" << rec.declaration << "</declaration>\n";
fout << " <file>" << rec.file << "</file>\n";
fout << " <comment>" << rec.comment << "</comment>\n";
fout << pad << "<class>\n";
fout << pad << " <name>" << add_entity_ref(rec.name) << "</name>\n";
fout << pad << " <scope>" << add_entity_ref(rec.scope) << "</scope>\n";
fout << pad << " <declaration>" << add_entity_ref(rec.declaration) << "</declaration>\n";
fout << pad << " <file>" << add_entity_ref(rec.file) << "</file>\n";
fout << pad << " <comment>" << add_entity_ref(rec.comment) << "</comment>\n";
fout << " <public_typedefs>\n";
for (unsigned long i = 0; i < rec.public_typedefs.size(); ++i) if (rec.public_typedefs.size() > 0)
{ {
fout << " <typedef>" << rec.public_typedefs[i].declaration << "</typedef>\n"; fout << pad << " <public_typedefs>\n";
for (unsigned long i = 0; i < rec.public_typedefs.size(); ++i)
{
fout << pad << " <typedef>" << add_entity_ref(rec.public_typedefs[i].declaration) << "</typedef>\n";
}
fout << pad << " </public_typedefs>\n";
} }
fout << " </public_typedefs>\n";
fout << " <public_variables>\n"; if (rec.public_variables.size() > 0)
for (unsigned long i = 0; i < rec.public_variables.size(); ++i)
{ {
fout << " <variable>" << rec.public_variables[i].declaration << "</variable>\n"; fout << pad << " <public_variables>\n";
for (unsigned long i = 0; i < rec.public_variables.size(); ++i)
{
fout << pad << " <variable>" << add_entity_ref(rec.public_variables[i].declaration) << "</variable>\n";
}
fout << pad << " </public_variables>\n";
} }
fout << " </public_variables>\n";
fout << " <public_methods>\n"; if (rec.public_methods.size() > 0)
for (unsigned long i = 0; i < rec.public_methods.size(); ++i)
{ {
fout << " <method>\n"; fout << pad << " <public_methods>\n";
fout << " <name>" << rec.public_methods[i].name << "</name>\n"; for (unsigned long i = 0; i < rec.public_methods.size(); ++i)
fout << " <declaration>" << rec.public_methods[i].declaration << "</declaration>\n"; {
fout << " <comment>" << rec.public_methods[i].comment << "</comment>\n"; fout << pad << " <method>\n";
fout << " </method>\n"; fout << pad << " <name>" << add_entity_ref(rec.public_methods[i].name) << "</name>\n";
fout << pad << " <declaration>" << add_entity_ref(rec.public_methods[i].declaration) << "</declaration>\n";
fout << pad << " <comment>" << add_entity_ref(rec.public_methods[i].comment) << "</comment>\n";
fout << pad << " </method>\n";
}
fout << pad << " </public_methods>\n";
} }
fout << " </public_methods>\n";
fout << " <public_subclasses>\n"; if (rec.public_subclasses.size() > 0)
for (unsigned long i = 0; i < rec.public_subclasses.size(); ++i)
{ {
write_as_xml(rec.public_subclasses[i], fout); fout << pad << " <public_subclasses>\n";
for (unsigned long i = 0; i < rec.public_subclasses.size(); ++i)
{
write_as_xml(rec.public_subclasses[i], fout, indent+4);
}
fout << pad << " </public_subclasses>\n";
} }
fout << " </public_subclasses>\n";
fout << " </class>\n"; fout << pad << "</class>\n";
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -1012,7 +1144,7 @@ void save_to_xml_file ( ...@@ -1012,7 +1144,7 @@ void save_to_xml_file (
fout << " <classes>" << endl; fout << " <classes>" << endl;
for (unsigned long i = 0; i < classes.size(); ++i) for (unsigned long i = 0; i < classes.size(); ++i)
{ {
write_as_xml(classes[i], fout); write_as_xml(classes[i], fout, 4);
fout << "\n"; fout << "\n";
} }
fout << " </classes>\n\n" << endl; fout << " </classes>\n\n" << endl;
...@@ -1054,10 +1186,6 @@ void generate_xml_markup( ...@@ -1054,10 +1186,6 @@ void generate_xml_markup(
cout << "\ntok_functions.size(): " << tok_functions.size() << endl; cout << "\ntok_functions.size(): " << tok_functions.size() << endl;
cout << "tok_classes.size(): " << tok_classes.size() << endl; cout << "tok_classes.size(): " << tok_classes.size() << endl;
cout << "tok_classes[0].public_methods.size(): " << tok_classes[0].public_methods.size() << endl;
cout << "tok_classes[0].public_typedefs.size(): " << tok_classes[0].public_typedefs.size() << endl;
cout << "tok_classes[0].public_variables.size(): " << tok_classes[0].public_variables.size() << endl;
cout << "tok_classes[0].public_subclasses.size(): " << tok_classes[0].public_subclasses.size() << endl;
cout << endl; cout << endl;
//cout << tok_functions[0].comment << endl; //cout << tok_functions[0].comment << endl;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment