domainParser.cpp
Go to the documentation of this file.
00001 #include "domainParser.h"
00002 #include <fstream>
00003 #include <QRegExp>
00004 #include <ros/ros.h>
00005 
00006 DomainParser::DomainParser()
00007 {
00008 }
00009 
00010 DomainParser::~DomainParser()
00011 {
00012 }
00013 
00014 bool DomainParser::parse(const std::string & filename)
00015 {
00016     std::ifstream f(filename.c_str());
00017     if(!f.good())
00018         return false;
00019 
00020     QString content;
00021     while(f.good()) {
00022         std::string l;
00023         getline(f, l);
00024         QString line(l.c_str());
00025         // strip comments
00026         int idx = line.indexOf(";");
00027         line = line.mid(0, idx);
00028         content += " " + line;
00029     }
00030 
00031     content = content.replace("(", " ( ");
00032     content = content.replace(")", " ) ");
00033 
00034     QStringList tokens = content.split(" ", QString::SkipEmptyParts);
00035 
00036     _parseTreeRoot = parseTreeItem(tokens);
00037 
00038     f.close();
00039 
00040     parseContent();
00041 
00042     return true;
00043 }
00044 
00048 DomainParser::TreeItem DomainParser::parseTreeItem(QStringList & tokens)
00049 {
00050     ROS_ASSERT(tokens.size() >= 2);
00051     ROS_ASSERT(tokens[0] == "(");
00052     tokens.removeFirst();
00053 
00054     TreeItem ret;
00055     // fill ret.items
00056     while(true) {
00057         ROS_ASSERT(!tokens.isEmpty());
00058         if(tokens[0] == ")") {  // done.
00059             tokens.removeFirst();
00060             return ret;
00061         }
00062         if(tokens[0] == "(") {
00063             ret.items.push_back(parseTreeItem(tokens)); 
00064         } else {
00065             TreeItem si;
00066             si.content = qPrintable(tokens[0]);
00067             ret.items.push_back(si);
00068             tokens.removeFirst();
00069         }
00070     }
00071 
00072     return ret;
00073 }
00074 
00075 void DomainParser::parseContent()
00076 {
00077     ROS_ASSERT(_parseTreeRoot.items.size() >= 2);
00078     ROS_ASSERT(_parseTreeRoot.items[0].content == "define");
00079     TreeItem & domainIt = _parseTreeRoot.items[1];
00080     ROS_ASSERT(domainIt.items.size() == 2);
00081     ROS_ASSERT(domainIt.items[0].content == "domain");
00082     _name = domainIt.items[1].content;
00083 }
00084 
00085 void DomainParser::dumpTree() const
00086 {
00087     dumpItem(_parseTreeRoot, 0);
00088 }
00089 
00090 void DomainParser::dumpItem(const TreeItem & item, unsigned int indent) const
00091 {
00092     if(item.items.empty()) {        // only string
00093         for(unsigned int i = 0; i < indent; i++) printf(" ");
00094         printf("%s\n", item.content.c_str());
00095         return;
00096     }
00097     for(deque<TreeItem>::const_iterator it = item.items.begin(); it != item.items.end(); it++) {
00098         dumpItem(*it, indent + 2);
00099     }
00100     printf("\n");
00101 }
00102 
00103 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Tue Jan 22 2013 12:25:03