00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00099
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 }