Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

sexpparser.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: sexpparser.cpp,v 1.5 2004/04/05 14:51:36 rollmark Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 #include "sexpparser.h"
00023 
00024 using namespace oxygen;
00025 using namespace zeitgeist;
00026 using namespace std;
00027 using namespace boost;
00028 
00029 
00030 shared_ptr<PredicateList>
00031 SexpParser::Parse(const std::string& input)
00032 {
00033     size_t len = input.length();
00034 
00035     shared_ptr<PredicateList> predList(new PredicateList);
00036     if (len == 0)
00037     {
00038             return predList;
00039     }
00040 
00041     char* c = const_cast<char*>(input.c_str());
00042     pcont_t* pcont = init_continuation(c);
00043     sexp_t* sexp = iparse_sexp(c,input.size(),pcont);
00044 
00045     while (sexp != 0)
00046     {
00047         SexpToPredicate(predList,sexp);
00048         destroy_sexp(sexp);
00049         sexp = iparse_sexp(c,input.size(),pcont);
00050     }
00051 
00052     destroy_continuation(pcont);
00053     return predList;
00054 }
00055 
00056 string
00057 SexpParser::Generate(boost::shared_ptr<PredicateList> input)
00058 {
00059     if (input.get() == 0)
00060     {
00061             return "";
00062     }
00063 
00064     stringstream ss;
00065     PredicateList& list = *input.get();
00066 
00067     for (
00068          PredicateList::TList::const_iterator i = list.begin();
00069          i != list.end();
00070          ++i
00071          )
00072         PredicateToString(ss,*i);
00073 
00074     return ss.str();
00075 }
00076 
00077 void
00078 SexpParser::SexpToList(ParameterList& arguments, const sexp_t* const sexp)
00079 {
00080     sexp_t* s = const_cast<sexp_t*>(sexp);
00081     while (s != 0)
00082     {
00083         if (s->ty == SEXP_VALUE)
00084         {
00085             arguments.AddValue(string(s->val));
00086         } else {
00087             ParameterList& elem = arguments.AddList();
00088             SexpToList(elem,s->list);
00089         }
00090         s = s->next;
00091     }
00092 }
00093 
00094 void
00095 SexpParser::SexpToPredicate
00096 (shared_ptr<PredicateList>& predList, const sexp_t* const sexp)
00097 {
00098     // throw away outer brackets (i.e. we have a list at the top
00099     // level)
00100     if (sexp->ty != SEXP_LIST)
00101     {
00102         return;
00103     }
00104 
00105     sexp_t* s = const_cast<sexp_t*>(sexp->list);
00106 
00107     if (
00108         (s == 0) ||
00109         (s->ty != SEXP_VALUE)
00110         )
00111         {
00112             return;
00113         }
00114 
00115     Predicate& predicate = predList->AddPredicate();
00116     predicate.name = string(s->val);
00117     SexpToList(predicate.parameter,s->next);
00118 }
00119 
00120 void
00121 SexpParser::ListToString(stringstream& ss, const ParameterList& lst)
00122 {
00123     string space;
00124 
00125     for (
00126          ParameterList::TVector::const_iterator i = lst.begin();
00127          i != lst.end();
00128          ++i
00129          )
00130     {
00131         if (i->type() == typeid(string))
00132         {
00133             ss << space;
00134             ss << boost::any_cast<string>(*i);
00135         }
00136         else if (i->type() == typeid(float))
00137         {
00138             ss << space;
00139             ss << boost::any_cast<float>(*i);
00140         }
00141         else if (i->type() == typeid(int))
00142         {
00143             ss << space;
00144             ss <<boost::any_cast<int>(*i);
00145         }
00146         else if (i->type() == typeid(ParameterList))
00147         {
00148             const any* v = &(*i);
00149             const ParameterList* lst = any_cast<ParameterList>(v);
00150             ss << space;
00151             ss << '(';
00152             ListToString(ss,*lst);
00153             ss << ')';
00154         }
00155         else
00156         {
00157             ss << space;
00158             ss << "(error data format unknown)";
00159         }
00160 
00161         space = " ";
00162     }
00163 }
00164 
00165 void
00166 SexpParser::PredicateToString(stringstream& ss, const Predicate& plist)
00167 {
00168     ss << '(';
00169     ss << plist.name;
00170     ss << ' ';
00171     ListToString(ss,plist.parameter);
00172     ss << ')';
00173 }

Generated on Thu Apr 6 15:25:40 2006 for rcssserver3d by  doxygen 1.4.4